forked from Exxo-Avalon/AvalonTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassExtensions.cs
More file actions
252 lines (217 loc) · 9.51 KB
/
ClassExtensions.cs
File metadata and controls
252 lines (217 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Avalon.Items.Accessories;
using Avalon.Players;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using Terraria;
using Terraria.GameContent.ItemDropRules;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace Avalon;
public static class ClassExtensions
{
private static readonly Action<Player, int, int, int> OpenChestAction = CacheOpenChestAction();
public static void Active(this Tile t, bool a) => t.HasTile = a;
public static ExxoPlayer Avalon(this Player p) => p.GetModPlayer<ExxoPlayer>();
public static ExxoBiomePlayer AvalonBiome(this Player p) => p.GetModPlayer<ExxoBiomePlayer>();
public static bool Exists(this Item item) => item.type > ItemID.None && item.stack > 0;
public static Asset<T> VanillaLoad<T>(this Asset<T> asset) where T : class
{
try
{
if (asset.State == AssetState.NotLoaded)
{
Main.Assets.Request<Texture2D>(asset.Name, AssetRequestMode.ImmediateLoad);
}
}
catch (AssetLoadException e)
{
}
return asset;
}
public static int FindClosestNPC(this Entity entity, float maxDistance, Func<NPC, bool> invalidNPCPredicate)
{
int closest = -1;
float lastDistance = maxDistance;
for (int i = 0; i < Main.npc.Length; i++)
{
NPC npc = Main.npc[i];
if (invalidNPCPredicate.Invoke(npc))
{
continue;
}
if (Vector2.Distance(entity.Center, npc.Center) < lastDistance)
{
lastDistance = Vector2.Distance(entity.Center, npc.Center);
closest = i;
}
}
return closest;
}
public static Rectangle GetDims(this ModTexturedType texturedType) =>
Main.netMode == NetmodeID.Server ? Rectangle.Empty : texturedType.GetTexture().Frame();
public static Rectangle GetDims(this ModItem modItem) =>
Main.netMode == NetmodeID.Server ? Rectangle.Empty : modItem.GetTexture().Frame();
public static Rectangle GetDims(this ModProjectile modProjectile) =>
Main.netMode == NetmodeID.Server ? Rectangle.Empty : modProjectile.GetTexture().Frame();
public static Asset<Texture2D> GetTexture(this ModTexturedType texturedType) =>
ModContent.Request<Texture2D>(texturedType.Texture);
public static Asset<Texture2D> GetTexture(this ModItem modItem) =>
ModContent.Request<Texture2D>(modItem.Texture);
public static Asset<Texture2D> GetTexture(this ModProjectile modProjectile) =>
ModContent.Request<Texture2D>(modProjectile.Texture);
/// <summary>
/// Checks if the current player has an item in their armor/accessory slots.
/// </summary>
/// <param name="p">The player.</param>
/// <param name="type">The item ID to check.</param>
/// <returns>Whether or not the item is found.</returns>
public static bool HasItemInArmor(this Player p, int type) => p.armor.Any(t => type == t.type);
public static LeadingConditionRule HideFromBestiary(this IItemDropRule itemDropRule)
{
var conditionRule = new LeadingConditionRule(new Conditions.NeverTrue());
conditionRule.OnFailedConditions(itemDropRule, true);
return conditionRule;
}
public static bool InPillarZone(this Player p)
{
if (!p.ZoneTowerStardust && !p.ZoneTowerVortex && !p.ZoneTowerSolar)
{
return p.ZoneTowerNebula;
}
return true;
}
/// <summary>
/// Helper method for checking if the current item is an armor piece - used for armor prefixes.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>Whether or not the item is an armor piece.</returns>
public static bool IsArmor(this Item item) =>
(item.headSlot != -1 || item.bodySlot != -1 || item.legSlot != -1) && !item.vanity;
/// <summary>
/// A helper method to check if the given Player is touching the ground.
/// </summary>
/// <param name="player">The player.</param>
/// <returns>True if the player is touching the ground, false otherwise.</returns>
public static bool IsOnGround(this Player player) =>
(Main.tile[(int)(player.position.X / 16f), (int)(player.position.Y / 16f) + 3].HasTile &&
Main.tileSolid[
Main.tile[(int)(player.position.X / 16f), (int)(player.position.Y / 16f) + 3].TileType]) ||
(Main.tile[(int)(player.position.X / 16f) + 1, (int)(player.position.Y / 16f) + 3].HasTile &&
Main.tileSolid[
Main.tile[(int)(player.position.X / 16f) + 1, (int)(player.position.Y / 16f) + 3].TileType] &&
player.velocity.Y == 0f);
public static void Load<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TagCompound tag)
where TKey : notnull
{
if (tag.ContainsKey("keys") && tag.ContainsKey("values"))
{
TKey[] keys = tag.Get<TKey[]>("keys");
TValue[] values = tag.Get<TValue[]>("values");
for (int i = 0; i < keys.Length; i++)
{
dictionary[keys[i]] = values[i];
}
}
}
public static Rectangle NewRectVector2(Vector2 v, Vector2 wH) => new((int)v.X, (int)v.Y, (int)wH.X, (int)wH.Y);
/// <summary>
/// This is calls a dynamically compiled expression which calls the vanilla private OpenChest method.
/// </summary>
/// <param name="player">The player.</param>
/// <param name="x">X coordinate of the chest.</param>
/// <param name="y">Y coordinate of the chest.</param>
/// <param name="newChest">The chest index.</param>
public static void OpenChest(this Player player, int x, int y, int newChest) =>
OpenChestAction.Invoke(player, x, y, newChest);
/// <summary>
/// Removes the specified index of a List of int - used for Golem dropping 2 items from its 11-drop loot table.
/// </summary>
/// <param name="list">The List of int.</param>
/// <param name="index">The index.</param>
/// <returns>Returns the item ID of the removed index.</returns>
public static int RemoveAtIndex(this List<int> list, int index)
{
int item = list[index];
list.RemoveAt(index);
return item;
}
/// <summary>
/// Rotate a Vector2.
/// </summary>
/// <param name="spinningPoint">The origin.</param>
/// <param name="radians">The angle in radians to rotate the Vector2 by.</param>
/// <param name="center">The center.</param>
/// <returns>The rotated Vector2.</returns>
public static Vector2 Rotate(this Vector2 spinningPoint, double radians, Vector2 center = default)
{
float num = (float)Math.Cos(radians);
float num2 = (float)Math.Sin(radians);
Vector2 vector = spinningPoint - center;
Vector2 result = center;
result.X += (vector.X * num) - (vector.Y * num2);
result.Y += (vector.X * num2) + (vector.Y * num);
return result;
}
public static TagCompound Save<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
where TKey : notnull
{
TKey[] keys = dictionary.Keys.ToArray();
TValue[] values = dictionary.Values.ToArray();
var tag = new TagCompound();
tag.Set("keys", keys);
tag.Set("values", values);
return tag;
}
/// <summary>
/// Used to draw float coordinates to rounded coordinates to avoid blurry rendering of textures.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The rounded vector.</returns>
public static Vector2 ToNearestPixel(this Vector2 vector) => new((int)(vector.X + 0.5f), (int)(vector.Y + 0.5f));
/// <summary>
/// Helper method for Vampire Teeth and Blah's Knives life steal.
/// </summary>
/// <param name="p">The player.</param>
/// <param name="dmg">The damage to use in the life steal calculation.</param>
/// <param name="position">The position to spawn the life steal projectile at.</param>
public static void VampireHeal(this Player p, int dmg, Vector2 position)
{
float num = dmg * 0.075f;
if ((int)num == 0)
{
return;
}
if (p.lifeSteal <= 0f)
{
return;
}
p.lifeSteal -= num;
int num2 = p.whoAmI;
Projectile.NewProjectile(
p.GetSource_Accessory(new Item(ModContent.ItemType<VampireTeeth>())),
position.X, position.Y, 0f, 0f, ProjectileID.VampireHeal, 0, 0f, p.whoAmI, num2, num);
}
private static Action<Player, int, int, int> CacheOpenChestAction()
{
ParameterExpression paramPlayer = Expression.Parameter(typeof(Player));
ParameterExpression paramX = Expression.Parameter(typeof(int));
ParameterExpression paramY = Expression.Parameter(typeof(int));
ParameterExpression paramNewChest = Expression.Parameter(typeof(int));
MethodInfo methodInfo = typeof(Player).GetMethod("OpenChest", BindingFlags.NonPublic | BindingFlags.Instance,
new[] { typeof(int), typeof(int), typeof(int) })!;
return Expression
.Lambda<Action<Player, int, int, int>>(
Expression.Call(
paramPlayer,
methodInfo,
new Expression[] { paramX, paramY, paramNewChest }),
paramPlayer, paramX, paramY, paramNewChest).Compile();
}
}