2012-05-09 22:43:05 +02:00
|
|
|
/**
|
2014-02-15 09:21:40 +01:00
|
|
|
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
|
|
|
|
* http://www.mod-buildcraft.com
|
2012-05-09 22:43:05 +02:00
|
|
|
*
|
2014-02-15 09:21:40 +01:00
|
|
|
* 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
|
2012-05-09 22:43:05 +02:00
|
|
|
* http://www.mod-buildcraft.com/MMPL-1.0.txt
|
|
|
|
*/
|
2012-09-05 22:29:38 +02:00
|
|
|
package buildcraft.core.utils;
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2014-01-13 20:26:16 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2014-02-10 13:04:20 +01:00
|
|
|
import io.netty.buffer.Unpooled;
|
2014-01-11 10:46:30 +01:00
|
|
|
|
2013-07-16 12:50:45 +02:00
|
|
|
import java.io.IOException;
|
2014-01-13 20:26:16 +01:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2013-07-12 23:21:44 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
2014-01-11 10:46:30 +01:00
|
|
|
|
2013-07-12 23:21:44 +02:00
|
|
|
import net.minecraft.block.Block;
|
2014-01-11 10:46:30 +01:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2013-07-12 23:21:44 +02:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-02-09 12:29:21 +01:00
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
2014-06-28 17:57:16 +02:00
|
|
|
import net.minecraft.nbt.NBTSizeTracker;
|
2013-07-12 23:21:44 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-01-11 10:46:30 +01:00
|
|
|
import net.minecraft.util.MathHelper;
|
2013-07-12 23:21:44 +02:00
|
|
|
import net.minecraft.world.IBlockAccess;
|
|
|
|
import net.minecraft.world.World;
|
2014-01-12 19:12:40 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-02-18 20:42:57 +01:00
|
|
|
import buildcraft.BuildCraftCore;
|
|
|
|
import buildcraft.api.core.IAreaProvider;
|
|
|
|
import buildcraft.api.core.LaserKind;
|
|
|
|
import buildcraft.api.core.Position;
|
|
|
|
import buildcraft.api.transport.IPipeTile;
|
|
|
|
import buildcraft.api.transport.IPipeTile.PipeType;
|
|
|
|
import buildcraft.core.BlockIndex;
|
|
|
|
import buildcraft.core.DefaultProps;
|
|
|
|
import buildcraft.core.EntityBlock;
|
|
|
|
import buildcraft.core.IDropControlInventory;
|
|
|
|
import buildcraft.core.IFramePipeConnection;
|
|
|
|
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.network.ISynchronizedTile;
|
|
|
|
import buildcraft.core.network.PacketUpdate;
|
|
|
|
import buildcraft.core.proxy.CoreProxy;
|
|
|
|
import buildcraft.energy.TileEngine;
|
|
|
|
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
|
2012-07-25 12:45:15 +02:00
|
|
|
|
2012-05-09 22:43:05 +02:00
|
|
|
public class Utils {
|
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
public static final Random RANDOM = new Random();
|
2013-05-25 04:40:42 +02:00
|
|
|
private static final List<ForgeDirection> directions = new ArrayList<ForgeDirection>(Arrays.asList(ForgeDirection.VALID_DIRECTIONS));
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2014-02-09 12:29:21 +01:00
|
|
|
public enum NBTTag_Types {
|
2014-02-18 20:42:57 +01:00
|
|
|
NBTTagEnd, NBTTagByte, NBTTagShort,
|
|
|
|
NBTTagInt, NBTTagLong, NBTTagFloat,
|
2014-02-09 12:29:21 +01:00
|
|
|
NBTTagDouble, NBTTagByteArray, NBTTagString,
|
|
|
|
NBTTagList, NBTTagCompound, NBTTagIntArray
|
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
2013-06-12 19:40:52 +02:00
|
|
|
/* IINVENTORY HELPERS */
|
2012-09-08 17:40:38 +02:00
|
|
|
/**
|
2013-05-25 04:40:42 +02:00
|
|
|
* Tries to add the passed stack to any valid inventories around the given
|
|
|
|
* coordinates.
|
|
|
|
*
|
2012-09-08 17:40:38 +02:00
|
|
|
* @param stack
|
|
|
|
* @param world
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2013-07-24 22:07:09 +02:00
|
|
|
* @return amount used
|
2012-09-08 17:40:38 +02:00
|
|
|
*/
|
2013-07-24 22:07:09 +02:00
|
|
|
public static int addToRandomInventoryAround(World world, int x, int y, int z, ItemStack stack) {
|
2013-05-25 04:40:42 +02:00
|
|
|
Collections.shuffle(directions);
|
|
|
|
for (ForgeDirection orientation : directions) {
|
2012-09-08 16:20:21 +02:00
|
|
|
Position pos = new Position(x, y, z, orientation);
|
|
|
|
pos.moveForwards(1.0);
|
|
|
|
|
2014-02-08 14:47:31 +01:00
|
|
|
TileEntity tileInventory = world.getTileEntity((int) pos.x, (int) pos.y, (int) pos.z);
|
2012-09-08 16:20:21 +02:00
|
|
|
ITransactor transactor = Transactor.getTransactorFor(tileInventory);
|
2013-05-23 14:13:08 +02:00
|
|
|
if (transactor != null && !(tileInventory instanceof TileEngine) && transactor.add(stack, orientation.getOpposite(), false).stackSize > 0) {
|
2013-07-24 22:07:09 +02:00
|
|
|
return transactor.add(stack, orientation.getOpposite(), true).stackSize;
|
2012-12-17 23:30:54 +01:00
|
|
|
}
|
2012-09-08 16:20:21 +02:00
|
|
|
}
|
2013-07-24 22:07:09 +02:00
|
|
|
return 0;
|
2012-10-02 05:37:17 +02:00
|
|
|
|
2012-09-08 16:20:21 +02:00
|
|
|
}
|
2012-10-02 05:37:17 +02:00
|
|
|
|
2014-01-11 10:46:30 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-01-11 10:56:01 +01:00
|
|
|
@Deprecated
|
2014-01-11 10:46:30 +01:00
|
|
|
private static ForgeDirection get2dOrientation(Position pos1, Position pos2) {
|
2012-05-09 22:43:05 +02:00
|
|
|
double Dx = pos1.x - pos2.x;
|
2012-06-04 22:48:18 +02:00
|
|
|
double Dz = pos1.z - pos2.z;
|
|
|
|
double angle = Math.atan2(Dz, Dx) / Math.PI * 180 + 180;
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (angle < 45 || angle > 315) {
|
2012-11-10 07:43:12 +01:00
|
|
|
return ForgeDirection.EAST;
|
2013-05-29 03:50:03 +02:00
|
|
|
} else if (angle < 135) {
|
2012-11-10 07:43:12 +01:00
|
|
|
return ForgeDirection.SOUTH;
|
2013-05-29 03:50:03 +02:00
|
|
|
} else if (angle < 225) {
|
2012-11-10 07:43:12 +01:00
|
|
|
return ForgeDirection.WEST;
|
2013-05-29 03:50:03 +02:00
|
|
|
} else {
|
2012-11-10 07:43:12 +01:00
|
|
|
return ForgeDirection.NORTH;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 07:43:12 +01:00
|
|
|
public static ForgeDirection get3dOrientation(Position pos1, Position pos2) {
|
2012-05-09 22:43:05 +02:00
|
|
|
double Dx = pos1.x - pos2.x;
|
2012-06-04 22:48:18 +02:00
|
|
|
double Dy = pos1.y - pos2.y;
|
|
|
|
double angle = Math.atan2(Dy, Dx) / Math.PI * 180 + 180;
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (angle > 45 && angle < 135) {
|
2012-11-10 07:43:12 +01:00
|
|
|
return ForgeDirection.UP;
|
2013-05-29 03:50:03 +02:00
|
|
|
} else if (angle > 225 && angle < 315) {
|
2012-11-10 07:43:12 +01:00
|
|
|
return ForgeDirection.DOWN;
|
2013-05-29 03:50:03 +02:00
|
|
|
} else {
|
2012-05-09 22:43:05 +02:00
|
|
|
return get2dOrientation(pos1, pos2);
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-29 03:50:03 +02:00
|
|
|
* Look around the tile given in parameter in all 6 position, tries to add
|
|
|
|
* the items to a random pipe entry 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.
|
2012-05-09 22:43:05 +02:00
|
|
|
*/
|
2013-07-24 22:07:09 +02:00
|
|
|
public static int addToRandomPipeAround(World world, int x, int y, int z, ForgeDirection from, ItemStack stack) {
|
|
|
|
List<IPipeTile> possiblePipes = new ArrayList<IPipeTile>();
|
2013-07-24 06:48:51 +02:00
|
|
|
List<ForgeDirection> pipeDirections = new ArrayList<ForgeDirection>();
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-07-24 22:07:09 +02:00
|
|
|
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
|
|
|
|
if (from.getOpposite() == side)
|
2012-05-09 22:43:05 +02:00
|
|
|
continue;
|
|
|
|
|
2013-07-24 22:07:09 +02:00
|
|
|
Position pos = new Position(x, y, z, side);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
|
|
|
pos.moveForwards(1.0);
|
|
|
|
|
2014-02-08 14:47:31 +01:00
|
|
|
TileEntity tile = world.getTileEntity((int) pos.x, (int) pos.y, (int) pos.z);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-07-24 22:07:09 +02:00
|
|
|
if (tile instanceof IPipeTile) {
|
|
|
|
IPipeTile pipe = (IPipeTile) tile;
|
|
|
|
if (pipe.getPipeType() != PipeType.ITEM)
|
|
|
|
continue;
|
|
|
|
if (!pipe.isPipeConnected(side.getOpposite()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
possiblePipes.add(pipe);
|
|
|
|
pipeDirections.add(side.getOpposite());
|
2012-10-05 18:01:50 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (possiblePipes.size() > 0) {
|
2013-07-24 22:07:09 +02:00
|
|
|
int choice = RANDOM.nextInt(possiblePipes.size());
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-07-24 22:07:09 +02:00
|
|
|
IPipeTile pipeEntry = possiblePipes.get(choice);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-07-24 22:07:09 +02:00
|
|
|
return pipeEntry.injectItem(stack, true, pipeDirections.get(choice));
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2013-07-24 22:07:09 +02:00
|
|
|
return 0;
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 07:43:12 +01:00
|
|
|
public static TileEntity getTile(World world, Position pos, ForgeDirection step) {
|
2012-06-04 22:48:18 +02:00
|
|
|
Position tmp = new Position(pos);
|
|
|
|
tmp.orientation = step;
|
|
|
|
tmp.moveForwards(1.0);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2014-02-08 14:47:31 +01:00
|
|
|
return world.getTileEntity((int) tmp.x, (int) tmp.y, (int) tmp.z);
|
2012-06-04 22:48:18 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2012-06-08 02:13:31 +02:00
|
|
|
public static IAreaProvider getNearbyAreaProvider(World world, int i, int j, int k) {
|
2014-02-08 14:47:31 +01:00
|
|
|
TileEntity a1 = world.getTileEntity(i + 1, j, k);
|
|
|
|
TileEntity a2 = world.getTileEntity(i - 1, j, k);
|
|
|
|
TileEntity a3 = world.getTileEntity(i, j, k + 1);
|
|
|
|
TileEntity a4 = world.getTileEntity(i, j, k - 1);
|
|
|
|
TileEntity a5 = world.getTileEntity(i, j + 1, k);
|
|
|
|
TileEntity a6 = world.getTileEntity(i, j - 1, k);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (a1 instanceof IAreaProvider) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return (IAreaProvider) a1;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (a2 instanceof IAreaProvider) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return (IAreaProvider) a2;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (a3 instanceof IAreaProvider) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return (IAreaProvider) a3;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (a4 instanceof IAreaProvider) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return (IAreaProvider) a4;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (a5 instanceof IAreaProvider) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return (IAreaProvider) a5;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (a6 instanceof IAreaProvider) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return (IAreaProvider) a6;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2012-06-04 22:48:18 +02:00
|
|
|
return null;
|
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2012-06-08 02:13:31 +02:00
|
|
|
public static EntityBlock createLaser(World world, Position p1, Position p2, LaserKind kind) {
|
2013-05-29 03:50:03 +02:00
|
|
|
if (p1.equals(p2)) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return null;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-03 18:24:18 +02:00
|
|
|
EntityBlock block = CoreProxy.proxy.newEntityBlock(world, i, j, k, iSize, jSize, kSize, kind);
|
2013-07-04 12:07:46 +02:00
|
|
|
block.setBrightness(210);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
|
|
|
world.spawnEntityInWorld(block);
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:30:54 +01:00
|
|
|
public static EntityBlock[] createLaserBox(World world, double xMin, double yMin, double zMin, double xMax, double yMax, double zMax, LaserKind kind) {
|
2012-06-04 22:48:18 +02:00
|
|
|
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);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
|
|
|
return lasers;
|
|
|
|
}
|
|
|
|
|
2013-07-16 13:27:13 +02:00
|
|
|
public static void handleBufferedDescription(ISynchronizedTile tileSynch) {
|
2012-05-09 22:43:05 +02:00
|
|
|
TileEntity tile = (TileEntity) tileSynch;
|
|
|
|
BlockIndex index = new BlockIndex(tile.xCoord, tile.yCoord, tile.zCoord);
|
|
|
|
|
|
|
|
if (BuildCraftCore.bufferedDescriptions.containsKey(index)) {
|
|
|
|
|
2012-06-08 02:13:31 +02:00
|
|
|
PacketUpdate payload = BuildCraftCore.bufferedDescriptions.get(index);
|
2012-05-09 22:43:05 +02:00
|
|
|
BuildCraftCore.bufferedDescriptions.remove(index);
|
|
|
|
|
2013-07-16 13:27:13 +02:00
|
|
|
try {
|
|
|
|
tileSynch.handleDescriptionPacket(payload);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
tileSynch.postPacketHandling(payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:48:18 +02:00
|
|
|
public static void preDestroyBlock(World world, int i, int j, int k) {
|
2014-02-08 14:47:31 +01:00
|
|
|
TileEntity tile = world.getTileEntity(i, j, k);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2014-02-10 18:06:44 +01:00
|
|
|
if (tile instanceof IInventory && !world.isRemote) {
|
2012-12-17 23:30:54 +01:00
|
|
|
if (!(tile instanceof IDropControlInventory) || ((IDropControlInventory) tile).doDrop()) {
|
2013-08-09 02:30:30 +02:00
|
|
|
InvUtils.dropItems(world, (IInventory) tile, i, j, k);
|
2013-10-12 04:16:57 +02:00
|
|
|
InvUtils.wipeInventory((IInventory) tile);
|
2012-12-17 23:30:54 +01:00
|
|
|
}
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2012-12-17 23:30:54 +01:00
|
|
|
if (tile instanceof TileBuildCraft) {
|
2012-05-09 22:43:05 +02:00
|
|
|
((TileBuildCraft) tile).destroy();
|
2012-12-17 23:30:54 +01:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 02:13:31 +02:00
|
|
|
public static boolean checkPipesConnections(TileEntity tile1, TileEntity tile2) {
|
2013-08-06 13:58:35 +02:00
|
|
|
if (tile1 == null || tile2 == null)
|
2012-05-09 22:43:05 +02:00
|
|
|
return false;
|
|
|
|
|
2013-08-06 13:58:35 +02:00
|
|
|
if (!(tile1 instanceof IPipeTile) && !(tile2 instanceof IPipeTile))
|
2012-05-09 22:43:05 +02:00
|
|
|
return false;
|
|
|
|
|
2012-11-10 07:43:12 +01:00
|
|
|
ForgeDirection o = ForgeDirection.UNKNOWN;
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-08-06 13:58:35 +02:00
|
|
|
if (tile1.xCoord - 1 == tile2.xCoord)
|
2012-11-10 07:43:12 +01:00
|
|
|
o = ForgeDirection.WEST;
|
2013-08-06 13:58:35 +02:00
|
|
|
else if (tile1.xCoord + 1 == tile2.xCoord)
|
2012-11-10 07:43:12 +01:00
|
|
|
o = ForgeDirection.EAST;
|
2013-08-06 13:58:35 +02:00
|
|
|
else if (tile1.yCoord - 1 == tile2.yCoord)
|
2012-11-10 07:43:12 +01:00
|
|
|
o = ForgeDirection.DOWN;
|
2013-08-06 13:58:35 +02:00
|
|
|
else if (tile1.yCoord + 1 == tile2.yCoord)
|
2012-11-10 07:43:12 +01:00
|
|
|
o = ForgeDirection.UP;
|
2013-08-06 13:58:35 +02:00
|
|
|
else if (tile1.zCoord - 1 == tile2.zCoord)
|
2012-11-10 07:43:12 +01:00
|
|
|
o = ForgeDirection.NORTH;
|
2013-08-06 13:58:35 +02:00
|
|
|
else if (tile1.zCoord + 1 == tile2.zCoord)
|
2012-11-10 07:43:12 +01:00
|
|
|
o = ForgeDirection.SOUTH;
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-08-06 13:58:35 +02:00
|
|
|
if (tile1 instanceof IPipeTile && !((IPipeTile) tile1).isPipeConnected(o))
|
2012-05-09 22:43:05 +02:00
|
|
|
return false;
|
|
|
|
|
2013-08-06 13:58:35 +02:00
|
|
|
if (tile2 instanceof IPipeTile && !((IPipeTile) tile2).isPipeConnected(o.getOpposite()))
|
2012-05-09 22:43:05 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-08 02:13:31 +02:00
|
|
|
public static boolean checkLegacyPipesConnections(IBlockAccess blockAccess, int x1, int y1, int z1, int x2, int y2, int z2) {
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2014-02-08 16:04:03 +01:00
|
|
|
Block b1 = blockAccess.getBlock(x1, y1, z1);
|
|
|
|
Block b2 = blockAccess.getBlock(x2, y2, z2);
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (!(b1 instanceof IFramePipeConnection) && !(b2 instanceof IFramePipeConnection)) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return false;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (b1 instanceof IFramePipeConnection && !((IFramePipeConnection) b1).isPipeConnected(blockAccess, x1, y1, z1, x2, y2, z2)) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return false;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
2013-05-29 03:50:03 +02:00
|
|
|
if (b2 instanceof IFramePipeConnection && !((IFramePipeConnection) b2).isPipeConnected(blockAccess, x2, y2, z2, x1, y1, z1)) {
|
2012-05-09 22:43:05 +02:00
|
|
|
return false;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> T[] concat(T[] first, T[] second) {
|
2012-06-04 22:48:18 +02:00
|
|
|
T[] result = Arrays.copyOf(first, first.length + second.length);
|
|
|
|
System.arraycopy(second, 0, result, first.length, second.length);
|
|
|
|
return result;
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
2012-06-04 22:48:18 +02:00
|
|
|
|
2012-05-09 22:43:05 +02:00
|
|
|
public static int[] concat(int[] first, int[] second) {
|
2012-06-04 22:48:18 +02:00
|
|
|
int[] result = Arrays.copyOf(first, first.length + second.length);
|
|
|
|
System.arraycopy(second, 0, result, first.length, second.length);
|
|
|
|
return result;
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
2012-06-04 22:48:18 +02:00
|
|
|
|
2012-05-09 22:43:05 +02:00
|
|
|
public static float[] concat(float[] first, float[] second) {
|
2012-06-04 22:48:18 +02:00
|
|
|
float[] result = Arrays.copyOf(first, first.length + second.length);
|
|
|
|
System.arraycopy(second, 0, result, first.length, second.length);
|
|
|
|
return result;
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|
2013-05-23 14:13:08 +02:00
|
|
|
|
2013-05-11 11:40:46 +02:00
|
|
|
public static int[] createSlotArray(int first, int count) {
|
|
|
|
int[] slots = new int[count];
|
2013-05-29 03:50:03 +02:00
|
|
|
for (int k = first; k < first + count; k++) {
|
2013-05-11 11:40:46 +02:00
|
|
|
slots[k - first] = k;
|
2013-05-29 03:50:03 +02:00
|
|
|
}
|
2013-05-11 11:40:46 +02:00
|
|
|
return slots;
|
2013-05-11 06:44:34 +02:00
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
|
|
|
public static void writeUTF (ByteBuf data, String str) {
|
2014-01-13 20:26:16 +01:00
|
|
|
try {
|
|
|
|
byte [] b = str.getBytes("UTF-8");
|
|
|
|
data.writeInt (b.length);
|
|
|
|
data.writeBytes(b);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
data.writeInt (0);
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
|
|
|
public static String readUTF (ByteBuf data) {
|
2014-01-13 20:26:16 +01:00
|
|
|
try {
|
|
|
|
int len = data.readInt();
|
|
|
|
byte [] b = new byte [len];
|
|
|
|
data.readBytes(b);
|
|
|
|
return new String (b, "UTF-8");
|
2014-02-18 20:42:57 +01:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
2014-01-13 20:26:16 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
2014-02-09 12:29:21 +01:00
|
|
|
public static void writeNBT (ByteBuf data, NBTTagCompound nbt) {
|
|
|
|
try {
|
|
|
|
byte[] compressed = CompressedStreamTools.compress(nbt);
|
|
|
|
data.writeShort(compressed.length);
|
|
|
|
data.writeBytes(compressed);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2014-02-18 20:42:57 +01:00
|
|
|
}
|
2014-02-09 12:29:21 +01:00
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
2014-02-09 12:29:21 +01:00
|
|
|
public static NBTTagCompound readNBT(ByteBuf data) {
|
|
|
|
try {
|
|
|
|
short length = data.readShort();
|
|
|
|
byte[] compressed = new byte[length];
|
|
|
|
data.readBytes(compressed);
|
2014-06-28 17:57:16 +02:00
|
|
|
return CompressedStreamTools.func_152457_a(compressed, NBTSizeTracker.field_152451_a);
|
2014-02-09 12:29:21 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
2014-02-09 12:29:21 +01:00
|
|
|
public static void writeStack (ByteBuf data, ItemStack stack) {
|
|
|
|
if (stack == null) {
|
|
|
|
data.writeBoolean(false);
|
|
|
|
} else {
|
|
|
|
data.writeBoolean(true);
|
2014-02-18 20:42:57 +01:00
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
2014-02-09 12:29:21 +01:00
|
|
|
stack.writeToNBT(nbt);
|
|
|
|
Utils.writeNBT(data, nbt);
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
2014-02-09 12:29:21 +01:00
|
|
|
|
|
|
|
public static ItemStack readStack(ByteBuf data) {
|
|
|
|
if (!data.readBoolean()) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
NBTTagCompound nbt = readNBT(data);
|
|
|
|
return ItemStack.loadItemStackFromNBT(nbt);
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 20:42:57 +01:00
|
|
|
|
2014-02-10 13:04:20 +01:00
|
|
|
/**
|
|
|
|
* This subprogram transforms a packet into a FML packet to be send in the
|
2014-02-18 20:42:57 +01:00
|
|
|
* minecraft default packet mechanism. This always use BC-CORE as a
|
2014-02-10 13:04:20 +01:00
|
|
|
* channel, and as a result, should use discriminators declared there.
|
2014-02-18 20:42:57 +01:00
|
|
|
*
|
2014-02-10 13:04:20 +01:00
|
|
|
* 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
|
2014-02-18 20:42:57 +01:00
|
|
|
* replaced eventually by some more solid mechanism.
|
2014-02-10 13:04:20 +01:00
|
|
|
*/
|
|
|
|
public static FMLProxyPacket toPacket (BuildCraftPacket packet, int discriminator) {
|
|
|
|
ByteBuf buf = Unpooled.buffer();
|
2014-02-18 20:42:57 +01:00
|
|
|
|
2014-02-10 13:04:20 +01:00
|
|
|
buf.writeByte((byte) discriminator);
|
|
|
|
packet.writeData(buf);
|
2014-02-10 18:06:44 +01:00
|
|
|
|
2014-02-18 20:42:57 +01:00
|
|
|
return new FMLProxyPacket(buf, DefaultProps.NET_CHANNEL_NAME + "-CORE");
|
2014-02-10 18:06:44 +01:00
|
|
|
}
|
2012-05-09 22:43:05 +02:00
|
|
|
}
|