Guest User

ContainerNPC.class

a guest
Oct 18th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.51 KB | None | 0 0
  1. package net.rexozz.pixelengine.server.container;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import net.minecraft.entity.EntityLiving;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.EntityEquipmentSlot;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.Slot;
  11. import net.minecraft.item.ItemArmor;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraftforge.fml.relauncher.Side;
  14. import net.minecraftforge.fml.relauncher.SideOnly;
  15. import net.rexozz.pixelengine.entity.EntityNPC;
  16.  
  17. public class ContainerNPC extends Container{
  18.  
  19.     private EntityNPC npc;
  20.     public IInventory inventoryNPC;
  21.     private static final EntityEquipmentSlot[] VALID_ARMOR_SLOTS = new EntityEquipmentSlot[] {EntityEquipmentSlot.HEAD, EntityEquipmentSlot.CHEST, EntityEquipmentSlot.LEGS, EntityEquipmentSlot.FEET};
  22.    
  23.     private int
  24.     ARMOR_START=0,
  25.     ARMOR_END=ARMOR_START+3,
  26.     MAIN_OFFHAND_START=ARMOR_END+1,
  27.     MAIN_OFFHAND_END=MAIN_OFFHAND_START+1,
  28.     INV_PLAYER_START=MAIN_OFFHAND_END+1,
  29.     INV_PLAYER_END=INV_PLAYER_START+26,
  30.     HOTBAR_PLAYER_START=INV_PLAYER_END+1,
  31.     HOTBAR_PLAYER_END=HOTBAR_PLAYER_START+8;
  32.    
  33.     public ContainerNPC(IInventory playerInventory, final IInventory npcInventory, final EntityNPC npc, EntityPlayer player){
  34.         this.npc=npc;
  35.         this.inventoryNPC=npcInventory;
  36.        
  37.         for (int k = 0; k < 4; k++){
  38.             final EntityEquipmentSlot entityequipmentslot = VALID_ARMOR_SLOTS[k];
  39.             this.addSlotToContainer(new Slot(npcInventory, k, 8, 8 + k * 18){
  40.                 public int getSlotStackLimit() {
  41.                     return 1;
  42.                 }
  43.  
  44.                 public boolean isItemValid(@Nullable ItemStack stack){
  45.                     if (stack == null)
  46.                         return false;
  47.                     else
  48.                         return stack.getItem().isValidArmor(stack, entityequipmentslot, npc);
  49.                 }
  50.                 @Nullable
  51.                 @SideOnly(Side.CLIENT)
  52.                 public String getSlotTexture(){
  53.                     return ItemArmor.EMPTY_SLOT_NAMES[entityequipmentslot.getIndex()];
  54.                 }
  55.             });
  56.         }
  57.        
  58.         this.addSlotToContainer(new Slot(npcInventory,4, 20, 0){
  59.             public int getSlotStackLimit() {
  60.                 return 1;
  61.             }
  62.            
  63.             public boolean isItemValid(@Nullable ItemStack stack){
  64.                 if (stack == null)
  65.                     return false;
  66.                 else
  67.                     return EntityLiving.getSlotForItemStack(stack)==EntityEquipmentSlot.OFFHAND;
  68.             }
  69.         });
  70.         this.addSlotToContainer(new Slot(npcInventory,5, 38, 0));
  71.        
  72.         for (int i = 0; i < 3; ++i) {
  73.             for (int j = 0; j < 9; ++j)
  74.                 this.addSlotToContainer(new Slot(playerInventory, j+i*9+9, 8 + j * 18, 84 + i * 18));
  75.         }
  76.  
  77.         for (int k = 0; k < 9; ++k)
  78.             this.addSlotToContainer(new Slot(playerInventory, k, 8 + k * 18, 142));
  79.     }
  80.    
  81.     public boolean canInteractWith(EntityPlayer playerIn) {
  82.         return this.inventoryNPC.isUseableByPlayer(playerIn)&&this.npc.isEntityAlive();
  83.     }
  84.    
  85.     @Nullable
  86.     public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
  87.          ItemStack itemstack = null;
  88.          Slot slot = (Slot)this.inventorySlots.get(index);
  89.          if (slot != null && slot.getHasStack()){
  90.              ItemStack itemstack1 = slot.getStack();
  91.              itemstack = itemstack1.copy();
  92.              EntityEquipmentSlot entityequipmentslot = EntityLiving.getSlotForItemStack(itemstack);
  93.    
  94.              if(index<INV_PLAYER_START){
  95.                  if(!this.mergeItemStack(itemstack1, INV_PLAYER_START, HOTBAR_PLAYER_END+1, true))
  96.                      return null;
  97.                  slot.onSlotChange(itemstack1, itemstack);
  98.              }else{
  99.                  if(entityequipmentslot.getSlotType()==EntityEquipmentSlot.Type.ARMOR&&!((Slot)this.inventorySlots.get(3-entityequipmentslot.getIndex())).getHasStack()){
  100.                      int i=3-entityequipmentslot.getIndex();
  101.                      if(!this.mergeItemStack(itemstack1, i, i+1, false));
  102.                         return null;
  103.                  }else if(entityequipmentslot==EntityEquipmentSlot.OFFHAND&&!((Slot)this.inventorySlots.get(4)).getHasStack()){
  104.                      if(!this.mergeItemStack(itemstack1, 4, 5, false));
  105.                         return null;
  106.                  }else if(entityequipmentslot==EntityEquipmentSlot.MAINHAND&&!((Slot)this.inventorySlots.get(5)).getHasStack()){
  107.                      if(!this.mergeItemStack(itemstack1, 5, 6, false));
  108.                         return null;
  109.                  }else if(index>INV_PLAYER_START&&index<INV_PLAYER_END+1){
  110.                      if(!this.mergeItemStack(itemstack1, HOTBAR_PLAYER_START, HOTBAR_PLAYER_END+1, false));
  111.                         return null;
  112.                  }else if(index>HOTBAR_PLAYER_START&&index<HOTBAR_PLAYER_END+1){
  113.                      if(!this.mergeItemStack(itemstack1, INV_PLAYER_START, INV_PLAYER_END+1, false));
  114.                         return null;
  115.                  }else{
  116.                      if(!this.mergeItemStack(itemstack1, INV_PLAYER_START, HOTBAR_PLAYER_END+1, false));
  117.                     return null;
  118.                  }
  119.              }
  120.              
  121.              if (itemstack1.stackSize == 0)
  122.                  slot.putStack((ItemStack)null);
  123.              else
  124.                  slot.onSlotChanged();
  125.    
  126.              if (itemstack1.stackSize == itemstack.stackSize)
  127.                  return null;
  128.    
  129.              slot.onPickupFromSlot(playerIn, itemstack1);
  130.          }
  131.    
  132.          return itemstack;
  133.     }
  134.    
  135.     public void onContainerClosed(EntityPlayer player) {
  136.         super.onContainerClosed(player);
  137.         this.inventoryNPC.closeInventory(player);
  138.     }
  139. }
Add Comment
Please, Sign In to add comment