Files
Chisel/src/main/java/team/chisel/inventory/ContainerChisel.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
6.0 KiB
Java
Raw Normal View History

2015-08-18 20:40:55 -04:00
package team.chisel.inventory;
2014-12-15 00:11:48 +00:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2020-02-23 19:48:37 +01:00
import team.chisel.carving.Carving;
import team.chisel.item.chisel.ItemChisel;
import team.chisel.utils.General;
2015-08-18 20:40:55 -04:00
import com.cricketcraft.chisel.api.carving.ICarvingRegistry;
public class ContainerChisel extends Container {
public final InventoryChiselSelection inventory;
public InventoryPlayer playerInventory;
int chiselSlot;
public ItemStack chisel;
public boolean finished;
public ICarvingRegistry carving;
public ContainerChisel(InventoryPlayer inventoryplayer, InventoryChiselSelection inv) {
inventory = inv;
playerInventory = inventoryplayer;
chiselSlot = playerInventory.currentItem;
inv.container = this;
2015-01-12 19:24:29 -05:00
int top = 8, left = 62;
2015-01-12 19:24:29 -05:00
// selection slots
for (int i = 0; i < InventoryChiselSelection.normalSlots; i++) {
2015-01-12 19:24:29 -05:00
addSlotToContainer(
new SlotChiselSelection(
this,
inventory,
inventory,
i,
left + ((i % 10) * 18),
top + ((i / 10) * 18)));
}
2015-01-12 19:24:29 -05:00
// main slot
addSlotToContainer(new SlotChiselInput(this, inventory, InventoryChiselSelection.normalSlots, 24, 24));
top += 112;
left += 9;
2015-01-12 19:24:29 -05:00
// main inv
for (int i = 0; i < 27; i++) {
addSlotToContainer(new Slot(inventoryplayer, i + 9, left + ((i % 9) * 18), top + (i / 9) * 18));
}
top += 58;
2015-01-12 19:24:29 -05:00
for (int i = 0; i < 9; i++) {
addSlotToContainer(new Slot(inventoryplayer, i, left + ((i % 9) * 18), top + (i / 9) * 18));
}
chisel = inventoryplayer.getCurrentItem();
if (chisel != null && chisel.stackTagCompound != null) {
ItemStack stack = ItemStack.loadItemStackFromNBT(chisel.stackTagCompound.getCompoundTag("chiselTarget"));
inventory.setInventorySlotContents(InventoryChiselSelection.normalSlots, stack);
}
Item item = General.getItem(chisel);
carving = item instanceof ItemChisel ? ItemChisel.carving : Carving.chisel;
inventory.updateItems();
}
@Override
public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer par4EntityPlayer) {
// we need to subtract away all the other slots
int clickedSlot = par1 - inventory.getSizeInventory() - 27;
// if the player has clicked on the chisel or is trying to use a number key to force an itemstack into the slot
// the chisel is in
if (clickedSlot == chiselSlot || (par3 == 2 && par2 == chiselSlot)) return null;
return super.slotClick(par1, par2, par3, par4EntityPlayer);
}
@Override
public void onContainerClosed(EntityPlayer entityplayer) {
inventory.clearItems();
super.onContainerClosed(entityplayer);
}
@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
return inventory.isUseableByPlayer(entityplayer);
}
@Override
2015-01-07 21:39:51 -05:00
public ItemStack transferStackInSlot(EntityPlayer entity, int slotIdx) {
ItemStack itemstack = null;
Slot slot = (Slot) this.inventorySlots.get(slotIdx);
2015-01-07 21:39:51 -05:00
if (slot != null && slot.getHasStack()) {
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
2015-01-14 21:03:22 -05:00
if (slotIdx > InventoryChiselSelection.normalSlots) {
if (!this.mergeItemStack(
itemstack1,
InventoryChiselSelection.normalSlots,
InventoryChiselSelection.normalSlots + 1,
false)) {
2015-01-07 21:39:51 -05:00
return null;
}
} else {
2020-02-23 19:48:37 +01:00
entity.inventory.setItemStack(itemstack1.copy());
slot.onPickupFromSlot(entity, itemstack1);
itemstack1 = entity.inventory.getItemStack();
entity.inventory.setItemStack(null);
if (!this.mergeItemStack(
itemstack1,
InventoryChiselSelection.normalSlots + 1,
InventoryChiselSelection.normalSlots + 1 + 36,
false)) {
entity.inventory.setItemStack(itemstack1);
return null;
2015-01-07 21:39:51 -05:00
}
}
slot.onSlotChange(itemstack1, itemstack);
2015-01-07 21:39:51 -05:00
if (itemstack1.stackSize == 0) {
2020-02-23 19:48:37 +01:00
slot.putStack(null);
2015-01-07 21:39:51 -05:00
} else {
2016-03-16 20:34:00 -04:00
slot.putStack(itemstack1);
2015-01-07 21:39:51 -05:00
slot.onSlotChanged();
}
if (itemstack1.stackSize == itemstack.stackSize) {
return null;
}
if (slotIdx >= InventoryChiselSelection.normalSlots) {
2015-01-07 21:39:51 -05:00
slot.onPickupFromSlot(entity, itemstack1);
}
if (itemstack1.stackSize == 0) {
slot.putStack(null);
return null;
}
}
2015-01-07 21:39:51 -05:00
return itemstack;
}
2020-02-23 19:48:37 +01:00
/**
* Added validation of slot input Only Chiselable allowed
*
* @author Léa Gris
*/
@Override
protected boolean mergeItemStack(ItemStack par1ItemStack, int fromIndex, int toIndex, boolean reversOrder) {
return ((Slot) this.inventorySlots.get(reversOrder ? toIndex - 1 : fromIndex)).isItemValid(par1ItemStack)
&& super.mergeItemStack(par1ItemStack, fromIndex, toIndex, reversOrder);
}
public void onChiselSlotChanged() {
ItemStack stack = playerInventory.mainInventory[chiselSlot];
if (stack == null || !stack.isItemEqual(chisel)) finished = true;
if (finished) return;
General.setChiselTarget(chisel, inventory.getStackInSpecialSlot());
playerInventory.mainInventory[chiselSlot] = chisel;
}
}