Guest User

ContainerWeightedPlayer

a guest
Dec 26th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package com.happykiller.weightlimit.player.inventory.container;
  2.  
  3. import com.happykiller.weightlimit.items.ItemBackpack;
  4. import com.happykiller.weightlimit.player.inventory.InventoryWeightLimit;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.player.InventoryPlayer;
  8. import net.minecraft.inventory.Container;
  9. import net.minecraft.inventory.Slot;
  10. import net.minecraft.item.ItemArmor;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraftforge.fml.relauncher.Side;
  13. import net.minecraftforge.fml.relauncher.SideOnly;
  14.  
  15. public class ContainerWeightedPlayer extends Container {
  16.    
  17.     private static final int ARMOR_START = InventoryWeightLimit.INV_SIZE, ARMOR_END = ARMOR_START + 3,
  18.             INV_START = ARMOR_END + 1, INV_END = INV_START + 26,
  19.             HOTBAR_START = INV_END + 1, HOTBAR_END = HOTBAR_START + 8;
  20.    
  21.     private final EntityPlayer thePlayer;
  22.    
  23.     public ContainerWeightedPlayer(EntityPlayer player, InventoryPlayer invPlayer, InventoryWeightLimit invW) {
  24.         thePlayer = player;
  25.        
  26.         int i;
  27.         int j;
  28.        
  29.         this.addSlotToContainer(new SlotWeightLimit(invW, 0, 80, 8));
  30.        
  31.         for (i = 0; i < 4; ++i) {
  32.            
  33.             final int k = i;
  34.            
  35.             this.addSlotToContainer(new Slot(invPlayer, invPlayer.getSizeInventory() - 1 - i, 8, 8 + i * 18) {
  36.  
  37.                 public int getSlotStackLimit() {
  38.                     return 1;
  39.                 }
  40.                
  41.                 public boolean isItemValid(ItemStack stack) {
  42.                     if (stack == null) return false;
  43.                     return stack.getItem().isValidArmor(stack, k, thePlayer);
  44.                 }
  45.                
  46.                 @SideOnly(Side.CLIENT)
  47.                 public String getSlotTexture() {
  48.                     return ItemArmor.EMPTY_SLOT_NAMES[k];
  49.                 }
  50.             });
  51.         }
  52.        
  53.         for (i = 0; i < 3; ++i) {
  54.             for (j = 0; j < 9; ++j) {
  55.                 this.addSlotToContainer(new Slot(invPlayer, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
  56.             }
  57.         }
  58.  
  59.         for (i = 0; i < 9; ++i) {
  60.             this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));
  61.         }
  62.     }
  63.  
  64.     public boolean canInteractWith(EntityPlayer playerIn) {
  65.         return true;
  66.     }
  67.    
  68.     public ItemStack transferStackInSlot(EntityPlayer player, int i) {
  69.         ItemStack stack = null;
  70.        
  71.         Slot slot = (Slot)this.inventorySlots.get(i);
  72.        
  73.         if(slot != null && slot.getHasStack()) {
  74.             ItemStack stack1 = slot.getStack();
  75.            
  76.             stack = stack1.copy();
  77.            
  78.             if(i < INV_START) {
  79.                 if(!this.mergeItemStack(stack1, INV_START, HOTBAR_END + 1, true)) {
  80.                     return null;
  81.                 }
  82.                
  83.                 slot.onSlotChange(stack1, stack);
  84.             }else {
  85.                 if(stack1.getItem() instanceof ItemBackpack) {
  86.                     if(!this.mergeItemStack(stack1, 0, InventoryWeightLimit.INV_SIZE, false)) {
  87.                         return null;
  88.                     }
  89.                 }else if(stack1.getItem() instanceof ItemArmor) {
  90.                     int type = ((ItemArmor)stack1.getItem()).armorType;
  91.                    
  92.                     if(!this.mergeItemStack(stack1, ARMOR_START + type, ARMOR_START + type + 1, false)) {
  93.                         return null;
  94.                     }
  95.                 }else if(i >= INV_START && i < HOTBAR_START) {
  96.                     if(!this.mergeItemStack(stack1, HOTBAR_START, HOTBAR_START + 1, false)) {
  97.                         return null;
  98.                     }
  99.                 }else if(i >= HOTBAR_START && i < HOTBAR_END + 1) {
  100.                     if(!this.mergeItemStack(stack1, INV_START, INV_END + 1, false)) {
  101.                         return null;
  102.                     }
  103.                 }
  104.             }
  105.            
  106.             if(stack1.stackSize == 0) {
  107.                 slot.putStack((ItemStack)null);
  108.             }else {
  109.                 slot.onSlotChanged();
  110.             }
  111.            
  112.             if(stack1.stackSize == stack.stackSize) {
  113.                 return null;
  114.             }
  115.            
  116.             slot.onPickupFromSlot(thePlayer, stack1); //TODO If problems are caused, change to player.
  117.         }
  118.        
  119.         return stack;
  120.     }
  121. }
Add Comment
Please, Sign In to add comment