123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- package me.km.inventory;
- import me.hammerle.snuviscript.code.Script;
- import me.km.networking.ModPacketHandler;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.player.ServerPlayerEntity;
- import net.minecraft.entity.player.PlayerInventory;
- import net.minecraft.inventory.container.ClickType;
- import net.minecraft.inventory.container.Container;
- import net.minecraft.inventory.container.Slot;
- import net.minecraft.item.ItemStack;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraft.util.text.TranslationTextComponent;
- public class CustomContainer extends Container
- {
- private final ModInventory inv;
- private final int numRows;
- public CustomContainer(int id, PlayerInventory pInv, ModInventory inv)
- {
- super(null, id);
- // basic stuff
- this.inv = inv;
- this.numRows = inv.getRows();
- inv.openInventory(pInv.player);
- 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))
- {
- addSlot(new Slot(inv, counter, 8 + x * 18, 18 + y * 18));
- counter++;
- }
- }
- }
- // plaver inventory slots
- for(int y = 0; y < 3; y++)
- {
- for(int x = 0; x < 9; x++)
- {
- addSlot(new Slot(pInv, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + i));
- }
- }
- for(int x = 0; x < 9; x++)
- {
- addSlot(new Slot(pInv, x, 8 + x * 18, 161 + i));
- }
- }
- public ModInventory getInventoryBase()
- {
- return inv;
- }
-
- public boolean onButtonClick(int slot, int dragType, ClickType click, PlayerEntity p)
- {
- return false;
- }
-
- public static void openForPlayer(ServerPlayerEntity p, ModInventory inv, String title, Script sc)
- {
- // taken from ServerPlayerEntity.openContainer
- if(p.isSpectator())
- {
- p.sendStatusMessage((new TranslationTextComponent("container.spectatorCantOpen")).applyTextStyle(TextFormatting.RED), true);
- return;
- }
- if(p.openContainer != p.container)
- {
- p.closeScreen();
- }
- p.getNextWindowId();
- Container container = new ServerCustomContainer(p.currentWindowId, p.inventory, inv, title, sc);
- ModPacketHandler.sendCustomInventory(p, p.currentWindowId, title, inv);
- container.addListener(p);
- p.openContainer = container;
- }
- @Override
- public boolean canInteractWith(PlayerEntity p)
- {
- return this.inv.isUsableByPlayer(p);
- }
- @Override
- public ItemStack transferStackInSlot(PlayerEntity p, int index)
- {
- // taken from ChestContainer, changed size
- 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;
- }
-
- @Override
- public boolean canMergeSlot(ItemStack stack, Slot slot)
- {
- return inv.getSlotStatus(slot.slotNumber) == 1;
- }
-
- @Override
- public boolean canDragIntoSlot(Slot slot)
- {
- return inv.getSlotStatus(slot.slotNumber) == 1;
- }
-
- @Override
- protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection)
- {
- boolean flag = false;
- int i = startIndex;
- if(reverseDirection)
- {
- i = endIndex - 1;
- }
- if(stack.isStackable())
- {
- while(!stack.isEmpty())
- {
- if(reverseDirection)
- {
- if(i < startIndex)
- {
- break;
- }
- }
- else if(i >= endIndex)
- {
- break;
- }
- if(inv.getSlotStatus(i) == 1)
- {
- Slot slot = this.inventorySlots.get(i);
- ItemStack itemstack = slot.getStack();
- if(!itemstack.isEmpty() && areItemsAndTagsEqual(stack, itemstack))
- {
- int j = itemstack.getCount() + stack.getCount();
- int maxSize = Math.min(slot.getSlotStackLimit(), stack.getMaxStackSize());
- if(j <= maxSize)
- {
- stack.setCount(0);
- itemstack.setCount(j);
- slot.onSlotChanged();
- flag = true;
- }
- else if(itemstack.getCount() < maxSize)
- {
- stack.shrink(maxSize - itemstack.getCount());
- itemstack.setCount(maxSize);
- slot.onSlotChanged();
- flag = true;
- }
- }
- }
- if(reverseDirection)
- {
- --i;
- }
- else
- {
- ++i;
- }
- }
- }
- if(!stack.isEmpty())
- {
- if(reverseDirection)
- {
- i = endIndex - 1;
- }
- else
- {
- i = startIndex;
- }
- while(true)
- {
- if(reverseDirection)
- {
- if(i < startIndex)
- {
- break;
- }
- }
- else if(i >= endIndex)
- {
- break;
- }
- if(inv.getSlotStatus(i) == 1)
- {
- Slot slot1 = this.inventorySlots.get(i);
- ItemStack itemstack1 = slot1.getStack();
- if(itemstack1.isEmpty() && slot1.isItemValid(stack))
- {
- if(stack.getCount() > slot1.getSlotStackLimit())
- {
- slot1.putStack(stack.split(slot1.getSlotStackLimit()));
- }
- else
- {
- slot1.putStack(stack.split(stack.getCount()));
- }
- slot1.onSlotChanged();
- flag = true;
- break;
- }
- }
- if(reverseDirection)
- {
- --i;
- }
- else
- {
- ++i;
- }
- }
- }
- return flag;
- }
-
- @Override
- public final ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity p)
- {
- if(slotId < 0 || slotId >= inv.getSizeInventory())
- {
- return super.slotClick(slotId, dragType, clickTypeIn, p);
- }
- switch(inv.getSlotStatus(slotId))
- {
- case 0: return ItemStack.EMPTY;
- case 1: return super.slotClick(slotId, dragType, clickTypeIn, p);
- case 2:
- case 3:
- onButtonClick(slotId, dragType, clickTypeIn, p);
- return ItemStack.EMPTY;
- }
- return ItemStack.EMPTY;
- }
- }
|