TechReborn/src/main/java/techreborn/tiles/TileBlastFurnace.java

281 lines
7.9 KiB
Java
Raw Normal View History

package techreborn.tiles;
import ic2.api.energy.prefab.BasicSink;
import ic2.api.tile.IWrenchable;
import net.minecraft.entity.player.EntityPlayer;
2015-04-28 19:23:48 +02:00
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
2015-04-28 19:23:48 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
2015-04-26 17:22:26 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
2015-04-28 22:04:28 +02:00
import techreborn.api.BlastFurnaceRecipe;
2015-04-28 19:23:48 +02:00
import techreborn.api.TechRebornAPI;
2015-04-26 17:22:26 +02:00
import techreborn.blocks.BlockMachineCasing;
2015-04-28 19:23:48 +02:00
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModBlocks;
2015-04-26 17:22:26 +02:00
import techreborn.lib.Location;
import techreborn.util.Inventory;
2015-04-28 22:04:28 +02:00
import techreborn.util.ItemUtils;
2015-04-28 19:23:48 +02:00
public class TileBlastFurnace extends TileMachineBase implements IWrenchable, IInventory {
public int tickTime;
public BasicSink energy;
2015-04-28 19:23:48 +02:00
public Inventory inventory = new Inventory(4, "TileBlastFurnace", 64);
2015-04-28 22:04:28 +02:00
public BlastFurnaceRecipe recipe;
public static int euTick = 5;
2015-04-28 19:23:48 +02:00
2015-04-28 22:04:28 +02:00
public TileBlastFurnace() {
2015-04-28 19:23:48 +02:00
//TODO configs
energy = new BasicSink(this, ConfigTechReborn.CentrifugeCharge,
ConfigTechReborn.CentrifugeTier);
}
@Override
2015-04-28 22:04:28 +02:00
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
return false;
}
@Override
2015-04-28 22:04:28 +02:00
public short getFacing() {
return 0;
}
@Override
2015-04-28 22:04:28 +02:00
public void setFacing(short facing) {
}
@Override
2015-04-28 22:04:28 +02:00
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return true;
}
@Override
2015-04-28 22:04:28 +02:00
public float getWrenchDropRate() {
return 1.0F;
}
@Override
2015-04-28 22:04:28 +02:00
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.BlastFurnace, 1);
}
2015-04-28 22:04:28 +02:00
public int getHeat() {
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
TileEntity tileEntity = worldObj.getTileEntity(xCoord + direction.offsetX, yCoord + direction.offsetY, zCoord + direction.offsetZ);
if (tileEntity instanceof TileMachineCasing) {
if ((tileEntity.getBlockType() instanceof BlockMachineCasing)) {
int heat;
heat = BlockMachineCasing.getHeatFromMeta(tileEntity.getBlockMetadata());
Location location = new Location(xCoord, yCoord, zCoord, direction);
location.modifyPositionFromSide(direction, 1);
if (worldObj.getBlock(location.getX(), location.getY(), location.getZ()).getUnlocalizedName().equals("tile.lava")) {
heat += 500;
}
return heat;
}
}
}
return 0;
}
public void update() {
2015-04-28 19:23:48 +02:00
super.updateEntity();
2015-04-28 22:04:28 +02:00
if (getStackInSlot(0) != null && getStackInSlot(1) != null) {
if (recipe == null) {
for (BlastFurnaceRecipe furnaceRecipe : TechRebornAPI.blastFurnaceRecipes) {
if (ItemUtils.isItemEqual(getStackInSlot(0), furnaceRecipe.getInput1(), true, true) && ItemUtils.isItemEqual(getStackInSlot(0), furnaceRecipe.getInput2(), true, true)) {
recipe = furnaceRecipe;
}
}
} else {
if (!ItemUtils.isItemEqual(getStackInSlot(0), recipe.getInput1(), true, true) || !ItemUtils.isItemEqual(getStackInSlot(0), recipe.getInput2(), true, true)) {
recipe = null;
tickTime = 0;
return;
}
if (tickTime >= recipe.getTickTime()) {
//When both slots are empty
if (getStackInSlot(2) == null && getStackInSlot(3) == null) {
setInventorySlotContents(2, recipe.getOutput1());
setInventorySlotContents(3, recipe.getOutput2());
tickTime = 0;
recipe = null;
}
//When both are the same as the recipe
if (ItemUtils.isItemEqual(getStackInSlot(2), recipe.getOutput1(), true, true) && ItemUtils.isItemEqual(getStackInSlot(3), recipe.getOutput2(), true, true) && !areBothOutputsFull()) {
decrStackSize(2, -recipe.getOutput1().stackSize);
decrStackSize(3, -recipe.getOutput2().stackSize);
tickTime = 0;
recipe = null;
}
//When slot one has stuff and slot 2 is empty
if (ItemUtils.isItemEqual(getStackInSlot(2), recipe.getOutput1(), true, true) && getStackInSlot(3) == null) {
//Stops if the first slot if full
if (recipe.getOutput1() != null
&& getStackInSlot(2) != null
&& getStackInSlot(2).stackSize
+ recipe.getOutput1().stackSize > recipe
.getOutput1().getMaxStackSize()) {
return;
}
decrStackSize(2, recipe.getOutput1().stackSize);
setInventorySlotContents(3, recipe.getOutput2());
tickTime = 0;
recipe = null;
}
//When slot 2 has stuff and slot 1 is empty
if (ItemUtils.isItemEqual(getStackInSlot(3), recipe.getInput2(), true, true) && getStackInSlot(2) == null) {
if (recipe.getOutput2() != null
&& getStackInSlot(3) != null
&& getStackInSlot(3).stackSize
+ recipe.getOutput2().stackSize > recipe
.getOutput1().getMaxStackSize()) {
return;
}
decrStackSize(3, recipe.getOutput2().stackSize);
setInventorySlotContents(2, recipe.getOutput1());
tickTime = 0;
recipe = null;
}
} else if (getHeat() >= recipe.getMinHeat()) {
if (energy.canUseEnergy(5)) {
tickTime++;
energy.useEnergy(5);
}
}
}
} else {
recipe = null;
tickTime = 0;
2015-04-28 19:23:48 +02:00
}
}
2015-04-28 22:04:28 +02:00
public boolean areBothOutputsFull() {
if (recipe.getOutput1() != null
&& getStackInSlot(2) != null
&& getStackInSlot(2).stackSize
+ recipe.getOutput1().stackSize > recipe
.getOutput1().getMaxStackSize()) {
return true;
}
if (recipe.getOutput2() != null
&& getStackInSlot(3) != null
&& getStackInSlot(3).stackSize
+ recipe.getOutput2().stackSize > recipe
.getOutput1().getMaxStackSize()) {
return true;
}
return false;
}
2015-04-28 19:23:48 +02:00
@Override
2015-04-28 22:04:28 +02:00
public int getSizeInventory() {
2015-04-28 19:23:48 +02:00
return inventory.getSizeInventory();
}
@Override
2015-04-28 22:04:28 +02:00
public ItemStack getStackInSlot(int p_70301_1_) {
2015-04-28 19:23:48 +02:00
return inventory.getStackInSlot(p_70301_1_);
}
@Override
2015-04-28 22:04:28 +02:00
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
2015-04-28 19:23:48 +02:00
return inventory.decrStackSize(p_70298_1_, p_70298_2_);
}
@Override
2015-04-28 22:04:28 +02:00
public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
2015-04-28 19:23:48 +02:00
return inventory.getStackInSlotOnClosing(p_70304_1_);
}
@Override
2015-04-28 22:04:28 +02:00
public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {
2015-04-28 19:23:48 +02:00
inventory.setInventorySlotContents(p_70299_1_, p_70299_2_);
}
@Override
2015-04-28 22:04:28 +02:00
public String getInventoryName() {
2015-04-28 19:23:48 +02:00
return inventory.getInventoryName();
}
@Override
2015-04-28 22:04:28 +02:00
public boolean hasCustomInventoryName() {
2015-04-28 19:23:48 +02:00
return inventory.hasCustomInventoryName();
}
@Override
2015-04-28 22:04:28 +02:00
public int getInventoryStackLimit() {
2015-04-28 19:23:48 +02:00
return inventory.getInventoryStackLimit();
}
@Override
2015-04-28 22:04:28 +02:00
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
2015-04-28 19:23:48 +02:00
return inventory.isUseableByPlayer(p_70300_1_);
}
@Override
2015-04-28 22:04:28 +02:00
public void openInventory() {
2015-04-28 19:23:48 +02:00
inventory.openInventory();
}
@Override
2015-04-28 22:04:28 +02:00
public void closeInventory() {
2015-04-28 19:23:48 +02:00
inventory.closeInventory();
}
@Override
2015-04-28 22:04:28 +02:00
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
2015-04-28 19:23:48 +02:00
return inventory.isItemValidForSlot(p_94041_1_, p_94041_2_);
}
@Override
2015-04-28 22:04:28 +02:00
public void invalidate() {
2015-04-28 19:23:48 +02:00
energy.invalidate();
}
@Override
2015-04-28 22:04:28 +02:00
public void onChunkUnload() {
2015-04-28 19:23:48 +02:00
energy.onChunkUnload();
}
2015-04-28 22:04:28 +02:00
public Packet getDescriptionPacket() {
2015-04-28 19:23:48 +02:00
NBTTagCompound nbtTag = new NBTTagCompound();
writeToNBT(nbtTag);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord,
this.zCoord, 1, nbtTag);
}
@Override
public void onDataPacket(NetworkManager net,
2015-04-28 22:04:28 +02:00
S35PacketUpdateTileEntity packet) {
2015-04-28 19:23:48 +02:00
worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord,
yCoord, zCoord);
readFromNBT(packet.func_148857_g());
}
@Override
2015-04-28 22:04:28 +02:00
public void readFromNBT(NBTTagCompound tagCompound) {
2015-04-28 19:23:48 +02:00
super.readFromNBT(tagCompound);
inventory.readFromNBT(tagCompound);
tickTime = tagCompound.getInteger("tickTime");
}
@Override
2015-04-28 22:04:28 +02:00
public void writeToNBT(NBTTagCompound tagCompound) {
2015-04-28 19:23:48 +02:00
super.writeToNBT(tagCompound);
inventory.writeToNBT(tagCompound);
writeUpdateToNBT(tagCompound);
}
2015-04-28 22:04:28 +02:00
public void writeUpdateToNBT(NBTTagCompound tagCompound) {
2015-04-28 19:23:48 +02:00
tagCompound.setInteger("tickTime", tickTime);
}
}