Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package qwertyasdef.alchemtrans.tile;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.play.server.SPacketUpdateTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextComponentTranslation;
- import net.minecraftforge.fluids.*;
- import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
- import net.minecraftforge.fluids.capability.IFluidHandlerItem;
- import qwertyasdef.alchemtrans.AlchemTrans;
- import qwertyasdef.alchemtrans.block.BlockNetherrackBoiler;
- import qwertyasdef.alchemtrans.item.crafting.RecipeDisintegrating;
- public class TileNetherrackBoiler extends TileEntity implements ITickable, IInventory {
- public String defaultName = "tile." + AlchemTrans.MODID + "." + BlockNetherrackBoiler.name;
- private String customName;
- private boolean hasHeat = false;
- private int cookTime = 0;
- private int totalCookTime = 0;
- private NonNullList<ItemStack> inventory = NonNullList.withSize(3, ItemStack.EMPTY);
- private FluidTank tank = new FluidTank(Fluid.BUCKET_VOLUME * 4);
- // Name
- public String getCustomName() {
- return this.customName;
- }
- public void setCustomName(String customName) {
- this.customName = customName;
- }
- @Override
- public String getName() {
- return this.hasCustomName() ? this.getCustomName() : this.defaultName + ".name";
- }
- @Override
- public boolean hasCustomName() {
- return this.getCustomName() != null && !this.getCustomName().equals("");
- }
- @Override
- public ITextComponent getDisplayName() {
- return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
- }
- // TileEntity
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- if (this.hasCustomName()) {
- compound.setString("CustomName", this.getCustomName());
- }
- compound.setBoolean("HasHeat", this.hasHeat);
- compound.setInteger("CookTime", this.cookTime);
- compound.setInteger("TotalCookTime", this.totalCookTime);
- ItemStackHelper.saveAllItems(compound, this.inventory);
- NBTTagCompound tag = new NBTTagCompound();
- if (tank.getFluid() != null) {
- tank.getFluid().writeToNBT(tag);
- }
- compound.setTag("Fluid", tag);
- return compound;
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- if (compound.hasKey("CustomName", 8)) {
- this.setCustomName(compound.getString("CustomName"));
- }
- this.hasHeat = compound.getBoolean("HasHeat");
- this.cookTime = compound.getInteger("CookTime");
- this.totalCookTime = compound.getInteger("TotalCookTime");
- ItemStackHelper.loadAllItems(compound, this.inventory);
- if (compound.hasKey("Fluid")) {
- NBTTagCompound tag = (NBTTagCompound) compound.getTag("Fluid");
- tank.setFluid(FluidStack.loadFluidStackFromNBT(tag));
- }
- }
- @Override
- public NBTTagCompound getUpdateTag() {
- return writeToNBT(new NBTTagCompound());
- }
- @Override
- public SPacketUpdateTileEntity getUpdatePacket() {
- return new SPacketUpdateTileEntity(getPos(), 0, getUpdateTag());
- }
- @Override
- public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
- this.readFromNBT(packet.getNbtCompound());
- }
- @Override
- public boolean isUsableByPlayer(EntityPlayer player) {
- return this.getWorld().getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
- }
- // ITickable
- @Override
- public void update() {
- boolean dataChanged = false;
- if (!world.isRemote) {
- dataChanged = transferFluid();
- // Smelt fluid
- FluidStack fluid = this.tank.getFluid();
- if (canSmelt()) {
- if (this.totalCookTime == 0) {
- this.totalCookTime = getCookTime(fluid);
- }
- ++this.cookTime;
- if (this.cookTime >= this.totalCookTime) {
- smeltFluid();
- fluid = this.tank.getFluid();
- if (fluid != null) {
- this.totalCookTime = getCookTime(fluid);
- } else {
- this.totalCookTime = 0;
- }
- this.cookTime = 0;
- }
- dataChanged = true;
- } else {
- if (this.cookTime != 0) {
- this.cookTime = 0;
- dataChanged = true;
- }
- }
- }
- if (dataChanged) {
- this.markDirty();
- }
- if (!world.isRemote) {
- System.out.println("Server heat: " + this.getField(2));
- } else {
- System.out.println("Client heat: " + this.getField(2));
- }
- }
- private boolean transferFluid() {
- // Transfer fluid from container to internal tank
- ItemStack containerInput = this.getStackInSlot(0);
- ItemStack containerOutput = this.getStackInSlot(1);
- if (!containerInput.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
- return false;
- }
- IFluidHandlerItem fluidContainer = FluidUtil.getFluidHandler(containerInput);
- FluidStack inputFluid = fluidContainer.getTankProperties()[0].getContents();
- FluidStack currentFluid = this.tank.getFluid();
- if (!fluidContainer.getContainer().isItemEqual(containerOutput) && !containerOutput.isEmpty()) {
- return false;
- }
- if (inputFluid == null || (!inputFluid.isFluidEqual(currentFluid) && currentFluid != null)) {
- return false;
- }
- int space = this.tank.getCapacity() - this.tank.getFluidAmount();
- FluidStack fluidDrained = fluidContainer.drain(space, true);
- this.tank.fill(fluidDrained, true);
- if (fluidContainer.getTankProperties()[0].getContents() == null) {
- containerInput.shrink(1);
- if (containerOutput.isEmpty()) {
- this.setInventorySlotContents(1, fluidContainer.getContainer().copy());
- } else {
- containerOutput.grow(1);
- }
- }
- return true;
- }
- private int getCookTime(FluidStack stack) {
- return 200;
- }
- private boolean canSmelt() {
- if (!this.hasHeat) {
- return false;
- }
- FluidStack input = this.tank.getFluid();
- if (input == null || input.amount <= 1000) {
- return false;
- }
- ItemStack result = RecipeDisintegrating.instance().getOutput(input);
- ItemStack output = this.getStackInSlot(2);
- if (result.isEmpty()) {
- return false;
- }
- if (output.isEmpty()) {
- return true;
- }
- if (!result.isItemEqual(output)) {
- return false;
- }
- if (result.getCount() + output.getCount() <= this.getInventoryStackLimit() && result.getCount() + output.getCount() <= output.getMaxStackSize()) {
- return true;
- }
- return false;
- }
- private void smeltFluid() {
- FluidStack input = this.tank.getFluid();
- ItemStack result = RecipeDisintegrating.instance().getOutput(input);
- ItemStack output = this.getStackInSlot(2);
- input.amount -= 1000;
- if (input.amount == 0) {
- this.tank.setFluid(null);
- }
- if (output.isEmpty()) {
- this.setInventorySlotContents(2, result.copy());
- } else {
- output.grow(result.getCount());
- }
- }
- // IInventory
- @Override
- public int getSizeInventory() {
- return this.inventory.size();
- }
- @Override
- public boolean isEmpty() {
- for (int i = 0; i < getSizeInventory(); i++) {
- if (!this.getStackInSlot(i).isEmpty()) {
- return false;
- }
- }
- return true;
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- if (index < 0 || index >= this.getSizeInventory())
- return ItemStack.EMPTY;
- return this.inventory.get(index);
- }
- @Override
- public ItemStack decrStackSize(int index, int count) {
- if (!this.getStackInSlot(index).isEmpty()) {
- ItemStack itemstack;
- if (this.getStackInSlot(index).getCount() <= count) {
- itemstack = this.getStackInSlot(index);
- this.setInventorySlotContents(index, ItemStack.EMPTY);
- this.markDirty();
- return itemstack;
- } else {
- itemstack = this.getStackInSlot(index).splitStack(count);
- if (this.getStackInSlot(index).getCount() <= 0) {
- this.setInventorySlotContents(index, ItemStack.EMPTY);
- } else {
- //Just to show that changes happened
- this.setInventorySlotContents(index, this.getStackInSlot(index));
- }
- this.markDirty();
- return itemstack;
- }
- } else {
- return ItemStack.EMPTY;
- }
- }
- @Override
- public ItemStack removeStackFromSlot(int index) {
- ItemStack stack = this.getStackInSlot(index);
- this.setInventorySlotContents(index, ItemStack.EMPTY);
- return stack;
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- if (index < 0 || index >= this.getSizeInventory()) {
- return;
- }
- if (!stack.isEmpty() && stack.getCount() > this.getInventoryStackLimit()) {
- stack.setCount(this.getInventoryStackLimit());
- }
- if (!stack.isEmpty() && stack.getCount() == 0) {
- stack = ItemStack.EMPTY;
- }
- this.inventory.set(index, stack);
- this.markDirty();
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public void openInventory(EntityPlayer player) {
- }
- @Override
- public void closeInventory(EntityPlayer player) {
- }
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return index == 0;
- }
- @Override
- public int getField(int id) {
- switch (id) {
- case 0:
- return this.cookTime;
- case 1:
- return this.totalCookTime;
- case 2:
- return this.hasHeat ? 1 : 0;
- default:
- return 0;
- }
- }
- @Override
- public void setField(int id, int value) {
- switch (id) {
- case 0:
- this.cookTime = value;
- break;
- case 1:
- this.totalCookTime = value;
- break;
- case 2:
- this.hasHeat = (value == 1);
- break;
- }
- }
- @Override
- public int getFieldCount() {
- return 3;
- }
- @Override
- public void clear() {
- for (int i = 0; i < this.getSizeInventory(); i++) {
- this.setInventorySlotContents(i, ItemStack.EMPTY);
- }
- }
- // Fluid stuff
- public FluidStack getFluid() {
- return this.tank.getFluid();
- }
- public void setFluid(FluidStack fluid) {
- this.tank.setFluid(fluid);
- }
- public int getFluidAmount() {
- if (this.getFluid() == null) {
- return 0;
- }
- return this.getFluid().amount;
- }
- public void setFluidAmount(int amount) {
- this.getFluid().amount = amount;
- }
- public int getFluidCapacity() {
- return this.tank.getCapacity();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement