2014-05-01 01:33:29 +02:00
|
|
|
package com.pahimar.ee3.inventory;
|
|
|
|
|
|
|
|
import com.pahimar.ee3.item.ItemAlchemicalBag;
|
|
|
|
import com.pahimar.ee3.reference.Names;
|
|
|
|
import com.pahimar.ee3.util.NBTHelper;
|
2015-04-10 14:32:55 +02:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
2014-05-01 01:33:29 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
2015-03-24 00:10:46 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
2014-05-01 01:33:29 +02:00
|
|
|
import net.minecraft.inventory.Slot;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
2015-04-10 14:32:55 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2014-07-17 21:20:53 +02:00
|
|
|
public class ContainerAlchemicalBag extends ContainerEE
|
2014-05-01 01:33:29 +02:00
|
|
|
{
|
|
|
|
// Small Bag
|
|
|
|
public static final int SMALL_BAG_INVENTORY_ROWS = 4;
|
|
|
|
public static final int SMALL_BAG_INVENTORY_COLUMNS = 12;
|
|
|
|
// Medium Bag
|
|
|
|
public static final int MEDIUM_BAG_INVENTORY_ROWS = 7;
|
|
|
|
public static final int MEDIUM_BAG_INVENTORY_COLUMNS = 12;
|
|
|
|
// Large Bag
|
|
|
|
public static final int LARGE_BAG_INVENTORY_ROWS = 9;
|
|
|
|
public static final int LARGE_BAG_INVENTORY_COLUMNS = 13;
|
|
|
|
private final EntityPlayer entityPlayer;
|
2015-04-10 14:32:55 +02:00
|
|
|
public final InventoryAlchemicalBag inventoryAlchemicalBag;
|
2014-07-17 21:20:53 +02:00
|
|
|
|
2014-05-01 01:33:29 +02:00
|
|
|
private int bagInventoryRows;
|
|
|
|
private int bagInventoryColumns;
|
|
|
|
|
|
|
|
public ContainerAlchemicalBag(EntityPlayer entityPlayer, InventoryAlchemicalBag inventoryAlchemicalBag)
|
|
|
|
{
|
|
|
|
this.entityPlayer = entityPlayer;
|
|
|
|
this.inventoryAlchemicalBag = inventoryAlchemicalBag;
|
|
|
|
|
|
|
|
if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 0)
|
|
|
|
{
|
|
|
|
bagInventoryRows = SMALL_BAG_INVENTORY_ROWS;
|
|
|
|
bagInventoryColumns = SMALL_BAG_INVENTORY_COLUMNS;
|
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 1)
|
|
|
|
{
|
|
|
|
bagInventoryRows = MEDIUM_BAG_INVENTORY_ROWS;
|
|
|
|
bagInventoryColumns = MEDIUM_BAG_INVENTORY_COLUMNS;
|
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 2)
|
|
|
|
{
|
|
|
|
bagInventoryRows = LARGE_BAG_INVENTORY_ROWS;
|
|
|
|
bagInventoryColumns = LARGE_BAG_INVENTORY_COLUMNS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the Alchemical Chest slots to the container
|
|
|
|
for (int bagRowIndex = 0; bagRowIndex < bagInventoryRows; ++bagRowIndex)
|
|
|
|
{
|
|
|
|
for (int bagColumnIndex = 0; bagColumnIndex < bagInventoryColumns; ++bagColumnIndex)
|
|
|
|
{
|
|
|
|
if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 0)
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new SlotAlchemicalBag(this, inventoryAlchemicalBag, entityPlayer, bagColumnIndex + bagRowIndex * bagInventoryColumns, 8 + bagColumnIndex * 18, 18 + bagRowIndex * 18));
|
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 1)
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new SlotAlchemicalBag(this, inventoryAlchemicalBag, entityPlayer, bagColumnIndex + bagRowIndex * bagInventoryColumns, 8 + bagColumnIndex * 18, 18 + bagRowIndex * 18));
|
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 2)
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new SlotAlchemicalBag(this, inventoryAlchemicalBag, entityPlayer, bagColumnIndex + bagRowIndex * bagInventoryColumns, 8 + bagColumnIndex * 18, 8 + bagRowIndex * 18));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the player's inventory slots to the container
|
|
|
|
for (int inventoryRowIndex = 0; inventoryRowIndex < PLAYER_INVENTORY_ROWS; ++inventoryRowIndex)
|
|
|
|
{
|
|
|
|
for (int inventoryColumnIndex = 0; inventoryColumnIndex < PLAYER_INVENTORY_COLUMNS; ++inventoryColumnIndex)
|
|
|
|
{
|
|
|
|
if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 0)
|
|
|
|
{
|
2015-04-10 14:32:55 +02:00
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, inventoryColumnIndex + inventoryRowIndex * 9 + 9, 35 + inventoryColumnIndex * 18, 104 + inventoryRowIndex * 18));
|
2014-05-01 01:33:29 +02:00
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 1)
|
|
|
|
{
|
2015-04-10 14:32:55 +02:00
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, inventoryColumnIndex + inventoryRowIndex * 9 + 9, 35 + inventoryColumnIndex * 18, 158 + inventoryRowIndex * 18));
|
2014-05-01 01:33:29 +02:00
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 2)
|
|
|
|
{
|
2015-04-10 14:32:55 +02:00
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, inventoryColumnIndex + inventoryRowIndex * 9 + 9, 44 + inventoryColumnIndex * 18, 174 + inventoryRowIndex * 18));
|
2014-05-01 01:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the player's action bar slots to the container
|
|
|
|
for (int actionBarSlotIndex = 0; actionBarSlotIndex < PLAYER_INVENTORY_COLUMNS; ++actionBarSlotIndex)
|
|
|
|
{
|
|
|
|
if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 0)
|
|
|
|
{
|
2015-06-03 15:45:45 +02:00
|
|
|
if(!(entityPlayer.inventory.getStackInSlot(actionBarSlotIndex)==inventoryAlchemicalBag.parentItemStack))
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, actionBarSlotIndex, 35 + actionBarSlotIndex * 18, 162));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, actionBarSlotIndex, 35 + actionBarSlotIndex * 18, 162)
|
|
|
|
{
|
|
|
|
public boolean canTakeStack(EntityPlayer p_82869_1_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-05-01 01:33:29 +02:00
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 1)
|
|
|
|
{
|
2015-06-03 15:45:45 +02:00
|
|
|
if(!(entityPlayer.inventory.getStackInSlot(actionBarSlotIndex)==inventoryAlchemicalBag.parentItemStack))
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, actionBarSlotIndex, 35 + actionBarSlotIndex * 18, 216));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, actionBarSlotIndex, 35 + actionBarSlotIndex * 18, 216)
|
|
|
|
{
|
|
|
|
public boolean canTakeStack(EntityPlayer p_82869_1_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-05-01 01:33:29 +02:00
|
|
|
}
|
|
|
|
else if (inventoryAlchemicalBag.parentItemStack.getItemDamage() == 2)
|
|
|
|
{
|
2015-06-03 15:45:45 +02:00
|
|
|
if(!(entityPlayer.inventory.getStackInSlot(actionBarSlotIndex)==inventoryAlchemicalBag.parentItemStack))
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, actionBarSlotIndex, 44 + actionBarSlotIndex * 18, 232));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.addSlotToContainer(new Slot(entityPlayer.inventory, actionBarSlotIndex, 44 + actionBarSlotIndex * 18, 232)
|
|
|
|
{
|
|
|
|
public boolean canTakeStack(EntityPlayer p_82869_1_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-05-01 01:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContainerClosed(EntityPlayer entityPlayer)
|
|
|
|
{
|
|
|
|
super.onContainerClosed(entityPlayer);
|
|
|
|
|
|
|
|
if (!entityPlayer.worldObj.isRemote)
|
|
|
|
{
|
|
|
|
// We can probably do this better now considering the InventoryAlchemicalBag has a findParent method
|
|
|
|
InventoryPlayer invPlayer = entityPlayer.inventory;
|
|
|
|
for (ItemStack itemStack : invPlayer.mainInventory)
|
|
|
|
{
|
|
|
|
if (itemStack != null)
|
|
|
|
{
|
2016-05-27 17:10:38 +02:00
|
|
|
if (NBTHelper.hasKey(itemStack, Names.NBT.ALCHEMICAL_BAG_GUI_OPEN))
|
2014-05-01 01:33:29 +02:00
|
|
|
{
|
|
|
|
NBTHelper.removeTag(itemStack, Names.NBT.ALCHEMICAL_BAG_GUI_OPEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveInventory(entityPlayer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 03:37:55 +02:00
|
|
|
@Override
|
|
|
|
public boolean canInteractWith(EntityPlayer entityPlayer)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:32:55 +02:00
|
|
|
public boolean isItemStackParent(ItemStack itemStack)
|
|
|
|
{
|
|
|
|
if (NBTHelper.hasUUID(itemStack))
|
|
|
|
{
|
|
|
|
UUID stackUUID = new UUID(itemStack.getTagCompound().getLong(Names.NBT.UUID_MOST_SIG), itemStack.getTagCompound().getLong(Names.NBT.UUID_LEAST_SIG));
|
|
|
|
return inventoryAlchemicalBag.matchesUUID(stackUUID);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-01 01:33:29 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack transferStackInSlot(EntityPlayer entityPlayer, int slotIndex)
|
|
|
|
{
|
|
|
|
ItemStack newItemStack = null;
|
|
|
|
Slot slot = (Slot) inventorySlots.get(slotIndex);
|
|
|
|
|
|
|
|
if (slot != null && slot.getHasStack())
|
|
|
|
{
|
|
|
|
ItemStack itemStack = slot.getStack();
|
|
|
|
newItemStack = itemStack.copy();
|
|
|
|
|
|
|
|
// Attempt to shift click something from the bag inventory into the player inventory
|
|
|
|
if (slotIndex < bagInventoryRows * bagInventoryColumns)
|
|
|
|
{
|
|
|
|
if (!this.mergeItemStack(itemStack, bagInventoryRows * bagInventoryColumns, inventorySlots.size(), false))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Special case if we are dealing with an Alchemical Bag being shift clicked
|
|
|
|
else if (itemStack.getItem() instanceof ItemAlchemicalBag)
|
|
|
|
{
|
|
|
|
// Attempt to shift click a bag from the player inventory into the hot bar inventory
|
|
|
|
if (slotIndex < (bagInventoryRows * bagInventoryColumns) + (PLAYER_INVENTORY_ROWS * PLAYER_INVENTORY_COLUMNS))
|
|
|
|
{
|
|
|
|
if (!this.mergeItemStack(itemStack, (bagInventoryRows * bagInventoryColumns) + (PLAYER_INVENTORY_ROWS * PLAYER_INVENTORY_COLUMNS), inventorySlots.size(), false))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Attempt to shift click a bag from the hot bar inventory into the player inventory
|
|
|
|
else if (!this.mergeItemStack(itemStack, bagInventoryRows * bagInventoryColumns, (bagInventoryRows * bagInventoryColumns) + (PLAYER_INVENTORY_ROWS * PLAYER_INVENTORY_COLUMNS), false))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Attempt to shift click a non-Alchemical Bag into the bag inventory
|
|
|
|
else if (!this.mergeItemStack(itemStack, 0, bagInventoryRows * bagInventoryColumns, false))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (itemStack.stackSize == 0)
|
|
|
|
{
|
|
|
|
slot.putStack(null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
slot.onSlotChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newItemStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveInventory(EntityPlayer entityPlayer)
|
|
|
|
{
|
|
|
|
inventoryAlchemicalBag.onGuiSaved(entityPlayer);
|
|
|
|
}
|
2015-03-24 00:10:46 +01:00
|
|
|
|
|
|
|
private class SlotAlchemicalBag extends Slot
|
|
|
|
{
|
|
|
|
private final EntityPlayer entityPlayer;
|
|
|
|
private ContainerAlchemicalBag containerAlchemicalBag;
|
|
|
|
|
|
|
|
public SlotAlchemicalBag(ContainerAlchemicalBag containerAlchemicalBag, IInventory inventory, EntityPlayer entityPlayer, int slotIndex, int x, int y)
|
|
|
|
{
|
|
|
|
super(inventory, slotIndex, x, y);
|
|
|
|
this.entityPlayer = entityPlayer;
|
|
|
|
this.containerAlchemicalBag = containerAlchemicalBag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-10 14:32:55 +02:00
|
|
|
public void onSlotChanged()
|
2015-03-24 00:10:46 +01:00
|
|
|
{
|
2015-04-10 14:32:55 +02:00
|
|
|
super.onSlotChanged();
|
2015-04-10 03:25:59 +02:00
|
|
|
|
2015-04-10 14:32:55 +02:00
|
|
|
if (FMLCommonHandler.instance().getEffectiveSide().isServer())
|
|
|
|
{
|
|
|
|
containerAlchemicalBag.saveInventory(entityPlayer);
|
|
|
|
}
|
2015-03-24 00:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean isItemValid(ItemStack itemStack)
|
|
|
|
{
|
|
|
|
return !(itemStack.getItem() instanceof ItemAlchemicalBag);
|
|
|
|
}
|
|
|
|
}
|
2014-05-01 01:33:29 +02:00
|
|
|
}
|