2013-12-01 05:19:24 +01:00
|
|
|
package mekanism.api;
|
2013-04-23 21:36:43 +02:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
import net.minecraft.block.Block;
|
2013-04-23 21:36:43 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.MathHelper;
|
2013-06-02 23:44:59 +02:00
|
|
|
import net.minecraft.world.IBlockAccess;
|
2013-08-19 03:57:57 +02:00
|
|
|
import net.minecraft.world.World;
|
2013-12-13 05:08:16 +01:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
2013-04-23 21:36:43 +02:00
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
import com.google.common.io.ByteArrayDataInput;
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Coord4D - an integer-based way to keep track of and perform operations on blocks in a Minecraft-based environment. This also takes
|
|
|
|
* in account the dimension the coordinate is in.
|
|
|
|
* @author aidancbrady
|
|
|
|
*
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public class Coord4D
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
|
|
|
public int xCoord;
|
|
|
|
public int yCoord;
|
|
|
|
public int zCoord;
|
2013-06-02 23:44:59 +02:00
|
|
|
|
2013-04-23 21:36:43 +02:00
|
|
|
public int dimensionId;
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Creates a Coord4D WITHOUT a dimensionId. Don't use unless absolutely necessary.
|
|
|
|
* @param x - x coordinate
|
|
|
|
* @param y - y coordinate
|
|
|
|
* @param z - z coordinate
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D(int x, int y, int z)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
|
|
|
xCoord = x;
|
|
|
|
yCoord = y;
|
|
|
|
zCoord = z;
|
2013-06-02 23:44:59 +02:00
|
|
|
|
2013-04-23 21:36:43 +02:00
|
|
|
dimensionId = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Creates a Coord4D from the defined x, y, z, and dimension values.
|
|
|
|
* @param x - x coordinate
|
|
|
|
* @param y - y coordinate
|
|
|
|
* @param z - z coordinate
|
|
|
|
* @param dimension - dimension ID
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D(int x, int y, int z, int dimension)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
|
|
|
xCoord = x;
|
|
|
|
yCoord = y;
|
|
|
|
zCoord = z;
|
2013-06-02 23:44:59 +02:00
|
|
|
|
2013-04-23 21:36:43 +02:00
|
|
|
dimensionId = dimension;
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Gets the metadata of the block representing this Coord4D.
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return the metadata of this Coord4D's block
|
|
|
|
*/
|
2013-06-02 23:44:59 +02:00
|
|
|
public int getMetadata(IBlockAccess world)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
|
|
|
return world.getBlockMetadata(xCoord, yCoord, zCoord);
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Gets the block ID of the block representing this Coord4D.
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return the block ID of this Coord4D's block
|
|
|
|
*/
|
2013-06-02 23:44:59 +02:00
|
|
|
public int getBlockId(IBlockAccess world)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
|
|
|
return world.getBlockId(xCoord, yCoord, zCoord);
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Gets the TileEntity of the block representing this Coord4D.
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return the TileEntity of this Coord4D's block
|
|
|
|
*/
|
2013-06-02 23:44:59 +02:00
|
|
|
public TileEntity getTileEntity(IBlockAccess world)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
2013-12-29 04:09:33 +01:00
|
|
|
if(world instanceof World && !exists((World)world))
|
2013-10-22 02:54:28 +02:00
|
|
|
{
|
2013-08-19 03:57:57 +02:00
|
|
|
return null;
|
2013-10-22 02:54:28 +02:00
|
|
|
}
|
|
|
|
|
2013-04-23 21:36:43 +02:00
|
|
|
return world.getBlockTileEntity(xCoord, yCoord, zCoord);
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Gets the Block value of the block representing this Coord4D.
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return the Block value of this Coord4D's block
|
|
|
|
*/
|
|
|
|
public Block getBlock(IBlockAccess world)
|
|
|
|
{
|
2013-12-29 04:09:33 +01:00
|
|
|
if(world instanceof World && !exists((World)world))
|
2013-12-23 23:06:22 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Block.blocksList[getBlockId(world)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes this Coord4D's data to an NBTTagCompound.
|
|
|
|
* @param nbtTags - tag compound to write to
|
|
|
|
* @return the tag compound with this Coord4D's data
|
|
|
|
*/
|
2013-11-09 21:30:30 +01:00
|
|
|
public NBTTagCompound write(NBTTagCompound nbtTags)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
|
|
|
nbtTags.setInteger("x", xCoord);
|
|
|
|
nbtTags.setInteger("y", yCoord);
|
|
|
|
nbtTags.setInteger("z", zCoord);
|
|
|
|
nbtTags.setInteger("dimensionId", dimensionId);
|
2013-11-09 21:30:30 +01:00
|
|
|
|
|
|
|
return nbtTags;
|
2013-04-23 21:36:43 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Writes this Coord4D's data to an ArrayList for packet transfer.
|
|
|
|
* @param data - the ArrayList to add the data to
|
|
|
|
*/
|
2013-10-22 02:54:28 +02:00
|
|
|
public void write(ArrayList data)
|
|
|
|
{
|
|
|
|
data.add(xCoord);
|
|
|
|
data.add(yCoord);
|
|
|
|
data.add(zCoord);
|
2013-12-23 23:06:22 +01:00
|
|
|
data.add(dimensionId);
|
2013-10-22 02:54:28 +02:00
|
|
|
}
|
2013-12-23 23:06:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates this Coord4D by the defined x, y, and z values.
|
|
|
|
* @param x - x value to translate
|
|
|
|
* @param y - y value to translate
|
|
|
|
* @param z - z value to translate
|
|
|
|
* @return translated Coord4D
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D translate(int x, int y, int z)
|
2013-05-01 05:07:16 +02:00
|
|
|
{
|
|
|
|
xCoord += x;
|
|
|
|
yCoord += y;
|
|
|
|
zCoord += z;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Creates and returns a new Coord4D translated to the defined offsets of the side.
|
|
|
|
* @param side - side to translate this Coord4D to
|
|
|
|
* @return translated Coord4D
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D getFromSide(ForgeDirection side)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return new Coord4D(xCoord+side.offsetX, yCoord+side.offsetY, zCoord+side.offsetZ, dimensionId);
|
2013-04-23 21:36:43 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Returns a new Coord4D from a defined TileEntity's xCoord, yCoord and zCoord values.
|
|
|
|
* @param tileEntity - TileEntity at the location that will represent this Coord4D
|
|
|
|
* @return the Coord4D object from the TileEntity
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public static Coord4D get(TileEntity tileEntity)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return new Coord4D(tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord, tileEntity.worldObj.provider.dimensionId);
|
2013-04-23 21:36:43 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Returns a new Coord4D from a tag compound.
|
|
|
|
* @param nbtTags - tag compound to read from
|
|
|
|
* @return the Coord4D from the tag compound
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public static Coord4D read(NBTTagCompound nbtTags)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return new Coord4D(nbtTags.getInteger("x"), nbtTags.getInteger("y"), nbtTags.getInteger("z"), nbtTags.getInteger("dimensionId"));
|
2013-04-23 21:36:43 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Returns a new Coord4D from a ByteArrayDataInput.
|
|
|
|
* @param dataStream - data input to read from
|
|
|
|
* @return the Coord4D from the data input
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public static Coord4D read(ByteArrayDataInput dataStream)
|
2013-10-22 02:54:28 +02:00
|
|
|
{
|
2013-12-23 23:06:22 +01:00
|
|
|
return new Coord4D(dataStream.readInt(), dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
|
2013-10-22 02:54:28 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Creates and returns a new Coord4D with values representing the difference between the defined Coord4D
|
|
|
|
* @param other - the Coord4D to subtract from this
|
|
|
|
* @return a Coord4D representing the distance between the defined Coord4D
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D difference(Coord4D other)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-12-23 23:06:22 +01:00
|
|
|
return new Coord4D(xCoord-other.xCoord, yCoord-other.yCoord, zCoord-other.zCoord, dimensionId);
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* A method used to find the ForgeDirection represented by the distance of the defined Coord4D. Most likely won't have many
|
|
|
|
* applicable uses.
|
|
|
|
* @param other - Coord4D to find the side difference of
|
|
|
|
* @return ForgeDirection representing the side the defined relative Coord4D is on to this
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public ForgeDirection sideDifference(Coord4D other)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
Coord4D diff = difference(other);
|
2013-10-21 04:47:20 +02:00
|
|
|
|
|
|
|
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
2013-10-22 05:46:24 +02:00
|
|
|
if(side.offsetX == diff.xCoord && side.offsetY == diff.yCoord && side.offsetZ == diff.zCoord)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
|
|
|
return side;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ForgeDirection.UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Gets the distance to a defined Coord4D.
|
|
|
|
* @param obj - the Coord4D to find the distance to
|
|
|
|
* @return the distance to the defined Coord4D
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public int distanceTo(Coord4D obj)
|
2013-04-23 21:36:43 +02:00
|
|
|
{
|
|
|
|
int subX = xCoord - obj.xCoord;
|
|
|
|
int subY = yCoord - obj.yCoord;
|
|
|
|
int subZ = zCoord - obj.zCoord;
|
|
|
|
return (int)MathHelper.sqrt_double(subX * subX + subY * subY + subZ * subZ);
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Whether or not the defined side of this Coord4D is visible.
|
|
|
|
* @param side - side to check
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return
|
|
|
|
*/
|
2013-06-02 23:44:59 +02:00
|
|
|
public boolean sideVisible(ForgeDirection side, IBlockAccess world)
|
2013-05-23 19:30:12 +02:00
|
|
|
{
|
2013-12-23 23:06:22 +01:00
|
|
|
return world.isAirBlock(xCoord+side.offsetX, yCoord+side.offsetY, zCoord+side.offsetZ);
|
2013-05-23 19:30:12 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Steps this Coord4D in the defined side's offset without creating a new value.
|
|
|
|
* @param side - side to step towards
|
|
|
|
* @return this Coord4D
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D step(ForgeDirection side)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
|
|
|
return translate(side.offsetX, side.offsetY, side.offsetZ);
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Whether or not the chunk this Coord4D is in exists and is loaded.
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return the chunk of this Coord4D
|
|
|
|
*/
|
2013-12-13 05:08:16 +01:00
|
|
|
public boolean exists(World world)
|
|
|
|
{
|
|
|
|
return world.getChunkProvider().chunkExists(xCoord >> 4, zCoord >> 4);
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Gets the chunk this Coord4D is in.
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return the chunk of this Coord4D
|
|
|
|
*/
|
2013-12-13 05:08:16 +01:00
|
|
|
public Chunk getChunk(World world)
|
|
|
|
{
|
|
|
|
return world.getChunkFromBlockCoords(xCoord >> 4, zCoord >> 4);
|
|
|
|
}
|
|
|
|
|
2014-01-05 21:06:54 +01:00
|
|
|
/**
|
|
|
|
* Whether or not the block this Coord4D represents is an air block.
|
|
|
|
* @param world - world this Coord4D is in
|
|
|
|
* @return if this Coord4D is an air block
|
|
|
|
*/
|
|
|
|
public boolean isAirBlock(IBlockAccess world)
|
|
|
|
{
|
|
|
|
return world.isAirBlock(xCoord, yCoord, zCoord);
|
|
|
|
}
|
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
@Override
|
|
|
|
public Coord4D clone()
|
|
|
|
{
|
|
|
|
return new Coord4D(xCoord, yCoord, zCoord, dimensionId);
|
|
|
|
}
|
|
|
|
|
2013-10-21 04:47:20 +02:00
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
return "[Object3D: " + xCoord + ", " + yCoord + ", " + zCoord + "]";
|
|
|
|
}
|
|
|
|
|
2013-04-23 21:36:43 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj)
|
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return obj instanceof Coord4D &&
|
|
|
|
((Coord4D)obj).xCoord == xCoord &&
|
|
|
|
((Coord4D)obj).yCoord == yCoord &&
|
|
|
|
((Coord4D)obj).zCoord == zCoord &&
|
|
|
|
((Coord4D)obj).dimensionId == dimensionId;
|
2013-04-23 21:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
int code = 1;
|
|
|
|
code = 31 * code + xCoord;
|
|
|
|
code = 31 * code + yCoord;
|
|
|
|
code = 31 * code + zCoord;
|
|
|
|
code = 31 * code + dimensionId;
|
|
|
|
return code;
|
|
|
|
}
|
2013-06-02 23:44:59 +02:00
|
|
|
}
|