Guest User

Tile Entity

a guest
Jun 3rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.73 KB | None | 0 0
  1. package com.monsterhunter.tileentity;
  2.  
  3. import com.monsterhunter.item.material.RecolorableItem;
  4.  
  5. import net.minecraft.block.BlockChest;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.player.InventoryPlayer;
  8. import net.minecraft.init.SoundEvents;
  9. import net.minecraft.inventory.Container;
  10. import net.minecraft.inventory.ContainerChest;
  11. import net.minecraft.inventory.IInventory;
  12. import net.minecraft.inventory.InventoryLargeChest;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.nbt.NBTTagList;
  16. import net.minecraft.tileentity.TileEntityLockableLoot;
  17. import net.minecraft.util.ITickable;
  18. import net.minecraft.util.SoundCategory;
  19. import net.minecraft.util.math.AxisAlignedBB;
  20. import net.minecraft.util.text.ITextComponent;
  21. import net.minecraft.util.text.TextComponentString;
  22. import net.minecraft.util.text.TextComponentTranslation;
  23.  
  24. public class MaterialChestTileEntity extends TileEntityLockableLoot implements ITickable, IInventory {
  25.     //private List<ItemStack> inventory;
  26.     private ItemStack[] inventory;
  27.     private final int SIZE = 27;
  28.     private final int MAX_STACK = 64;
  29.  
  30.     public float lidAngle;
  31.     public float prevLidAngle;
  32.     public int numPlayersUsing;
  33.     private int ticksSinceSync;
  34.  
  35.     private String customName;
  36.  
  37.     private static final String DEFAULT_NAME = "container.material_chest_tile_entity";
  38.  
  39.     public MaterialChestTileEntity() {
  40.         this.inventory = new ItemStack[SIZE];
  41.     }
  42.  
  43.     public String getCustomName() {
  44.         return this.customName;
  45.     }
  46.  
  47.     public void setCustomName(String customName) {
  48.         this.customName = customName;
  49.     }
  50.  
  51.     @Override
  52.     public String getName() {
  53.         return this.hasCustomName() ? this.customName : DEFAULT_NAME;
  54.     }
  55.  
  56.     @Override
  57.     public boolean hasCustomName() {
  58.         return this.customName != null && !this.customName.equals("");
  59.     }
  60.  
  61.     @Override
  62.     public ITextComponent getDisplayName() {
  63.         return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
  64.     }
  65.  
  66.     @Override
  67.     public int getSizeInventory() {
  68.         return SIZE;
  69.     }
  70.  
  71.     @Override
  72.     public ItemStack getStackInSlot(int index) {
  73.         if (index < 0 || index >= this.getSizeInventory())
  74.             return null;
  75.         return this.inventory[index];
  76.     }
  77.  
  78.     @Override
  79.     public ItemStack decrStackSize(int index, int count) {
  80.          if (this.getStackInSlot(index) != null) {
  81.                 ItemStack itemstack;
  82.  
  83.                 if (this.getStackInSlot(index).stackSize <= count) {
  84.                     itemstack = this.getStackInSlot(index);
  85.                     this.setInventorySlotContents(index, null);
  86.                     this.markDirty();
  87.                     return itemstack;
  88.                 } else {
  89.                     itemstack = this.getStackInSlot(index).splitStack(count);
  90.  
  91.                     if (this.getStackInSlot(index).stackSize <= 0) {
  92.                         this.setInventorySlotContents(index, null);
  93.                     } else {
  94.                         //Just to show that changes happened
  95.                         this.setInventorySlotContents(index, this.getStackInSlot(index));
  96.                     }
  97.  
  98.                     this.markDirty();
  99.                     return itemstack;
  100.                 }
  101.             } else {
  102.                 return null;
  103.             }
  104.     }
  105.  
  106.     @Override
  107.     public ItemStack removeStackFromSlot(int index) {
  108.         if(this.getStackInSlot(index) != null) {
  109.             ItemStack temp = this.getStackInSlot(index);
  110.             inventory[index] = null;
  111.             return temp;
  112.         } else {
  113.             return null;
  114.         }
  115.     }
  116.  
  117.     @Override
  118.     public void setInventorySlotContents(int index, ItemStack stack) {
  119.         if (index < 0 || index >= this.getSizeInventory())
  120.             return;
  121.  
  122.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  123.             stack.stackSize = this.getInventoryStackLimit();
  124.  
  125.         if (stack != null && stack.stackSize == 0)
  126.             stack = null;
  127.  
  128.         this.inventory[index] = stack;
  129.         this.markDirty();
  130.     }
  131.  
  132.     @Override
  133.     public int getInventoryStackLimit() {
  134.         return MAX_STACK;
  135.     }
  136.  
  137.     @Override
  138.     public boolean isUseableByPlayer(EntityPlayer player) {
  139.         return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
  140.     }
  141.  
  142.     public void openInventory(EntityPlayer player)
  143.     {
  144.         if (!player.isSpectator())
  145.         {
  146.             if (this.numPlayersUsing < 0)
  147.             {
  148.                 this.numPlayersUsing = 0;
  149.             }
  150.  
  151.             ++this.numPlayersUsing;
  152.             this.worldObj.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
  153.             this.worldObj.notifyNeighborsOfStateChange(this.pos, this.getBlockType());
  154.             this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), this.getBlockType());
  155.         }
  156.     }
  157.  
  158.     public void closeInventory(EntityPlayer player)
  159.     {
  160.         if (!player.isSpectator() && this.getBlockType() instanceof BlockChest)
  161.         {
  162.             --this.numPlayersUsing;
  163.             this.worldObj.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
  164.             this.worldObj.notifyNeighborsOfStateChange(this.pos, this.getBlockType());
  165.             this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), this.getBlockType());
  166.         }
  167.     }
  168.  
  169.     @Override
  170.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  171.         return stack.getItem() instanceof RecolorableItem;
  172.     }
  173.  
  174.     @Override
  175.     public int getField(int id) {
  176.         return 0;
  177.     }
  178.  
  179.     @Override
  180.     public void setField(int id, int value) {
  181.  
  182.     }
  183.  
  184.     @Override
  185.     public int getFieldCount() {
  186.         return 0;
  187.     }
  188.  
  189.     @Override
  190.     public void clear() {
  191.         for(int i = 0; i < this.getSizeInventory(); i++) {
  192.             this.setInventorySlotContents(i, null);
  193.         }
  194.     }
  195.  
  196.     @Override
  197.     public void writeToNBT(NBTTagCompound nbt) {
  198.         super.writeToNBT(nbt);
  199.  
  200.          NBTTagList list = new NBTTagList();
  201.         for (int i = 0; i < this.getSizeInventory(); ++i) {
  202.             if (this.getStackInSlot(i) != null) {
  203.                 NBTTagCompound stackTag = new NBTTagCompound();
  204.                 stackTag.setByte("Slot", (byte) i);
  205.                 this.getStackInSlot(i).writeToNBT(stackTag);
  206.                 list.appendTag(stackTag);
  207.             }
  208.         }
  209.         nbt.setTag("Items", list);
  210.  
  211.         if (this.hasCustomName()) {
  212.             nbt.setString("CustomName", this.getCustomName());
  213.         }
  214.     }
  215.  
  216.     @Override
  217.     public void readFromNBT(NBTTagCompound nbt) {
  218.         super.readFromNBT(nbt);
  219.  
  220.         NBTTagList list = nbt.getTagList("Items", 10);
  221.         for (int i = 0; i < list.tagCount(); ++i) {
  222.             NBTTagCompound stackTag = list.getCompoundTagAt(i);
  223.             int slot = stackTag.getByte("Slot") & 255;
  224.             this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
  225.         }
  226.  
  227.         if (nbt.hasKey("CustomName", 8)) {
  228.             this.setCustomName(nbt.getString("CustomName"));
  229.         }
  230.     }
  231.  
  232.     @Override
  233.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
  234.         this.fillWithLoot(playerIn);
  235.         return new ContainerChest(playerInventory, this, playerIn);
  236.     }
  237.  
  238.     @Override
  239.     public String getGuiID() {
  240.         return "minecraft:chest";
  241.     }
  242.  
  243.     @Override
  244.     public void update() {
  245.         int i = this.pos.getX();
  246.         int j = this.pos.getY();
  247.         int k = this.pos.getZ();
  248.  
  249.         ++ticksSinceSync;
  250.  
  251.         if (!this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + i + j + k) % 200 == 0) {
  252.             this.numPlayersUsing = 0;
  253.             float f = 5.0F;
  254.  
  255.             for (EntityPlayer entityplayer : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB((double)((float)i - f), (double)((float)j - f), (double)((float)k - f), (double)((float)(i + 1) + f), (double)((float)(j + 1) + f), (double)((float)(k + 1) + f))))
  256.             {
  257.                 if (entityplayer.openContainer instanceof ContainerChest)
  258.                 {
  259.                     IInventory iinventory = ((ContainerChest)entityplayer.openContainer).getLowerChestInventory();
  260.  
  261.                     if (iinventory == this || iinventory instanceof InventoryLargeChest && ((InventoryLargeChest)iinventory).isPartOfLargeChest(this))
  262.                     {
  263.                         ++this.numPlayersUsing;
  264.                     }
  265.                 }
  266.             }
  267.         }
  268.  
  269.         this.prevLidAngle = this.lidAngle;
  270.         float f1 = 0.1F;
  271.  
  272.         if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F )
  273.         {
  274.             double d1 = (double)i + 0.5D;
  275.             double d2 = (double)k + 0.5D;
  276.  
  277.             this.worldObj.playSound((EntityPlayer)null, d1, (double)j + 0.5D, d2, SoundEvents.block_chest_open, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
  278.         }
  279.  
  280.         if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
  281.         {
  282.             float f2 = this.lidAngle;
  283.  
  284.             if (this.numPlayersUsing > 0)
  285.             {
  286.                 this.lidAngle += f1;
  287.             }
  288.             else
  289.             {
  290.                 this.lidAngle -= f1;
  291.             }
  292.  
  293.             if (this.lidAngle > 1.0F)
  294.             {
  295.                 this.lidAngle = 1.0F;
  296.             }
  297.  
  298.             float f3 = 0.5F;
  299.  
  300.             if (this.lidAngle < f3 && f2 >= f3)
  301.             {
  302.                 double d3 = (double)i + 0.5D;
  303.                 double d0 = (double)k + 0.5D;
  304.  
  305.                 this.worldObj.playSound((EntityPlayer)null, d3, (double)j + 0.5D, d0, SoundEvents.block_chest_close, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
  306.             }
  307.  
  308.             if (this.lidAngle < 0.0F)
  309.             {
  310.                 this.lidAngle = 0.0F;
  311.             }
  312.         }
  313.  
  314.     }
  315.  
  316.     public boolean receiveClientEvent(int id, int type)
  317.     {
  318.         if (id == 1)
  319.         {
  320.             this.numPlayersUsing = type;
  321.             return true;
  322.         }
  323.         else
  324.         {
  325.             return super.receiveClientEvent(id, type);
  326.         }
  327.     }
  328.  
  329.  
  330. }
Add Comment
Please, Sign In to add comment