Draco18s

TileEntityTanningRack

Oct 27th, 2016
121
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.draco18s.farming.entities;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import javax.annotation.Nullable;
  6.  
  7. import org.apache.logging.log4j.Level;
  8.  
  9. import com.draco18s.farming.FarmingBase;
  10. import com.draco18s.farming.entities.capabilities.ItemLeatherHandler;
  11. import com.draco18s.farming.entities.capabilities.ItemSaltHandler;
  12.  
  13. import net.minecraft.block.BlockHorizontal;
  14. import net.minecraft.block.state.IBlockState;
  15. import net.minecraft.init.Items;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.network.NetworkManager;
  19. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  20. import net.minecraft.tileentity.TileEntity;
  21. import net.minecraft.util.EnumFacing;
  22. import net.minecraft.util.ITickable;
  23. import net.minecraft.util.math.BlockPos;
  24. import net.minecraft.world.World;
  25. import net.minecraftforge.common.capabilities.Capability;
  26. import net.minecraftforge.items.CapabilityItemHandler;
  27. import net.minecraftforge.items.ItemStackHandler;
  28. import net.minecraftforge.items.wrapper.CombinedInvWrapper;
  29.  
  30. public class TileEntityTanner extends TileEntity implements ITickable {
  31.  
  32.     protected ItemLeatherHandler leftSlot;
  33.     protected ItemLeatherHandler rightSlot;
  34.     protected ItemSaltHandler saltSlot;
  35.     protected int[] tanningTime;
  36.     protected int saltTime;
  37.     private boolean shouldUpdate;
  38.  
  39.     public TileEntityTanner() {
  40.         leftSlot = new ItemLeatherHandler(1);
  41.         rightSlot = new ItemLeatherHandler(1);
  42.         saltSlot = new ItemSaltHandler(1);
  43.         tanningTime = new int[]{0,0};
  44.     }
  45.  
  46.     @Override
  47.     public void update() {
  48.         if(leftSlot.getStackInSlot(0) != null) {
  49.             if(leftSlot.getStackInSlot(0).getItem() == FarmingBase.rawLeather) {
  50.                 if(cureLeather(0)) {
  51.                     leftSlot.setStackInSlot(0, new ItemStack(Items.LEATHER));
  52.                     setBlockToUpdate();
  53.                 }
  54.             }
  55.         }
  56.         else {
  57.             tanningTime[0] = 0;
  58.         }
  59.         if(rightSlot.getStackInSlot(0) != null) {
  60.             if(rightSlot.getStackInSlot(0).getItem() == FarmingBase.rawLeather) {
  61.                 if(cureLeather(1)) {
  62.                     rightSlot.setStackInSlot(0, new ItemStack(Items.LEATHER));
  63.                     setBlockToUpdate();
  64.                 }
  65.             }
  66.         }
  67.         else {
  68.             tanningTime[1] = 0;
  69.         }
  70.         if(saltTime > 0) {
  71.             saltTime -= (canRainHere()?2:1);
  72.             if(saltTime <= 0)
  73.                 setBlockToUpdate();
  74.         }
  75.         //if(!worldObj.isRemote)
  76.             //FarmingBase.logger.log(Level.INFO, tanningTime[0] + "," + tanningTime[1] + "|" + saltTime);
  77.         int v = Math.max(Math.max(tanningTime[0], tanningTime[1]),saltTime);
  78.         if(v > 0 && v % 100 <= 1) {
  79.             setBlockToUpdate();
  80.         }
  81.         if(shouldUpdate) {
  82.             sendUpdates();
  83.             shouldUpdate = false;
  84.         }
  85.     }
  86.    
  87.     private void sendUpdates() {
  88.         markDirty();
  89.         worldObj.markBlockRangeForRenderUpdate(pos, pos);
  90.         worldObj.notifyBlockUpdate(pos, getState(), getState(), 3);
  91.     }
  92.  
  93.     private boolean canRainHere() {
  94.         return worldObj.isRaining() && worldObj.getPrecipitationHeight(pos).getY() <= pos.getY();
  95.     }
  96.  
  97.     private IBlockState getState() {
  98.         return worldObj.getBlockState(pos);
  99.     }
  100.  
  101.     private boolean cureLeather(int i) {
  102.         if(tanningTime[i] < 3) {
  103.             setBlockToUpdate();
  104.         }
  105.         tanningTime[i] += (getSaltTimer()?2:1);
  106.         return tanningTime[i] >= 2400;
  107.     }
  108.  
  109.     private boolean getSaltTimer() {
  110.         if(saltTime>0) {
  111.             return true;
  112.         }
  113.         else if(saltSlot.getStackInSlot(0) != null) {
  114.             saltSlot.extractItem(0, 1, false);
  115.             saltTime = 4800 - 1;//time enough to cure 8 leather (optimal)
  116.             setBlockToUpdate();
  117.             return true;
  118.         }
  119.         return false;
  120.     }
  121.  
  122.     @Override
  123.     public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  124.         return this.getCapability(capability, facing) != null;
  125.     }
  126.  
  127.     @Override
  128.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  129.         IBlockState bs = worldObj.getBlockState(pos);
  130.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  131.             FarmingBase.logger.log(Level.INFO, "" + saltSlot.getStackInSlot(0));
  132.             if(saltSlot.getStackInSlot(0) != null)
  133.                 FarmingBase.logger.log(Level.INFO, "" + saltSlot.getStackInSlot(0).stackSize);
  134.             setBlockToUpdate();
  135.             if(bs.getBlock() != getBlockType()) {//if the block at myself isn't myself, allow full access (Block Broken)
  136.                 return (T) new CombinedInvWrapper(leftSlot, rightSlot, saltSlot);
  137.             }
  138.             if(facing == null) {
  139.                 return (T) new CombinedInvWrapper(leftSlot, rightSlot, saltSlot);
  140.             }
  141.             if(facing == EnumFacing.UP) {
  142.                 return (T) saltSlot;
  143.             }
  144.             if(facing == EnumFacing.DOWN) {
  145.                 return (T) getOuputSlots();
  146.             }
  147.             EnumFacing bface = bs.getValue(BlockHorizontal.FACING);
  148.             if(bface == facing) {
  149.                 return (T) leftSlot;
  150.             }
  151.             if(bface.getOpposite() == facing) {
  152.                 return (T) rightSlot;
  153.             }
  154.         }
  155.         return super.getCapability(capability, facing);
  156.     }
  157.  
  158.  
  159.     private void setBlockToUpdate() {
  160.         shouldUpdate = true;
  161.     }
  162.  
  163.     private CombinedInvWrapper getOuputSlots() {
  164.         ArrayList<ItemStackHandler> allSlots = new ArrayList();
  165.         if(leftSlot.getStackInSlot(0) != null && leftSlot.getStackInSlot(0).getItem() == Items.LEATHER) {
  166.             allSlots.add(leftSlot);
  167.         }
  168.         if(rightSlot.getStackInSlot(0) != null && rightSlot.getStackInSlot(0).getItem() == Items.LEATHER) {
  169.             allSlots.add(rightSlot);
  170.         }
  171.         return new CombinedInvWrapper(allSlots.toArray(new ItemStackHandler[0]));
  172.     }
  173.  
  174.     @Override
  175.     @Nullable
  176.     public SPacketUpdateTileEntity getUpdatePacket() {
  177.         return new SPacketUpdateTileEntity(this.pos, 3, this.getUpdateTag());
  178.     }
  179.  
  180.     @Override
  181.     public NBTTagCompound getUpdateTag() {
  182.         return this.writeToNBT(new NBTTagCompound());
  183.     }
  184.    
  185.     @Override
  186.     public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
  187.         super.onDataPacket(net, pkt);
  188.         handleUpdateTag(pkt.getNbtCompound());
  189.     }
  190.    
  191.     @Override
  192.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  193.         super.writeToNBT(compound);
  194.         compound.setTag("harderfarming:leftSlot", leftSlot.serializeNBT());
  195.         compound.setTag("harderfarming:rightSlot", rightSlot.serializeNBT());
  196.         compound.setTag("harderfarming:saltSlot", saltSlot.serializeNBT());
  197.         compound.setIntArray("harderfarming:tanningTime", tanningTime);
  198.         compound.setInteger("harderfarming:saltTime", saltTime);
  199.         return compound;
  200.     }
  201.    
  202.     @Override
  203.     public void readFromNBT(NBTTagCompound compound) {
  204.         super.readFromNBT(compound);
  205.         if(leftSlot == null) {
  206.             leftSlot = new ItemLeatherHandler(1);
  207.             rightSlot = new ItemLeatherHandler(1);
  208.             saltSlot = new ItemSaltHandler(1);
  209.         }
  210.         if(compound.hasKey("harderfarming:leftSlot")) {
  211.             leftSlot.deserializeNBT((NBTTagCompound) compound.getTag("harderfarming:leftSlot"));
  212.         }
  213.         if(compound.hasKey("harderfarming:rightSlot")) {
  214.             rightSlot.deserializeNBT((NBTTagCompound) compound.getTag("harderfarming:rightSlot"));
  215.         }
  216.         if(compound.hasKey("harderfarming:saltSlot")) {
  217.             saltSlot.deserializeNBT((NBTTagCompound) compound.getTag("harderfarming:saltSlot"));
  218.         }
  219.         tanningTime = compound.getIntArray("harderfarming:tanningTime");
  220.         if(tanningTime.length < 2) {
  221.             tanningTime = new int[]{0,0};
  222.         }
  223.         saltTime = compound.getInteger("harderfarming:saltTime");
  224.     }
  225.  
  226.     public float getTime() {
  227.         return 0;
  228.         //return tanningTime;
  229.     }
  230.    
  231.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
  232.         return oldState.getBlock() != newState.getBlock();
  233.     }
  234.  
  235.     public int getSalt() {
  236.         if(saltSlot.getStackInSlot(0) == null) {
  237.             return (saltTime > 0)?1:0;
  238.         }
  239.         int size = saltSlot.getStackInSlot(0).stackSize;
  240.         if(size <= 2) return 2;
  241.         if(size <= 16) return 3;
  242.         if(size <= 32) return 4;
  243.         if(size <= 48) return 5;
  244.         return 6;
  245.     }
  246.  
  247.     public int getLeather(int slot) {
  248.         //FarmingBase.logger.log(Level.INFO, "Slot " + slot);
  249.         if(slot == 0) {
  250.             //FarmingBase.logger.log(Level.INFO, "      " + leftSlot.getStackInSlot(0));
  251.             //return leftSlot.getStackInSlot(0) != null;
  252.             if(leftSlot.getStackInSlot(0) == null) return 0;
  253.             if(leftSlot.getStackInSlot(0).getItem() == FarmingBase.rawLeather) return 1;
  254.             if(leftSlot.getStackInSlot(0).getItem() == Items.LEATHER) return 2;
  255.         }
  256.         if(slot == 1) {
  257.             //FarmingBase.logger.log(Level.INFO, "      " + rightSlot.getStackInSlot(0));
  258.             if(rightSlot.getStackInSlot(0) == null) return 0;
  259.             if(rightSlot.getStackInSlot(0).getItem() == FarmingBase.rawLeather) return 1;
  260.             if(rightSlot.getStackInSlot(0).getItem() == Items.LEATHER) return 2;
  261.         }
  262.         return 0;
  263.     }
  264. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×