package me.km.inventory; import me.km.networking.ModPacketHandler; import net.minecraft.advancements.CriteriaTriggers; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IContainerListener; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.inventory.SlotCrafting; import net.minecraft.item.ItemStack; import net.minecraft.network.play.server.SPacketSetSlot; import net.minecraft.network.play.server.SPacketWindowItems; import net.minecraft.network.play.server.SPacketWindowProperty; import net.minecraft.util.NonNullList; public abstract class CustomContainerBase extends Container { protected final EntityPlayer viewer; protected final boolean isServerSide; protected final InventoryBase inv; private final int numRows; protected CustomContainerBase(InventoryBase inv, EntityPlayer viewer) { // basic stuff this.viewer = viewer; this.isServerSide = viewer instanceof EntityPlayerMP; this.inv = inv; this.numRows = inv.getRows(); int i = (this.numRows - 4) * 18; // inventory slots int counter = 0; for(int y = 0; y < this.numRows; y++) { for(int x = 0; x < 9; x++) { if(inv.isSlotValid(x, y)) { super.addSlotToContainer(new IdSlot(inv, counter, 8 + x * 18, 18 + y * 18, x + y * 9)); counter++; } } } InventoryPlayer pInv = this.viewer.inventory; // plaver inventory slots for(int y = 0; y < 3; y++) { for(int x = 0; x < 9; x++) { super.addSlotToContainer(new Slot(pInv, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + i)); } } for(int x = 0; x < 9; x++) { super.addSlotToContainer(new Slot(pInv, x, 8 + x * 18, 161 + i)); } } public InventoryBase getInventoryBase() { return inv; } public void closeSafe() { viewer.closeScreen(); } public void openForPlayer() { if(isServerSide) { EntityPlayerMP p = (EntityPlayerMP) viewer; if(p.openContainer != p.inventoryContainer) { p.closeScreen(); } p.getNextWindowId(); p.openContainer = this; p.openContainer.windowId = p.currentWindowId; ModPacketHandler.sendCustomInventory(p, windowId, inv); p.openContainer.addListener(new IContainerListener() { @Override public void sendAllContents(Container containerToSend, NonNullList itemsList) { p.connection.sendPacket(new SPacketWindowItems(containerToSend.windowId, itemsList)); p.connection.sendPacket(new SPacketSetSlot(-1, -1, p.inventory.getItemStack())); } @Override public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack) { if(!(containerToSend.getSlot(slotInd) instanceof SlotCrafting)) { if(containerToSend == p.inventoryContainer) { CriteriaTriggers.INVENTORY_CHANGED.trigger(p, p.inventory); } if(!p.isChangingQuantityOnly) { p.connection.sendPacket(new SPacketSetSlot(containerToSend.windowId, slotInd, stack)); } } } @Override public void sendWindowProperty(Container containerIn, int varToUpdate, int newValue) { } @Override public void sendAllWindowProperties(Container containerIn, IInventory inventory) { } }); } } @Override public boolean canInteractWith(EntityPlayer p) { return this.inv.isUsableByPlayer(p); } @Override public ItemStack transferStackInSlot(EntityPlayer p, int index) { ItemStack stack = ItemStack.EMPTY; Slot slot = this.inventorySlots.get(index); int size = inv.getSizeInventory(); if(slot != null && slot.getHasStack()) { ItemStack slotStack = slot.getStack(); stack = slotStack.copy(); if(index < size) { if(!this.mergeItemStack(slotStack, size, this.inventorySlots.size(), true)) { return ItemStack.EMPTY; } } else if(!this.mergeItemStack(slotStack, 0, size, false)) { return ItemStack.EMPTY; } if(slotStack.isEmpty()) { slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } } return stack; } }