Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.rexozz.pixelengine.server.container;
- import javax.annotation.Nullable;
- import net.minecraft.entity.EntityLiving;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.EntityEquipmentSlot;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.Slot;
- import net.minecraft.item.ItemArmor;
- import net.minecraft.item.ItemStack;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- import net.rexozz.pixelengine.entity.EntityNPC;
- public class ContainerNPC extends Container{
- private EntityNPC npc;
- public IInventory inventoryNPC;
- private static final EntityEquipmentSlot[] VALID_ARMOR_SLOTS = new EntityEquipmentSlot[] {EntityEquipmentSlot.HEAD, EntityEquipmentSlot.CHEST, EntityEquipmentSlot.LEGS, EntityEquipmentSlot.FEET};
- private int
- ARMOR_START=0,
- ARMOR_END=ARMOR_START+3,
- MAIN_OFFHAND_START=ARMOR_END+1,
- MAIN_OFFHAND_END=MAIN_OFFHAND_START+1,
- INV_PLAYER_START=MAIN_OFFHAND_END+1,
- INV_PLAYER_END=INV_PLAYER_START+26,
- HOTBAR_PLAYER_START=INV_PLAYER_END+1,
- HOTBAR_PLAYER_END=HOTBAR_PLAYER_START+8;
- public ContainerNPC(IInventory playerInventory, final IInventory npcInventory, final EntityNPC npc, EntityPlayer player){
- this.npc=npc;
- this.inventoryNPC=npcInventory;
- for (int k = 0; k < 4; k++){
- final EntityEquipmentSlot entityequipmentslot = VALID_ARMOR_SLOTS[k];
- this.addSlotToContainer(new Slot(npcInventory, k, 8, 8 + k * 18){
- public int getSlotStackLimit() {
- return 1;
- }
- public boolean isItemValid(@Nullable ItemStack stack){
- if (stack == null)
- return false;
- else
- return stack.getItem().isValidArmor(stack, entityequipmentslot, npc);
- }
- @Nullable
- @SideOnly(Side.CLIENT)
- public String getSlotTexture(){
- return ItemArmor.EMPTY_SLOT_NAMES[entityequipmentslot.getIndex()];
- }
- });
- }
- this.addSlotToContainer(new Slot(npcInventory,4, 20, 0){
- public int getSlotStackLimit() {
- return 1;
- }
- public boolean isItemValid(@Nullable ItemStack stack){
- if (stack == null)
- return false;
- else
- return EntityLiving.getSlotForItemStack(stack)==EntityEquipmentSlot.OFFHAND;
- }
- });
- this.addSlotToContainer(new Slot(npcInventory,5, 38, 0));
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 9; ++j)
- this.addSlotToContainer(new Slot(playerInventory, j+i*9+9, 8 + j * 18, 84 + i * 18));
- }
- for (int k = 0; k < 9; ++k)
- this.addSlotToContainer(new Slot(playerInventory, k, 8 + k * 18, 142));
- }
- public boolean canInteractWith(EntityPlayer playerIn) {
- return this.inventoryNPC.isUseableByPlayer(playerIn)&&this.npc.isEntityAlive();
- }
- @Nullable
- public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
- ItemStack itemstack = null;
- Slot slot = (Slot)this.inventorySlots.get(index);
- if (slot != null && slot.getHasStack()){
- ItemStack itemstack1 = slot.getStack();
- itemstack = itemstack1.copy();
- EntityEquipmentSlot entityequipmentslot = EntityLiving.getSlotForItemStack(itemstack);
- if(index<INV_PLAYER_START){
- if(!this.mergeItemStack(itemstack1, INV_PLAYER_START, HOTBAR_PLAYER_END+1, true))
- return null;
- slot.onSlotChange(itemstack1, itemstack);
- }else{
- if(entityequipmentslot.getSlotType()==EntityEquipmentSlot.Type.ARMOR&&!((Slot)this.inventorySlots.get(3-entityequipmentslot.getIndex())).getHasStack()){
- int i=3-entityequipmentslot.getIndex();
- if(!this.mergeItemStack(itemstack1, i, i+1, false));
- return null;
- }else if(entityequipmentslot==EntityEquipmentSlot.OFFHAND&&!((Slot)this.inventorySlots.get(4)).getHasStack()){
- if(!this.mergeItemStack(itemstack1, 4, 5, false));
- return null;
- }else if(entityequipmentslot==EntityEquipmentSlot.MAINHAND&&!((Slot)this.inventorySlots.get(5)).getHasStack()){
- if(!this.mergeItemStack(itemstack1, 5, 6, false));
- return null;
- }else if(index>INV_PLAYER_START&&index<INV_PLAYER_END+1){
- if(!this.mergeItemStack(itemstack1, HOTBAR_PLAYER_START, HOTBAR_PLAYER_END+1, false));
- return null;
- }else if(index>HOTBAR_PLAYER_START&&index<HOTBAR_PLAYER_END+1){
- if(!this.mergeItemStack(itemstack1, INV_PLAYER_START, INV_PLAYER_END+1, false));
- return null;
- }else{
- if(!this.mergeItemStack(itemstack1, INV_PLAYER_START, HOTBAR_PLAYER_END+1, false));
- return null;
- }
- }
- if (itemstack1.stackSize == 0)
- slot.putStack((ItemStack)null);
- else
- slot.onSlotChanged();
- if (itemstack1.stackSize == itemstack.stackSize)
- return null;
- slot.onPickupFromSlot(playerIn, itemstack1);
- }
- return itemstack;
- }
- public void onContainerClosed(EntityPlayer player) {
- super.onContainerClosed(player);
- this.inventoryNPC.closeInventory(player);
- }
- }
Add Comment
Please, Sign In to add comment