Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mrolas.muchMoney.common.tileEntities;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import mrolas.muchMoney.common.item.Items;
- import mrolas.muchMoney.common.lib.EnumCoins;
- import mrolas.muchMoney.common.lib.Reference;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- public class TileEntityCoinBank
- extends TileEntity
- implements IInventory
- {
- public boolean locked;
- public String owner;
- private ItemStack[] stacks;
- private long storedValue = 0;
- private int storedValueInt1 = 0;
- private int storedValueInt2 = 0;
- private int wO = 0;
- private int wP = 0;
- private int wG = 0;
- private int wS = 0;
- private int wC = 0;
- private boolean hasError = false;
- private String errMessage = "";
- public TileEntityCoinBank ()
- {
- stacks = new ItemStack[6];
- owner = "";
- }
- @Override
- public int getSizeInventory ()
- {
- return stacks.length;
- }
- @Override
- public ItemStack getStackInSlot (int i)
- {
- return stacks[i];
- }
- @Override
- public ItemStack decrStackSize (int i, int count)
- {
- ItemStack stack = getStackInSlot(i);
- if (stack != null)
- {
- if (stack.stackSize <= count)
- {
- setInventorySlotContents(i, null);
- }
- else
- {
- stack = stack.splitStack(count);
- onInventoryChanged();
- }
- }
- return stack;
- }
- @Override
- public ItemStack getStackInSlotOnClosing (int i)
- {
- ItemStack stack = getStackInSlot(i);
- setInventorySlotContents(i, null);
- return stack;
- }
- @Override
- public void setInventorySlotContents (int i, ItemStack itemstack)
- {
- stacks[i] = itemstack;
- if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
- {
- itemstack.stackSize = getInventoryStackLimit();
- }
- onInventoryChanged();
- }
- @Override
- public String getInvName ()
- {
- return "InventoryCoinBank";
- }
- @Override
- public boolean isInvNameLocalized ()
- {
- return false;
- }
- @Override
- public int getInventoryStackLimit ()
- {
- return 64;
- }
- @Override
- public boolean isUseableByPlayer (EntityPlayer entityplayer)
- {
- return entityplayer.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) <= 64;
- }
- @Override
- public void openChest () {}
- @Override
- public void closeChest () {}
- @Override
- public boolean isItemValidForSlot (int slot, ItemStack itemstack)
- {
- if (slot == 0)
- return itemstack.itemID == Reference.COIN_ID;
- else
- return false;
- }
- @Override
- public void writeToNBT (NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- NBTTagList items = new NBTTagList();
- for (int i = 0; i < getSizeInventory(); i++)
- {
- ItemStack stack = getStackInSlot(i);
- if (stack != null)
- {
- NBTTagCompound item = new NBTTagCompound();
- item.setByte("Slot", (byte)i);
- stack.writeToNBT(compound);
- items.appendTag(item);
- }
- }
- compound.setTag("Items", items);
- compound.setLong("StoredValue", storedValue);
- compound.setInteger("StoredValueInt1", storedValueInt1);
- compound.setInteger("StoredValueInt2", storedValueInt2);
- compound.setInteger("WithdrawlCopper", wC);
- compound.setInteger("WithdrawlSilver", wS);
- compound.setInteger("WithdrawlGold", wG);
- compound.setInteger("WithdrawlPlatinum", wP);
- compound.setInteger("WithdrawlOsmium", wO);
- compound.setBoolean("Locked", locked);
- compound.setString("Owner", owner);
- }
- @Override
- public void readFromNBT (NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- NBTTagList items = compound.getTagList("Items");
- for (int i = 0; i < items.tagCount(); i++)
- {
- NBTTagCompound item = (NBTTagCompound)items.tagAt(i);
- int slot = item.getByte("Slot");
- if (slot >= 0 && slot < getSizeInventory())
- {
- setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
- }
- }
- storedValue = compound.getLong("StoredValue");
- storedValueInt1 = compound.getInteger("StoredValueInt1");
- storedValueInt2 = compound.getInteger("StoredValueInt2");
- wC = compound.getInteger("WithdrawlCopper");
- wS = compound.getInteger("WithdrawlSilver");
- wG = compound.getInteger("WithdrawlGold");
- wP = compound.getInteger("WithdrawlPlatinum");
- wO = compound.getInteger("WithdrawlOsmium");
- locked = compound.getBoolean("Locked");
- owner = compound.getString("Owner");
- }
- /*
- * Updates
- */
- @Override
- public void onInventoryChanged ()
- {
- super.onInventoryChanged();
- updateHasError();
- }
- public void updateHasError ()
- {
- if (stacks[1] != null || stacks[2] != null || stacks[3] != null || stacks[4] != null || stacks[5] != null)
- {
- errMessage = "Withdrawl slot obstructed";
- hasError = true;
- }
- else if (stacks[0] != null && stacks[0].itemID == Items.coin.itemID && storedValue + EnumCoins.index(stacks[0].getItemDamage()).value * stacks[0].stackSize > Long.MAX_VALUE)
- {
- errMessage = "Not enough room";
- hasError = true;
- }
- else
- {
- errMessage = "";
- hasError = false;
- }
- }
- public void updateInts ()
- {
- storedValueInt1 = (int)(storedValue >> 32);
- storedValueInt2 = (int)storedValue;
- }
- public void updateStoredValue ()
- {
- storedValue = ((long)storedValueInt1 << 32 | ((long)storedValueInt2 & 0xffffffffL));
- }
- /*
- * Getters and Setters
- */
- public long getStoredValue ()
- {
- return storedValue;
- }
- public void setStoredValue (long value)
- {
- storedValue = value;
- updateInts();
- }
- public int getStoredValueInt1 ()
- {
- return storedValueInt1;
- }
- public void setStoredValueInt1 (int value)
- {
- storedValueInt1 = value;
- updateStoredValue();
- }
- public int getStoredValueInt2 ()
- {
- return storedValueInt2;
- }
- public void setStoredValueInt2 (int value)
- {
- storedValueInt2 = value;
- updateStoredValue();
- }
- public int getWithdrawlC ()
- {
- return wC;
- }
- public void setWithdrawlC (int value)
- {
- wC = value;
- }
- public int getWithdrawlS ()
- {
- return wS;
- }
- public void setWithdrawlS (int value)
- {
- wS = value;
- }
- public int getWithdrawlG ()
- {
- return wG;
- }
- public void setWithdrawlG (int value)
- {
- wG = value;
- }
- public int getWithdrawlP ()
- {
- return wP;
- }
- public void setWithdrawlP (int value)
- {
- wP = value;
- }
- public int getWithdrawlO ()
- {
- return wO;
- }
- public void setWithdrawlO (int value)
- {
- wO = value;
- }
- public boolean getHasError ()
- {
- return hasError;
- }
- public void setHasError (boolean hasError)
- {
- this.hasError = hasError;
- }
- public String getError ()
- {
- return errMessage;
- }
- /*
- * Button Actions
- */
- public void depositCoin ()
- {
- if (stacks[0] != null && stacks[0].itemID == Items.coin.itemID)
- {
- storedValue += EnumCoins.index(stacks[0].getItemDamage()).value * stacks[0].stackSize;
- updateInts();
- stacks[0] = null;
- }
- }
- public void clearWithdrawls ()
- {
- wO = 0;
- wP = 0;
- wG = 0;
- wS = 0;
- wC = 0;
- }
- public void withdrawlAmount ()
- {
- if (getWantedValue() <= storedValue)
- {
- if (stacks[1] == null && stacks[2] == null && stacks[3] == null && stacks[4] == null && stacks[5] == null)
- {
- if (wO > 0)
- {
- stacks[1] = new ItemStack(Items.coin, wO, EnumCoins.osmium.ord);
- storedValue -= wO * EnumCoins.osmium.value;
- updateInts();
- }
- if (wP > 0)
- {
- stacks[2] = new ItemStack(Items.coin, wP, EnumCoins.platinum.ord);
- storedValue -= wP * EnumCoins.platinum.value;
- updateInts();
- }
- if (wG > 0)
- {
- stacks[3] = new ItemStack(Items.coin, wG, EnumCoins.gold.ord);
- storedValue -= wG * EnumCoins.gold.value;
- updateInts();
- }
- if (wS > 0)
- {
- stacks[4] = new ItemStack(Items.coin, wS, EnumCoins.silver.ord);
- storedValue -= wS * EnumCoins.silver.value;
- updateInts();
- }
- if (wC > 0)
- {
- stacks[5] = new ItemStack(Items.coin, wC, EnumCoins.copper.ord);
- storedValue -= wC * EnumCoins.copper.value;
- updateInts();
- }
- clearWithdrawls();
- }
- }
- }
- private int getWantedValue ()
- {
- return wC*EnumCoins.copper.value + wS*EnumCoins.silver.value + wG*EnumCoins.gold.value + wP*EnumCoins.platinum.value + wO*EnumCoins.osmium.value;
- }
- public void receiveButtonEvent (EntityPlayer player, byte buttonId)
- {
- System.out.println(owner);
- switch(buttonId)
- {
- //Deposit
- case 0:
- if (stacks[0] != null && stacks[0].itemID == Items.coin.itemID)
- {
- if (storedValue + EnumCoins.index(stacks[0].getItemDamage()).value * stacks[0].stackSize <= Long.MAX_VALUE)
- {
- depositCoin();
- }
- }
- break;
- //Clear
- case 1:
- clearWithdrawls();
- break;
- //Withdrawl
- case 2:
- if (!hasError)
- {
- if (player.getEntityName().equals(owner) || owner.equals(null) || owner.equals(""))
- withdrawlAmount();
- }
- break;
- //Copper Withdrawl
- case 3:
- if (wC <= 63);
- wC += 1;
- break;
- case 4:
- if (wC <= 56)
- wC += 8;
- break;
- //Silver Withdrawl
- case 5:
- if (wS <= 63)
- wS += 1;
- break;
- case 6:
- if (wS <= 56)
- wS += 8;
- break;
- //Gold Withdrawl
- case 7:
- if (wG <= 63)
- wG += 1;
- break;
- case 8:
- if (wG <= 56)
- wG += 8;
- break;
- //Platinum Withdrawl
- case 9:
- if (wP <= 63)
- wP += 1;
- break;
- case 10:
- if (wP <= 56)
- wP += 8;
- break;
- //Osmium Withdrawl
- case 11:
- if (wO <= 63)
- wO += 1;
- break;
- case 12:
- if (wO <= 56)
- wO += 8;
- break;
- case 13: //When Locked
- if (locked)
- {
- if (player.getEntityName().equals(owner))
- {
- locked = false;
- owner = "";
- }
- }
- break;
- case 14: //When Unlocked
- if (!locked)
- {
- locked = true;
- owner = player.getEntityName();
- }
- break;
- }
- onInventoryChanged();
- }
- public int[] getAccessibleSlotsFromSide(int side)
- {
- return new int[] {0};
- }
- public boolean canInsertItem(int slot, ItemStack itemstack, int side)
- {
- return slot == 0 ? isItemValidForSlot(slot, itemstack) : false;
- }
- public boolean canExtractItem(int slot, ItemStack itemstack, int side)
- {
- return false;
- }
- public void setOwner (String name)
- {
- owner = name;
- }
- public boolean isOwner (String name)
- {
- if (name.equals(owner))
- {
- return true;
- }
- return false;
- }
- public byte[] stringToByteArray (String str)
- {
- char[] buffer = str.toCharArray();
- byte[] array = new byte[buffer.length];
- for (int i = 0; i < buffer.length; i++)
- array[i] = (byte)buffer[i];
- return array;
- }
- public String byteArrayToString (byte[] array)
- {
- return new String(array);
- }
- }
Add Comment
Please, Sign In to add comment