Guest User

wirelesschestentity

a guest
May 4th, 2015
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.68 KB | None | 0 0
  1. package com.cclloyd.ccmodpack;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.inventory.IInventory;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.nbt.NBTTagList;
  10. import net.minecraft.tileentity.TileEntity;
  11. import net.minecraft.util.ChatComponentText;
  12. import net.minecraft.util.ChatComponentTranslation;
  13. import net.minecraft.util.IChatComponent;
  14.  
  15. public class WirelessChestEntity extends TileEntity implements IInventory {
  16.    
  17.     /**
  18.      * This is a simple tile entity implementing IInventory that can store 9 item stacks
  19.      */
  20.    
  21.     // Create and initialize the items variable that will store store the items
  22.     final int NUMBER_OF_SLOTS = 27;
  23.     private ItemStack[] itemStacks = new ItemStack[NUMBER_OF_SLOTS];
  24.     private int frequency = 0;
  25.     public final static String name = "wirelessChestEntity";
  26.  
  27.     public WirelessChestEntity() {
  28.         this.frequency = 0;
  29.     }
  30.    
  31.     public int getFrequency() {
  32.         return frequency;
  33.     }
  34.    
  35.     public String getFrequencyString() {
  36.         return Integer.toString(frequency);
  37.     }
  38.    
  39.     public void setFrequency(int freq) {
  40.         this.frequency = freq;
  41.         markDirty();
  42.     }
  43.    
  44.     /* The following are some IInventory methods you are required to override */
  45.  
  46.     // Gets the number of slots in the inventory
  47.     @Override
  48.     public int getSizeInventory() {
  49.         return itemStacks.length;
  50.     }
  51.  
  52.     // Gets the stack in the given slot
  53.     @Override
  54.     public ItemStack getStackInSlot(int slotIndex) {
  55.         return itemStacks[slotIndex];
  56.     }
  57.  
  58.     /**
  59.      * Removes some of the units from itemstack in the given slot, and returns as a separate itemstack
  60.      * @param slotIndex the slot number to remove the items from
  61.      * @param count the number of units to remove
  62.      * @return a new itemstack containing the units removed from the slot
  63.      */
  64.     @Override
  65.     public ItemStack decrStackSize(int slotIndex, int count) {
  66.         ItemStack itemStackInSlot = getStackInSlot(slotIndex);
  67.         if (itemStackInSlot == null) return null;
  68.  
  69.         ItemStack itemStackRemoved;
  70.         if (itemStackInSlot.stackSize <= count) {
  71.             itemStackRemoved = itemStackInSlot;
  72.             setInventorySlotContents(slotIndex, null);
  73.         } else {
  74.             itemStackRemoved = itemStackInSlot.splitStack(count);
  75.             if (itemStackInSlot.stackSize == 0) {
  76.                 setInventorySlotContents(slotIndex, null);
  77.             }
  78.         }
  79.       markDirty();
  80.         return itemStackRemoved;
  81.     }
  82.  
  83.     // overwrites the stack in the given slotIndex with the given stack
  84.     @Override
  85.     public void setInventorySlotContents(int slotIndex, ItemStack itemstack) {
  86.         itemStacks[slotIndex] = itemstack;
  87.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
  88.             itemstack.stackSize = getInventoryStackLimit();
  89.         }
  90.         markDirty();
  91.     }
  92.  
  93.     // This is the maximum number if items allowed in each slot
  94.     // This only affects things such as hoppers trying to insert items you need to use the container to enforce this for players
  95.     // inserting items via the gui
  96.     @Override
  97.     public int getInventoryStackLimit() {
  98.         return 64;
  99.     }
  100.  
  101.     // Return true if the given player is able to use this block. In this case it checks that
  102.     // 1) the world tileentity hasn't been replaced in the meantime, and
  103.     // 2) the player isn't too far away from the centre of the block
  104.     @Override
  105.     public boolean isUseableByPlayer(EntityPlayer player) {
  106.         if (this.worldObj.getTileEntity(this.pos) != this) return false;
  107.         final double X_CENTRE_OFFSET = 0.5;
  108.         final double Y_CENTRE_OFFSET = 0.5;
  109.         final double Z_CENTRE_OFFSET = 0.5;
  110.         final double MAXIMUM_DISTANCE_SQ = 8.0 * 8.0;
  111.         return player.getDistanceSq(pos.getX() + X_CENTRE_OFFSET, pos.getY() + Y_CENTRE_OFFSET, pos.getZ() + Z_CENTRE_OFFSET) < MAXIMUM_DISTANCE_SQ;
  112.     }
  113.  
  114.     // Return true if the given stack is allowed to go in the given slot.  In this case, we can insert anything.
  115.     // This only affects things such as hoppers trying to insert items you need to use the container to enforce this for players
  116.     // inserting items via the gui
  117.     @Override
  118.     public boolean isItemValidForSlot(int slotIndex, ItemStack itemstack) {
  119.         return true;
  120.     }
  121.  
  122.     // This is where you save any data that you don't want to lose when the tile entity unloads
  123.     // In this case, it saves the itemstacks stored in the container
  124.     @Override
  125.     public void writeToNBT(NBTTagCompound parentNBTTagCompound)
  126.     {
  127.         super.writeToNBT(parentNBTTagCompound); // The super call is required to save and load the tileEntity's location
  128.  
  129.         // to use an analogy with Java, this code generates an array of hashmaps
  130.         // The itemStack in each slot is converted to an NBTTagCompound, which is effectively a hashmap of key->value pairs such
  131.         //   as slot=1, id=2353, count=1, etc
  132.         // Each of these NBTTagCompound are then inserted into NBTTagList, which is similar to an array.
  133.         parentNBTTagCompound.setInteger("Frequency", this.frequency);
  134.  
  135.         NBTTagList dataForAllSlots = new NBTTagList();
  136.         for (int i = 0; i < this.itemStacks.length; ++i) {
  137.             if (this.itemStacks[i] != null) {
  138.                 NBTTagCompound dataForThisSlot = new NBTTagCompound();
  139.                 dataForThisSlot.setByte("Slot", (byte) i);
  140.                 this.itemStacks[i].writeToNBT(dataForThisSlot);
  141.                 dataForAllSlots.appendTag(dataForThisSlot);
  142.             }
  143.         }
  144.         parentNBTTagCompound.setTag("Items", dataForAllSlots);
  145.  
  146.  
  147.     }
  148.  
  149.    
  150.     // This is where you load the data that you saved in writeToNBT
  151.     @Override
  152.     public void readFromNBT(NBTTagCompound parentNBTTagCompound)
  153.     {
  154.         super.readFromNBT(parentNBTTagCompound); // The super call is required to save and load the tiles location
  155.         final byte NBT_TYPE_COMPOUND = 10;       // See NBTBase.createNewByType() for a listing
  156.         NBTTagList dataForAllSlots = parentNBTTagCompound.getTagList("Items", NBT_TYPE_COMPOUND);
  157.  
  158.         this.frequency = parentNBTTagCompound.getInteger("Frequency");
  159.  
  160.         Arrays.fill(itemStacks, null);           // set all slots to empty
  161.         for (int i = 0; i < dataForAllSlots.tagCount(); ++i) {
  162.             NBTTagCompound dataForOneSlot = dataForAllSlots.getCompoundTagAt(i);
  163.             int slotIndex = dataForOneSlot.getByte("Slot") & 255;
  164.  
  165.             if (slotIndex >= 0 && slotIndex < this.itemStacks.length) {
  166.                 this.itemStacks[slotIndex] = ItemStack.loadItemStackFromNBT(dataForOneSlot);
  167.             }
  168.         }
  169.        
  170.        
  171.     }
  172.  
  173.     // set all slots to empty
  174.     @Override
  175.     public void clear() {
  176.         Arrays.fill(itemStacks, null);
  177.     }
  178.  
  179.     // will add a key for this container to the lang file so we can name it in the GUI
  180.     @Override
  181.     public String getName() {
  182.         return "container.wirelessChestEntity.name";
  183.     }
  184.  
  185.     @Override
  186.     public boolean hasCustomName() {
  187.         return false;
  188.     }
  189.  
  190.     // standard code to look up what the human-readable name is
  191.     @Override
  192.     public IChatComponent getDisplayName() {
  193.         return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
  194.     }
  195.  
  196.     // -----------------------------------------------------------------------------------------------------------
  197.     // The following methods are not needed for this example but are part of IInventory so they must be implemented
  198.  
  199.     /**
  200.      * This method removes the entire contents of the given slot and returns it.
  201.      * Used by containers such as crafting tables which return any items in their slots when you close the GUI
  202.      * @param slotIndex
  203.      * @return
  204.      */
  205.     @Override
  206.     public ItemStack getStackInSlotOnClosing(int slotIndex) {
  207.         ItemStack itemStack = getStackInSlot(slotIndex);
  208.         if (itemStack != null) setInventorySlotContents(slotIndex, null);
  209.         return itemStack;
  210.     }
  211.  
  212.     @Override
  213.     public void openInventory(EntityPlayer player) {}
  214.  
  215.     @Override
  216.     public void closeInventory(EntityPlayer player) {}
  217.  
  218.     @Override
  219.     public int getField(int id) {
  220.         return 0;
  221.     }
  222.  
  223.     @Override
  224.     public void setField(int id, int value) {}
  225.  
  226.     @Override
  227.     public int getFieldCount() {
  228.         return 0;
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment