1
0
Fork 0

improve container gui

This commit is contained in:
Sen 2025-09-07 18:17:13 +02:00
parent 448d54c441
commit c8d4b8a48e
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
21 changed files with 226 additions and 296 deletions

View file

@ -14,8 +14,6 @@ import common.util.Equipment;
public abstract class Container
{
public static final int INVENTORY_WIDTH = 16;
public List<ItemStack> inventoryItemStacks = Lists.<ItemStack>newArrayList();
public List<Slot> inventorySlots = Lists.<Slot>newArrayList();
public int windowId;
@ -23,25 +21,15 @@ public abstract class Container
protected int offset;
protected List<IPlayer> crafters = Lists.<IPlayer>newArrayList();
private Set<EntityNPC> playerList = Sets.<EntityNPC>newHashSet();
public int getInventoryOffsetX() {
return 16;
}
public int getInventoryOffsetY() {
return 168;
}
public final int getPlayerInventoryOffset() {
return this.offset;
}
protected void addPlayerSlots(final EntityNPC player) {
int x = this.getInventoryOffsetX();
int y = this.getInventoryOffsetY();
for(Equipment slot : Equipment.ARMOR) {
final Equipment type = slot;
this.addSlotToContainer(new Slot(player, Equipment.INVENTORY_SLOTS + slot.getIndex(), x + slot.getIndex() * 36, y) {
this.addSlotToContainer(new Slot(player, Equipment.INVENTORY_SLOTS + slot.getIndex(), false) {
public boolean canStackItems() {
return false;
}
@ -62,7 +50,7 @@ public abstract class Container
this.offset = this.inventorySlots.size();
List<SlotCommon> list = Lists.newArrayList();
for(int z = 0; z < Equipment.INVENTORY_SLOTS; ++z) {
this.addSlotToContainer(new SlotCommon(list, player, z, x + (z % INVENTORY_WIDTH) * 36, y + 40 + (z / INVENTORY_WIDTH) * 36));
this.addSlotToContainer(new SlotCommon(list, player, z, true));
}
}

View file

@ -28,20 +28,12 @@ public class ContainerChest extends Container
{
for (int k = 0; k < this.width; ++k)
{
this.addSlotToContainer(new SlotCommon(list, chest, k + j * this.width, 16 + k * 36, 36 + j * 36));
this.addSlotToContainer(new SlotCommon(list, chest, k + j * this.width, false));
}
}
this.addPlayerSlots(player);
}
public int getInventoryOffsetX() {
return super.getInventoryOffsetX() + (this.width < INVENTORY_WIDTH ? 0 : (this.width - INVENTORY_WIDTH) * 36 / 2);
}
public int getInventoryOffsetY() {
return 170 + (this.height - 3) * 36;
}
protected boolean canMergeStacks() {
return true;

View file

@ -47,7 +47,7 @@ public class ContainerEnchantment extends Container
this.world = worldIn;
this.position = pos;
this.seed = playerInv.getEnchSeed();
this.addSlotToContainer(new Slot(this.table, 0, 50, 94)
this.addSlotToContainer(new Slot(this.table, 0, false)
{
public boolean isItemValid(ItemStack stack)
{

View file

@ -20,14 +20,14 @@ public class ContainerEntityInventory extends Container
this.entity = entity;
if(this.entity instanceof EntityHorse) {
final EntityHorse horse = (EntityHorse)this.entity;
this.addSlotToContainer(new Slot(entityInv, 0, 16, 36)
this.addSlotToContainer(new Slot(entityInv, 0, false)
{
public boolean isItemValid(ItemStack stack)
{
return super.isItemValid(stack) && stack.getItem() == Items.saddle && !this.getHasStack();
}
});
this.addSlotToContainer(new Slot(entityInv, 1, 16, 72)
this.addSlotToContainer(new Slot(entityInv, 1, false)
{
public boolean isItemValid(ItemStack stack)
{
@ -45,7 +45,7 @@ public class ContainerEntityInventory extends Container
{
for (int l = 0; l < 5; ++l)
{
this.addSlotToContainer(new SlotCommon(list, entityInv, 2 + l + k * 5, 160 + l * 36, 36 + k * 36));
this.addSlotToContainer(new SlotCommon(list, entityInv, 2 + l + k * 5, true));
}
}
}

View file

@ -15,9 +15,9 @@ public class ContainerMerchant extends Container
private int traded;
// private final EntityVillager theMerchant;
public SlotMerchantResult(EntityNPC player, InventoryMerchant merchantInventory, int slotIndex, int xPosition, int yPosition)
public SlotMerchantResult(EntityNPC player, InventoryMerchant merchantInventory, int slotIndex)
{
super(merchantInventory, slotIndex, xPosition, yPosition);
super(merchantInventory, slotIndex, true);
this.thePlayer = player;
// this.theMerchant = merchant;
this.theMerchantInventory = merchantInventory;
@ -134,17 +134,17 @@ public class ContainerMerchant extends Container
this.theMerchant = merchant;
this.theWorld = worldIn;
this.merchantInventory = new InventoryMerchant(playerInventory, merchant);
this.addSlotToContainer(new Slot(this.merchantInventory, 0, 72, 106) {
this.addSlotToContainer(new Slot(this.merchantInventory, 0, false) {
public boolean canEditItem() {
return false;
}
});
this.addSlotToContainer(new Slot(this.merchantInventory, 1, 124, 106) {
this.addSlotToContainer(new Slot(this.merchantInventory, 1, false) {
public boolean canEditItem() {
return false;
}
});
this.addSlotToContainer(new SlotMerchantResult(playerInventory, this.merchantInventory, 2, 240, 106));
this.addSlotToContainer(new SlotMerchantResult(playerInventory, this.merchantInventory, 2));
this.addPlayerSlots(playerInventory);
}

View file

@ -10,10 +10,6 @@ public class ContainerPlayer extends Container {
this.addPlayerSlots(player);
}
public int getInventoryOffsetY() {
return 32;
}
public ItemStack getSingleRecipe(ItemStack stack) {
return null;
// this.baseCrafting.setInventorySlotContents(0, stack);

View file

@ -114,17 +114,17 @@ public class ContainerRepair extends Container
};
this.selfPosition = blockPosIn;
this.theWorld = worldIn;
this.addSlotToContainer(new Slot(this.inputSlots, 0, 54, 94) {
this.addSlotToContainer(new Slot(this.inputSlots, 0, false) {
public boolean canEditItem() {
return false;
}
});
this.addSlotToContainer(new Slot(this.inputSlots, 1, 152, 94) {
this.addSlotToContainer(new Slot(this.inputSlots, 1, false) {
public boolean canEditItem() {
return false;
}
});
this.addSlotToContainer(new Slot(this.outputSlot, 2, 268, 94)
this.addSlotToContainer(new Slot(this.outputSlot, 2, true)
{
public boolean isItemValid(ItemStack stack)
{

View file

@ -35,7 +35,7 @@ public class ContainerTile extends Container
for (int idx = 0; idx < tileInv.getSizeInventory(); ++idx)
{
final int index = idx;
this.addSlotToContainer(new Slot(tileInv, idx, 16 + (this.tile.isInput(index) ? input : 8 - output) * 36, 162) {
this.addSlotToContainer(new Slot(tileInv, idx, !this.tile.isInput(index)) {
public boolean isItemValid(ItemStack stack)
{
return ContainerTile.this.tile.isItemValidForSlot(index, stack);
@ -53,10 +53,6 @@ public class ContainerTile extends Container
this.addPlayerSlots(player);
}
public int getInventoryOffsetY() {
return 224;
}
public boolean canInteractWith(EntityNPC playerIn)
{

View file

@ -5,32 +5,19 @@ import common.item.ItemStack;
public class Slot
{
/** The index of the slot in the inventory. */
private final int slotIndex;
/** The inventory we want to extract a slot from. */
public final IInventory inventory;
public final boolean slotRight;
/** the id of the slot(also the index in the inventory arraylist) */
public int slotNumber;
/** display position of the inventory slot on the screen x axis */
public int xDisplayPosition;
/** display position of the inventory slot on the screen y axis */
public int yDisplayPosition;
public Slot(IInventory inventoryIn, int index, int xPosition, int yPosition)
public Slot(IInventory inventoryIn, int index, boolean right)
{
this.inventory = inventoryIn;
this.slotIndex = index;
this.xDisplayPosition = xPosition;
this.yDisplayPosition = yPosition;
this.slotRight = right;
}
/**
* if par2 has more items than par1, onCrafting(item,countIncrease) is called
*/
public void onSlotChange(ItemStack p_75220_1_, ItemStack p_75220_2_)
{
if (p_75220_1_ != null && p_75220_2_ != null)
@ -47,17 +34,10 @@ public class Slot
}
}
/**
* 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)
{
}
/**
* the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
*/
protected void onCrafting(ItemStack stack)
{
}
@ -67,42 +47,27 @@ public class Slot
this.onSlotChanged();
}
/**
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
*/
public boolean isItemValid(ItemStack stack)
{
return true;
}
/**
* Helper fnct to get the stack in the slot.
*/
public ItemStack getStack()
{
return this.inventory.getStackInSlot(this.slotIndex);
}
/**
* Returns if this slot contains a stack.
*/
public final boolean getHasStack()
{
return this.getStack() != null;
}
/**
* Helper method to put a stack in the slot.
*/
public void putStack(ItemStack stack)
{
this.inventory.setInventorySlotContents(this.slotIndex, stack);
this.onSlotChanged();
}
/**
* Called when the stack in a Slot changes
*/
public void onSlotChanged()
{
this.inventory.markDirty();
@ -118,26 +83,16 @@ public class Slot
return this.canStackItems() ? 1000000000 : 1;
}
/**
* 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)
{
return this.inventory.decrStackSize(this.slotIndex, amount);
}
/**
* returns true if the slot exists in the given inventory and location
*/
public boolean isHere(IInventory inv, int slotIn)
{
return inv == this.inventory && slotIn == this.slotIndex;
}
/**
* Return whether this slot's stack can be taken from this slot.
*/
public boolean canTakeStack(EntityNPC playerIn)
{
return true;

View file

@ -6,11 +6,8 @@ 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);
public SlotCommon(List<SlotCommon> list, IInventory inventoryIn, int index, boolean right) {
super(inventoryIn, index, right);
this.list = list;
this.draw = list.isEmpty() ? null : list.getFirst();
list.add(this);
@ -19,33 +16,4 @@ public class SlotCommon extends Slot {
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 + 36);
y2 = Math.max(y2, slot.yDisplayPosition + 36);
}
this.w = (x2 - x1) / 36;
this.h = (y2 - y1) / 36;
}
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;
}
}