1
0
Fork 0

change inventory system

This commit is contained in:
Sen 2025-09-04 18:37:02 +02:00
parent 9bd1ac3b45
commit 2d9ea3d654
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
15 changed files with 349 additions and 245 deletions

View file

@ -188,6 +188,15 @@ public abstract class GuiContainer extends Gui
return list;
}
public int getScale() {
return this.container_scale;
}
public ItemStack getHoverItem(Slot slot) {
return this.gm.player != null && this.gm.player.getMouseItem() != null ? this.gm.player.getMouseItem() : (this.gm.itemCheat && this.cheatStack != null ? this.cheatStack : (Bind.CRAFT.isDown()
&& slot.canCheatItem() && slot.getHasStack() ? this.gm.player.inventoryContainer.getSingleRecipe(slot.getStack()) : null));
}
public Label label(String text, int x, int y) {
x = x * this.container_scale + this.container_x;
@ -292,7 +301,7 @@ public abstract class GuiContainer extends Gui
if(this.inventorySlots != null) {
for (int i1 = 0; i1 < this.inventorySlots.inventorySlots.size(); ++i1) {
Slot slot = this.inventorySlots.inventorySlots.get(i1);
if(slot.getTexture() == null || !slot.getTexture().isEmpty())
if(slot.canDraw())
this.slot(slot.xDisplayPosition - 1, slot.yDisplayPosition - 1, 18, 18, slot);
}
}
@ -316,7 +325,7 @@ public abstract class GuiContainer extends Gui
Slot slot = (Slot)this.inventorySlots.inventorySlots.get(i1);
this.drawSlot(slot);
if (this.isMouseOverSlot(slot, mouseX, mouseY) && slot.canBeHovered())
if (this.isMouseOverSlot(slot, mouseX, mouseY) && slot.canHover())
{
this.theSlot = slot;
}

View file

@ -1,23 +1,34 @@
package client.gui.element;
import client.Client;
import client.gui.Gui;
import client.gui.container.GuiContainer;
import client.renderer.Drawing;
import common.inventory.Slot;
import common.item.ItemStack;
public class InventoryButton extends Element {
private final boolean bordered;
private final Slot slot;
private final String texture;
private final boolean background;
private final int bgW;
private final int bgH;
public InventoryButton(int x, int y, int w, int h, boolean bordered, Slot slot) {
super(x, y, w, h, null);
this.bordered = bordered;
this.texture = slot == null || slot.getTexture() == null ? null : "textures/items/icon_" + slot.getTexture() + ".png";
this.slot = this.texture == null ? null : slot;
this.texture = slot == null || slot.getTexture() == null || slot.getTexture().isEmpty() ? null : "textures/items/icon_" + slot.getTexture() + ".png";
this.background = slot == null || slot.getTexture() != null;
this.slot = slot;
int scale = this.bordered ? 2 : 1;
this.bgW = slot == null ? w : slot.getBackgroundWidth() * 18 * scale;
this.bgH = slot == null ? h : slot.getBackgroundHeight() * 18 * scale;
}
protected void drawBackground() {
drawButton(this.gm, this.pos_x, this.pos_y, this.size_x, this.size_y, this.bordered);
if(this.background)
drawButton(this.gm, this.pos_x, this.pos_y, this.bgW, this.bgH, this.bordered);
if(this.texture != null && !this.slot.getHasStack()) {
int scale = this.bordered ? 2 : 1;
Drawing.drawTexturedRect(this.gm, this.texture, 16 * scale, 16 * scale, this.pos_x + scale, this.pos_y + scale, 0, 0, this.size_x - scale * 2, this.size_y - scale * 2);
@ -33,4 +44,20 @@ public class InventoryButton extends Element {
else
Drawing.drawGradient(x, y, w, h, gm.style.fill_top, gm.style.fill_btm, gm.style.brdr_top, gm.style.brdr_btm);
}
public boolean canHover() {
if(this.slot == null)
return true;
ItemStack hover = ((GuiContainer)this.gui).getHoverItem(this.slot);
return hover == null ? this.slot.getHasStack() : this.slot.isItemValid(hover);
}
public void drawHover() {
int scale = this.bordered ? 2 : 1;
Drawing.drawRect(this.pos_x + scale, this.pos_y + scale, 16 * scale, 16 * scale, Gui.HOVER_COLOR);
}
public boolean canClick() {
return false;
}
}

View file

@ -1,6 +1,9 @@
package common.inventory;
import java.util.List;
import common.block.tech.BlockChest;
import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.tileentity.TileEntityChest;
@ -22,17 +25,19 @@ public class ContainerChest extends Container
int xOffset = this.width < 12 ? 0 : (this.width - 12) * 18 / 2;
int yOffset = (this.height - 3) * 18;
List<SlotCommon> list1 = Lists.newArrayList();
for (int j = 0; j < this.height; ++j)
{
for (int k = 0; k < this.width; ++k)
{
this.addSlotToContainer(new Slot(chest, k + j * this.width, 8 + k * 18, 18 + j * 18));
this.addSlotToContainer(new SlotCommon(list1, chest, k + j * this.width, 8 + k * 18, 18 + j * 18));
}
}
List<SlotCommon> list2 = Lists.newArrayList();
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new Slot(player, l, 8 + (l % 12) * 18 + xOffset, 85 + (l / 12) * 18 + yOffset));
this.addSlotToContainer(new SlotCommon(list2, player, l, 8 + (l % 12) * 18 + xOffset, 85 + (l / 12) * 18 + yOffset));
}
}

View file

@ -2,6 +2,7 @@ package common.inventory;
import java.util.List;
import common.collect.Lists;
import common.enchantment.Enchantment;
import common.enchantment.EnchantmentHelper;
import common.enchantment.RngEnchantment;
@ -63,9 +64,10 @@ public class ContainerEnchantment extends Container
}
});
List<SlotCommon> list = Lists.newArrayList();
for (int l = 0; l < playerInv.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new Slot(playerInv, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
this.addSlotToContainer(new SlotCommon(list, playerInv, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}

View file

@ -1,5 +1,8 @@
package common.inventory;
import java.util.List;
import common.collect.Lists;
import common.entity.Entity;
import common.entity.animal.EntityHorse;
import common.entity.npc.EntityNPC;
@ -32,26 +35,28 @@ public class ContainerEntityInventory extends Container
{
return super.isItemValid(stack) && horse.canWearArmor() && EntityHorse.isArmorItem(stack.getItem());
}
public boolean canBeHovered()
public boolean canDraw()
{
return horse.canWearArmor();
}
});
if (horse.isChested())
{
List<SlotCommon> list = Lists.newArrayList();
for (int k = 0; k < i; ++k)
{
for (int l = 0; l < 5; ++l)
{
this.addSlotToContainer(new Slot(entityInv, 2 + l + k * 5, 80 + l * 18, 18 + k * 18));
this.addSlotToContainer(new SlotCommon(list, entityInv, 2 + l + k * 5, 80 + l * 18, 18 + k * 18));
}
}
}
}
List<SlotCommon> list = Lists.newArrayList();
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new Slot(player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}

View file

@ -1,12 +1,133 @@
package common.inventory;
import java.util.List;
import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.network.IPlayer;
import common.village.MerchantRecipe;
import common.world.World;
public class ContainerMerchant extends Container
{
private class SlotMerchantResult extends Slot
{
private final InventoryMerchant theMerchantInventory;
private EntityNPC thePlayer;
private int traded;
// private final EntityVillager theMerchant;
public SlotMerchantResult(EntityNPC player, InventoryMerchant merchantInventory, int slotIndex, int xPosition, int yPosition)
{
super(merchantInventory, slotIndex, xPosition, yPosition);
this.thePlayer = player;
// this.theMerchant = merchant;
this.theMerchantInventory = merchantInventory;
}
/**
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
*/
public boolean isItemValid(ItemStack stack)
{
return false;
}
/**
* Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
* stack.
*/
public ItemStack decrStackSize(int amount)
{
if (this.getHasStack())
{
this.traded += Math.min(amount, this.getStack().getSize());
}
return super.decrStackSize(amount);
}
/**
* the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
* internal count then calls onCrafting(item).
*/
protected void onCrafting(ItemStack stack, int amount)
{
this.traded += amount;
this.onCrafting(stack);
}
/**
* the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
*/
protected void onCrafting(ItemStack stack)
{
// stack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.traded);
this.traded = 0;
}
public void onPickupFromSlot(EntityNPC playerIn, ItemStack stack)
{
this.onCrafting(stack);
MerchantRecipe merchantrecipe = this.theMerchantInventory.getCurrentRecipe();
if (merchantrecipe != null)
{
ItemStack itemstack = this.theMerchantInventory.getStackInSlot(0);
ItemStack itemstack1 = this.theMerchantInventory.getStackInSlot(1);
if (this.doTrade(merchantrecipe, itemstack, itemstack1) || this.doTrade(merchantrecipe, itemstack1, itemstack))
{
// if(this.theMerchant != null)
// this.theMerchant.useRecipe(merchantrecipe);
// playerIn.triggerAchievement(StatRegistry.timesTradedWithNpcStat);
if (itemstack != null && itemstack.isEmpty())
{
itemstack = null;
}
if (itemstack1 != null && itemstack1.isEmpty())
{
itemstack1 = null;
}
this.theMerchantInventory.setInventorySlotContents(0, itemstack);
this.theMerchantInventory.setInventorySlotContents(1, itemstack1);
}
}
}
private boolean doTrade(MerchantRecipe trade, ItemStack firstItem, ItemStack secondItem)
{
ItemStack itemstack = trade.first();
ItemStack itemstack1 = trade.second();
if (firstItem != null && firstItem.getItem() == itemstack.getItem())
{
if (itemstack1 != null && secondItem != null && itemstack1.getItem() == secondItem.getItem())
{
firstItem.decrSize(itemstack.getSize());
secondItem.decrSize(itemstack1.getSize());
return true;
}
if (itemstack1 == null && secondItem == null)
{
firstItem.decrSize(itemstack.getSize());
return true;
}
}
return false;
}
public boolean canCheatItem() {
return false;
}
}
private EntityNPC theMerchant;
private InventoryMerchant merchantInventory;
private final World theWorld;
@ -28,9 +149,10 @@ public class ContainerMerchant extends Container
});
this.addSlotToContainer(new SlotMerchantResult(playerInventory, this.merchantInventory, 2, 120, 53));
List<SlotCommon> list = Lists.newArrayList();
for (int l = 0; l < playerInventory.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new Slot(playerInventory, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
this.addSlotToContainer(new SlotCommon(list, playerInventory, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}

View file

@ -1,5 +1,8 @@
package common.inventory;
import java.util.List;
import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.item.material.ItemArmor;
@ -26,14 +29,18 @@ public class ContainerPlayer extends Container {
return stack != null && stack.getItem() instanceof ItemArmor armor && armor.getArmorType().canUseInSlot(ContainerPlayer.this.thePlayer, type);
}
public String getTexture() {
return ContainerPlayer.this.thePlayer.hasArmorSlot(type) ? (type.isRing() ? "ring" : type.getName()) : "";
return type.isRing() ? "ring" : type.getName();
}
public boolean canDraw() {
return ContainerPlayer.this.thePlayer.hasArmorSlot(type);
}
});
}
List<SlotCommon> list = Lists.newArrayList();
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new Slot(player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}

View file

@ -1,9 +1,11 @@
package common.inventory;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import common.block.tech.BlockAnvil;
import common.collect.Lists;
import common.enchantment.Enchantment;
import common.enchantment.EnchantmentHelper;
import common.entity.npc.EntityNPC;
@ -17,6 +19,78 @@ import common.world.World;
public class ContainerRepair extends Container
{
private class InventoryCraftResult implements IInventory
{
/** A list of one item containing the result of the crafting formula */
private ItemStack[] stackResult = new ItemStack[1];
/**
* Returns the number of slots in the inventory.
*/
public int getSizeInventory()
{
return 1;
}
/**
* Returns the stack in the given slot.
*/
public ItemStack getStackInSlot(int index)
{
return this.stackResult[0];
}
/**
* Removes up to a specified number of items from an inventory slot and returns them in a new stack.
*/
public ItemStack decrStackSize(int index, int count)
{
if (this.stackResult[0] != null)
{
ItemStack itemstack = this.stackResult[0];
this.stackResult[0] = null;
return itemstack;
}
else
{
return null;
}
}
/**
* Removes a stack from the given slot and returns it.
*/
public ItemStack removeStackFromSlot(int index)
{
if (this.stackResult[0] != null)
{
ItemStack itemstack = this.stackResult[0];
this.stackResult[0] = null;
return itemstack;
}
else
{
return null;
}
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
public void setInventorySlotContents(int index, ItemStack stack)
{
this.stackResult[0] = stack;
}
public void clear()
{
for (int i = 0; i < this.stackResult.length; ++i)
{
this.stackResult[i] = null;
}
}
}
private IInventory outputSlot;
private IInventory inputSlots;
private World theWorld;
@ -121,9 +195,10 @@ public class ContainerRepair extends Container
}
});
List<SlotCommon> list = Lists.newArrayList();
for (int l = 0; l < playerInventory.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new Slot(playerInventory, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
this.addSlotToContainer(new SlotCommon(list, playerInventory, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
}
}

View file

@ -1,5 +1,8 @@
package common.inventory;
import java.util.List;
import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.network.IPlayer;
@ -51,9 +54,10 @@ public class ContainerTile extends Container
++output;
}
List<SlotCommon> list = Lists.newArrayList();
for (int l = 0; l < player.getInventoryCapacity(); ++l)
{
this.addSlotToContainer(new Slot(player, l, 8 + (l % 12) * 18, 112 + (l / 12) * 18));
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 112 + (l / 12) * 18));
}
}

View file

@ -1,6 +1,9 @@
package common.inventory;
import java.util.List;
import common.block.tech.BlockWorkbench;
import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.util.LocalPos;
@ -15,8 +18,10 @@ public class ContainerWorkbench extends Container {
this.block = block;
this.worldObj = worldIn;
this.pos = posIn;
List<SlotCommon> list = Lists.newArrayList();
for(int l = 0; l < playerInventory.getInventoryCapacity(); ++l) {
this.addSlotToContainer(new Slot(playerInventory, l, 8 + (l % 12) * 18, 30 + 3 * 18 + (l / 12) * 18));
this.addSlotToContainer(new SlotCommon(list, playerInventory, l, 8 + (l % 12) * 18, 30 + 3 * 18 + (l / 12) * 18));
}
}

View file

@ -1,75 +0,0 @@
package common.inventory;
import common.item.ItemStack;
public class InventoryCraftResult implements IInventory
{
/** A list of one item containing the result of the crafting formula */
private ItemStack[] stackResult = new ItemStack[1];
/**
* Returns the number of slots in the inventory.
*/
public int getSizeInventory()
{
return 1;
}
/**
* Returns the stack in the given slot.
*/
public ItemStack getStackInSlot(int index)
{
return this.stackResult[0];
}
/**
* Removes up to a specified number of items from an inventory slot and returns them in a new stack.
*/
public ItemStack decrStackSize(int index, int count)
{
if (this.stackResult[0] != null)
{
ItemStack itemstack = this.stackResult[0];
this.stackResult[0] = null;
return itemstack;
}
else
{
return null;
}
}
/**
* Removes a stack from the given slot and returns it.
*/
public ItemStack removeStackFromSlot(int index)
{
if (this.stackResult[0] != null)
{
ItemStack itemstack = this.stackResult[0];
this.stackResult[0] = null;
return itemstack;
}
else
{
return null;
}
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
public void setInventorySlotContents(int index, ItemStack stack)
{
this.stackResult[0] = stack;
}
public void clear()
{
for (int i = 0; i < this.stackResult.length; ++i)
{
this.stackResult[i] = null;
}
}
}

View file

@ -118,11 +118,6 @@ public class Slot
return this.canStackItems() ? 1000000000 : 1;
}
public String getTexture()
{
return null;
}
/**
* Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
* stack.
@ -147,15 +142,6 @@ public class Slot
{
return true;
}
/**
* Actualy only call when we want to render the white square effect over the slots. Return always True, except for
* the armor slot of the Donkey/Mule (we can't interact with the Undead and Skeleton horses)
*/
public boolean canBeHovered()
{
return true;
}
public int getIndex() {
return this.slotIndex;
@ -168,4 +154,24 @@ public class Slot
public boolean canEditItem() {
return this.canCheatItem();
}
public int getBackgroundWidth() {
return 1;
}
public int getBackgroundHeight() {
return 1;
}
public String getTexture() {
return "";
}
public boolean canDraw() {
return true;
}
public boolean canHover() {
return this.canDraw();
}
}

View file

@ -0,0 +1,51 @@
package common.inventory;
import java.util.List;
public class SlotCommon extends Slot {
private final List<SlotCommon> list;
private final SlotCommon draw;
private int w;
private int h;
public SlotCommon(List<SlotCommon> list, IInventory inventoryIn, int index, int xPosition, int yPosition) {
super(inventoryIn, index, xPosition, yPosition);
this.list = list;
this.draw = list.isEmpty() ? null : list.getFirst();
list.add(this);
}
public String getTexture() {
return this.draw == null ? super.getTexture() : null;
}
private void init() {
int x1 = Integer.MAX_VALUE;
int y1 = Integer.MAX_VALUE;
int x2 = Integer.MIN_VALUE;
int y2 = Integer.MIN_VALUE;
for(SlotCommon slot : this.list) {
x1 = Math.min(x1, slot.xDisplayPosition);
y1 = Math.min(y1, slot.yDisplayPosition);
x2 = Math.max(x2, slot.xDisplayPosition + 18);
y2 = Math.max(y2, slot.yDisplayPosition + 18);
}
this.w = (x2 - x1) / 18;
this.h = (y2 - y1) / 18;
}
public int getBackgroundWidth() {
if(this.draw != null)
return this.draw.getBackgroundWidth();
this.init();
return this.w;
}
public int getBackgroundHeight() {
if(this.draw != null)
return this.draw.getBackgroundHeight();
this.init();
return this.h;
}
}

View file

@ -1,122 +0,0 @@
package common.inventory;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.village.MerchantRecipe;
public class SlotMerchantResult extends Slot
{
private final InventoryMerchant theMerchantInventory;
private EntityNPC thePlayer;
private int traded;
// private final EntityVillager theMerchant;
public SlotMerchantResult(EntityNPC player, InventoryMerchant merchantInventory, int slotIndex, int xPosition, int yPosition)
{
super(merchantInventory, slotIndex, xPosition, yPosition);
this.thePlayer = player;
// this.theMerchant = merchant;
this.theMerchantInventory = merchantInventory;
}
/**
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
*/
public boolean isItemValid(ItemStack stack)
{
return false;
}
/**
* Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
* stack.
*/
public ItemStack decrStackSize(int amount)
{
if (this.getHasStack())
{
this.traded += Math.min(amount, this.getStack().getSize());
}
return super.decrStackSize(amount);
}
/**
* the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
* internal count then calls onCrafting(item).
*/
protected void onCrafting(ItemStack stack, int amount)
{
this.traded += amount;
this.onCrafting(stack);
}
/**
* the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
*/
protected void onCrafting(ItemStack stack)
{
// stack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.traded);
this.traded = 0;
}
public void onPickupFromSlot(EntityNPC playerIn, ItemStack stack)
{
this.onCrafting(stack);
MerchantRecipe merchantrecipe = this.theMerchantInventory.getCurrentRecipe();
if (merchantrecipe != null)
{
ItemStack itemstack = this.theMerchantInventory.getStackInSlot(0);
ItemStack itemstack1 = this.theMerchantInventory.getStackInSlot(1);
if (this.doTrade(merchantrecipe, itemstack, itemstack1) || this.doTrade(merchantrecipe, itemstack1, itemstack))
{
// if(this.theMerchant != null)
// this.theMerchant.useRecipe(merchantrecipe);
// playerIn.triggerAchievement(StatRegistry.timesTradedWithNpcStat);
if (itemstack != null && itemstack.isEmpty())
{
itemstack = null;
}
if (itemstack1 != null && itemstack1.isEmpty())
{
itemstack1 = null;
}
this.theMerchantInventory.setInventorySlotContents(0, itemstack);
this.theMerchantInventory.setInventorySlotContents(1, itemstack1);
}
}
}
private boolean doTrade(MerchantRecipe trade, ItemStack firstItem, ItemStack secondItem)
{
ItemStack itemstack = trade.first();
ItemStack itemstack1 = trade.second();
if (firstItem != null && firstItem.getItem() == itemstack.getItem())
{
if (itemstack1 != null && secondItem != null && itemstack1.getItem() == secondItem.getItem())
{
firstItem.decrSize(itemstack.getSize());
secondItem.decrSize(itemstack1.getSize());
return true;
}
if (itemstack1 == null && secondItem == null)
{
firstItem.decrSize(itemstack.getSize());
return true;
}
}
return false;
}
public boolean canCheatItem() {
return false;
}
}

View file

@ -1,17 +0,0 @@
package common.inventory;
import common.item.ItemStack;
public class SlotOutput extends Slot {
public SlotOutput(IInventory inventoryIn, int slotIndex, int xPosition, int yPosition) {
super(inventoryIn, slotIndex, xPosition, yPosition);
}
public boolean isItemValid(ItemStack stack) {
return false;
}
public boolean canCheatItem() {
return false;
}
}