SHARE
TWEET

TileEntityChickenChest.java

FlowArg Sep 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fr.flowarg.gunsofchickens.blocks.tileentities;
  2.  
  3. import fr.flowarg.gunsofchickens.blocks.containers.ContainerChickenChest;
  4. import fr.flowarg.gunsofchickens.utils.References;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.player.InventoryPlayer;
  7. import net.minecraft.init.SoundEvents;
  8. import net.minecraft.inventory.Container;
  9. import net.minecraft.inventory.ItemStackHelper;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.tileentity.TileEntityLockableLoot;
  13. import net.minecraft.util.ITickable;
  14. import net.minecraft.util.NonNullList;
  15. import net.minecraft.util.SoundCategory;
  16. import net.minecraft.util.math.AxisAlignedBB;
  17.  
  18. public class TileEntityChickenChest extends TileEntityLockableLoot implements ITickable
  19. {
  20.     private NonNullList<ItemStack> chestContents = NonNullList.<ItemStack>withSize(72, ItemStack.EMPTY);
  21.     public int numPlayersUsing, ticksSinceSync;
  22.     public float lidAngle, prevLidAngle;
  23.  
  24.     @Override
  25.     public int getSizeInventory()
  26.     {
  27.         return 72;
  28.     }
  29.  
  30.     @Override
  31.     public int getInventoryStackLimit()
  32.     {
  33.         return 64;
  34.     }
  35.  
  36.     @Override
  37.     public boolean isEmpty()
  38.     {
  39.        for (ItemStack itemStack : this.chestContents)
  40.        {
  41.            if (!itemStack.isEmpty()) return false;
  42.        }
  43.        return true;
  44.     }
  45.  
  46.     @Override
  47.     public String getName()
  48.     {
  49.         return this.hasCustomName() ? this.customName : "container.chicken_chest";
  50.     }
  51.  
  52.     @Override
  53.     public void readFromNBT(NBTTagCompound compound)
  54.     {
  55.         super.readFromNBT(compound);
  56.         this.chestContents = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  57.  
  58.         if (!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, chestContents);
  59.         if (compound.hasKey("CustomChickenChestName", 8)) this.customName = compound.getString("CustomChickenChestName");
  60.     }
  61.  
  62.     @Override
  63.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  64.     {
  65.         super.writeToNBT(compound);
  66.  
  67.         if (!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, chestContents);
  68.         if (compound.hasKey("CustomChickenChestName", 8)) compound.setString("CustomChickenChestName", this.customName);
  69.  
  70.         return compound;
  71.     }
  72.  
  73.     @Override
  74.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  75.     {
  76.         return new ContainerChickenChest(playerInventory, this);
  77.     }
  78.  
  79.     @Override
  80.     public String getGuiID()
  81.     {
  82.         return References.MODID + ":chicken_chest";
  83.     }
  84.  
  85.     @Override
  86.     protected NonNullList<ItemStack> getItems()
  87.     {
  88.         return this.chestContents;
  89.     }
  90.  
  91.     @Override
  92.     public void update()
  93.     {
  94.         if (!this.world.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + pos.getX() + pos.getY() + pos.getZ()) % 20 == 0)
  95.         {
  96.             this.numPlayersUsing = 0;
  97.             float f = 5.0f;
  98.  
  99.             for (EntityPlayer entityPlayer : this.world.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB((double) ((float) pos.getX() - 5.0f), (double) ((float) pos.getY() - 5.0f), (double) ((float) pos.getZ() - 5.0f), (double) ((float) (pos.getX() + 1) + 5.0F), (double) ((float) (pos.getY() + 1) + 5.0F), (double) ((float) (pos.getZ() + 1) + 5.0F))))
  100.             {
  101.                 if (entityPlayer.openContainer instanceof ContainerChickenChest)
  102.                 {
  103.                     if (((ContainerChickenChest) entityPlayer.openContainer).getChestInventory() == this)
  104.                     {
  105.                         ++this.numPlayersUsing;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.  
  111.         this.prevLidAngle = this.lidAngle;
  112.         float f1 = 0.1F;
  113.  
  114.         if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F)
  115.         {
  116.             double d1 = (double) pos.getX() + 0.5D;
  117.             double d2 = (double) pos.getZ() + 0.5D;
  118.             this.world.playSound((EntityPlayer) null, d1, (double) pos.getY() + 0.5D, d2, SoundEvents.BLOCK_IRON_TRAPDOOR_OPEN, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
  119.         }
  120.  
  121.         if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
  122.         {
  123.             float f2 = this.lidAngle;
  124.  
  125.             if (this.numPlayersUsing > 0)
  126.             {
  127.                 this.lidAngle += 0.1F;
  128.             } else {
  129.                 this.lidAngle -= 0.1F;
  130.             }
  131.  
  132.             if (this.lidAngle > 1.0F)
  133.             {
  134.                 this.lidAngle = 1.0F;
  135.             }
  136.  
  137.             float f3 = 0.5F;
  138.  
  139.             if (this.lidAngle < 0.5F && f2 >= 0.5F)
  140.             {
  141.                 double d3 = (double) pos.getX() + 0.5D;
  142.                 double d0 = (double) pos.getZ() + 0.5D;
  143.                 this.world.playSound((EntityPlayer) null, d3, (double) pos.getY() + 0.5D, d0, SoundEvents.BLOCK_IRON_TRAPDOOR_CLOSE, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
  144.             }
  145.  
  146.             if (this.lidAngle < 0.0f)
  147.             {
  148.                 this.lidAngle = 0.0f;
  149.             }
  150.         }
  151.     }
  152.  
  153.     @Override
  154.     public void openInventory(EntityPlayer player)
  155.     {
  156.         ++this.numPlayersUsing;
  157.         this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
  158.         this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
  159.     }
  160.  
  161.     @Override
  162.     public void closeInventory(EntityPlayer player)
  163.     {
  164.         --this.numPlayersUsing;
  165.         this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
  166.         this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
  167.     }
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top