Advertisement
PSquishyP

StonesChestTileEntity

Nov 3rd, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. /*
  2. *Error reported in console on TE placement:
  3. *[FML]: A TileEntity type noahc3.abilitystones.tileentity.StonesChestTileEntity has throw an exception trying to write state. It *will not persist. Report this to the mod author
  4. *java.lang.RuntimeException: class noahc3.abilitystones.tileentity.StonesChestTileEntity is missing a mapping! This is a bug!
  5. *This is the mentioned class
  6. */
  7.  
  8. package noahc3.abilitystones.tileentity;
  9.  
  10. import net.minecraft.block.state.IBlockState;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.inventory.IInventory;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.nbt.NBTTagList;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraft.util.BlockPos;
  18. import net.minecraft.util.ChatComponentText;
  19. import net.minecraft.util.ChatComponentTranslation;
  20. import net.minecraft.util.EnumFacing;
  21. import net.minecraft.util.IChatComponent;
  22. import net.minecraft.world.World;
  23. import noahc3.abilitystones.AbilityStones;
  24. import noahc3.abilitystones.gui.ASGuiHandler;
  25.  
  26. public class StonesChestTileEntity extends TileEntity implements IInventory
  27. {
  28.  
  29.     private ItemStack[] inventory;
  30.     private String customName;
  31.    
  32.     public StonesChestTileEntity() {
  33.         this.inventory = new ItemStack[this.getSizeInventory()];
  34.     }
  35.    
  36.     public String getCustomName() {
  37.         return this.customName;
  38.     }
  39.    
  40.     public void setCustomName(String customName) {
  41.         this.customName = customName;
  42.     }
  43.    
  44.     @Override
  45.     public String getName() {
  46.         return this.hasCustomName() ? this.customName : "container.stones_chest_tile_entity";
  47.     }
  48.  
  49.     @Override
  50.     public boolean hasCustomName() {
  51.         return this.customName != null && !this.customName.equals("");
  52.     }
  53.  
  54.     @Override
  55.     public IChatComponent getDisplayName() {
  56.         return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
  57.     }
  58.    
  59.     @Override
  60.     public int getSizeInventory() {
  61.         return 9;
  62.     }
  63.    
  64.     @Override
  65.     public ItemStack getStackInSlot(int index) {
  66.         if (index < 0 || index >= this.getSizeInventory())
  67.             return null;
  68.         return this.inventory[index];
  69.     }
  70.  
  71.     @Override
  72.     public ItemStack decrStackSize(int index, int count) {
  73.         if (this.getStackInSlot(index) != null) {
  74.             ItemStack itemstack;
  75.  
  76.             if (this.getStackInSlot(index).stackSize <= count) {
  77.                 itemstack = this.getStackInSlot(index);
  78.                 this.setInventorySlotContents(index, null);
  79.                 this.markDirty();
  80.                 return itemstack;
  81.             } else {
  82.                 itemstack = this.getStackInSlot(index).splitStack(count);
  83.  
  84.                 if (this.getStackInSlot(index).stackSize <= 0) {
  85.                     this.setInventorySlotContents(index, null);
  86.                 } else {
  87.                     //Just to show that changes happened
  88.                     this.setInventorySlotContents(index, this.getStackInSlot(index));
  89.                 }
  90.  
  91.                 this.markDirty();
  92.                 return itemstack;
  93.             }
  94.         } else {
  95.             return null;
  96.         }
  97.     }
  98.  
  99.     @Override
  100.     public ItemStack getStackInSlotOnClosing(int index) {
  101.         ItemStack stack = this.getStackInSlot(index);
  102.         this.setInventorySlotContents(index, null);
  103.         return stack;
  104.     }
  105.  
  106.     @Override
  107.     public void setInventorySlotContents(int index, ItemStack stack) {
  108.         if (index < 0 || index >= this.getSizeInventory())
  109.             return;
  110.  
  111.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  112.             stack.stackSize = this.getInventoryStackLimit();
  113.            
  114.         if (stack != null && stack.stackSize == 0)
  115.             stack = null;
  116.  
  117.         this.inventory[index] = stack;
  118.         this.markDirty();
  119.     }
  120.    
  121.     @Override
  122.     public int getInventoryStackLimit() {
  123.         return 64;
  124.     }
  125.    
  126.     @Override
  127.     public boolean isUseableByPlayer(EntityPlayer player) {
  128.         return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
  129.     }
  130.    
  131.     @Override
  132.     public void openInventory(EntityPlayer player) {
  133.     }
  134.  
  135.     @Override
  136.     public void closeInventory(EntityPlayer player) {
  137.     }
  138.    
  139.     @Override
  140.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  141.         return true;
  142.     }
  143.    
  144.     @Override
  145.     public int getField(int id) {
  146.         return 0;
  147.     }
  148.  
  149.     @Override
  150.     public void setField(int id, int value) {
  151.     }
  152.  
  153.     @Override
  154.     public int getFieldCount() {
  155.         return 0;
  156.     }
  157.    
  158.     @Override
  159.     public void clear() {
  160.         for (int i = 0; i < this.getSizeInventory(); i++)
  161.             this.setInventorySlotContents(i, null);
  162.     }
  163.    
  164.     @Override
  165.     public void writeToNBT(NBTTagCompound nbt) {
  166.         super.writeToNBT(nbt);
  167.  
  168.         NBTTagList list = new NBTTagList();
  169.         for (int i = 0; i < this.getSizeInventory(); ++i) {
  170.             if (this.getStackInSlot(i) != null) {
  171.                 NBTTagCompound stackTag = new NBTTagCompound();
  172.                 stackTag.setByte("Slot", (byte) i);
  173.                 this.getStackInSlot(i).writeToNBT(stackTag);
  174.                 list.appendTag(stackTag);
  175.             }
  176.         }
  177.         nbt.setTag("Items", list);
  178.  
  179.         if (this.hasCustomName()) {
  180.             nbt.setString("CustomName", this.getCustomName());
  181.         }
  182.     }
  183.  
  184.  
  185.     @Override
  186.     public void readFromNBT(NBTTagCompound nbt) {
  187.         super.readFromNBT(nbt);
  188.  
  189.         NBTTagList list = nbt.getTagList("Items", 10);
  190.         for (int i = 0; i < list.tagCount(); ++i) {
  191.             NBTTagCompound stackTag = list.getCompoundTagAt(i);
  192.             int slot = stackTag.getByte("Slot") & 255;
  193.             this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
  194.         }
  195.  
  196.         if (nbt.hasKey("CustomName", 8)) {
  197.             this.setCustomName(nbt.getString("CustomName"));
  198.         }
  199.     }
  200.    
  201.    
  202.    
  203.    
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement