equivalent-exchange-3/src/main/java/com/pahimar/ee3/tileentity/TileEntityAlchemyArray.java

650 lines
19 KiB
Java
Raw Normal View History

package com.pahimar.ee3.tileentity;
2015-05-07 19:45:06 +02:00
import com.pahimar.ee3.api.array.AlchemyArray;
import com.pahimar.ee3.network.PacketHandler;
import com.pahimar.ee3.network.message.MessageTileEntityAlchemyArray;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
2023-01-03 17:47:36 +01:00
public class TileEntityAlchemyArray extends TileEntityEE implements ISidedInventory {
private AlchemyArray alchemyArray;
private ForgeDirection rotation;
private int size;
private int tickCount;
2023-01-03 17:47:36 +01:00
public TileEntityAlchemyArray() {
super();
rotation = ForgeDirection.UNKNOWN;
size = 0;
alchemyArray = null;
}
2023-01-03 17:47:36 +01:00
public AlchemyArray getAlchemyArray() {
return alchemyArray;
}
2023-01-03 17:47:36 +01:00
public void setAlchemyArray(AlchemyArray alchemyArray) {
setAlchemyArray(alchemyArray, 1);
}
2023-01-03 17:47:36 +01:00
public void setAlchemyArray(AlchemyArray alchemyArray, int size) {
this.alchemyArray = alchemyArray;
this.size = size;
}
2023-01-03 17:47:36 +01:00
public ForgeDirection getRotation() {
return rotation;
}
2023-01-03 17:47:36 +01:00
public void setRotation(ForgeDirection rotation) {
this.rotation = rotation;
}
2023-01-03 17:47:36 +01:00
public void setRotation(int rotation, int facing) {
if (this.orientation == ForgeDirection.UP) {
if ((rotation + facing) % 4 == 0) {
this.rotation = ForgeDirection.NORTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 1) {
this.rotation = ForgeDirection.EAST;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 2) {
this.rotation = ForgeDirection.SOUTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 3) {
this.rotation = ForgeDirection.WEST;
}
2023-01-03 17:47:36 +01:00
} else if (this.orientation == ForgeDirection.DOWN) {
if ((rotation + facing) % 4 == 0) {
this.rotation = ForgeDirection.NORTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 1) {
this.rotation = ForgeDirection.EAST;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 2) {
this.rotation = ForgeDirection.SOUTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 3) {
this.rotation = ForgeDirection.WEST;
}
2023-01-03 17:47:36 +01:00
} else if (this.orientation == ForgeDirection.NORTH) {
if ((rotation + facing) % 4 == 0) {
this.rotation = ForgeDirection.UP;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 1) {
this.rotation = ForgeDirection.EAST;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 2) {
this.rotation = ForgeDirection.DOWN;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 3) {
this.rotation = ForgeDirection.WEST;
}
2023-01-03 17:47:36 +01:00
} else if (this.orientation == ForgeDirection.SOUTH) {
if ((rotation + facing) % 4 == 0) {
this.rotation = ForgeDirection.DOWN;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 1) {
this.rotation = ForgeDirection.EAST;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 2) {
this.rotation = ForgeDirection.UP;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 3) {
this.rotation = ForgeDirection.WEST;
}
2023-01-03 17:47:36 +01:00
} else if (this.orientation == ForgeDirection.EAST) {
if ((rotation + facing) % 4 == 0) {
this.rotation = ForgeDirection.NORTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 1) {
this.rotation = ForgeDirection.UP;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 2) {
this.rotation = ForgeDirection.SOUTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 3) {
this.rotation = ForgeDirection.DOWN;
}
2023-01-03 17:47:36 +01:00
} else if (this.orientation == ForgeDirection.WEST) {
if ((rotation + facing) % 4 == 0) {
this.rotation = ForgeDirection.NORTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 1) {
this.rotation = ForgeDirection.DOWN;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 2) {
this.rotation = ForgeDirection.SOUTH;
2023-01-03 17:47:36 +01:00
} else if ((rotation + facing) % 4 == 3) {
this.rotation = ForgeDirection.UP;
}
}
}
2023-01-03 17:47:36 +01:00
public int getSize() {
return size;
}
2023-01-03 17:47:36 +01:00
public void setSize(int size) {
this.size = size;
}
@Override
@SideOnly(Side.CLIENT)
2023-01-03 17:47:36 +01:00
public AxisAlignedBB getRenderBoundingBox() {
if (this.orientation == ForgeDirection.UP
|| this.orientation == ForgeDirection.DOWN) {
return AxisAlignedBB.getBoundingBox(
xCoord - size,
yCoord - 1,
zCoord - size,
xCoord + size,
yCoord + 1,
zCoord + size
);
}
else if (this.orientation == ForgeDirection.NORTH || this.orientation == ForgeDirection.SOUTH)
{
2023-01-03 17:47:36 +01:00
return AxisAlignedBB.getBoundingBox(
xCoord - size,
yCoord - size,
zCoord - 1,
xCoord + size,
yCoord + size,
zCoord + 1
);
}
else if (this.orientation == ForgeDirection.EAST || this.orientation == ForgeDirection.WEST)
{
2023-01-03 17:47:36 +01:00
return AxisAlignedBB.getBoundingBox(
xCoord - 1,
yCoord - size,
zCoord - size,
xCoord + 1,
yCoord + size,
zCoord + size
);
}
return super.getRenderBoundingBox();
}
@Override
2023-01-03 17:47:36 +01:00
public void updateEntity() {
super.updateEntity();
2023-01-03 17:47:36 +01:00
if (!worldObj.isRemote) {
++tickCount;
2023-01-03 17:47:36 +01:00
if (tickCount % 100 == 0) {
if (!areDummyBlocksValid()) {
this.invalidate();
worldObj.setBlockToAir(xCoord, yCoord, zCoord);
}
}
onUpdate(worldObj, xCoord, yCoord, zCoord, tickCount);
}
}
2023-01-03 17:47:36 +01:00
public int getLightLevel() {
if (alchemyArray != null) {
return alchemyArray.getLightLevel();
}
return 0;
}
2023-01-03 17:47:36 +01:00
public void onBlockPlacedBy(
World world,
int x,
int y,
int z,
EntityLivingBase entityLiving,
ItemStack itemStack
) {
onBlockPlacedBy(
world, x, y, z, this.xCoord, this.yCoord, this.zCoord, entityLiving, itemStack
);
}
public void onBlockPlacedBy(
World world,
int eventX,
int eventY,
int eventZ,
int arrayX,
int arrayY,
int arrayZ,
EntityLivingBase entityLiving,
ItemStack itemStack
) {
if (alchemyArray != null) {
alchemyArray.onArrayPlacedBy(
world,
eventX,
eventY,
eventZ,
arrayX,
arrayY,
arrayZ,
entityLiving,
itemStack
);
}
}
public void onBlockActivated(
World world,
int x,
int y,
int z,
EntityPlayer entityPlayer,
int sideHit,
float hitX,
float hitY,
float hitZ
) {
onBlockActivated(
world,
x,
y,
z,
this.xCoord,
this.yCoord,
this.zCoord,
entityPlayer,
sideHit,
hitX,
hitY,
hitZ
);
}
public void onBlockActivated(
World world,
int eventX,
int eventY,
int eventZ,
int arrayX,
int arrayY,
int arrayZ,
EntityPlayer entityPlayer,
int sideHit,
float hitX,
float hitY,
float hitZ
) {
if (alchemyArray != null) {
alchemyArray.onArrayActivated(
world,
eventX,
eventY,
eventZ,
arrayX,
arrayY,
arrayZ,
entityPlayer,
sideHit,
hitX,
hitY,
hitZ
);
}
}
public void
onBlockClicked(World world, int x, int y, int z, EntityPlayer entityPlayer) {
onBlockClicked(
world, x, y, z, this.xCoord, this.yCoord, this.zCoord, entityPlayer
);
}
public void onBlockClicked(
World world,
int eventX,
int eventY,
int eventZ,
int arrayX,
int arrayY,
int arrayZ,
EntityPlayer entityPlayer
) {
if (alchemyArray != null) {
alchemyArray.onArrayClicked(
world, eventX, eventY, eventZ, arrayX, arrayY, arrayZ, entityPlayer
);
}
}
public void
onBlockDestroyedByExplosion(World world, int x, int y, int z, Explosion explosion) {
onBlockDestroyedByExplosion(
world, x, y, z, this.xCoord, this.yCoord, this.zCoord, explosion
);
}
public void onBlockDestroyedByExplosion(
World world,
int eventX,
int eventY,
int eventZ,
int arrayX,
int arrayY,
int arrayZ,
Explosion explosion
) {
if (alchemyArray != null) {
alchemyArray.onArrayDestroyedByExplosion(
world, eventX, eventY, eventZ, arrayX, arrayY, arrayZ, explosion
);
}
}
public void onBlockDestroyedByPlayer(World world, int x, int y, int z, int metaData) {
onBlockDestroyedByPlayer(
world, x, y, z, this.xCoord, this.yCoord, this.zCoord, metaData
);
}
public void onBlockDestroyedByPlayer(
World world,
int eventX,
int eventY,
int eventZ,
int arrayX,
int arrayY,
int arrayZ,
int metaData
) {
if (alchemyArray != null) {
alchemyArray.onArrayDestroyedByPlayer(
world, eventX, eventY, eventZ, arrayX, arrayY, arrayZ, metaData
);
}
}
public void
onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {
onEntityCollidedWithBlock(
world, x, y, z, this.xCoord, this.yCoord, this.zCoord, entity
);
}
public void onEntityCollidedWithBlock(
World world,
int eventX,
int eventY,
int eventZ,
int arrayX,
int arrayY,
int arrayZ,
Entity entity
) {
if (alchemyArray != null) {
alchemyArray.onEntityCollidedWithArray(
world, eventX, eventY, eventZ, arrayX, arrayY, arrayZ, entity
);
}
}
public void
onFallenUpon(World world, int x, int y, int z, Entity entity, float fallDistance) {
onFallenUpon(
world, x, y, z, this.xCoord, this.yCoord, this.zCoord, entity, fallDistance
);
}
public void onFallenUpon(
World world,
int eventX,
int eventY,
int eventZ,
int arrayX,
int arrayY,
int arrayZ,
Entity entity,
float fallDistance
) {
if (alchemyArray != null) {
alchemyArray.onArrayFallenUpon(
world,
eventX,
eventY,
eventZ,
arrayX,
arrayY,
arrayZ,
entity,
fallDistance
);
}
}
public void onUpdate(World world, int x, int y, int z, int tickCount) {
if (alchemyArray != null) {
alchemyArray.onUpdate(world, x, y, z, tickCount);
}
}
@Override
2023-01-03 17:47:36 +01:00
public Packet getDescriptionPacket() {
return PacketHandler.INSTANCE.getPacketFrom(new MessageTileEntityAlchemyArray(this
));
}
@Override
2023-01-03 17:47:36 +01:00
public void readFromNBT(NBTTagCompound nbtTagCompound) {
super.readFromNBT(nbtTagCompound);
rotation = ForgeDirection.getOrientation(nbtTagCompound.getInteger("rotation"));
size = nbtTagCompound.getInteger("size");
2023-01-03 17:47:36 +01:00
NBTTagCompound alchemyArrayTagCompound
= nbtTagCompound.getCompoundTag("alchemyArray");
2023-01-03 17:47:36 +01:00
if (!alchemyArrayTagCompound.hasNoTags()) {
alchemyArray = AlchemyArray.readArrayFromNBT(alchemyArrayTagCompound);
2023-01-03 17:47:36 +01:00
try {
Class clazz = Class.forName(
alchemyArray.getClassName(),
true,
Loader.instance().getModClassLoader()
);
alchemyArray = (AlchemyArray) clazz.getConstructor().newInstance();
alchemyArray.readFromNBT(alchemyArrayTagCompound);
2023-01-03 17:47:36 +01:00
} catch (Exception e) {
this.invalidate();
2023-01-03 17:47:36 +01:00
if (worldObj != null) {
worldObj.setBlockToAir(xCoord, yCoord, zCoord);
}
}
2023-01-03 17:47:36 +01:00
} else {
this.invalidate();
}
}
@Override
2023-01-03 17:47:36 +01:00
public void writeToNBT(NBTTagCompound nbtTagCompound) {
super.writeToNBT(nbtTagCompound);
nbtTagCompound.setInteger("rotation", rotation.ordinal());
nbtTagCompound.setInteger("size", size);
NBTTagCompound alchemyArrayTagCompound = new NBTTagCompound();
2023-01-03 17:47:36 +01:00
if (alchemyArray != null) {
alchemyArray.writeToNBT(alchemyArrayTagCompound);
}
nbtTagCompound.setTag("alchemyArray", alchemyArrayTagCompound);
}
2023-01-03 17:47:36 +01:00
private boolean areDummyBlocksValid() {
boolean validDummyBlocks = true;
int coordOffset = this.size / 2;
2023-01-03 17:47:36 +01:00
if (this.orientation == ForgeDirection.UP
|| this.orientation == ForgeDirection.DOWN) {
for (int i = this.xCoord - coordOffset; i <= this.xCoord + coordOffset; i++) {
for (int j = this.zCoord - coordOffset; j <= this.zCoord + coordOffset;
j++) {
if ((i != this.xCoord || j != this.zCoord)
&& !isValidDummyBlock(i, this.yCoord, j)) {
validDummyBlocks = false;
}
}
}
}
else if (this.orientation == ForgeDirection.NORTH || this.orientation == ForgeDirection.SOUTH)
{
2023-01-03 17:47:36 +01:00
for (int i = this.xCoord - coordOffset; i <= this.xCoord + coordOffset; i++) {
for (int j = this.yCoord - coordOffset; j <= this.yCoord + coordOffset;
j++) {
if ((i != this.xCoord || j != this.yCoord)
&& !isValidDummyBlock(i, j, this.zCoord)) {
validDummyBlocks = false;
}
}
}
}
else if (this.orientation == ForgeDirection.EAST || this.orientation == ForgeDirection.WEST)
{
2023-01-03 17:47:36 +01:00
for (int i = this.yCoord - coordOffset; i <= this.yCoord + coordOffset; i++) {
for (int j = this.zCoord - coordOffset; j <= this.zCoord + coordOffset;
j++) {
if ((i != this.yCoord || j != this.zCoord)
&& !isValidDummyBlock(this.xCoord, i, j)) {
validDummyBlocks = false;
}
}
}
}
return validDummyBlocks;
}
2023-01-03 17:47:36 +01:00
private boolean isValidDummyBlock(int x, int y, int z) {
if (!this.worldObj.isRemote) {
if (this.worldObj.getTileEntity(x, y, z) instanceof TileEntityDummyArray) {
TileEntityDummyArray tileEntityDummyArray
= (TileEntityDummyArray) this.worldObj.getTileEntity(x, y, z);
return tileEntityDummyArray.getOrientation() == this.orientation
&& tileEntityDummyArray.getTrueXCoord() == this.xCoord
&& tileEntityDummyArray.getTrueYCoord() == this.yCoord
&& tileEntityDummyArray.getTrueZCoord() == this.zCoord;
}
}
return false;
}
@Override
2023-01-03 17:47:36 +01:00
public int getSizeInventory() {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).getSizeInventory();
}
return 0;
}
@Override
2023-01-03 17:47:36 +01:00
public ItemStack getStackInSlot(int slotIndex) {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).getStackInSlot(slotIndex);
}
return null;
}
@Override
2023-01-03 17:47:36 +01:00
public ItemStack decrStackSize(int slotIndex, int decrementAmount) {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).decrStackSize(slotIndex, decrementAmount);
}
return null;
}
@Override
2023-01-03 17:47:36 +01:00
public ItemStack getStackInSlotOnClosing(int slotIndex) {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).getStackInSlotOnClosing(slotIndex);
}
return null;
}
@Override
2023-01-03 17:47:36 +01:00
public void setInventorySlotContents(int slotIndex, ItemStack itemStack) {
if (alchemyArray instanceof IInventory) {
((IInventory) alchemyArray).setInventorySlotContents(slotIndex, itemStack);
}
}
@Override
2023-01-03 17:47:36 +01:00
public String getInventoryName() {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).getInventoryName();
}
return null;
}
@Override
2023-01-03 17:47:36 +01:00
public boolean hasCustomInventoryName() {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).hasCustomInventoryName();
}
return false;
}
@Override
2023-01-03 17:47:36 +01:00
public int getInventoryStackLimit() {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).getInventoryStackLimit();
}
return 0;
}
@Override
2023-01-03 17:47:36 +01:00
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).isUseableByPlayer(entityplayer);
}
return false;
}
@Override
2023-01-03 17:47:36 +01:00
public void openInventory() {
if (alchemyArray instanceof IInventory) {
((IInventory) alchemyArray).openInventory();
}
}
@Override
2023-01-03 17:47:36 +01:00
public void closeInventory() {
if (alchemyArray instanceof IInventory) {
((IInventory) alchemyArray).closeInventory();
}
}
@Override
2023-01-03 17:47:36 +01:00
public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack) {
if (alchemyArray instanceof IInventory) {
return ((IInventory) alchemyArray).isItemValidForSlot(slotIndex, itemStack);
}
return false;
}
@Override
2023-01-03 17:47:36 +01:00
public int[] getAccessibleSlotsFromSide(int slotIndex) {
return new int[0];
}
@Override
2023-01-03 17:47:36 +01:00
public boolean canInsertItem(int slotIndex, ItemStack itemStack, int side) {
return false;
}
@Override
2023-01-03 17:47:36 +01:00
public boolean canExtractItem(int slotIndex, ItemStack itemStack, int side) {
return false;
}
}