Advertisement
Guest User

TileNetherrackBoiler.java

a guest
Jul 6th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.48 KB | None | 0 0
  1. package qwertyasdef.alchemtrans.tile;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.inventory.IInventory;
  5. import net.minecraft.inventory.ItemStackHelper;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.network.NetworkManager;
  9. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  10. import net.minecraft.tileentity.TileEntity;
  11. import net.minecraft.util.EnumFacing;
  12. import net.minecraft.util.ITickable;
  13. import net.minecraft.util.NonNullList;
  14. import net.minecraft.util.text.ITextComponent;
  15. import net.minecraft.util.text.TextComponentString;
  16. import net.minecraft.util.text.TextComponentTranslation;
  17. import net.minecraftforge.fluids.*;
  18. import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
  19. import net.minecraftforge.fluids.capability.IFluidHandlerItem;
  20. import qwertyasdef.alchemtrans.AlchemTrans;
  21. import qwertyasdef.alchemtrans.block.BlockNetherrackBoiler;
  22. import qwertyasdef.alchemtrans.item.crafting.RecipeDisintegrating;
  23.  
  24. public class TileNetherrackBoiler extends TileEntity implements ITickable, IInventory {
  25.  
  26.     public String defaultName = "tile." + AlchemTrans.MODID + "." + BlockNetherrackBoiler.name;
  27.  
  28.     private String customName;
  29.  
  30.     private boolean hasHeat = false;
  31.     private int cookTime = 0;
  32.     private int totalCookTime = 0;
  33.  
  34.     private NonNullList<ItemStack> inventory = NonNullList.withSize(3, ItemStack.EMPTY);
  35.     private FluidTank tank = new FluidTank(Fluid.BUCKET_VOLUME * 4);
  36.  
  37.     // Name
  38.     public String getCustomName() {
  39.         return this.customName;
  40.     }
  41.  
  42.     public void setCustomName(String customName) {
  43.         this.customName = customName;
  44.     }
  45.  
  46.     @Override
  47.     public String getName() {
  48.         return this.hasCustomName() ? this.getCustomName() : this.defaultName + ".name";
  49.     }
  50.  
  51.     @Override
  52.     public boolean hasCustomName() {
  53.         return this.getCustomName() != null && !this.getCustomName().equals("");
  54.     }
  55.  
  56.     @Override
  57.     public ITextComponent getDisplayName() {
  58.         return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
  59.     }
  60.  
  61.     // TileEntity
  62.     @Override
  63.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  64.         super.writeToNBT(compound);
  65.  
  66.         if (this.hasCustomName()) {
  67.             compound.setString("CustomName", this.getCustomName());
  68.         }
  69.  
  70.         compound.setBoolean("HasHeat", this.hasHeat);
  71.         compound.setInteger("CookTime", this.cookTime);
  72.         compound.setInteger("TotalCookTime", this.totalCookTime);
  73.  
  74.         ItemStackHelper.saveAllItems(compound, this.inventory);
  75.         NBTTagCompound tag = new NBTTagCompound();
  76.         if (tank.getFluid() != null) {
  77.             tank.getFluid().writeToNBT(tag);
  78.         }
  79.         compound.setTag("Fluid", tag);
  80.  
  81.         return compound;
  82.     }
  83.  
  84.     @Override
  85.     public void readFromNBT(NBTTagCompound compound) {
  86.         super.readFromNBT(compound);
  87.  
  88.         if (compound.hasKey("CustomName", 8)) {
  89.             this.setCustomName(compound.getString("CustomName"));
  90.         }
  91.  
  92.         this.hasHeat = compound.getBoolean("HasHeat");
  93.         this.cookTime = compound.getInteger("CookTime");
  94.         this.totalCookTime = compound.getInteger("TotalCookTime");
  95.  
  96.         ItemStackHelper.loadAllItems(compound, this.inventory);
  97.         if (compound.hasKey("Fluid")) {
  98.             NBTTagCompound tag = (NBTTagCompound) compound.getTag("Fluid");
  99.             tank.setFluid(FluidStack.loadFluidStackFromNBT(tag));
  100.         }
  101.     }
  102.  
  103.     @Override
  104.     public NBTTagCompound getUpdateTag() {
  105.         return writeToNBT(new NBTTagCompound());
  106.     }
  107.  
  108.     @Override
  109.     public SPacketUpdateTileEntity getUpdatePacket() {
  110.         return new SPacketUpdateTileEntity(getPos(), 0, getUpdateTag());
  111.     }
  112.  
  113.     @Override
  114.     public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
  115.         this.readFromNBT(packet.getNbtCompound());
  116.     }
  117.  
  118.     @Override
  119.     public boolean isUsableByPlayer(EntityPlayer player) {
  120.         return this.getWorld().getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
  121.     }
  122.  
  123.     // ITickable
  124.     @Override
  125.     public void update() {
  126.         boolean dataChanged = false;
  127.         if (!world.isRemote) {
  128.             dataChanged = transferFluid();
  129.  
  130.             // Smelt fluid
  131.             FluidStack fluid = this.tank.getFluid();
  132.             if (canSmelt()) {
  133.                 if (this.totalCookTime == 0) {
  134.                     this.totalCookTime = getCookTime(fluid);
  135.                 }
  136.  
  137.                 ++this.cookTime;
  138.  
  139.                 if (this.cookTime >= this.totalCookTime) {
  140.                     smeltFluid();
  141.                     fluid = this.tank.getFluid();
  142.  
  143.                     if (fluid != null) {
  144.                         this.totalCookTime = getCookTime(fluid);
  145.                     } else {
  146.                         this.totalCookTime = 0;
  147.                     }
  148.                     this.cookTime = 0;
  149.                 }
  150.  
  151.                 dataChanged = true;
  152.             } else {
  153.                 if (this.cookTime != 0) {
  154.                     this.cookTime = 0;
  155.                     dataChanged = true;
  156.                 }
  157.             }
  158.         }
  159.  
  160.         if (dataChanged) {
  161.             this.markDirty();
  162.         }
  163.  
  164.         if (!world.isRemote) {
  165.             System.out.println("Server heat: " + this.getField(2));
  166.         } else {
  167.             System.out.println("Client heat: " + this.getField(2));
  168.         }
  169.     }
  170.  
  171.     private boolean transferFluid() {
  172.         // Transfer fluid from container to internal tank
  173.         ItemStack containerInput = this.getStackInSlot(0);
  174.         ItemStack containerOutput = this.getStackInSlot(1);
  175.         if (!containerInput.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
  176.             return false;
  177.         }
  178.  
  179.         IFluidHandlerItem fluidContainer = FluidUtil.getFluidHandler(containerInput);
  180.         FluidStack inputFluid = fluidContainer.getTankProperties()[0].getContents();
  181.         FluidStack currentFluid = this.tank.getFluid();
  182.  
  183.         if (!fluidContainer.getContainer().isItemEqual(containerOutput) && !containerOutput.isEmpty()) {
  184.             return false;
  185.         }
  186.  
  187.         if (inputFluid == null || (!inputFluid.isFluidEqual(currentFluid) && currentFluid != null)) {
  188.             return false;
  189.         }
  190.  
  191.         int space = this.tank.getCapacity() - this.tank.getFluidAmount();
  192.         FluidStack fluidDrained = fluidContainer.drain(space, true);
  193.         this.tank.fill(fluidDrained, true);
  194.  
  195.         if (fluidContainer.getTankProperties()[0].getContents() == null) {
  196.             containerInput.shrink(1);
  197.             if (containerOutput.isEmpty()) {
  198.                 this.setInventorySlotContents(1, fluidContainer.getContainer().copy());
  199.             } else {
  200.                 containerOutput.grow(1);
  201.             }
  202.         }
  203.  
  204.         return true;
  205.  
  206.     }
  207.  
  208.     private int getCookTime(FluidStack stack) {
  209.         return 200;
  210.     }
  211.  
  212.     private boolean canSmelt() {
  213.         if (!this.hasHeat) {
  214.             return false;
  215.         }
  216.  
  217.         FluidStack input = this.tank.getFluid();
  218.         if (input == null || input.amount <= 1000) {
  219.             return false;
  220.         }
  221.  
  222.         ItemStack result = RecipeDisintegrating.instance().getOutput(input);
  223.         ItemStack output = this.getStackInSlot(2);
  224.  
  225.         if (result.isEmpty()) {
  226.             return false;
  227.         }
  228.  
  229.         if (output.isEmpty()) {
  230.             return true;
  231.         }
  232.  
  233.         if (!result.isItemEqual(output)) {
  234.             return false;
  235.         }
  236.  
  237.         if (result.getCount() + output.getCount() <= this.getInventoryStackLimit() && result.getCount() + output.getCount() <= output.getMaxStackSize()) {
  238.             return true;
  239.         }
  240.  
  241.         return false;
  242.     }
  243.  
  244.     private void smeltFluid() {
  245.         FluidStack input = this.tank.getFluid();
  246.         ItemStack result = RecipeDisintegrating.instance().getOutput(input);
  247.         ItemStack output = this.getStackInSlot(2);
  248.  
  249.         input.amount -= 1000;
  250.         if (input.amount == 0) {
  251.             this.tank.setFluid(null);
  252.         }
  253.  
  254.         if (output.isEmpty()) {
  255.             this.setInventorySlotContents(2, result.copy());
  256.         } else {
  257.             output.grow(result.getCount());
  258.         }
  259.     }
  260.  
  261.     // IInventory
  262.     @Override
  263.     public int getSizeInventory() {
  264.         return this.inventory.size();
  265.     }
  266.  
  267.     @Override
  268.     public boolean isEmpty() {
  269.         for (int i = 0; i < getSizeInventory(); i++) {
  270.             if (!this.getStackInSlot(i).isEmpty()) {
  271.                 return false;
  272.             }
  273.         }
  274.         return true;
  275.     }
  276.  
  277.     @Override
  278.     public ItemStack getStackInSlot(int index) {
  279.         if (index < 0 || index >= this.getSizeInventory())
  280.             return ItemStack.EMPTY;
  281.         return this.inventory.get(index);
  282.     }
  283.  
  284.     @Override
  285.     public ItemStack decrStackSize(int index, int count) {
  286.         if (!this.getStackInSlot(index).isEmpty()) {
  287.             ItemStack itemstack;
  288.  
  289.             if (this.getStackInSlot(index).getCount() <= count) {
  290.                 itemstack = this.getStackInSlot(index);
  291.                 this.setInventorySlotContents(index, ItemStack.EMPTY);
  292.                 this.markDirty();
  293.                 return itemstack;
  294.             } else {
  295.                 itemstack = this.getStackInSlot(index).splitStack(count);
  296.  
  297.                 if (this.getStackInSlot(index).getCount() <= 0) {
  298.                     this.setInventorySlotContents(index, ItemStack.EMPTY);
  299.                 } else {
  300.                     //Just to show that changes happened
  301.                     this.setInventorySlotContents(index, this.getStackInSlot(index));
  302.                 }
  303.  
  304.                 this.markDirty();
  305.                 return itemstack;
  306.             }
  307.         } else {
  308.             return ItemStack.EMPTY;
  309.         }
  310.     }
  311.  
  312.     @Override
  313.     public ItemStack removeStackFromSlot(int index) {
  314.         ItemStack stack = this.getStackInSlot(index);
  315.         this.setInventorySlotContents(index, ItemStack.EMPTY);
  316.         return stack;
  317.     }
  318.  
  319.     @Override
  320.     public void setInventorySlotContents(int index, ItemStack stack) {
  321.         if (index < 0 || index >= this.getSizeInventory()) {
  322.             return;
  323.         }
  324.  
  325.         if (!stack.isEmpty() && stack.getCount() > this.getInventoryStackLimit()) {
  326.             stack.setCount(this.getInventoryStackLimit());
  327.         }
  328.  
  329.         if (!stack.isEmpty() && stack.getCount() == 0) {
  330.             stack = ItemStack.EMPTY;
  331.         }
  332.  
  333.         this.inventory.set(index, stack);
  334.         this.markDirty();
  335.     }
  336.  
  337.     @Override
  338.     public int getInventoryStackLimit() {
  339.         return 64;
  340.     }
  341.  
  342.     @Override
  343.     public void openInventory(EntityPlayer player) {
  344.     }
  345.  
  346.     @Override
  347.     public void closeInventory(EntityPlayer player) {
  348.     }
  349.  
  350.     @Override
  351.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  352.         return index == 0;
  353.     }
  354.  
  355.     @Override
  356.     public int getField(int id) {
  357.         switch (id) {
  358.             case 0:
  359.                 return this.cookTime;
  360.             case 1:
  361.                 return this.totalCookTime;
  362.             case 2:
  363.                 return this.hasHeat ? 1 : 0;
  364.             default:
  365.                 return 0;
  366.         }
  367.     }
  368.  
  369.     @Override
  370.     public void setField(int id, int value) {
  371.         switch (id) {
  372.             case 0:
  373.                 this.cookTime = value;
  374.                 break;
  375.             case 1:
  376.                 this.totalCookTime = value;
  377.                 break;
  378.             case 2:
  379.                 this.hasHeat = (value == 1);
  380.                 break;
  381.         }
  382.     }
  383.  
  384.     @Override
  385.     public int getFieldCount() {
  386.         return 3;
  387.     }
  388.  
  389.     @Override
  390.     public void clear() {
  391.         for (int i = 0; i < this.getSizeInventory(); i++) {
  392.             this.setInventorySlotContents(i, ItemStack.EMPTY);
  393.         }
  394.     }
  395.  
  396.     // Fluid stuff
  397.     public FluidStack getFluid() {
  398.         return this.tank.getFluid();
  399.     }
  400.  
  401.     public void setFluid(FluidStack fluid) {
  402.         this.tank.setFluid(fluid);
  403.     }
  404.  
  405.     public int getFluidAmount() {
  406.         if (this.getFluid() == null) {
  407.             return 0;
  408.         }
  409.  
  410.         return this.getFluid().amount;
  411.     }
  412.  
  413.     public void setFluidAmount(int amount) {
  414.         this.getFluid().amount = amount;
  415.     }
  416.  
  417.     public int getFluidCapacity() {
  418.         return this.tank.getCapacity();
  419.     }
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement