| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- package me.km.inventory;
- import java.util.Arrays;
- import net.minecraft.entity.Entity;
- import net.minecraft.inventory.InventoryBasic;
- import net.minecraft.item.ItemStack;
- public class InventoryBase extends InventoryBasic
- {
- private final Entity owner;
- private final int rows;
- private final byte[] data;
- private final byte[] smallData;
-
- public InventoryBase(String title, byte[] data, int slots, Entity owner)
- {
- super(title, true, slots);
- this.owner = owner;
- this.rows = slots / 9 + (slots % 9 == 0 ? 0 : 1);
- this.data = data;
- this.smallData = deleteZeros(data);
- }
-
- private static int countZeros(String slots)
- {
- int i = 0;
- for(char c : slots.toCharArray())
- {
- if(c == '0')
- {
- i++;
- }
- }
- return i;
- }
-
- private static byte[] deleteZeros(byte[] bytes)
- {
- int size = 0;
- for(byte b : bytes)
- {
- size += (b & 3) != 0 ? 1 : 0;
- size += (b & 12) != 0 ? 1 : 0;
- size += (b & 48) != 0 ? 1 : 0;
- size += (b & 192) != 0 ? 1 : 0;
- }
- size = (size >> 2) + ((size & 3) != 0 ? 1 : 0);
-
- byte[] b = new byte[size];
- byte current;
- int index = 0;
- int pos = 0;
- for(int i = 0; i < bytes.length; i++)
- {
- for(int j = 0; j < 8; j += 2)
- {
- current = (byte) ((bytes[i] >> j) & 3);
- if(current != 0)
- {
- b[index] |= current << pos;
- pos += 2;
- if(pos == 8)
- {
- pos = 0;
- index++;
- }
- }
- }
- }
- return b;
- }
-
- public InventoryBase(String title, String slots, Entity owner)
- {
- super(title, true, Math.min(54, slots.length() - countZeros(slots)));
- this.owner = owner;
-
- int slotAmount = Math.min(54, slots.length());
- this.rows = slotAmount / 9 + (slotAmount % 9 == 0 ? 0 : 1);
-
- this.data = new byte[14];
-
- int id;
- for(int i = 0; i < slotAmount; i++)
- {
- id = Character.getNumericValue(slots.charAt(i));
- if(id > 3 || id < 0)
- {
- id = 0;
- }
- switch(i % 4)
- {
- case 0:
- data[i / 4] += id;
- break;
- case 1:
- data[i / 4] += id << 2;
- break;
- case 2:
- data[i / 4] += id << 4;
- break;
- case 3:
- data[i / 4] += id << 6;
- break;
- }
- }
- this.smallData = deleteZeros(data);
- }
-
- public InventoryBase(String title, int slots, Entity owner)
- {
- super(title, true, Math.max(9, Math.min(54, ((slots / 9) + ((slots % 9) == 0 ? 0 : 1)) * 9)));
- this.owner = owner;
- this.rows = super.getSizeInventory() / 9;
- this.data = new byte[14];
- // default is 3 to every 2 bits
- Arrays.fill(this.data, (byte) 0xFF);
- this.smallData = data;
- }
- public byte[] getData()
- {
- return data;
- }
- public Entity getOwner()
- {
- return owner;
- }
-
- public void setContents(Iterable<? extends ItemStack> stacks)
- {
- int counter = 0;
- int size = super.getSizeInventory();
- for(ItemStack stack : stacks)
- {
- this.setInventorySlotContents(counter, stack);
- counter++;
- if(counter >= size)
- {
- return;
- }
- }
- }
-
- public int getRows()
- {
- return rows;
- }
-
- public int getSlotStatus(int slot)
- {
- if(slot < 0 || slot >= this.getSizeInventory())
- {
- return 1;
- }
- int index = Math.max(0, Math.min(smallData.length, slot / 4));
- switch(slot % 4)
- {
- case 0: return smallData[index] & 3;
- case 1: return (smallData[index] >> 2) & 3;
- case 2: return (smallData[index] >> 4) & 3;
- case 3: return (smallData[index] >> 6) & 3;
- }
- return 0;
- }
-
- public int getSlotStatus(int x, int y)
- {
- int slot = y * 9 + x;
- int index = Math.max(0, Math.min(13, slot / 4));
- switch(slot % 4)
- {
- case 0: return data[index] & 3;
- case 1: return (data[index] >> 2) & 3;
- case 2: return (data[index] >> 4) & 3;
- case 3: return (data[index] >> 6) & 3;
- }
- return 0;
- }
-
- public boolean isSlotValid(int slot)
- {
- return getSlotStatus(slot) != 0;
- }
-
- public boolean isSlotValid(int x, int y)
- {
- return getSlotStatus(x, y) != 0;
- }
-
- @Override
- public ItemStack addItem(ItemStack itemstack)
- {
- ItemStack stack = itemstack.copy();
- for(int i = 0; i < this.getSizeInventory(); i++)
- {
- // ignore special fields
- if(getSlotStatus(i) != 1)
- {
- continue;
- }
- ItemStack slotStack = this.getStackInSlot(i);
-
- if(slotStack.isEmpty())
- {
- this.setInventorySlotContents(i, stack);
- this.markDirty();
- return ItemStack.EMPTY;
- }
- if(ItemStack.areItemsEqual(slotStack, stack))
- {
- int j = Math.min(this.getInventoryStackLimit(), slotStack.getMaxStackSize());
- int k = Math.min(stack.getCount(), j - slotStack.getCount());
- if (k > 0)
- {
- slotStack.grow(k);
- stack.shrink(k);
- if (stack.isEmpty())
- {
- this.markDirty();
- return ItemStack.EMPTY;
- }
- }
- }
- }
- if (stack.getCount() != stack.getCount())
- {
- this.markDirty();
- }
- return stack;
- }
- }
|