-
Notifications
You must be signed in to change notification settings - Fork 497
/
Utils.java
549 lines (457 loc) · 16.1 KB
/
Utils.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.IAreaProvider;
import buildcraft.api.core.Position;
import buildcraft.api.transport.IInjectable;
import buildcraft.api.transport.IPipeTile;
import buildcraft.compat.CompatHooks;
import buildcraft.core.DefaultProps;
import buildcraft.core.EntityBlock;
import buildcraft.core.IDropControlInventory;
import buildcraft.core.IFramePipeConnection;
import buildcraft.core.LaserData;
import buildcraft.core.LaserKind;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.inventory.ITransactor;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.network.BuildCraftPacket;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.energy.TileEngine;
public final class Utils {
public static final Random RANDOM = new Random();
private static final List<ForgeDirection> directions = new ArrayList<ForgeDirection>(Arrays.asList(ForgeDirection.VALID_DIRECTIONS));
/**
* Deactivate constructor
*/
private Utils() {
}
/**
* Tries to add the passed stack to any valid inventories around the given
* coordinates.
*
* @param stack
* @param world
* @param x
* @param y
* @param z
* @return amount used
*/
public static int addToRandomInventoryAround(World world, int x, int y, int z, ItemStack stack) {
Collections.shuffle(directions);
for (ForgeDirection orientation : directions) {
Position pos = new Position(x, y, z, orientation);
pos.moveForwards(1.0);
TileEntity tileInventory = BlockUtils.getTileEntity(world, (int) pos.x, (int) pos.y, (int) pos.z);
ITransactor transactor = Transactor.getTransactorFor(tileInventory);
if (transactor != null && !(tileInventory instanceof TileEngine) && transactor.add(stack, orientation.getOpposite(), false).stackSize > 0) {
return transactor.add(stack, orientation.getOpposite(), true).stackSize;
}
}
return 0;
}
/**
* Returns the cardinal direction of the entity depending on its
* rotationYaw
*/
public static ForgeDirection get2dOrientation(EntityLivingBase entityliving) {
ForgeDirection[] orientationTable = { ForgeDirection.SOUTH,
ForgeDirection.WEST, ForgeDirection.NORTH, ForgeDirection.EAST };
int orientationIndex = MathHelper.floor_double((entityliving.rotationYaw + 45.0) / 90.0) & 3;
return orientationTable[orientationIndex];
}
/*
* FIXME This is only kept here for the purpose of get3dOrientation, which
* should probably be removed following the same principles
*/
@Deprecated
private static ForgeDirection get2dOrientation(Position pos1, Position pos2) {
double dX = pos1.x - pos2.x;
double dZ = pos1.z - pos2.z;
double angle = Math.atan2(dZ, dX) / Math.PI * 180 + 180;
if (angle < 45 || angle > 315) {
return ForgeDirection.EAST;
} else if (angle < 135) {
return ForgeDirection.SOUTH;
} else if (angle < 225) {
return ForgeDirection.WEST;
} else {
return ForgeDirection.NORTH;
}
}
public static ForgeDirection get3dOrientation(Position pos1, Position pos2) {
double dX = pos1.x - pos2.x;
double dY = pos1.y - pos2.y;
double angle = Math.atan2(dY, dX) / Math.PI * 180 + 180;
if (angle > 45 && angle < 135) {
return ForgeDirection.UP;
} else if (angle > 225 && angle < 315) {
return ForgeDirection.DOWN;
} else {
return get2dOrientation(pos1, pos2);
}
}
/**
* Look around the tile given in parameter in all 6 position, tries to add
* the items to a random injectable tile around. Will make sure that the location
* from which the items are coming from (identified by the from parameter)
* isn't used again so that entities doesn't go backwards. Returns true if
* successful, false otherwise.
*/
public static int addToRandomInjectableAround(World world, int x, int y, int z, ForgeDirection from, ItemStack stack) {
List<IInjectable> possiblePipes = new ArrayList<IInjectable>();
List<ForgeDirection> pipeDirections = new ArrayList<ForgeDirection>();
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
if (from.getOpposite() == side) {
continue;
}
Position pos = new Position(x, y, z, side);
pos.moveForwards(1.0);
TileEntity tile = BlockUtils.getTileEntity(world, (int) pos.x, (int) pos.y, (int) pos.z);
if (tile instanceof IInjectable) {
if (!((IInjectable) tile).canInjectItems(side.getOpposite())) {
continue;
}
possiblePipes.add((IInjectable) tile);
pipeDirections.add(side.getOpposite());
} else {
IInjectable wrapper = CompatHooks.INSTANCE.getInjectableWrapper(tile, side);
if (wrapper != null) {
possiblePipes.add(wrapper);
pipeDirections.add(side.getOpposite());
}
}
}
if (possiblePipes.size() > 0) {
int choice = RANDOM.nextInt(possiblePipes.size());
IInjectable pipeEntry = possiblePipes.get(choice);
return pipeEntry.injectItem(stack, true, pipeDirections.get(choice));
}
return 0;
}
public static TileEntity getTile(World world, Position pos, ForgeDirection step) {
Position tmp = new Position(pos);
tmp.orientation = step;
tmp.moveForwards(1.0);
return BlockUtils.getTileEntity(world, (int) tmp.x, (int) tmp.y, (int) tmp.z);
}
public static IAreaProvider getNearbyAreaProvider(World world, int i, int j, int k) {
TileEntity a1 = BlockUtils.getTileEntity(world, i + 1, j, k);
if (a1 instanceof IAreaProvider) {
return (IAreaProvider) a1;
}
TileEntity a2 = BlockUtils.getTileEntity(world, i - 1, j, k);
if (a2 instanceof IAreaProvider) {
return (IAreaProvider) a2;
}
TileEntity a3 = BlockUtils.getTileEntity(world, i, j, k + 1);
if (a3 instanceof IAreaProvider) {
return (IAreaProvider) a3;
}
TileEntity a4 = BlockUtils.getTileEntity(world, i, j, k - 1);
if (a4 instanceof IAreaProvider) {
return (IAreaProvider) a4;
}
TileEntity a5 = BlockUtils.getTileEntity(world, i, j + 1, k);
if (a5 instanceof IAreaProvider) {
return (IAreaProvider) a5;
}
TileEntity a6 = BlockUtils.getTileEntity(world, i, j - 1, k);
if (a6 instanceof IAreaProvider) {
return (IAreaProvider) a6;
}
return null;
}
public static EntityBlock createLaser(World world, Position p1, Position p2, LaserKind kind) {
if (p1.equals(p2)) {
return null;
}
double iSize = p2.x - p1.x;
double jSize = p2.y - p1.y;
double kSize = p2.z - p1.z;
double i = p1.x;
double j = p1.y;
double k = p1.z;
if (iSize != 0) {
i += 0.5;
j += 0.45;
k += 0.45;
jSize = 0.10;
kSize = 0.10;
} else if (jSize != 0) {
i += 0.45;
j += 0.5;
k += 0.45;
iSize = 0.10;
kSize = 0.10;
} else if (kSize != 0) {
i += 0.45;
j += 0.45;
k += 0.5;
iSize = 0.10;
jSize = 0.10;
}
EntityBlock block = CoreProxy.proxy.newEntityBlock(world, i, j, k, iSize, jSize, kSize, kind);
block.setBrightness(210);
world.spawnEntityInWorld(block);
return block;
}
public static EntityBlock[] createLaserBox(World world, double xMin, double yMin, double zMin, double xMax, double yMax, double zMax, LaserKind kind) {
EntityBlock[] lasers = new EntityBlock[12];
Position[] p = new Position[8];
p[0] = new Position(xMin, yMin, zMin);
p[1] = new Position(xMax, yMin, zMin);
p[2] = new Position(xMin, yMax, zMin);
p[3] = new Position(xMax, yMax, zMin);
p[4] = new Position(xMin, yMin, zMax);
p[5] = new Position(xMax, yMin, zMax);
p[6] = new Position(xMin, yMax, zMax);
p[7] = new Position(xMax, yMax, zMax);
lasers[0] = Utils.createLaser(world, p[0], p[1], kind);
lasers[1] = Utils.createLaser(world, p[0], p[2], kind);
lasers[2] = Utils.createLaser(world, p[2], p[3], kind);
lasers[3] = Utils.createLaser(world, p[1], p[3], kind);
lasers[4] = Utils.createLaser(world, p[4], p[5], kind);
lasers[5] = Utils.createLaser(world, p[4], p[6], kind);
lasers[6] = Utils.createLaser(world, p[5], p[7], kind);
lasers[7] = Utils.createLaser(world, p[6], p[7], kind);
lasers[8] = Utils.createLaser(world, p[0], p[4], kind);
lasers[9] = Utils.createLaser(world, p[1], p[5], kind);
lasers[10] = Utils.createLaser(world, p[2], p[6], kind);
lasers[11] = Utils.createLaser(world, p[3], p[7], kind);
return lasers;
}
public static LaserData[] createLaserDataBox(double xMin, double yMin, double zMin, double xMax, double yMax, double zMax) {
LaserData[] lasers = new LaserData[12];
Position[] p = new Position[8];
p[0] = new Position(xMin, yMin, zMin);
p[1] = new Position(xMax, yMin, zMin);
p[2] = new Position(xMin, yMax, zMin);
p[3] = new Position(xMax, yMax, zMin);
p[4] = new Position(xMin, yMin, zMax);
p[5] = new Position(xMax, yMin, zMax);
p[6] = new Position(xMin, yMax, zMax);
p[7] = new Position(xMax, yMax, zMax);
lasers[0] = new LaserData (p[0], p[1]);
lasers[1] = new LaserData (p[0], p[2]);
lasers[2] = new LaserData (p[2], p[3]);
lasers[3] = new LaserData (p[1], p[3]);
lasers[4] = new LaserData (p[4], p[5]);
lasers[5] = new LaserData (p[4], p[6]);
lasers[6] = new LaserData (p[5], p[7]);
lasers[7] = new LaserData (p[6], p[7]);
lasers[8] = new LaserData (p[0], p[4]);
lasers[9] = new LaserData (p[1], p[5]);
lasers[10] = new LaserData (p[2], p[6]);
lasers[11] = new LaserData (p[3], p[7]);
return lasers;
}
public static void preDestroyBlock(World world, int i, int j, int k) {
TileEntity tile = world.getTileEntity(i, j, k);
if (tile instanceof IInventory && !world.isRemote) {
if (!(tile instanceof IDropControlInventory) || ((IDropControlInventory) tile).doDrop()) {
InvUtils.dropItems(world, (IInventory) tile, i, j, k);
InvUtils.wipeInventory((IInventory) tile);
}
}
if (tile instanceof TileBuildCraft) {
((TileBuildCraft) tile).destroy();
}
}
public static boolean isFakePlayer(EntityPlayer player) {
if (player instanceof FakePlayer) {
return true;
}
// Tip donated by skyboy - addedToChunk must be set to false by a fake player
// or it becomes a chunk-loading entity.
if (!player.addedToChunk) {
return true;
}
return false;
}
public static boolean checkPipesConnections(TileEntity tile1, TileEntity tile2) {
if (tile1 == null || tile2 == null) {
return false;
}
if (!(tile1 instanceof IPipeTile) && !(tile2 instanceof IPipeTile)) {
return false;
}
ForgeDirection o = ForgeDirection.UNKNOWN;
if (tile1.xCoord - 1 == tile2.xCoord) {
o = ForgeDirection.WEST;
} else if (tile1.xCoord + 1 == tile2.xCoord) {
o = ForgeDirection.EAST;
} else if (tile1.yCoord - 1 == tile2.yCoord) {
o = ForgeDirection.DOWN;
} else if (tile1.yCoord + 1 == tile2.yCoord) {
o = ForgeDirection.UP;
} else if (tile1.zCoord - 1 == tile2.zCoord) {
o = ForgeDirection.NORTH;
} else if (tile1.zCoord + 1 == tile2.zCoord) {
o = ForgeDirection.SOUTH;
}
if (tile1 instanceof IPipeTile && !((IPipeTile) tile1).isPipeConnected(o)) {
return false;
}
if (tile2 instanceof IPipeTile && !((IPipeTile) tile2).isPipeConnected(o.getOpposite())) {
return false;
}
return true;
}
public static boolean checkLegacyPipesConnections(IBlockAccess blockAccess, int x1, int y1, int z1, int x2, int y2, int z2) {
Block b1 = blockAccess.getBlock(x1, y1, z1);
Block b2 = blockAccess.getBlock(x2, y2, z2);
if (!(b1 instanceof IFramePipeConnection) && !(b2 instanceof IFramePipeConnection)) {
return false;
}
if (b1 instanceof IFramePipeConnection && !((IFramePipeConnection) b1).isPipeConnected(blockAccess, x1, y1, z1, x2, y2, z2)) {
return false;
}
if (b2 instanceof IFramePipeConnection && !((IFramePipeConnection) b2).isPipeConnected(blockAccess, x2, y2, z2, x1, y1, z1)) {
return false;
}
return true;
}
public static int[] createSlotArray(int first, int count) {
int[] slots = new int[count];
for (int k = first; k < first + count; k++) {
slots[k - first] = k;
}
return slots;
}
public static void writeUTF (ByteBuf data, String str) {
try {
if (str == null) {
data.writeInt(0);
return;
}
byte [] b = str.getBytes("UTF-8");
data.writeInt (b.length);
data.writeBytes(b);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
data.writeInt (0);
}
}
public static String readUTF (ByteBuf data) {
try {
int len = data.readInt();
if (len == 0) {
return "";
}
byte [] b = new byte [len];
data.readBytes(b);
return new String (b, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static void writeNBT (ByteBuf data, NBTTagCompound nbt) {
try {
byte[] compressed = CompressedStreamTools.compress(nbt);
data.writeInt(compressed.length);
data.writeBytes(compressed);
} catch (IOException e) {
e.printStackTrace();
}
}
public static NBTTagCompound readNBT(ByteBuf data) {
try {
int length = data.readInt();
byte[] compressed = new byte[length];
data.readBytes(compressed);
return CompressedStreamTools.func_152457_a(compressed, NBTSizeTracker.field_152451_a);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void writeStack (ByteBuf data, ItemStack stack) {
if (stack == null || stack.getItem() == null || stack.stackSize < 0) {
data.writeByte(0);
} else {
// ItemStacks generally shouldn't have a stackSize above 64,
// so we use this "trick" to save bandwidth by storing it in the first byte.
data.writeByte((MathUtils.clamp(stack.stackSize + 1, 0, 64) & 0x7F) | (stack.hasTagCompound() ? 128 : 0));
data.writeShort(Item.getIdFromItem(stack.getItem()));
data.writeShort(stack.getItemDamage());
if (stack.hasTagCompound()) {
Utils.writeNBT(data, stack.getTagCompound());
}
}
}
public static ItemStack readStack(ByteBuf data) {
int flags = data.readUnsignedByte();
if (flags == 0) {
return null;
} else {
boolean hasCompound = (flags & 0x80) != 0;
int stackSize = (flags & 0x7F) - 1;
int itemId = data.readUnsignedShort();
int itemDamage = data.readShort();
ItemStack stack = new ItemStack(Item.getItemById(itemId), stackSize, itemDamage);
if (hasCompound) {
stack.setTagCompound(Utils.readNBT(data));
}
return stack;
}
}
public static void writeByteArray(ByteBuf stream, byte[] data) {
stream.writeInt(data.length);
stream.writeBytes(data);
}
public static byte[] readByteArray(ByteBuf stream) {
byte[] data = new byte[stream.readInt()];
stream.readBytes(data, 0, data.length);
return data;
}
/**
* This subprogram transforms a packet into a FML packet to be send in the
* minecraft default packet mechanism. This always use BC-CORE as a
* channel, and as a result, should use discriminators declared there.
*
* WARNING! The implementation of this subprogram relies on the internal
* behavior of #FMLIndexedMessageToMessageCodec (in particular the encode
* member). It is probably opening a maintenance issue and should be
* replaced eventually by some more solid mechanism.
*/
public static FMLProxyPacket toPacket (BuildCraftPacket packet, int discriminator) {
ByteBuf buf = Unpooled.buffer();
buf.writeByte((byte) discriminator);
packet.writeData(buf);
return new FMLProxyPacket(buf, DefaultProps.NET_CHANNEL_NAME + "-CORE");
}
}