-
Notifications
You must be signed in to change notification settings - Fork 3
/
ThaumcraftApiHelper.java
369 lines (321 loc) · 11.7 KB
/
ThaumcraftApiHelper.java
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package thaumcraft.api;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.List;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import thaumcraft.api.aspects.IEssentiaTransport;
public class ThaumcraftApiHelper {
public static boolean areItemsEqual(ItemStack s1,ItemStack s2)
{
if (s1.isItemStackDamageable() && s2.isItemStackDamageable())
{
return s1.getItem() == s2.getItem();
} else
return s1.getItem() == s2.getItem() && s1.getItemDamage() == s2.getItemDamage();
}
/**
* Notifies thaumcraft that something with regards to runic shielding has changed and that it should
* rescan worn items to determine what the max shielding value is.
* It automatically rescans once every 5 seconds, but this makes it happen sooner.
* @param entity
*/
public static void markRunicDirty(Entity entity) {
ThaumcraftApi.internalMethods.markRunicDirty(entity);
}
public static boolean containsMatch(boolean strict, ItemStack[] inputs, List<ItemStack> targets)
{
for (ItemStack input : inputs)
{
for (ItemStack target : targets)
{
if (OreDictionary.itemMatches(target, input, strict))
{
return true;
}
}
}
return false;
}
public static boolean areItemStacksEqualForCrafting(ItemStack stack0, Object in)
{
if (stack0==null && in!=null) return false;
if (stack0!=null && in==null) return false;
if (stack0==null && in==null) return true;
if (in instanceof Object[]) return true;
if (in instanceof String) {
List<ItemStack> l = OreDictionary.getOres((String) in);
return containsMatch(false, new ItemStack[]{stack0}, l);
}
if (in instanceof ItemStack) {
//nbt
boolean t1=areItemStackTagsEqualForCrafting(stack0, (ItemStack) in);
if (!t1) return false;
return OreDictionary.itemMatches((ItemStack) in, stack0, false);
}
return false;
}
public static boolean areItemStackTagsEqualForCrafting(ItemStack slotItem,ItemStack recipeItem)
{
if (recipeItem == null || slotItem == null) return false;
if (recipeItem.getTagCompound()!=null && slotItem.getTagCompound()==null ) return false;
if (recipeItem.getTagCompound()==null ) return true;
Iterator iterator = recipeItem.getTagCompound().getKeySet().iterator();
while (iterator.hasNext())
{
String s = (String)iterator.next();
if (slotItem.getTagCompound().hasKey(s)) {
if (!slotItem.getTagCompound().getTag(s).toString().equals(
recipeItem.getTagCompound().getTag(s).toString())) {
return false;
}
} else {
return false;
}
}
return true;
}
public static TileEntity getConnectableTile(World world, BlockPos pos, EnumFacing face) {
TileEntity te = world.getTileEntity(pos.offset(face));
if (te instanceof IEssentiaTransport && ((IEssentiaTransport)te).isConnectable(face.getOpposite()))
return te;
else
return null;
}
public static TileEntity getConnectableTile(IBlockAccess world, BlockPos pos, EnumFacing face) {
TileEntity te = world.getTileEntity(pos.offset(face));
if (te instanceof IEssentiaTransport && ((IEssentiaTransport)te).isConnectable(face.getOpposite()))
return te;
else
return null;
}
// private static HashMap<Integer, AspectList> allAspects= new HashMap<Integer, AspectList>();
// private static HashMap<Integer, AspectList> allCompoundAspects= new HashMap<Integer, AspectList>();
//
// public static AspectList getAllAspects(int amount) {
// if (allAspects.get(amount)==null) {
// AspectList al = new AspectList();
// for (Aspect aspect:Aspect.aspects.values()) {
// al.add(aspect, amount);
// }
// allAspects.put(amount, al);
// }
// return allAspects.get(amount);
// }
//
// public static AspectList getAllCompoundAspects(int amount) {
// if (allCompoundAspects.get(amount)==null) {
// AspectList al = new AspectList();
// for (Aspect aspect:Aspect.getCompoundAspects()) {
// al.add(aspect, amount);
// }
// allCompoundAspects.put(amount, al);
// }
// return allCompoundAspects.get(amount);
// }
public static MovingObjectPosition rayTraceIgnoringSource(World world, Vec3 v1, Vec3 v2,
boolean bool1, boolean bool2, boolean bool3)
{
if (!Double.isNaN(v1.xCoord) && !Double.isNaN(v1.yCoord) && !Double.isNaN(v1.zCoord))
{
if (!Double.isNaN(v2.xCoord) && !Double.isNaN(v2.yCoord) && !Double.isNaN(v2.zCoord))
{
int i = MathHelper.floor_double(v2.xCoord);
int j = MathHelper.floor_double(v2.yCoord);
int k = MathHelper.floor_double(v2.zCoord);
int l = MathHelper.floor_double(v1.xCoord);
int i1 = MathHelper.floor_double(v1.yCoord);
int j1 = MathHelper.floor_double(v1.zCoord);
IBlockState block = world.getBlockState(new BlockPos(l, i1, j1));
MovingObjectPosition movingobjectposition2 = null;
int k1 = 200;
while (k1-- >= 0)
{
if (Double.isNaN(v1.xCoord) || Double.isNaN(v1.yCoord) || Double.isNaN(v1.zCoord))
{
return null;
}
if (l == i && i1 == j && j1 == k)
{
continue;
}
boolean flag6 = true;
boolean flag3 = true;
boolean flag4 = true;
double d0 = 999.0D;
double d1 = 999.0D;
double d2 = 999.0D;
if (i > l)
{
d0 = (double)l + 1.0D;
}
else if (i < l)
{
d0 = (double)l + 0.0D;
}
else
{
flag6 = false;
}
if (j > i1)
{
d1 = (double)i1 + 1.0D;
}
else if (j < i1)
{
d1 = (double)i1 + 0.0D;
}
else
{
flag3 = false;
}
if (k > j1)
{
d2 = (double)j1 + 1.0D;
}
else if (k < j1)
{
d2 = (double)j1 + 0.0D;
}
else
{
flag4 = false;
}
double d3 = 999.0D;
double d4 = 999.0D;
double d5 = 999.0D;
double d6 = v2.xCoord - v1.xCoord;
double d7 = v2.yCoord - v1.yCoord;
double d8 = v2.zCoord - v1.zCoord;
if (flag6)
{
d3 = (d0 - v1.xCoord) / d6;
}
if (flag3)
{
d4 = (d1 - v1.yCoord) / d7;
}
if (flag4)
{
d5 = (d2 - v1.zCoord) / d8;
}
if (d3 == -0.0D)
{
d3 = -1.0E-4D;
}
if (d4 == -0.0D)
{
d4 = -1.0E-4D;
}
if (d5 == -0.0D)
{
d5 = -1.0E-4D;
}
EnumFacing enumfacing;
if (d3 < d4 && d3 < d5)
{
enumfacing = i > l ? EnumFacing.WEST : EnumFacing.EAST;
v1 = new Vec3(d0, v1.yCoord + d7 * d3, v1.zCoord + d8 * d3);
}
else if (d4 < d5)
{
enumfacing = j > i1 ? EnumFacing.DOWN : EnumFacing.UP;
v1 = new Vec3(v1.xCoord + d6 * d4, d1, v1.zCoord + d8 * d4);
}
else
{
enumfacing = k > j1 ? EnumFacing.NORTH : EnumFacing.SOUTH;
v1 = new Vec3(v1.xCoord + d6 * d5, v1.yCoord + d7 * d5, d2);
}
l = MathHelper.floor_double(v1.xCoord) - (enumfacing == EnumFacing.EAST ? 1 : 0);
i1 = MathHelper.floor_double(v1.yCoord) - (enumfacing == EnumFacing.UP ? 1 : 0);
j1 = MathHelper.floor_double(v1.zCoord) - (enumfacing == EnumFacing.SOUTH ? 1 : 0);
IBlockState block1 = world.getBlockState(new BlockPos(l, i1, j1));
if (!bool2 || block1.getBlock().getCollisionBoundingBox(world, new BlockPos(l, i1, j1), block1) != null)
{
if (block1.getBlock().canCollideCheck(block1, bool1))
{
MovingObjectPosition movingobjectposition1 = block1.getBlock().collisionRayTrace(world, new BlockPos(l, i1, j1), v1, v2);
if (movingobjectposition1 != null)
{
return movingobjectposition1;
}
}
else
{
movingobjectposition2 = new MovingObjectPosition(MovingObjectPosition.MovingObjectType.MISS, v1, enumfacing, new BlockPos(l, i1, j1));
}
}
}
return bool3 ? movingobjectposition2 : null;
}
else
{
return null;
}
}
else
{
return null;
}
}
public static Object getNBTDataFromId(NBTTagCompound nbt, byte id, String key) {
switch (id) {
case 1: return nbt.getByte(key);
case 2: return nbt.getShort(key);
case 3: return nbt.getInteger(key);
case 4: return nbt.getLong(key);
case 5: return nbt.getFloat(key);
case 6: return nbt.getDouble(key);
case 7: return nbt.getByteArray(key);
case 8: return nbt.getString(key);
case 9: return nbt.getTagList(key, (byte) 10);
case 10: return nbt.getTag(key);
case 11: return nbt.getIntArray(key);
default: return null;
}
}
public static int setByteInInt(int data, byte b, int index)
{
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(0,data);
bb.put(index, b);
return bb.getInt(0);
}
public static byte getByteInInt(int data, int index) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(0,data);
return bb.get(index);
}
public static long setByteInLong(long data, byte b, int index)
{
ByteBuffer bb = ByteBuffer.allocate(8);
bb.putLong(0,data);
bb.put(index, b);
return bb.getLong(0);
}
public static byte getByteInLong(long data, int index) {
ByteBuffer bb = ByteBuffer.allocate(8);
bb.putLong(0,data);
return bb.get(index);
}
public static int setNibbleInInt(int data, int nibble, int nibbleIndex)
{
int shift = nibbleIndex * 4;
return (data & ~(0xf << shift)) | (nibble << shift);
}
public static int getNibbleInInt(int data, int nibbleIndex) {
return (data >> (nibbleIndex << 2)) & 0xF;
}
}