Guest User

TileEntityCorpse

a guest
Oct 18th, 2023
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. public class TileEntityCorpse extends TileEntityLockableLoot implements ITickable {
  2.     private NonNullList<ItemStack> chestContents = NonNullList.<ItemStack>withSize(45, ItemStack.EMPTY);
  3.     public int numPlayersUsing, ticksSinceSync;
  4.     public float lidAngle, prevLidAngle;
  5.  
  6.  
  7.     @Override
  8.     public int getSizeInventory()
  9.     {
  10.         return 45;
  11.     }
  12.  
  13.     @Override
  14.     public int getInventoryStackLimit()
  15.     {
  16.         return 64;
  17.     }
  18.  
  19.     @Override
  20.     public boolean isEmpty()
  21.     {
  22.         for(ItemStack stack : this.chestContents)
  23.         {
  24.             if(!stack.isEmpty()) return false;
  25.         }
  26.  
  27.         return true;
  28.     }
  29.  
  30.     @Override
  31.     public String getName()
  32.     {
  33.         return this.hasCustomName() ? this.customName : "container.corpse";
  34.     }
  35.    
  36.  
  37.     public void addItem(ItemStack item) {
  38.         this.chestContents.add(item);
  39.     }
  40.  
  41.     @Override
  42.     public void readFromNBT(NBTTagCompound compound)
  43.     {
  44.         super.readFromNBT(compound);
  45.         this.chestContents = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  46.  
  47.         if(!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, chestContents);
  48.         if(compound.hasKey("CustomName", 8)) this.customName = compound.getString("CustomName");
  49.     }
  50.  
  51.     @Override
  52.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  53.     {
  54.         super.writeToNBT(compound);
  55.  
  56.         if(!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, chestContents);
  57.         if(compound.hasKey("CustomName", 8)) compound.setString("CustomName", this.customName);
  58.  
  59.         return compound;
  60.     }
  61.  
  62.     @Override
  63.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  64.     {
  65.         return new ContainerCorpse(playerInventory, this, playerIn);
  66.     }
  67.  
  68.     @Override
  69.     public String getGuiID()
  70.     {
  71.         return MersaMod.MOD_ID + ":corpse";
  72.     }
  73.  
  74.     @Override
  75.     protected NonNullList<ItemStack> getItems()
  76.     {
  77.         return this.chestContents;
  78.     }
  79.  
  80.     @Override
  81.     public void update()
  82.     {
  83.         if (!this.world.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + pos.getX() + pos.getY() + pos.getZ()) % 200 == 0)
  84.         {
  85.             this.numPlayersUsing = 0;
  86.             float f = 5.0F;
  87.  
  88.             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))))
  89.             {
  90.                 if (entityplayer.openContainer instanceof ContainerCorpse)
  91.                 {
  92.                     if (((ContainerCorpse)entityplayer.openContainer).getChestInventory() == this)
  93.                     {
  94.                         ++this.numPlayersUsing;
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.  
  100.         this.prevLidAngle = this.lidAngle;
  101.         float f1 = 0.1F;
  102.  
  103.         if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F)
  104.         {
  105.             double d1 = (double)pos.getX() + 0.5D;
  106.             double d2 = (double)pos.getZ() + 0.5D;
  107.             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);
  108.         }
  109.  
  110.         if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
  111.         {
  112.             float f2 = this.lidAngle;
  113.  
  114.             if (this.numPlayersUsing > 0)
  115.             {
  116.                 this.lidAngle += 0.1F;
  117.             }
  118.             else
  119.             {
  120.                 this.lidAngle -= 0.1F;
  121.             }
  122.  
  123.             if (this.lidAngle > 1.0F)
  124.             {
  125.                 this.lidAngle = 1.0F;
  126.             }
  127.  
  128.             float f3 = 0.5F;
  129.  
  130.             if (this.lidAngle < 0.5F && f2 >= 0.5F)
  131.             {
  132.                 double d3 = (double)pos.getX() + 0.5D;
  133.                 double d0 = (double)pos.getZ() + 0.5D;
  134.                 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);
  135.             }
  136.  
  137.             if (this.lidAngle < 0.0F)
  138.             {
  139.                 this.lidAngle = 0.0F;
  140.             }
  141.         }
  142.     }
  143.  
  144.     @Override
  145.     public void openInventory(EntityPlayer player)
  146.     {
  147.         ++this.numPlayersUsing;
  148.         this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
  149.         this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
  150.     }
  151.  
  152.     @Override
  153.     public void closeInventory(EntityPlayer player)
  154.     {
  155.         --this.numPlayersUsing;
  156.         this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
  157.         this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment