Mrolas

TileEntityCoinBank.java

Dec 31st, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.06 KB | None | 0 0
  1. package mrolas.muchMoney.common.tileEntities;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.CharBuffer;
  5.  
  6. import mrolas.muchMoney.common.item.Items;
  7. import mrolas.muchMoney.common.lib.EnumCoins;
  8. import mrolas.muchMoney.common.lib.Reference;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.inventory.IInventory;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.nbt.NBTTagList;
  14. import net.minecraft.tileentity.TileEntity;
  15.  
  16. public class TileEntityCoinBank
  17.     extends TileEntity
  18.     implements IInventory
  19. {
  20.     public boolean locked;
  21.     public String owner;
  22.    
  23.     private ItemStack[] stacks;
  24.    
  25.     private long storedValue = 0;
  26.     private int storedValueInt1 = 0;
  27.     private int storedValueInt2 = 0;
  28.    
  29.     private int wO = 0;
  30.     private int wP = 0;
  31.     private int wG = 0;
  32.     private int wS = 0;
  33.     private int wC = 0;
  34.    
  35.     private boolean hasError = false;
  36.     private String errMessage = "";
  37.    
  38.     public TileEntityCoinBank ()
  39.     {
  40.         stacks = new ItemStack[6];
  41.         owner = "";
  42.     }
  43.    
  44.     @Override
  45.     public int getSizeInventory ()
  46.     {
  47.         return stacks.length;
  48.     }
  49.  
  50.     @Override
  51.     public ItemStack getStackInSlot (int i)
  52.     {
  53.         return stacks[i];
  54.     }
  55.  
  56.     @Override
  57.     public ItemStack decrStackSize (int i, int count)
  58.     {
  59.         ItemStack stack = getStackInSlot(i);
  60.        
  61.         if (stack != null)
  62.         {
  63.             if (stack.stackSize <= count)
  64.             {
  65.                 setInventorySlotContents(i, null);
  66.             }
  67.             else
  68.             {
  69.                 stack = stack.splitStack(count);
  70.                 onInventoryChanged();
  71.             }
  72.         }
  73.        
  74.         return stack;
  75.     }
  76.  
  77.     @Override
  78.     public ItemStack getStackInSlotOnClosing (int i)
  79.     {
  80.         ItemStack stack = getStackInSlot(i);
  81.         setInventorySlotContents(i, null);
  82.         return stack;
  83.     }
  84.  
  85.     @Override
  86.     public void setInventorySlotContents (int i, ItemStack itemstack)
  87.     {
  88.         stacks[i] = itemstack;
  89.        
  90.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
  91.         {
  92.             itemstack.stackSize = getInventoryStackLimit();
  93.         }
  94.        
  95.         onInventoryChanged();
  96.     }
  97.  
  98.     @Override
  99.     public String getInvName ()
  100.     {
  101.         return "InventoryCoinBank";
  102.     }
  103.  
  104.     @Override
  105.     public boolean isInvNameLocalized ()
  106.     {
  107.         return false;
  108.     }
  109.  
  110.     @Override
  111.     public int getInventoryStackLimit ()
  112.     {
  113.         return 64;
  114.     }
  115.  
  116.     @Override
  117.     public boolean isUseableByPlayer (EntityPlayer entityplayer)
  118.     {
  119.         return entityplayer.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) <= 64;
  120.     }
  121.  
  122.     @Override
  123.     public void openChest () {}
  124.  
  125.     @Override
  126.     public void closeChest () {}
  127.  
  128.     @Override
  129.     public boolean isItemValidForSlot (int slot, ItemStack itemstack)
  130.     {
  131.         if (slot == 0)
  132.             return itemstack.itemID == Reference.COIN_ID;
  133.         else
  134.             return false;
  135.     }
  136.    
  137.     @Override
  138.     public void writeToNBT (NBTTagCompound compound)
  139.     {
  140.         super.writeToNBT(compound);
  141.        
  142.         NBTTagList items = new NBTTagList();
  143.        
  144.         for (int i = 0; i < getSizeInventory(); i++)
  145.         {
  146.             ItemStack stack = getStackInSlot(i);
  147.            
  148.             if (stack != null)
  149.             {
  150.                 NBTTagCompound item = new NBTTagCompound();
  151.                 item.setByte("Slot", (byte)i);
  152.                 stack.writeToNBT(compound);
  153.                 items.appendTag(item);
  154.             }
  155.         }
  156.        
  157.         compound.setTag("Items", items);   
  158.         compound.setLong("StoredValue", storedValue);
  159.         compound.setInteger("StoredValueInt1", storedValueInt1);
  160.         compound.setInteger("StoredValueInt2", storedValueInt2);
  161.         compound.setInteger("WithdrawlCopper", wC);
  162.         compound.setInteger("WithdrawlSilver", wS);
  163.         compound.setInteger("WithdrawlGold", wG);
  164.         compound.setInteger("WithdrawlPlatinum", wP);
  165.         compound.setInteger("WithdrawlOsmium", wO);
  166.         compound.setBoolean("Locked", locked);
  167.         compound.setString("Owner", owner);
  168.     }
  169.    
  170.     @Override
  171.     public void readFromNBT (NBTTagCompound compound)
  172.     {
  173.         super.readFromNBT(compound);
  174.        
  175.         NBTTagList items = compound.getTagList("Items");
  176.        
  177.         for (int i = 0; i < items.tagCount(); i++)
  178.         {
  179.             NBTTagCompound item = (NBTTagCompound)items.tagAt(i);
  180.            
  181.             int slot = item.getByte("Slot");
  182.            
  183.             if (slot >= 0 && slot < getSizeInventory())
  184.             {
  185.                 setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
  186.             }
  187.         }
  188.        
  189.         storedValue = compound.getLong("StoredValue");
  190.         storedValueInt1 = compound.getInteger("StoredValueInt1");
  191.         storedValueInt2 = compound.getInteger("StoredValueInt2");
  192.         wC = compound.getInteger("WithdrawlCopper");
  193.         wS = compound.getInteger("WithdrawlSilver");
  194.         wG = compound.getInteger("WithdrawlGold");
  195.         wP = compound.getInteger("WithdrawlPlatinum");
  196.         wO = compound.getInteger("WithdrawlOsmium");
  197.         locked = compound.getBoolean("Locked");
  198.         owner = compound.getString("Owner");
  199.     }
  200.    
  201.     /*
  202.      * Updates
  203.      */
  204.    
  205.     @Override
  206.     public void onInventoryChanged ()
  207.     {
  208.         super.onInventoryChanged();
  209.        
  210.         updateHasError();
  211.     }
  212.    
  213.     public void updateHasError ()
  214.     {
  215.         if (stacks[1] != null || stacks[2] != null || stacks[3] != null || stacks[4] != null || stacks[5] != null)
  216.         {
  217.             errMessage = "Withdrawl slot obstructed";
  218.             hasError = true;
  219.         }
  220.         else if (stacks[0] != null && stacks[0].itemID == Items.coin.itemID && storedValue + EnumCoins.index(stacks[0].getItemDamage()).value * stacks[0].stackSize > Long.MAX_VALUE)
  221.         {
  222.             errMessage = "Not enough room";
  223.             hasError = true;
  224.         }
  225.         else
  226.         {
  227.             errMessage = "";
  228.             hasError = false;
  229.         }
  230.     }
  231.    
  232.     public void updateInts ()
  233.     {
  234.         storedValueInt1 = (int)(storedValue >> 32);
  235.         storedValueInt2 = (int)storedValue;
  236.     }
  237.    
  238.     public void updateStoredValue ()
  239.     {
  240.         storedValue = ((long)storedValueInt1 << 32 | ((long)storedValueInt2 & 0xffffffffL));
  241.     }
  242.    
  243.     /*
  244.      * Getters and Setters
  245.      */
  246.    
  247.     public long getStoredValue ()
  248.     {
  249.         return storedValue;
  250.     }
  251.    
  252.     public void setStoredValue (long value)
  253.     {
  254.         storedValue = value;
  255.         updateInts();
  256.     }
  257.    
  258.     public int getStoredValueInt1 ()
  259.     {
  260.         return storedValueInt1;
  261.     }
  262.    
  263.     public void setStoredValueInt1 (int value)
  264.     {
  265.         storedValueInt1 = value;
  266.         updateStoredValue();
  267.     }
  268.    
  269.     public int getStoredValueInt2 ()
  270.     {
  271.         return storedValueInt2;
  272.     }
  273.    
  274.     public void setStoredValueInt2 (int value)
  275.     {
  276.         storedValueInt2 = value;
  277.         updateStoredValue();
  278.     }
  279.    
  280.     public int getWithdrawlC ()
  281.     {
  282.         return wC;
  283.     }
  284.    
  285.     public void setWithdrawlC (int value)
  286.     {
  287.         wC = value;
  288.     }
  289.    
  290.     public int getWithdrawlS ()
  291.     {
  292.         return wS;
  293.     }
  294.    
  295.     public void setWithdrawlS (int value)
  296.     {
  297.         wS = value;
  298.     }
  299.    
  300.     public int getWithdrawlG ()
  301.     {
  302.         return wG;
  303.     }
  304.    
  305.     public void setWithdrawlG (int value)
  306.     {
  307.         wG = value;
  308.     }
  309.    
  310.     public int getWithdrawlP ()
  311.     {
  312.         return wP;
  313.     }
  314.    
  315.     public void setWithdrawlP (int value)
  316.     {
  317.         wP = value;
  318.     }
  319.    
  320.     public int getWithdrawlO ()
  321.     {
  322.         return wO;
  323.     }
  324.    
  325.     public void setWithdrawlO (int value)
  326.     {
  327.         wO = value;
  328.     }
  329.    
  330.     public boolean getHasError ()
  331.     {
  332.         return hasError;
  333.     }
  334.    
  335.     public void setHasError (boolean hasError)
  336.     {
  337.         this.hasError = hasError;
  338.     }
  339.    
  340.     public String getError ()
  341.     {
  342.         return errMessage;
  343.     }
  344.    
  345.     /*
  346.      * Button Actions
  347.      */
  348.    
  349.     public void depositCoin ()
  350.     {
  351.         if (stacks[0] != null && stacks[0].itemID == Items.coin.itemID)
  352.         {
  353.             storedValue += EnumCoins.index(stacks[0].getItemDamage()).value * stacks[0].stackSize;
  354.             updateInts();
  355.             stacks[0] = null;
  356.         }
  357.     }
  358.    
  359.     public void clearWithdrawls ()
  360.     {
  361.         wO = 0;
  362.         wP = 0;
  363.         wG = 0;
  364.         wS = 0;
  365.         wC = 0;
  366.     }
  367.    
  368.     public void withdrawlAmount ()
  369.     {
  370.         if (getWantedValue() <= storedValue)
  371.         {
  372.             if (stacks[1] == null && stacks[2] == null && stacks[3] == null && stacks[4] == null && stacks[5] == null)
  373.             {
  374.                 if (wO > 0)
  375.                 {
  376.                     stacks[1] = new ItemStack(Items.coin, wO, EnumCoins.osmium.ord);
  377.                     storedValue -= wO * EnumCoins.osmium.value;
  378.                     updateInts();
  379.                 }
  380.                 if (wP > 0)
  381.                 {
  382.                     stacks[2] = new ItemStack(Items.coin, wP, EnumCoins.platinum.ord);
  383.                     storedValue -= wP * EnumCoins.platinum.value;
  384.                     updateInts();
  385.                 }
  386.                 if (wG > 0)
  387.                 {
  388.                     stacks[3] = new ItemStack(Items.coin, wG, EnumCoins.gold.ord);
  389.                     storedValue -= wG * EnumCoins.gold.value;
  390.                     updateInts();
  391.                 }
  392.                 if (wS > 0)
  393.                 {
  394.                     stacks[4] = new ItemStack(Items.coin, wS, EnumCoins.silver.ord);
  395.                     storedValue -= wS * EnumCoins.silver.value;
  396.                     updateInts();
  397.                 }
  398.                 if (wC > 0)
  399.                 {
  400.                     stacks[5] = new ItemStack(Items.coin, wC, EnumCoins.copper.ord);
  401.                     storedValue -= wC * EnumCoins.copper.value;
  402.                     updateInts();
  403.                 }
  404.                
  405.                 clearWithdrawls();
  406.             }
  407.         }
  408.     }
  409.    
  410.     private int getWantedValue ()
  411.     {
  412.         return wC*EnumCoins.copper.value + wS*EnumCoins.silver.value + wG*EnumCoins.gold.value + wP*EnumCoins.platinum.value + wO*EnumCoins.osmium.value;
  413.     }
  414.    
  415.     public void receiveButtonEvent (EntityPlayer player, byte buttonId)
  416.     {  
  417.         System.out.println(owner);
  418.         switch(buttonId)
  419.         {
  420.             //Deposit
  421.             case 0:
  422.                 if (stacks[0] != null && stacks[0].itemID == Items.coin.itemID)
  423.                 {
  424.                     if (storedValue + EnumCoins.index(stacks[0].getItemDamage()).value * stacks[0].stackSize <= Long.MAX_VALUE)
  425.                     {
  426.                         depositCoin();
  427.                     }
  428.                 }
  429.                 break;
  430.                
  431.             //Clear
  432.             case 1:
  433.                 clearWithdrawls();
  434.                 break;
  435.            
  436.             //Withdrawl
  437.             case 2:
  438.                 if (!hasError)
  439.                 {
  440.                     if (player.getEntityName().equals(owner) || owner.equals(null) || owner.equals(""))
  441.                         withdrawlAmount();
  442.                 }
  443.                 break;
  444.                
  445.             //Copper Withdrawl
  446.             case 3:
  447.                 if (wC <= 63);
  448.                     wC += 1;
  449.                 break;
  450.             case 4:
  451.                 if (wC <= 56)
  452.                     wC += 8;
  453.                 break;
  454.            
  455.             //Silver Withdrawl
  456.             case 5:
  457.                 if (wS <= 63)
  458.                     wS += 1;
  459.                 break;
  460.             case 6:
  461.                 if (wS <= 56)
  462.                     wS += 8;
  463.                 break;
  464.                
  465.             //Gold Withdrawl
  466.             case 7:
  467.                 if (wG <= 63)
  468.                     wG += 1;
  469.                 break;
  470.             case 8:
  471.                 if (wG <= 56)
  472.                     wG += 8;
  473.                 break;
  474.                
  475.             //Platinum Withdrawl
  476.             case 9:
  477.                 if (wP <= 63)
  478.                     wP += 1;
  479.                 break;
  480.             case 10:
  481.                 if (wP <= 56)
  482.                     wP += 8;
  483.                 break;
  484.                
  485.             //Osmium Withdrawl
  486.             case 11:
  487.                 if (wO <= 63)
  488.                     wO += 1;
  489.                 break;
  490.             case 12:
  491.                 if (wO <= 56)
  492.                     wO += 8;
  493.                 break;
  494.             case 13: //When Locked
  495.                 if (locked)
  496.                 {
  497.                     if (player.getEntityName().equals(owner))
  498.                     {
  499.                         locked = false;
  500.                         owner = "";
  501.                     }
  502.                 }
  503.                 break;
  504.             case 14: //When Unlocked
  505.                 if (!locked)
  506.                 {
  507.                     locked = true;
  508.                     owner = player.getEntityName();
  509.                 }
  510.                 break;
  511.         }
  512.         onInventoryChanged();
  513.     }
  514.    
  515.     public int[] getAccessibleSlotsFromSide(int side)
  516.     {
  517.         return new int[] {0};
  518.     }
  519.  
  520.     public boolean canInsertItem(int slot, ItemStack itemstack, int side)
  521.     {
  522.         return slot == 0 ? isItemValidForSlot(slot, itemstack) : false;
  523.     }
  524.  
  525.     public boolean canExtractItem(int slot, ItemStack itemstack, int side)
  526.     {
  527.         return false;
  528.     }
  529.    
  530.     public void setOwner (String name)
  531.     {
  532.         owner = name;
  533.     }
  534.    
  535.     public boolean isOwner (String name)
  536.     {
  537.         if (name.equals(owner))
  538.         {
  539.             return true;
  540.         }
  541.         return false;
  542.     }
  543.    
  544.     public byte[] stringToByteArray (String str)
  545.     {
  546.         char[] buffer = str.toCharArray();
  547.         byte[] array = new byte[buffer.length];
  548.         for (int i = 0; i < buffer.length; i++)
  549.             array[i] = (byte)buffer[i];
  550.         return array;
  551.     }
  552.    
  553.     public String byteArrayToString (byte[] array)
  554.     {
  555.         return new String(array);
  556.     }
  557. }
Add Comment
Please, Sign In to add comment