ironbarrels/IronChests2/common/cpw/mods/ironchest/ContainerIronChestBase.java

119 lines
3.9 KiB
Java
Raw Normal View History

/*******************************************************************************
* Copyright (c) 2012 cpw.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
2012-08-11 07:46:49 +02:00
*
* Contributors:
* cpw - initial API and implementation
******************************************************************************/
2012-01-28 01:07:39 +01:00
package cpw.mods.ironchest;
2012-12-13 14:02:41 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
2012-01-28 01:07:39 +01:00
public class ContainerIronChestBase extends Container {
private IronChestType type;
private EntityPlayer player;
private IInventory chest;
2012-01-28 01:07:39 +01:00
public ContainerIronChestBase(IInventory playerInventory, IInventory chestInventory, IronChestType type, int xSize, int ySize)
{
chest = chestInventory;
player = ((InventoryPlayer) playerInventory).player;
this.type = type;
chestInventory.openChest();
layoutContainer(playerInventory, chestInventory, type, xSize, ySize);
}
2012-01-28 01:07:39 +01:00
@Override
public boolean canInteractWith(EntityPlayer player)
{
return chest.isUseableByPlayer(player);
}
2012-04-16 00:30:49 +02:00
@Override
public ItemStack transferStackInSlot(EntityPlayer p, int i)
2012-01-28 01:07:39 +01:00
{
ItemStack itemstack = null;
Slot slot = (Slot) inventorySlots.get(i);
if (slot != null && slot.getHasStack())
2012-01-28 01:07:39 +01:00
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (i < type.size)
{
if (!mergeItemStack(itemstack1, type.size, inventorySlots.size(), true))
{
return null;
}
}
2013-09-16 19:43:32 +02:00
else if (!type.acceptsStack(itemstack1))
{
return null;
}
else if (!mergeItemStack(itemstack1, 0, type.size, false))
{
return null;
}
if (itemstack1.stackSize == 0)
{
slot.putStack(null);
}
else
{
slot.onSlotChanged();
}
2012-01-28 01:07:39 +01:00
}
return itemstack;
2012-01-28 01:07:39 +01:00
}
2012-04-16 00:30:49 +02:00
@Override
public void onContainerClosed(EntityPlayer entityplayer)
2012-01-28 01:07:39 +01:00
{
super.onContainerClosed(entityplayer);
chest.closeChest();
2012-01-28 01:07:39 +01:00
}
protected void layoutContainer(IInventory playerInventory, IInventory chestInventory, IronChestType type, int xSize, int ySize)
2012-04-16 00:30:49 +02:00
{
2013-09-16 19:43:32 +02:00
if (type == IronChestType.DIRTCHEST9000) {
addSlotToContainer(type.makeSlot(chestInventory, 0, 12 + 4 * 18, 8 + 2 * 18));
} else {
for (int chestRow = 0; chestRow < type.getRowCount(); chestRow++)
{
2013-09-16 19:43:32 +02:00
for (int chestCol = 0; chestCol < type.getRowLength(); chestCol++)
{
addSlotToContainer(type.makeSlot(chestInventory, chestCol + chestRow * type.getRowLength(), 12 + chestCol * 18, 8 + chestRow * 18));
}
}
}
int leftCol = (xSize - 162) / 2 + 1;
for (int playerInvRow = 0; playerInvRow < 3; playerInvRow++)
{
for (int playerInvCol = 0; playerInvCol < 9; playerInvCol++)
{
addSlotToContainer(new Slot(playerInventory, playerInvCol + playerInvRow * 9 + 9, leftCol + playerInvCol * 18, ySize - (4 - playerInvRow) * 18
- 10));
}
}
2012-04-16 00:30:49 +02:00
for (int hotbarSlot = 0; hotbarSlot < 9; hotbarSlot++)
{
addSlotToContainer(new Slot(playerInventory, hotbarSlot, leftCol + hotbarSlot * 18, ySize - 24));
}
2012-04-16 00:30:49 +02:00
}
public EntityPlayer getPlayer()
2012-04-16 00:30:49 +02:00
{
return player;
2012-04-16 00:30:49 +02:00
}
2012-01-28 01:07:39 +01:00
}