Guest User

TileEntitySpit.class

a guest
May 22nd, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. package com.github.wolfiewaffle.tanspit.tileentity;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.item.ItemFood;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.item.crafting.FurnaceRecipes;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.network.NetworkManager;
  11. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.util.EnumFacing;
  14. import net.minecraft.util.ITickable;
  15. import net.minecraftforge.common.capabilities.Capability;
  16. import net.minecraftforge.items.CapabilityItemHandler;
  17. import net.minecraftforge.items.IItemHandler;
  18. import net.minecraftforge.items.ItemStackHandler;
  19. import toughasnails.api.TANBlocks;
  20. import toughasnails.block.BlockTANCampfire;
  21.  
  22. public class TileEntitySpit extends TileEntity implements ITickable {
  23.     public int cookTimeRemaining = 700;
  24.     private static final int totalTime = 700;
  25.  
  26.     public ItemStackHandler items = new ItemStackHandler(3) {
  27.         @Override
  28.         protected int getStackLimit(int slot, ItemStack stack) {
  29.             return 1;
  30.         }
  31.  
  32.         // @formatter:off
  33.         @Override
  34.         public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
  35.             if ((stack != null) && ((stack.getItem() instanceof ItemFood)) &&
  36.                     (FurnaceRecipes.instance().getSmeltingResult(stack) != null) &&
  37.                     (FurnaceRecipes.instance().getSmeltingResult(stack).getItem() instanceof ItemFood)) {
  38.                 return super.insertItem(slot, stack, simulate);
  39.             }
  40.             return stack;
  41.         }
  42.         // @formatter:on
  43.  
  44.         // Unsure what this is for
  45.         @Override
  46.         protected void onContentsChanged(int slot) {
  47.             super.onContentsChanged(slot);
  48.             TileEntitySpit.this.markDirty();
  49.  
  50.             IBlockState state = world.getBlockState(pos);
  51.             world.notifyBlockUpdate(pos, state, state, 3);
  52.         }
  53.     };
  54.  
  55.     // Unsure what this is for
  56.     @Override
  57.     public SPacketUpdateTileEntity getUpdatePacket() {
  58.         NBTTagCompound tag = new NBTTagCompound();
  59.         writeToNBT(tag);
  60.         return new SPacketUpdateTileEntity(pos, 0, tag);
  61.     }
  62.  
  63.     // Unsure what this is for
  64.     @Override
  65.     public NBTTagCompound getUpdateTag() {
  66.         return this.writeToNBT(new NBTTagCompound());
  67.     }
  68.  
  69.     // Unsure what this is for
  70.     @Override
  71.     public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
  72.         super.onDataPacket(net, packet);
  73.         readFromNBT(packet.getNbtCompound());
  74.  
  75.         IBlockState state = world.getBlockState(pos);
  76.         world.notifyBlockUpdate(pos, state, state, 3);
  77.     }
  78.  
  79.     private boolean isCooking() {
  80.         IBlockState down = world.getBlockState(pos.down());
  81.  
  82.         if (down.getBlock() == TANBlocks.campfire) {
  83.             if (down.getValue(BlockTANCampfire.BURNING)) {
  84.                 return true;
  85.             }
  86.         }
  87.         return false;
  88.     }
  89.  
  90.     @Override
  91.     public void update() {
  92.  
  93.         // Only update on server
  94.         if (!world.isRemote) {
  95.             if (isCooking() && canCookAnything()) {
  96.                 cookTimeRemaining--;
  97.  
  98.                 // Time has reached 0
  99.                 if (cookTimeRemaining <= 0) {
  100.                     for (int i = 0; i < inventory().getSlots(); i++) {
  101.  
  102.                         // Attempt to cook items
  103.                         ItemStack stack = items.getStackInSlot(i);
  104.  
  105.                         if (!getCookingResult(stack).isEmpty()) {
  106.                             items.setStackInSlot(i, getCookingResult(stack));
  107.                         }
  108.                     }
  109.                     cookTimeRemaining = totalTime;
  110.                 }
  111.                 markDirty();
  112.             } else {
  113.  
  114.                 // Nothing to cook, cool down
  115.                 if (cookTimeRemaining < totalTime) {
  116.                     cookTimeRemaining++;
  117.                     markDirty();
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123.     public static ItemStack getCookingResult(ItemStack stack) {
  124.         return FurnaceRecipes.instance().getSmeltingResult(stack).copy();
  125.     }
  126.  
  127.     private boolean canCookAnything() {
  128.         for (int i = 0; i < inventory().getSlots(); i++) {
  129.             if (!FurnaceRecipes.instance().getSmeltingResult(items.getStackInSlot(i)).copy().isEmpty()) {
  130.                 return true;
  131.             }
  132.         }
  133.         return false;
  134.     }
  135.  
  136.     // Capabilities
  137.     @Override
  138.     public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
  139.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  140.             return true;
  141.         return super.hasCapability(capability, facing);
  142.     }
  143.  
  144.     // Capabilities
  145.     @SuppressWarnings("unchecked")
  146.     @Override
  147.     public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
  148.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  149.             return (T) items;
  150.         return super.getCapability(capability, facing);
  151.     }
  152.  
  153.     /*public boolean isUseableByPlayer(EntityPlayer player) {
  154.         return world.getTileEntity(pos) == this
  155.                 && player.getDistanceSq(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D) <= 64.0D;
  156.     }*/
  157.  
  158.     @Override
  159.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  160.         compound = super.writeToNBT(compound);
  161.         compound.setTag("Items", items.serializeNBT());
  162.         compound.setInteger("CookTime", (short)this.cookTimeRemaining);
  163.         return compound;
  164.     }
  165.  
  166.     @Override
  167.     public void readFromNBT(NBTTagCompound compound) {
  168.         super.readFromNBT(compound);
  169.         items.deserializeNBT(compound.getCompoundTag("Items"));
  170.         cookTimeRemaining = compound.getInteger("CookTime");
  171.     }
  172.  
  173.     public IItemHandler inventory() {
  174.         return items;
  175.     }
  176.    
  177. }
Add Comment
Please, Sign In to add comment