1
0
Fork 0

improve inventory code

This commit is contained in:
Sen 2025-09-07 11:51:01 +02:00
parent c374f35393
commit cfcb590574
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
25 changed files with 144 additions and 184 deletions

View file

@ -11,13 +11,9 @@ public class GuiChest extends GuiContainer {
public GuiChest(EntityNPC player, IInventory chest, Block block) { public GuiChest(EntityNPC player, IInventory chest, Block block) {
super(new ContainerChest(player, chest)); super(new ContainerChest(player, chest));
this.block = block; this.block = block;
if(((ContainerChest)this.inventorySlots).getWidth() > 12)
this.xSize += (((ContainerChest)this.inventorySlots).getWidth() - 12) * 18;
this.ySize = 37 + 18 * ((player.getInventoryCapacity() + 11) / 12) + ((ContainerChest)this.inventorySlots).getHeight() * 18;
} }
public void addElements() { public void addElements() {
this.label(this.block.getDisplay(), 8, 16); this.label(this.block.getDisplay(), 8, 16);
this.label("Inventar", 8, 37 - 8 + ((ContainerChest)this.inventorySlots).getHeight() * 18);
} }
} }

View file

@ -29,9 +29,9 @@ import client.window.Button;
import common.collect.Lists; import common.collect.Lists;
import common.enchantment.Enchantment; import common.enchantment.Enchantment;
import common.entity.npc.Attribute; import common.entity.npc.Attribute;
import common.entity.npc.EntityNPC;
import common.init.ItemRegistry; import common.init.ItemRegistry;
import common.inventory.Container; import common.inventory.Container;
import common.inventory.ContainerChest;
import common.inventory.Slot; import common.inventory.Slot;
import common.item.CheatTab; import common.item.CheatTab;
import common.item.ItemStack; import common.item.ItemStack;
@ -40,6 +40,7 @@ import common.packet.CPacketCheat;
import common.util.ExtMath; import common.util.ExtMath;
import common.util.Util; import common.util.Util;
import common.util.Color; import common.util.Color;
import common.util.Equipment;
public abstract class GuiContainer extends Gui public abstract class GuiContainer extends Gui
{ {
@ -63,8 +64,8 @@ public abstract class GuiContainer extends Gui
private static CheatTab selectedTab = CheatTab.ALL; private static CheatTab selectedTab = CheatTab.ALL;
protected RenderItem itemRender; protected RenderItem itemRender;
protected int xSize = 14 + 18 * 12; protected final int xSize;
protected int ySize = 166; protected final int ySize;
public Container inventorySlots; public Container inventorySlots;
private Slot theSlot; private Slot theSlot;
protected final List<Overlay> drawnOverlays = Lists.<Overlay>newArrayList(); protected final List<Overlay> drawnOverlays = Lists.<Overlay>newArrayList();
@ -259,12 +260,8 @@ public abstract class GuiContainer extends Gui
{ {
this.inventorySlots = container; this.inventorySlots = container;
this.ignoreMouseUp = true; this.ignoreMouseUp = true;
} this.xSize = (container.getInventoryOffsetX() - 1) * 2 + 18 * 12;
this.ySize = container.getInventoryOffsetY() + 6 + 18 * ((Equipment.INVENTORY_SLOTS + 11) / 12);
public GuiContainer(Container container, EntityNPC player)
{
this(container);
this.ySize = 90 + 18 * ((player.getInventoryCapacity() + 11) / 12);
} }
public void init(int width, int height) { public void init(int width, int height) {
@ -277,6 +274,7 @@ public abstract class GuiContainer extends Gui
this.initGui(); this.initGui();
this.addButtons(); this.addButtons();
this.addElements(); this.addElements();
this.label("Inventar", this.inventorySlots.getInventoryOffsetX(), this.inventorySlots.getInventoryOffsetY() - 2);
} }
public void hover(String text, int x, int y) { public void hover(String text, int x, int y) {

View file

@ -12,11 +12,9 @@ public class GuiCrafting extends GuiContainer {
public GuiCrafting(EntityNPC inv, World world, BlockWorkbench type) { public GuiCrafting(EntityNPC inv, World world, BlockWorkbench type) {
super(new ContainerWorkbench(inv, world, LocalPos.ORIGIN, type)); super(new ContainerWorkbench(inv, world, LocalPos.ORIGIN, type));
this.type = type; this.type = type;
this.ySize = 36 + 18 * ((inv.getInventoryCapacity() + 11) / 12) + 18 * 3;
} }
public void addElements() { public void addElements() {
this.label(this.type.getDisplay(), 8, 16); this.label(this.type.getDisplay(), 8, 16);
this.label("Inventar", 8, 36 - 8 + 18 * 3);
} }
} }

View file

@ -24,7 +24,6 @@ public class GuiDevice extends GuiContainer {
super(new ContainerTile(player, tile, inv)); super(new ContainerTile(player, tile, inv));
this.playerInv = player; this.playerInv = player;
this.tileInv = tile; this.tileInv = tile;
this.ySize = 118 + 18 * ((player.getInventoryCapacity() + 11) / 12);
this.tile = tile; this.tile = tile;
} }
@ -49,7 +48,6 @@ public class GuiDevice extends GuiContainer {
public void addElements() { public void addElements() {
this.header = this.label("", 8, 16); this.header = this.label("", 8, 16);
this.label("Inventar", 8, 118 - 8);
this.desc = this.display("", 8, 18, 160, 4); this.desc = this.display("", 8, 18, 160, 4);
this.progress = this.tile.hasProgress() ? this.bar(7, 70, 162, 9) : null; this.progress = this.tile.hasProgress() ? this.bar(7, 70, 162, 9) : null;
this.temperature = this.tile.hasTemperature() ? this.bar(7, this.progress == null ? 70 : 60, 162, 9) : null; this.temperature = this.tile.hasTemperature() ? this.bar(7, this.progress == null ? 70 : 60, 162, 9) : null;

View file

@ -26,7 +26,7 @@ public class GuiEnchant extends GuiContainer implements ButtonCallback {
private final Label[] mana = new Label[3]; private final Label[] mana = new Label[3];
public GuiEnchant(EntityNPC inv, World world) { public GuiEnchant(EntityNPC inv, World world) {
super(new ContainerEnchantment(inv, world), inv); super(new ContainerEnchantment(inv, world));
this.enchantment = (ContainerEnchantment)this.inventorySlots; this.enchantment = (ContainerEnchantment)this.inventorySlots;
} }
@ -60,7 +60,6 @@ public class GuiEnchant extends GuiContainer implements ButtonCallback {
public void addElements() { public void addElements() {
this.label(Blocks.enchanting_table.getDisplay(), 8, 16); this.label(Blocks.enchanting_table.getDisplay(), 8, 16);
this.label("Inventar", 8, 90 - 8);
for(int l = 0; l < 3; ++l) { for(int l = 0; l < 3; ++l) {
int i1 = 60; int i1 = 60;
int j1 = i1 + 2; int j1 = i1 + 2;

View file

@ -9,12 +9,11 @@ public class GuiEntity extends GuiContainer {
private final String title; private final String title;
public GuiEntity(EntityNPC player, IInventory entityInv, Entity entity) { public GuiEntity(EntityNPC player, IInventory entityInv, Entity entity) {
super(new ContainerEntityInventory(player, entityInv, entity), player); super(new ContainerEntityInventory(player, entityInv, entity));
this.title = entity.getName(); this.title = entity.getName();
} }
public void addElements() { public void addElements() {
this.label(this.title, 8, 16); this.label(this.title, 8, 16);
this.label("Inventar", 8, 90 - 8);
} }
} }

View file

@ -4,7 +4,7 @@ import common.entity.npc.EntityNPC;
public class GuiInventory extends GuiContainer { public class GuiInventory extends GuiContainer {
public GuiInventory(EntityNPC player) { public GuiInventory(EntityNPC player) {
super(player.inventoryContainer, player); super(player.inventoryContainer);
} }
public void addElements() { public void addElements() {

View file

@ -24,7 +24,7 @@ public class GuiMerchant extends GuiContainer implements ButtonCallback {
private ActButton nextBtn; private ActButton nextBtn;
public GuiMerchant(EntityNPC inv, Entity entity, World world) { public GuiMerchant(EntityNPC inv, Entity entity, World world) {
super(new ContainerMerchant(inv, null, world), inv); super(new ContainerMerchant(inv, null, world));
this.title = entity.getName(); this.title = entity.getName();
} }
@ -41,7 +41,6 @@ public class GuiMerchant extends GuiContainer implements ButtonCallback {
public void addElements() { public void addElements() {
this.label(this.title, 8, 16); this.label(this.title, 8, 16);
this.label("Inventar", 8, 90 - 8);
} }
public void drawOverlays() { public void drawOverlays() {

View file

@ -12,7 +12,7 @@ public class GuiRepair extends GuiContainer {
private EntityNPC playerInv; private EntityNPC playerInv;
public GuiRepair(EntityNPC inv, World world) { public GuiRepair(EntityNPC inv, World world) {
super(new ContainerRepair(inv, world), inv); super(new ContainerRepair(inv, world));
this.playerInv = inv; this.playerInv = inv;
this.anvil = (ContainerRepair)this.inventorySlots; this.anvil = (ContainerRepair)this.inventorySlots;
} }
@ -32,6 +32,5 @@ public class GuiRepair extends GuiContainer {
public void addElements() { public void addElements() {
this.label("Amboss", 8, 16); this.label("Amboss", 8, 16);
this.info = this.label("", 60, 77); this.info = this.label("", 60, 77);
this.label("Inventar", 8, 90 - 8);
} }
} }

View file

@ -1132,7 +1132,7 @@ public class ClientPlayer implements IClientPlayer
// flag = true; // guicontainercreative.getSelectedTabIndex() != CheatTab.tabInventory.getIndex(); // flag = true; // guicontainercreative.getSelectedTabIndex() != CheatTab.tabInventory.getIndex();
// } // }
if (packetIn.getWindowId() == 0 && packetIn.getSlot() >= Equipment.ARMOR_SLOTS && packetIn.getSlot() < Equipment.ARMOR_SLOTS + entityplayer.getInventoryCapacity()) if (packetIn.getWindowId() == 0 && packetIn.getSlot() >= Equipment.ARMOR_SLOTS && packetIn.getSlot() < Equipment.ARMOR_SLOTS + Equipment.INVENTORY_SLOTS)
{ {
ItemStack itemstack = entityplayer.inventoryContainer.getSlot(packetIn.getSlot()).getStack(); ItemStack itemstack = entityplayer.inventoryContainer.getSlot(packetIn.getSlot()).getStack();

View file

@ -5,6 +5,7 @@ import common.entity.npc.EntityNPC;
import common.init.Items; import common.init.Items;
import common.item.Item; import common.item.Item;
import common.item.ItemStack; import common.item.ItemStack;
import common.util.Equipment;
import common.util.ExtMath; import common.util.ExtMath;
public class EntityAIShareItems extends EntityAIWatchClosest2 public class EntityAIShareItems extends EntityAIWatchClosest2
@ -48,7 +49,7 @@ public class EntityAIShareItems extends EntityAIWatchClosest2
if (this.interactionDelay == 0) if (this.interactionDelay == 0)
{ {
for (int i = 0; i < this.entity.getInventoryCapacity(); ++i) for (int i = 0; i < Equipment.INVENTORY_SLOTS; ++i)
{ {
ItemStack itemstack = this.entity.getStackInSlot(i); ItemStack itemstack = this.entity.getStackInSlot(i);
ItemStack itemstack1 = null; ItemStack itemstack1 = null;

View file

@ -203,7 +203,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
private ItemStack mouseItem; private ItemStack mouseItem;
private final ItemStack[] armor = new ItemStack[Equipment.ARMOR_SLOTS]; private final ItemStack[] armor = new ItemStack[Equipment.ARMOR_SLOTS];
private final ItemStack[] prevArmor = new ItemStack[this.armor.length]; private final ItemStack[] prevArmor = new ItemStack[this.armor.length];
private final ItemStack[] items = new ItemStack[this.getInventoryCapacity()]; private final ItemStack[] items = new ItemStack[Equipment.INVENTORY_SLOTS];
private final ItemStack[] prevItems = new ItemStack[this.items.length]; private final ItemStack[] prevItems = new ItemStack[this.items.length];
private int inLove; private int inLove;
protected Alignment alignment = Alignment.NEUTRAL; protected Alignment alignment = Alignment.NEUTRAL;
@ -1430,7 +1430,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
this.prevHeldItem = held == null ? null : held.copy(); this.prevHeldItem = held == null ? null : held.copy();
} }
for (int index = 0; index < this.getInventoryCapacity(); index++) for (int index = 0; index < Equipment.INVENTORY_SLOTS; index++)
{ {
ItemStack last = this.prevItems[index]; ItemStack last = this.prevItems[index];
ItemStack current = this.getStackInSlot(index); ItemStack current = this.getStackInSlot(index);
@ -2485,7 +2485,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
} }
public int getHotbarSize() { public int getHotbarSize() {
return this.getInventoryCapacity(); return Equipment.INVENTORY_SLOTS;
} }
public int getSelectedIndex() { public int getSelectedIndex() {
@ -2556,7 +2556,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
public int getInventorySlotContainItem(Item itemIn) public int getInventorySlotContainItem(Item itemIn)
{ {
for (int i = 0; i < this.getInventoryCapacity(); ++i) for (int i = 0; i < Equipment.INVENTORY_SLOTS; ++i)
{ {
if (this.getInventory()[i] != null && this.getInventory()[i].getItem() == itemIn) if (this.getInventory()[i] != null && this.getInventory()[i].getItem() == itemIn)
{ {
@ -2569,7 +2569,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
private int storeItemStack(ItemStack itemStackIn) private int storeItemStack(ItemStack itemStackIn)
{ {
for (int i = 0; i < this.getInventoryCapacity(); ++i) for (int i = 0; i < Equipment.INVENTORY_SLOTS; ++i)
{ {
if (this.getInventory()[i] != null && this.getInventory()[i].getItem() == itemStackIn.getItem() && this.getInventory()[i].isStackable() && !this.getInventory()[i].isFull() && ItemStack.dataEquals(this.getInventory()[i], itemStackIn)) if (this.getInventory()[i] != null && this.getInventory()[i].getItem() == itemStackIn.getItem() && this.getInventory()[i].isStackable() && !this.getInventory()[i].isFull() && ItemStack.dataEquals(this.getInventory()[i], itemStackIn))
{ {
@ -2582,7 +2582,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
public int getFirstEmptyStack() public int getFirstEmptyStack()
{ {
for (int i = 0; i < this.getInventoryCapacity(); ++i) for (int i = 0; i < Equipment.INVENTORY_SLOTS; ++i)
{ {
if (this.getInventory()[i] == null) if (this.getInventory()[i] == null)
{ {
@ -2708,7 +2708,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
public ItemStack addStack(ItemStack stack) public ItemStack addStack(ItemStack stack)
{ {
ItemStack itemstack = stack.copy(); ItemStack itemstack = stack.copy();
for (int i = 0; i < this.getInventoryCapacity(); ++i) for (int i = 0; i < Equipment.INVENTORY_SLOTS; ++i)
{ {
ItemStack itemstack1 = this.getInventory()[i]; ItemStack itemstack1 = this.getInventory()[i];
@ -2746,7 +2746,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
} }
} }
for (int j = 0; j < this.getInventoryCapacity(); ++j) for (int j = 0; j < Equipment.INVENTORY_SLOTS; ++j)
{ {
if (this.getInventory()[j] != null && this.getInventory()[j].itemEquals(itemStackIn)) if (this.getInventory()[j] != null && this.getInventory()[j].itemEquals(itemStackIn))
{ {
@ -3300,7 +3300,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
List<TagObject> list = tag.getList("items"); List<TagObject> list = tag.getList("items");
this.clear(); this.clear();
for(int z = 0; z < list.size() && z < this.getInventoryCapacity(); z++) { for(int z = 0; z < list.size() && z < Equipment.INVENTORY_SLOTS; z++) {
TagObject item = list.get(z); TagObject item = list.get(z);
ItemStack stack = ItemStack.readFromTag(item); ItemStack stack = ItemStack.readFromTag(item);
if(stack != null) if(stack != null)
@ -3425,7 +3425,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
// tagCompound.setBoolean("CanPickUpLoot", this.canPickUpLoot()); // tagCompound.setBoolean("CanPickUpLoot", this.canPickUpLoot());
List<TagObject> list = Lists.newArrayList(); List<TagObject> list = Lists.newArrayList();
for(int z = 0; z < this.getInventoryCapacity(); z++) { for(int z = 0; z < Equipment.INVENTORY_SLOTS; z++) {
if(this.getStackInSlot(z) != null) { if(this.getStackInSlot(z) != null) {
TagObject item = new TagObject(); TagObject item = new TagObject();
this.getStackInSlot(z).writeTags(item); this.getStackInSlot(z).writeTags(item);
@ -4505,10 +4505,10 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
{ {
ItemStack[] aitemstack = this.getInventory(); ItemStack[] aitemstack = this.getInventory();
if (index >= this.getInventoryCapacity()) if (index >= Equipment.INVENTORY_SLOTS)
{ {
aitemstack = this.getArmor(); aitemstack = this.getArmor();
index -= this.getInventoryCapacity(); index -= Equipment.INVENTORY_SLOTS;
} }
if (aitemstack[index] != null) if (aitemstack[index] != null)
@ -4541,10 +4541,10 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
{ {
ItemStack[] aitemstack = this.getInventory(); ItemStack[] aitemstack = this.getInventory();
if (index >= this.getInventoryCapacity()) if (index >= Equipment.INVENTORY_SLOTS)
{ {
aitemstack = this.getArmor(); aitemstack = this.getArmor();
index -= this.getInventoryCapacity(); index -= Equipment.INVENTORY_SLOTS;
} }
if (aitemstack[index] != null) if (aitemstack[index] != null)
@ -4574,7 +4574,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
public int getSizeInventory() public int getSizeInventory()
{ {
return this.getInventoryCapacity() + this.getArmor().length; return Equipment.INVENTORY_SLOTS + this.getArmor().length;
} }
public ItemStack getStackInSlot(int index) public ItemStack getStackInSlot(int index)
@ -4597,7 +4597,7 @@ public abstract class EntityNPC extends EntityLiving implements IInventory
public void clear() public void clear()
{ {
for (int i = 0; i < this.getInventoryCapacity(); ++i) for (int i = 0; i < Equipment.INVENTORY_SLOTS; ++i)
{ {
this.getInventory()[i] = null; this.getInventory()[i] = null;
} }

View file

@ -305,7 +305,7 @@ public abstract class CraftingRegistry
if(input != null) { if(input != null) {
for(ItemStack stack : recipe.getRequiredItems()) { for(ItemStack stack : recipe.getRequiredItems()) {
int amount = stack.getSize(); int amount = stack.getSize();
int size = input instanceof EntityNPC entity ? entity.getInventoryCapacity() : input.getSizeInventory(); int size = input instanceof EntityNPC entity ? Equipment.INVENTORY_SLOTS : input.getSizeInventory();
for(int z = 0; z < size && amount > 0; z++) { for(int z = 0; z < size && amount > 0; z++) {
ItemStack current = input.getStackInSlot(z); ItemStack current = input.getStackInSlot(z);
if(current != null && stack.itemEquals(current) && stack.dataEquals(current)) { if(current != null && stack.itemEquals(current) && stack.dataEquals(current)) {
@ -321,7 +321,7 @@ public abstract class CraftingRegistry
if(output != null && (!allowDrop || !(output instanceof EntityNPC))) { if(output != null && (!allowDrop || !(output instanceof EntityNPC))) {
for(ItemStack stack : recipe.getCraftedItems()) { for(ItemStack stack : recipe.getCraftedItems()) {
int amount = stack.getSize(); int amount = stack.getSize();
int size = output instanceof EntityNPC entity ? entity.getInventoryCapacity() : output.getSizeInventory(); int size = output instanceof EntityNPC entity ? Equipment.INVENTORY_SLOTS : output.getSizeInventory();
for(int z = 0; z < size && amount > 0; z++) { for(int z = 0; z < size && amount > 0; z++) {
ItemStack current = output.getStackInSlot(z); ItemStack current = output.getStackInSlot(z);
if(current == null) { if(current == null) {
@ -369,7 +369,7 @@ public abstract class CraftingRegistry
private static void swapItems(IInventory input, IInventory output, IRecipe recipe) { private static void swapItems(IInventory input, IInventory output, IRecipe recipe) {
for(ItemStack stack : recipe.getRequiredItems()) { for(ItemStack stack : recipe.getRequiredItems()) {
int amount = stack.getSize(); int amount = stack.getSize();
int size = input instanceof EntityNPC entity ? entity.getInventoryCapacity() : input.getSizeInventory(); int size = input instanceof EntityNPC entity ? Equipment.INVENTORY_SLOTS : input.getSizeInventory();
for(int z = 0; z < size && amount > 0; z++) { for(int z = 0; z < size && amount > 0; z++) {
ItemStack current = input.getStackInSlot(z); ItemStack current = input.getStackInSlot(z);
if(current != null && stack.itemEquals(current) && stack.dataEquals(current)) { if(current != null && stack.itemEquals(current) && stack.dataEquals(current)) {
@ -383,7 +383,7 @@ public abstract class CraftingRegistry
} }
for(ItemStack stack : recipe.getCraftedItems()) { for(ItemStack stack : recipe.getCraftedItems()) {
int amount = stack.getSize(); int amount = stack.getSize();
int size = output instanceof EntityNPC entity ? entity.getInventoryCapacity() : output.getSizeInventory(); int size = output instanceof EntityNPC entity ? Equipment.INVENTORY_SLOTS : output.getSizeInventory();
for(int z = 0; z < size && amount > 0; z++) { for(int z = 0; z < size && amount > 0; z++) {
ItemStack current = output.getStackInSlot(z); ItemStack current = output.getStackInSlot(z);
if(current == null) { if(current == null) {

View file

@ -9,6 +9,7 @@ import common.entity.npc.EntityNPC;
import common.item.Item; import common.item.Item;
import common.item.ItemStack; import common.item.ItemStack;
import common.network.IPlayer; import common.network.IPlayer;
import common.util.Equipment;
public abstract class Container public abstract class Container
{ {
@ -16,12 +17,32 @@ public abstract class Container
public List<Slot> inventorySlots = Lists.<Slot>newArrayList(); public List<Slot> inventorySlots = Lists.<Slot>newArrayList();
public int windowId; public int windowId;
private short transactionID; private short transactionID;
protected int offset;
protected List<IPlayer> crafters = Lists.<IPlayer>newArrayList(); protected List<IPlayer> crafters = Lists.<IPlayer>newArrayList();
private Set<EntityNPC> playerList = Sets.<EntityNPC>newHashSet(); private Set<EntityNPC> playerList = Sets.<EntityNPC>newHashSet();
/** public int getInventoryOffsetX() {
* Adds an item slot to this container return 8;
*/ }
public int getInventoryOffsetY() {
return 84;
}
public final int getPlayerInventoryOffset() {
return this.offset;
}
protected void addPlayerSlots(EntityNPC player) {
this.offset = this.inventorySlots.size();
int x = this.getInventoryOffsetX();
int y = this.getInventoryOffsetY();
List<SlotCommon> list = Lists.newArrayList();
for(int z = 0; z < Equipment.INVENTORY_SLOTS; ++z) {
this.addSlotToContainer(new SlotCommon(list, player, z, x + (z % 12) * 18, y + (z / 12) * 18));
}
}
protected Slot addSlotToContainer(Slot slotIn) protected Slot addSlotToContainer(Slot slotIn)
{ {
slotIn.slotNumber = this.inventorySlots.size(); slotIn.slotNumber = this.inventorySlots.size();
@ -39,9 +60,7 @@ public abstract class Container
else else
{ {
this.crafters.add(listener); this.crafters.add(listener);
EntityNPC entity = listener.getPresentEntity(); this.resortStacks();
if(entity != null)
this.resortStacks(entity);
listener.sendContainer(this, this.getInventory()); listener.sendContainer(this, this.getInventory());
this.detectAndSendChanges(); this.detectAndSendChanges();
} }
@ -400,7 +419,7 @@ public abstract class Container
this.detectAndSendChanges(); this.detectAndSendChanges();
} }
this.resortStacks(playerIn); this.resortStacks();
return itemstack; return itemstack;
} }
@ -420,13 +439,50 @@ public abstract class Container
} }
} }
public void resortStacks(EntityNPC player) { private void removeEmptySlots(int offset, int size) {
if(this.canMergeStacks()) int lastFull = Integer.MIN_VALUE;
this.reorganize(this.getMergeOffset(), this.getMergeSize()); int firstEmpty = Integer.MAX_VALUE;
this.reorganize(this.getPlayerInventoryOffset(), player.getInventoryCapacity()); for(int z = 0; z < size; z++) {
ItemStack stack = this.inventorySlots.get(offset + z).getStack();
if(stack == null && z < firstEmpty)
firstEmpty = z;
else if(stack != null && z > lastFull)
lastFull = z;
}
if(firstEmpty > lastFull)
return;
for(int z = 0; z < size; z++) {
ItemStack stack = this.inventorySlots.get(offset + z).getStack();
if(stack == null) {
int shift = -1;
for(int n = 1; z + n < size; n++) {
ItemStack next = this.inventorySlots.get(offset + z + n).getStack();
if(next == null) {
if(shift >= 0)
break;
}
else {
if(shift < 0)
shift = n;
this.inventorySlots.get(offset + z + n).putStack(null);
this.inventorySlots.get(offset + z + n - shift).putStack(next);
}
}
}
}
} }
protected abstract int getPlayerInventoryOffset(); public void resortStacks() {
if(this.canMergeStacks())
this.reorganize(this.getMergeOffset(), this.getMergeSize());
this.reorganize(this.offset, Equipment.INVENTORY_SLOTS);
}
public void clearStacks() {
if(this.canMergeStacks())
this.removeEmptySlots(this.getMergeOffset(), this.getMergeSize());
this.removeEmptySlots(this.offset, Equipment.INVENTORY_SLOTS);
}
protected boolean canMergeStacks() { protected boolean canMergeStacks() {
return false; return false;
@ -610,37 +666,4 @@ public abstract class Container
return flag; return flag;
} }
public static void removeEmptySlots(IInventory inv, int offset, int size) {
int lastFull = Integer.MIN_VALUE;
int firstEmpty = Integer.MAX_VALUE;
for(int z = 0; z < size; z++) {
ItemStack stack = inv.getStackInSlot(offset + z);
if(stack == null && z < firstEmpty)
firstEmpty = z;
else if(stack != null && z > lastFull)
lastFull = z;
}
if(firstEmpty > lastFull)
return;
for(int z = 0; z < size; z++) {
ItemStack stack = inv.getStackInSlot(offset + z);
if(stack == null) {
int shift = -1;
for(int n = 1; z + n < size; n++) {
ItemStack next = inv.getStackInSlot(offset + z + n);
if(next == null) {
if(shift >= 0)
break;
}
else {
if(shift < 0)
shift = n;
inv.setInventorySlotContents(offset + z + n, null);
inv.setInventorySlotContents(offset + z + n - shift, next);
}
}
}
}
}
} }

View file

@ -22,28 +22,26 @@ public class ContainerChest extends Container
BlockChest block = BlockChest.getChest(this.chestSize); BlockChest block = BlockChest.getChest(this.chestSize);
this.width = block.getInventoryWidth(); this.width = block.getInventoryWidth();
this.height = block.getInventoryHeight(); this.height = block.getInventoryHeight();
int xOffset = this.width < 12 ? 0 : (this.width - 12) * 18 / 2;
int yOffset = (this.height - 3) * 18;
List<SlotCommon> list1 = Lists.newArrayList(); List<SlotCommon> list = Lists.newArrayList();
for (int j = 0; j < this.height; ++j) for (int j = 0; j < this.height; ++j)
{ {
for (int k = 0; k < this.width; ++k) for (int k = 0; k < this.width; ++k)
{ {
this.addSlotToContainer(new SlotCommon(list1, chest, k + j * this.width, 8 + k * 18, 18 + j * 18)); this.addSlotToContainer(new SlotCommon(list, chest, k + j * this.width, 8 + k * 18, 18 + j * 18));
} }
} }
List<SlotCommon> list2 = Lists.newArrayList(); this.addPlayerSlots(player);
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new SlotCommon(list2, player, l, 8 + (l % 12) * 18 + xOffset, 85 + (l / 12) * 18 + yOffset));
}
}
protected int getPlayerInventoryOffset() {
return this.chestSize;
} }
public int getInventoryOffsetX() {
return 8 + (this.width < 12 ? 0 : (this.width - 12) * 18 / 2);
}
public int getInventoryOffsetY() {
return 85 + (this.height - 3) * 18;
}
protected boolean canMergeStacks() { protected boolean canMergeStacks() {
return true; return true;

View file

@ -13,6 +13,7 @@ import common.item.ItemStack;
import common.item.material.ItemEnchantedBook; import common.item.material.ItemEnchantedBook;
import common.network.IPlayer; import common.network.IPlayer;
import common.rng.Random; import common.rng.Random;
import common.util.Equipment;
import common.util.LocalPos; import common.util.LocalPos;
import common.util.Pair; import common.util.Pair;
import common.world.World; import common.world.World;
@ -64,15 +65,7 @@ public class ContainerEnchantment extends Container
} }
}); });
List<SlotCommon> list = Lists.newArrayList(); this.addPlayerSlots(playerInv);
for (int l = 0; l < playerInv.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new SlotCommon(list, playerInv, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}
protected int getPlayerInventoryOffset() {
return 1;
} }
public static int encodeData(Pair<Enchantment, Integer> data) { public static int encodeData(Pair<Enchantment, Integer> data) {

View file

@ -8,6 +8,7 @@ import common.entity.animal.EntityHorse;
import common.entity.npc.EntityNPC; import common.entity.npc.EntityNPC;
import common.init.Items; import common.init.Items;
import common.item.ItemStack; import common.item.ItemStack;
import common.util.Equipment;
public class ContainerEntityInventory extends Container public class ContainerEntityInventory extends Container
{ {
@ -51,19 +52,7 @@ public class ContainerEntityInventory extends Container
} }
} }
List<SlotCommon> list = Lists.newArrayList(); this.addPlayerSlots(player);
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}
protected int getPlayerInventoryOffset() {
return this.entity instanceof EntityHorse horse && horse.isChested() ? (horse.isChested() ? this.entityInventory.getSizeInventory() : 2) : 0;
}
public IInventory getEntityInventory() {
return this.entity instanceof EntityHorse horse && horse.isChested() ? this.entityInventory : null;
} }
protected boolean canMergeStacks() { protected boolean canMergeStacks() {

View file

@ -6,6 +6,7 @@ import common.collect.Lists;
import common.entity.npc.EntityNPC; import common.entity.npc.EntityNPC;
import common.item.ItemStack; import common.item.ItemStack;
import common.network.IPlayer; import common.network.IPlayer;
import common.util.Equipment;
import common.village.MerchantRecipe; import common.village.MerchantRecipe;
import common.world.World; import common.world.World;
@ -149,15 +150,7 @@ public class ContainerMerchant extends Container
}); });
this.addSlotToContainer(new SlotMerchantResult(playerInventory, this.merchantInventory, 2, 120, 53)); this.addSlotToContainer(new SlotMerchantResult(playerInventory, this.merchantInventory, 2, 120, 53));
List<SlotCommon> list = Lists.newArrayList(); this.addPlayerSlots(playerInventory);
for (int l = 0; l < playerInventory.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new SlotCommon(list, playerInventory, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}
protected int getPlayerInventoryOffset() {
return 3;
} }
public InventoryMerchant getMerchantInventory() public InventoryMerchant getMerchantInventory()

View file

@ -18,7 +18,7 @@ public class ContainerPlayer extends Container {
for (Equipment slot : Equipment.ARMOR) for (Equipment slot : Equipment.ARMOR)
{ {
final Equipment type = slot; final Equipment type = slot;
this.addSlotToContainer(new Slot(player, player.getInventoryCapacity() + slot.getIndex(), 8 + (slot.getIndex() / 4) * 18, 8 + (slot.getIndex() % 4) * 18) this.addSlotToContainer(new Slot(player, Equipment.INVENTORY_SLOTS + slot.getIndex(), 8 + (slot.getIndex() / 4) * 18, 8 + (slot.getIndex() % 4) * 18)
{ {
public boolean canStackItems() public boolean canStackItems()
{ {
@ -36,17 +36,13 @@ public class ContainerPlayer extends Container {
} }
}); });
} }
List<SlotCommon> list = Lists.newArrayList(); this.addPlayerSlots(player);
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}
protected int getPlayerInventoryOffset() {
return Equipment.ARMOR_SLOTS;
} }
public int getInventoryOffsetY() {
return 92;
}
public ItemStack getSingleRecipe(ItemStack stack) { public ItemStack getSingleRecipe(ItemStack stack) {
return null; return null;
@ -90,7 +86,7 @@ public class ContainerPlayer extends Container {
if (index >= 0 && index < Equipment.ARMOR_SLOTS) if (index >= 0 && index < Equipment.ARMOR_SLOTS)
{ {
if (!this.mergeItemStack(itemstack1, Equipment.ARMOR_SLOTS, Equipment.ARMOR_SLOTS + playerIn.getInventoryCapacity())) if (!this.mergeItemStack(itemstack1, Equipment.ARMOR_SLOTS, Equipment.ARMOR_SLOTS + Equipment.INVENTORY_SLOTS))
{ {
return null; return null;
} }
@ -102,11 +98,11 @@ public class ContainerPlayer extends Container {
return null; return null;
} }
} }
else if (index >= Equipment.ARMOR_SLOTS && index < Equipment.ARMOR_SLOTS + playerIn.getInventoryCapacity()) else if (index >= Equipment.ARMOR_SLOTS && index < Equipment.ARMOR_SLOTS + Equipment.INVENTORY_SLOTS)
{ {
return null; return null;
} }
else if (!this.mergeItemStack(itemstack1, Equipment.ARMOR_SLOTS, Equipment.ARMOR_SLOTS + playerIn.getInventoryCapacity())) else if (!this.mergeItemStack(itemstack1, Equipment.ARMOR_SLOTS, Equipment.ARMOR_SLOTS + Equipment.INVENTORY_SLOTS))
{ {
return null; return null;
} }

View file

@ -12,6 +12,7 @@ import common.entity.npc.EntityNPC;
import common.item.ItemStack; import common.item.ItemStack;
import common.item.material.ItemEnchantedBook; import common.item.material.ItemEnchantedBook;
import common.network.IPlayer; import common.network.IPlayer;
import common.util.Equipment;
import common.util.LocalPos; import common.util.LocalPos;
import common.vars.Vars; import common.vars.Vars;
import common.world.State; import common.world.State;
@ -195,15 +196,7 @@ public class ContainerRepair extends Container
} }
}); });
List<SlotCommon> list = Lists.newArrayList(); this.addPlayerSlots(playerInventory);
for (int l = 0; l < playerInventory.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new SlotCommon(list, playerInventory, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}
protected int getPlayerInventoryOffset() {
return 3;
} }
private void onChanged(IInventory inventoryIn) private void onChanged(IInventory inventoryIn)

View file

@ -8,6 +8,7 @@ import common.item.ItemStack;
import common.network.IPlayer; import common.network.IPlayer;
import common.tileentity.Device; import common.tileentity.Device;
import common.tileentity.Device.Status; import common.tileentity.Device.Status;
import common.util.Equipment;
public class ContainerTile extends Container public class ContainerTile extends Container
{ {
@ -54,16 +55,12 @@ public class ContainerTile extends Container
++output; ++output;
} }
List<SlotCommon> list = Lists.newArrayList(); this.addPlayerSlots(player);
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 112 + (l / 12) * 18));
}
}
protected int getPlayerInventoryOffset() {
return this.tileInv.getSizeInventory();
} }
public int getInventoryOffsetY() {
return 112;
}
public boolean canInteractWith(EntityNPC playerIn) public boolean canInteractWith(EntityNPC playerIn)
{ {

View file

@ -6,6 +6,7 @@ import common.block.tech.BlockWorkbench;
import common.collect.Lists; import common.collect.Lists;
import common.entity.npc.EntityNPC; import common.entity.npc.EntityNPC;
import common.item.ItemStack; import common.item.ItemStack;
import common.util.Equipment;
import common.util.LocalPos; import common.util.LocalPos;
import common.world.World; import common.world.World;
@ -19,15 +20,8 @@ public class ContainerWorkbench extends Container {
this.worldObj = worldIn; this.worldObj = worldIn;
this.pos = posIn; this.pos = posIn;
List<SlotCommon> list = Lists.newArrayList(); this.addPlayerSlots(playerInventory);
for(int l = 0; l < playerInventory.getInventoryCapacity(); ++l) {
this.addSlotToContainer(new SlotCommon(list, playerInventory, l, 8 + (l % 12) * 18, 30 + 3 * 18 + (l / 12) * 18));
}
} }
protected int getPlayerInventoryOffset() {
return 0;
}
public int getTier() { public int getTier() {
return this.block.getTier(); return this.block.getTier();

View file

@ -86,7 +86,7 @@ public class Slot
/** /**
* Returns if this slot contains a stack. * Returns if this slot contains a stack.
*/ */
public boolean getHasStack() public final boolean getHasStack()
{ {
return this.getStack() != null; return this.getStack() != null;
} }

View file

@ -30,6 +30,7 @@ public enum Equipment implements Identifyable, Displayable {
HORSE_ARMOR("horse_armor", "Pferderüstung", EntityHorse.class, 8, 0); HORSE_ARMOR("horse_armor", "Pferderüstung", EntityHorse.class, 8, 0);
public static final int INVENTORY_SLOTS = 64;
public static final int ARMOR_SLOTS; public static final int ARMOR_SLOTS;
public static final Equipment[] ARMOR; public static final Equipment[] ARMOR;

View file

@ -955,11 +955,7 @@ public class Player extends User implements Executor, IPlayer
--this.entity.hurtResistance; --this.entity.hurtResistance;
} }
Container.removeEmptySlots(this.entity, 0, this.entity.getInventoryCapacity()); this.entity.openContainer.clearStacks();
if(this.entity.openContainer instanceof ContainerChest chest)
Container.removeEmptySlots(chest.getChestInventory(), 0, chest.getChestInventory().getSizeInventory());
else if(this.entity.openContainer instanceof ContainerEntityInventory ent && ent.getEntityInventory() != null)
Container.removeEmptySlots(ent.getEntityInventory(), 2, ent.getEntityInventory().getSizeInventory() - 2);
this.entity.openContainer.detectAndSendChanges(); this.entity.openContainer.detectAndSendChanges();
// if(!this.worldObj.client) // if(!this.worldObj.client)
@ -2953,7 +2949,7 @@ public class Player extends User implements Executor, IPlayer
return; return;
slot.putStack(stack); slot.putStack(stack);
} }
this.entity.openContainer.resortStacks(this.entity); this.entity.openContainer.resortStacks();
this.entity.openContainer.detectAndSendChanges(); this.entity.openContainer.detectAndSendChanges();
this.entity.worldObj.playSoundAtEntity(this.entity, SoundEvent.POP, 0.2F); this.entity.worldObj.playSoundAtEntity(this.entity, SoundEvent.POP, 0.2F);
if(amount == 1) if(amount == 1)