Advertisement
Atijaf

Energy Tile Entity

Jan 31st, 2016
28
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3.  * @author Fajita
  4.  * This takes care of the general-ness of Tile Entities that use my Energy System
  5.  */
  6. public class EnergyTE extends TileEntity implements IInventory{
  7.    
  8.     public ItemStack[] inventory;
  9.     private String customName;
  10.    
  11.     /** Normally the slot the capsule or EO Item goes into */
  12.     private int inventorySize = 1;
  13.    
  14.     /** energy used per use */
  15.     private int eupu;
  16.    
  17.     // Usually the energy that is stored in either the Energy Capsule or the EO Item
  18.     private int currentEnergy;
  19.    
  20.     public EnergyTE(int invSize) {
  21.         inventorySize = invSize;
  22.         inventory = new ItemStack[inventorySize];
  23.     }
  24.    
  25.     /** Sets Current Energy for te */
  26.     public final void setCurrentEnergy(int currentEnergy){
  27.         this.currentEnergy = currentEnergy;
  28.     }
  29.    
  30.     /** Gets Current Energy from TE*/
  31.     public final int getCurrentEnergy(){
  32.         return currentEnergy;
  33.     }
  34.    
  35.     /**
  36.      * @param eupu mean Energy Used per use
  37.      */
  38.     public final void setEUPU(int eupu){
  39.         this.eupu = eupu;
  40.     }
  41.    
  42.     /** Returns Energy Used per use */
  43.     public final int getEUPU(){
  44.         return eupu;
  45.     }
  46.    
  47.     /** Use Current energy from te */
  48.     public final void useEnergy(int energyUsed){
  49.         this.currentEnergy -= energyUsed;
  50.     }
  51.    
  52.     /**
  53.      * Use this when Energy Capsule is in inventory
  54.      * If no Energy Capsule is present, returns false
  55.      */
  56.     public final boolean saveEnergyCapsule(){
  57.         ItemStack stack = getStackInSlot(0);
  58.         if(stack != null && stack.getItem() instanceof EnergyItem){
  59.             EnergyItem.saveCurrentEnergy(stack, currentEnergy);
  60.         }
  61.         return false;
  62.     }
  63.    
  64.     /**
  65.      * Use this when Energy Capsule isn't in inventory
  66.      * @param stack: The stack that's saving
  67.      */
  68.     public final void saveEnergyCapsule(ItemStack stack){
  69.         if(stack != null && stack.getItem() instanceof EnergyItem){
  70.             EnergyItem.saveCurrentEnergy(stack, currentEnergy);
  71.         }
  72.     }
  73.    
  74.    
  75.     @Override
  76.     public int getSizeInventory() {
  77.         return inventorySize;
  78.     }
  79.    
  80.     @Override
  81.     public final String getCommandSenderName() {
  82.         // TODO Auto-generated method stub
  83.         return null;
  84.     }
  85.  
  86.     @Override
  87.     public final boolean hasCustomName() {
  88.         // TODO Auto-generated method stub
  89.         return (customName != null && !customName.equals(""));
  90.     }
  91.    
  92.     public final void setCustomName(String name){
  93.         customName = name;
  94.     }
  95.    
  96.     public final String getCustomName(){
  97.         return customName;
  98.     }
  99.  
  100.     @Override
  101.     public final IChatComponent getDisplayName() {
  102.         return this.hasCustomName() ? new ChatComponentText(this.getCustomName()) : new ChatComponentTranslation(this.getCustomName());
  103.     }
  104.    
  105.    
  106.     @Override
  107.     public final ItemStack getStackInSlot(int index) {
  108.         if(index >= 0 && index <= inventorySize){
  109.             return inventory[index];
  110.         }
  111.         return null;
  112.     }
  113.  
  114.     @Override
  115.     public final ItemStack decrStackSize(int index, int count) {
  116.         if (this.getStackInSlot(index) != null) {
  117.             ItemStack itemstack;
  118.  
  119.             if (this.getStackInSlot(index).stackSize <= count) {
  120.                 itemstack = this.getStackInSlot(index);
  121.                 this.setInventorySlotContents(index, null);
  122.                 this.markDirty();
  123.                 return itemstack;
  124.             } else {
  125.                 itemstack = this.getStackInSlot(index).splitStack(count);
  126.  
  127.                 if (this.getStackInSlot(index).stackSize <= 0) {
  128.                     this.setInventorySlotContents(index, null);
  129.                 } else {
  130.                     //Just to show that changes happened
  131.                     this.setInventorySlotContents(index, this.getStackInSlot(index));
  132.                 }
  133.  
  134.                 this.markDirty();
  135.                 return itemstack;
  136.             }
  137.         } else {
  138.             return null;
  139.         }
  140.     }
  141.  
  142.     @Override
  143.     public final ItemStack getStackInSlotOnClosing(int index) {
  144.         ItemStack stack = this.getStackInSlot(index);
  145.         this.setInventorySlotContents(index, null);
  146.         return stack;
  147.     }
  148.  
  149.     @Override
  150.     public void setInventorySlotContents(int index, ItemStack stack) {
  151.         if (index >= 0 && index <= getSizeInventory()){
  152.             if(stack != null){
  153.                 if(stack.stackSize > this.getInventoryStackLimit()){
  154.                     stack.stackSize = getInventoryStackLimit();
  155.                 }
  156.                 else if(stack.stackSize == 0){
  157.                     stack = null;
  158.                 }
  159.             }
  160.         }
  161.         else{
  162.             return;
  163.         }
  164.         inventory[index] = stack;
  165.         markDirty();
  166.     }
  167.  
  168.     @Override
  169.     public int getInventoryStackLimit() {
  170.         return 64;
  171.     }
  172.  
  173.     @Override
  174.     public final boolean isUseableByPlayer(EntityPlayer player) {
  175.         return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
  176.     }
  177.  
  178.     @Override
  179.     public void openInventory(EntityPlayer player) {}
  180.  
  181.     @Override
  182.     public void closeInventory(EntityPlayer player) {}
  183.    
  184.     /**
  185.      * @return True if index is 0 and if item being placed is of energyItem;
  186.      */
  187.     @Override
  188.     public boolean isItemValidForSlot(int index, ItemStack stack){
  189.         Item item = (stack != null) ? stack.getItem(): Items.beef;
  190.         return (index == 0 && item instanceof EnergyItem);
  191.     }
  192.    
  193.    
  194.     /**
  195.      * Keeps currentEnergy of the TE synced with client
  196.      * @param id 0: Current Energy
  197.      * @param id 1: eupu
  198.      */
  199.     @Override
  200.     public int getField(int id) {
  201.         switch(id){
  202.             case 0: return this.currentEnergy;
  203.             case 1: return (int) eupu;
  204.         }
  205.         return 0;
  206.     }
  207.  
  208.     /**
  209.      * Syncs CurrentEnergy and eupu from TE with client
  210.      * @param id 0: Current Energy
  211.      * @param id 1: eupu
  212.      */
  213.     @Override
  214.     public void setField(int id, int value) {
  215.         switch(id){
  216.             case 0: setCurrentEnergy(value); break;
  217.             case 1: setEUPU(value); break;
  218.         }
  219.     }
  220.    
  221.     /**
  222.      * call this and add to it for child classes that have more than the setField option
  223.      */
  224.     @Override
  225.     public int getFieldCount() {
  226.         return 2;
  227.     }
  228.  
  229.     /** Deletes inventory */
  230.     @Override
  231.     public final void clear() {
  232.         for(int i = 0; i < getSizeInventory(); i++){
  233.             inventory[i] = null;
  234.         }
  235.         markDirty();
  236.     }
  237.    
  238.     /**
  239.      * ONLY saves the inventory.  Be SURE to call super, and then save whatever else you need to
  240.      */
  241.     @Override
  242.     public void writeToNBT(NBTTagCompound compound) {
  243.         super.writeToNBT(compound);
  244.         NBTTagList items = new NBTTagList();
  245.         for(int i = 0; i < getSizeInventory(); i++){
  246.             if(getStackInSlot(i) != null){
  247.                 NBTTagCompound item = new NBTTagCompound();
  248.                 item.setByte("Slot", (byte) i);
  249.                 getStackInSlot(i).writeToNBT(item);
  250.                 items.appendTag(item);
  251.             }
  252.         }
  253.        
  254.         compound.setTag("Items", items);
  255.     }
  256.    
  257.     /**
  258.      * ONLY reads the inventory.  Be SURE to call super, and then read whatever else you need to
  259.      */
  260.     @Override
  261.     public void readFromNBT(NBTTagCompound compound) {
  262.         super.readFromNBT(compound);
  263.        
  264.         NBTTagList items = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
  265.         for(int i = 0; i < items.tagCount(); i++){
  266.             NBTTagCompound item = items.getCompoundTagAt(i);
  267.             int slot = item.getByte("Slot");
  268.             setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
  269.         }
  270.        
  271.         if(compound.hasKey("CustomName", 8)){
  272.             setCustomName(compound.getString("CustomName"));
  273.         }
  274.     }
  275.  
  276.    
  277.    
  278. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement