Yurim64

Uncolossal Chest TileEntity

May 3rd, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.30 KB | None | 0 0
  1. package com.ike.tenchest.chest.uncolossalchest;
  2.  
  3. import com.ike.tenchest.Blocks;
  4. import net.minecraft.block.BlockState;
  5. import net.minecraft.entity.player.PlayerEntity;
  6. import net.minecraft.inventory.IInventory;
  7. import net.minecraft.inventory.ItemStackHelper;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.CompoundNBT;
  10. import net.minecraft.tileentity.IChestLid;
  11. import net.minecraft.tileentity.ITickableTileEntity;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.tileentity.TileEntityType;
  14. import net.minecraft.util.NonNullList;
  15. import net.minecraft.util.SoundCategory;
  16. import net.minecraft.util.SoundEvents;
  17. import net.minecraft.util.math.MathHelper;
  18. import net.minecraftforge.api.distmarker.Dist;
  19. import net.minecraftforge.api.distmarker.OnlyIn;
  20.  
  21. /**
  22.  * @author Ike
  23.  * @version 1.0A
  24.  **/
  25. public class UncolossalChestTileEntity extends TileEntity implements IInventory, IChestLid, ITickableTileEntity {
  26.     public float openness;
  27.     public float oOpenness;
  28.     public int openCount;
  29.     private int tickInterval;
  30.  
  31.     public UncolossalChestTileEntity() {
  32.         super(TileEntityType.ENDER_CHEST);
  33.     }
  34.  
  35.     public void tick() {
  36.         if (++this.tickInterval % 20 * 4 == 0) {
  37.             this.level.blockEvent(this.worldPosition, Blocks.uncolossalChest, 1, this.openCount);
  38.         }
  39.  
  40.         this.oOpenness = this.openness;
  41.         int i = this.worldPosition.getX();
  42.         int j = this.worldPosition.getY();
  43.         int k = this.worldPosition.getZ();
  44.         float f = 0.1F;
  45.         if (this.openCount > 0 && this.openness == 0.0F) {
  46.             double d0 = (double) i + 0.5D;
  47.             double d1 = (double) k + 0.5D;
  48.             this.level.playSound((PlayerEntity) null, d0, (double) j + 0.5D, d1, SoundEvents.ENDER_CHEST_OPEN, SoundCategory.BLOCKS, 0.5F, this.level.random.nextFloat() * 0.1F + 0.9F);
  49.         }
  50.  
  51.         if (this.openCount == 0 && this.openness > 0.0F || this.openCount > 0 && this.openness < 1.0F) {
  52.             float f2 = this.openness;
  53.             if (this.openCount > 0) {
  54.                 this.openness += 0.1F;
  55.             } else {
  56.                 this.openness -= 0.1F;
  57.             }
  58.  
  59.             if (this.openness > 1.0F) {
  60.                 this.openness = 1.0F;
  61.             }
  62.  
  63.             float f1 = 0.5F;
  64.             if (this.openness < 0.5F && f2 >= 0.5F) {
  65.                 double d3 = (double) i + 0.5D;
  66.                 double d2 = (double) k + 0.5D;
  67.                 this.level.playSound((PlayerEntity) null, d3, (double) j + 0.5D, d2, SoundEvents.ENDER_CHEST_CLOSE, SoundCategory.BLOCKS, 0.5F, this.level.random.nextFloat() * 0.1F + 0.9F);
  68.             }
  69.  
  70.             if (this.openness < 0.0F) {
  71.                 this.openness = 0.0F;
  72.             }
  73.         }
  74.  
  75.     }
  76.  
  77.     public boolean triggerEvent(int p_145842_1_, int p_145842_2_) {
  78.         if (p_145842_1_ == 1) {
  79.             this.openCount = p_145842_2_;
  80.             return true;
  81.         } else {
  82.             return super.triggerEvent(p_145842_1_, p_145842_2_);
  83.         }
  84.     }
  85.  
  86.     public void setRemoved() {
  87.         this.clearCache();
  88.         super.setRemoved();
  89.     }
  90.  
  91.     public void startOpen() {
  92.         ++this.openCount;
  93.         this.level.blockEvent(this.worldPosition, Blocks.uncolossalChest, 1, this.openCount);
  94.     }
  95.  
  96.     public void stopOpen() {
  97.         --this.openCount;
  98.         this.level.blockEvent(this.worldPosition, Blocks.uncolossalChest, 1, this.openCount);
  99.     }
  100.  
  101.     private NonNullList<ItemStack> inventory = NonNullList.withSize(27, ItemStack.EMPTY);
  102.  
  103.     @Override
  104.     public CompoundNBT save(CompoundNBT nbt) {
  105.         super.save(nbt);
  106.         ItemStackHelper.saveAllItems(nbt, this.inventory);
  107.         return nbt;
  108.     }
  109.  
  110.     @Override
  111.     public void load(BlockState state, CompoundNBT nbt) {
  112.         super.load(state, nbt);
  113.         this.inventory = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
  114.         ItemStackHelper.loadAllItems(nbt, this.inventory);
  115.     }
  116.  
  117.     @Override
  118.     public int getContainerSize() {
  119.         return 27;
  120.     }
  121.  
  122.     @Override
  123.     public boolean isEmpty() {
  124.         return inventory.isEmpty();
  125.     }
  126.  
  127.     @Override
  128.     public ItemStack getItem(int index) {
  129.         return inventory.get(index);
  130.     }
  131.  
  132.     @Override
  133.     public ItemStack removeItem(int index, int quantity) {
  134.         ItemStack itemStack = inventory.get(index);
  135.         if (itemStack.getCount() <= quantity) {
  136.             inventory.set(index, ItemStack.EMPTY);
  137.             return itemStack;
  138.         } else {
  139.             int count = itemStack.getCount() - quantity;
  140.             itemStack.setCount(count);
  141.             inventory.set(index, itemStack);
  142.             return new ItemStack(itemStack.getItem(), count);
  143.         }
  144.     }
  145.  
  146.     @Override
  147.     public ItemStack removeItemNoUpdate(int index) {
  148.         return inventory.remove(index);
  149.     }
  150.  
  151.     @Override
  152.     public void setItem(int index, ItemStack stack) {
  153.         this.inventory.set(index, stack);
  154.     }
  155.  
  156.     @Override
  157.     public boolean stillValid(PlayerEntity p_70300_1_) {
  158.         return true;
  159.     }
  160.  
  161.     @Override
  162.     public void clearContent() {
  163.         this.inventory.clear();
  164.     }
  165.  
  166.     @OnlyIn(Dist.CLIENT)
  167.     public float getOpenNess(float p_195480_1_) {
  168.         return MathHelper.lerp(p_195480_1_, this.oOpenness, this.openness);
  169.     }
  170. }
  171.  
Add Comment
Please, Sign In to add comment