Arctic_Wolfy

Store Container + TE

Oct 5th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.61 KB | None | 0 0
  1. public abstract class TEStorage extends TileEntity implements IInventory {
  2.  
  3.     @Override
  4.     public void readFromNBT(NBTTagCompound tag) {
  5.         super.readFromNBT(tag);
  6.         NBTTagList inv = tag.getTagList("Items",10);
  7.  
  8.         for (int i = 0; i < inv.tagCount(); ++i) {
  9.             NBTTagCompound item = inv.getCompoundTagAt(i);
  10.             int j = item.getByte("Slot") & 255;
  11.  
  12.             if (j >= 0 && j < getSizeInventory()) {
  13.                 setInventorySlotContents(j,ItemStack.loadItemStackFromNBT(item));
  14.             }
  15.         }
  16.     }
  17.  
  18.     @Override
  19.     public void writeToNBT(NBTTagCompound tag) {
  20.         super.writeToNBT(tag);
  21.         NBTTagList inv = new NBTTagList();
  22.  
  23.         for (int i = 0; i < getSizeInventory(); i++) {
  24.             ItemStack stack = getStackInSlot(i);
  25.             if (stack !=null){
  26.                 NBTTagCompound item = new NBTTagCompound();
  27.                 item.setByte("Slot", (byte)i);
  28.                 stack.writeToNBT(item);
  29.                 inv.appendTag(item);
  30.             }
  31.         }
  32.  
  33.         tag.setTag("Items",inv);
  34.  
  35.     }
  36.  
  37.     @Override public abstract int getSizeInventory();
  38.     @Override public abstract ItemStack getStackInSlot(int slot);
  39.     @Override public abstract ItemStack decrStackSize(int slot, int amt);
  40.     @Override public abstract ItemStack getStackInSlotOnClosing(int slot);
  41.     @Override public abstract void setInventorySlotContents(int slot, ItemStack stack);
  42.     @Override public abstract String getInventoryName();
  43.     @Override public abstract boolean hasCustomInventoryName();
  44.     @Override public abstract int getInventoryStackLimit();
  45.     @Override public abstract boolean isUseableByPlayer(EntityPlayer player);
  46.     @Override public abstract void openInventory();
  47.     @Override public abstract void closeInventory();
  48.     @Override public abstract boolean isItemValidForSlot(int slot, ItemStack stack);
  49. }
  50.  
  51. public class TEShop extends TEStorage{
  52.  
  53.     private ItemStack[] inv = new ItemStack[5];
  54.     private UUID owner;
  55.  
  56.     public void setOwner(UUID owner) {
  57.         this.owner = owner;
  58.         this.markDirty();
  59.     }
  60.     public UUID getOwner() {return owner;}
  61.  
  62.     public float getCardCredit(){
  63.         if (inv[0] == null)return 0f;
  64.         else if (inv[0].getItem() == ModItems.creditCard){
  65.             if (inv[0].hasTagCompound()){
  66.                 BankAccount account = BankAccount.fromNBT(worldObj,inv[0].getTagCompound().getCompoundTag(NBTTags.Card.ACCOUNTS_TAG),null);
  67.                 return account.getCredit();
  68.             }
  69.         }
  70.         return 0f;
  71.     }
  72.  
  73.     @Override
  74.     public void writeToNBT(NBTTagCompound tag) {
  75.         super.writeToNBT(tag);
  76.         tag.setTag(NBTTags.Card.UUID_OWNER_TAG, Converters.getTagFromUUID(owner));
  77.     }
  78.  
  79.     @Override
  80.     public void readFromNBT(NBTTagCompound tag) {
  81.         super.readFromNBT(tag);
  82.         owner = Converters.getUUIDFromTag(tag.getCompoundTag(NBTTags.Card.UUID_OWNER_TAG));
  83.     }
  84.  
  85.     @Override
  86.     public int getSizeInventory() {
  87.         return inv.length;
  88.     }
  89.  
  90.     @Override
  91.     public ItemStack getStackInSlot(int slot) {
  92.         return inv[slot];
  93.     }
  94.  
  95.     @Override
  96.     public ItemStack decrStackSize(int slot, int amt) {
  97.         ItemStack stack = getStackInSlot(slot);
  98.         if (stack != null) {
  99.             if (stack.stackSize <= amt) {
  100.                 setInventorySlotContents(slot, null);
  101.             } else {
  102.                 stack = stack.splitStack(amt);
  103.                 if (stack.stackSize == 0) {
  104.                     setInventorySlotContents(slot, null);
  105.                 }
  106.             }
  107.         }
  108.         return stack;
  109.     }
  110.     @Override
  111.     public ItemStack getStackInSlotOnClosing(int slot) {
  112.         return getStackInSlot(slot);
  113.     }
  114.  
  115.     @Override
  116.     public void setInventorySlotContents(int slot, ItemStack stack) {
  117.         inv[slot] = stack;
  118.     }
  119.  
  120.     @Override public String getInventoryName() {return "Store";}
  121.     @Override public boolean hasCustomInventoryName() {return false;}
  122.     @Override public int getInventoryStackLimit() {return 64;}
  123.     @Override public boolean isUseableByPlayer(EntityPlayer player) {return true;}
  124.     @Override public void openInventory() {}
  125.     @Override public void closeInventory() {}
  126.     @Override public boolean isItemValidForSlot(int slot, ItemStack stack) {return true;}
  127. }
  128.  
  129. public class BlockShop extends BlockMTContainer {
  130.     public BlockShop(){
  131.         super();
  132.         this.setBlockName("shop");
  133.     }
  134.  
  135.     @Override
  136.     public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase livingBase, ItemStack stack) {
  137.         if (livingBase instanceof EntityPlayer){
  138.             EntityPlayer player = (EntityPlayer) livingBase;
  139.             TEShop shop = (TEShop) world.getTileEntity(x,y,z);
  140.             shop.setOwner(player.getGameProfile().getId());
  141.         }
  142.     }
  143.  
  144.     @Override
  145.     public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int meta, float hitX, float hitY, float hitZ) {
  146.         player.openGui(MoneyMod.instance,1,world,x,y,z);
  147.         return super.onBlockActivated(world, x, y, z, player, meta, hitX, hitY, hitZ);
  148.     }
  149.  
  150.     @Override
  151.     public TileEntity createNewTileEntity(World world, int meta) {
  152.         return new TEShop();
  153.     }
  154. }
  155.  
  156. public static class StoreContainer extends Container {
  157.         private IInventory playerInv, storeInv;
  158.  
  159.         public StoreContainer(EntityPlayer player, final IInventory storeInv){
  160.             super();
  161.             this.playerInv = player.inventory;
  162.             this.storeInv = storeInv;
  163.  
  164.             //((TEShop) storeInv).setPlayer(player);
  165.  
  166.             storeInv.openInventory();
  167.  
  168.             int i = (6 - 4) * 18 + 1;
  169.             int j;
  170.             int k;
  171.  
  172.             this.addSlotToContainer(new Slot(storeInv, 0, 8, 18));
  173.             for (j = 0; j < 4; j++) {
  174.                 this.addSlotToContainer(new Slot(storeInv, j + 1, 8 + 22, 18 + (22 * (j))){
  175.                     @SuppressWarnings("Contract")
  176.                     @Override
  177.                     public boolean canTakeStack(EntityPlayer player) {
  178.                         return ((TEShop) storeInv).getOwner() != null &&
  179.                                 (((TEShop) storeInv).getOwner().equals(player.getGameProfile().getId()) ||
  180.                                         ((TEShop) storeInv).getCardCredit() >= 64f);
  181.                     }
  182.                 });
  183.             }
  184.  
  185.  
  186.             for (j = 0; j < 3; ++j) {
  187.                 for (k = 0; k < 9; ++k) {
  188.                     this.addSlotToContainer(new Slot(playerInv, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
  189.                 }
  190.             }
  191.  
  192.             for (j = 0; j < 9; ++j) {
  193.                 this.addSlotToContainer(new Slot(playerInv, j, 8 + j * 18, 161 + i));
  194.             }
  195.         }
  196.  
  197.         @Override
  198.         public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
  199.             ItemStack stack = null;
  200.             Slot slotObject = (Slot) inventorySlots.get(slot);
  201.  
  202.             //null checks and checks if the item can be stacked (maxStackSize > 1)
  203.             if (slotObject != null && slotObject.getHasStack()) {
  204.                 ItemStack stackInSlot = slotObject.getStack();
  205.                 stack = stackInSlot.copy();
  206.  
  207.                 //merges the item into player inventory since its in the tileEntity
  208.                 if (slot < storeInv.getSizeInventory()) {
  209.                     if (!this.mergeItemStack(stackInSlot, storeInv.getSizeInventory(), 36+ storeInv.getSizeInventory(), true)) {
  210.                         return null;
  211.                     }
  212.                 }
  213.                 //places it into the tileEntity is possible since its in the player inventory
  214.                 else if (!this.mergeItemStack(stackInSlot, 0, storeInv.getSizeInventory(), false)) {
  215.                     return null;
  216.                 }
  217.  
  218.                 if (stackInSlot.stackSize == 0) {
  219.                     slotObject.putStack(null);
  220.                 } else {
  221.                     slotObject.onSlotChanged();
  222.                 }
  223.  
  224.                 if (stackInSlot.stackSize == stack.stackSize) {
  225.                     return null;
  226.                 }
  227.                 slotObject.onPickupFromSlot(player, stackInSlot);
  228.             }
  229.             return stack;
  230.         }
  231.  
  232.         @Override public boolean canInteractWith(EntityPlayer player) {return true;}
  233.  
  234.         @Override
  235.         public boolean isPlayerNotUsingContainer(EntityPlayer p_75129_1_) {
  236.             return super.isPlayerNotUsingContainer(p_75129_1_);
  237.         }
  238.  
  239.         @Override
  240.         public void onContainerClosed(EntityPlayer player) {
  241.             storeInv.closeInventory();
  242.         }
  243.     }
Advertisement
Add Comment
Please, Sign In to add comment