buildcraft/api/buildcraft/api/core/BlockIndex.java

116 lines
2.5 KiB
Java
Raw Normal View History

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
2012-05-09 22:43:05 +02:00
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
2012-05-09 22:43:05 +02:00
*/
2014-06-13 13:36:02 +02:00
package buildcraft.api.core;
2012-05-09 22:43:05 +02:00
2014-02-08 14:47:31 +01:00
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
2012-12-17 23:29:42 +01:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
2012-12-17 23:29:42 +01:00
import net.minecraft.world.World;
2012-05-09 22:43:05 +02:00
/**
2012-12-17 23:30:54 +01:00
* This class is a comparable container for block positions. TODO: should this be merged with position?
2012-05-09 22:43:05 +02:00
*/
public class BlockIndex implements Comparable<BlockIndex> {
2012-06-04 22:48:18 +02:00
public int x;
public int y;
public int z;
2012-05-09 22:43:05 +02:00
public BlockIndex() {
}
2012-05-09 22:43:05 +02:00
/**
* Creates an index for a block located on x, y. z
2012-05-09 22:43:05 +02:00
*/
public BlockIndex(int x, int y, int z) {
2012-06-04 22:48:18 +02:00
this.x = x;
this.y = y;
this.z = z;
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public BlockIndex(NBTTagCompound c) {
this.x = c.getInteger("i");
this.y = c.getInteger("j");
this.z = c.getInteger("k");
2012-05-09 22:43:05 +02:00
}
public BlockIndex(Entity entity) {
x = (int) Math.floor(entity.posX);
y = (int) Math.floor(entity.posY);
z = (int) Math.floor(entity.posZ);
}
public BlockIndex(TileEntity entity) {
this(entity.xCoord, entity.yCoord, entity.zCoord);
}
2012-05-09 22:43:05 +02:00
/**
* Provides a deterministic and complete ordering of block positions.
*/
@Override
public int compareTo(BlockIndex o) {
2012-06-04 22:48:18 +02:00
if (o.x < x) {
2012-05-09 22:43:05 +02:00
return 1;
} else if (o.x > x) {
2012-05-09 22:43:05 +02:00
return -1;
} else if (o.z < z) {
2012-05-09 22:43:05 +02:00
return 1;
} else if (o.z > z) {
2012-05-09 22:43:05 +02:00
return -1;
} else if (o.y < y) {
2012-05-09 22:43:05 +02:00
return 1;
} else if (o.y > y) {
2012-05-09 22:43:05 +02:00
return -1;
} else {
2012-05-09 22:43:05 +02:00
return 0;
}
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public void writeTo(NBTTagCompound c) {
c.setInteger("i", x);
c.setInteger("j", y);
c.setInteger("k", z);
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
2014-02-08 14:47:31 +01:00
public Block getBlock(World world) {
return world.getBlock(x, y, z);
}
2012-05-09 22:43:05 +02:00
@Override
2012-06-04 22:48:18 +02:00
public String toString() {
return "{" + x + ", " + y + ", " + z + "}";
2012-05-09 22:43:05 +02:00
}
2013-05-06 15:21:17 +02:00
@Override
public boolean equals(Object obj) {
if (obj instanceof BlockIndex) {
BlockIndex b = (BlockIndex) obj;
return b.x == x && b.y == y && b.z == z;
2013-05-06 15:21:17 +02:00
}
return super.equals(obj);
}
@Override
public int hashCode() {
return (x * 37 + y) * 37 + z;
}
public boolean nextTo(BlockIndex blockIndex) {
return (Math.abs(blockIndex.x - x) <= 1 && blockIndex.y == y && blockIndex.z == z)
|| (blockIndex.x == x && Math.abs(blockIndex.y - y) <= 1 && blockIndex.z == z)
|| (blockIndex.x == x && blockIndex.y == y && Math.abs(blockIndex.z - z) <= 1);
}
2012-05-09 22:43:05 +02:00
}