1
0
Fork 0

change invntory logic

This commit is contained in:
Sen 2025-09-05 17:15:59 +02:00
parent c6ba1703a2
commit 9f452b410e
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
11 changed files with 118 additions and 9 deletions

View file

@ -39,6 +39,9 @@ public abstract class Container
else else
{ {
this.crafters.add(listener); this.crafters.add(listener);
EntityNPC entity = listener.getPresentEntity();
if(entity != null)
this.resortStacks(entity);
listener.sendContainer(this, this.getInventory()); listener.sendContainer(this, this.getInventory());
this.detectAndSendChanges(); this.detectAndSendChanges();
} }
@ -397,9 +400,46 @@ public abstract class Container
this.detectAndSendChanges(); this.detectAndSendChanges();
} }
this.resortStacks(playerIn);
return itemstack; return itemstack;
} }
private void reorganize(int offset, int size) {
ItemStack[] reorganize = new ItemStack[size];
for(int z = 0; z < size; z++) {
Slot slot = this.inventorySlots.get(offset + z);
if(slot.getHasStack()) {
reorganize[z] = slot.getStack();
slot.putStack(null);
}
}
for(int z = 0; z < size; z++) {
if(reorganize[z] != null)
this.mergeItemStack(reorganize[z], offset, offset + size);
}
}
public void resortStacks(EntityNPC player) {
if(this.canMergeStacks())
this.reorganize(this.getMergeOffset(), this.getMergeSize());
this.reorganize(this.getPlayerInventoryOffset(), player.getInventoryCapacity());
}
protected abstract int getPlayerInventoryOffset();
protected boolean canMergeStacks() {
return false;
}
protected int getMergeOffset() {
return 0;
}
protected int getMergeSize() {
return this.inventorySlots.size();
}
/** /**
* Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
* is null for the initial slot that was double-clicked. * is null for the initial slot that was double-clicked.

View file

@ -7,6 +7,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.tileentity.TileEntityChest; import common.tileentity.TileEntityChest;
import common.util.Equipment;
public class ContainerChest extends Container public class ContainerChest extends Container
{ {
@ -41,6 +42,22 @@ public class ContainerChest extends Container
} }
} }
protected int getPlayerInventoryOffset() {
return this.chestSize;
}
protected boolean canMergeStacks() {
return true;
}
protected int getMergeOffset() {
return 0;
}
protected int getMergeSize() {
return this.chestSize;
}
public boolean canInteractWith(EntityNPC player) public boolean canInteractWith(EntityNPC player)
{ {
return this.chest.isUseableByPlayer(player); return this.chest.isUseableByPlayer(player);

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;
@ -71,6 +72,10 @@ public class ContainerEnchantment extends Container
} }
} }
protected int getPlayerInventoryOffset() {
return 1;
}
public static int encodeData(Pair<Enchantment, Integer> data) { public static int encodeData(Pair<Enchantment, Integer> data) {
return data == null ? -1 : data.first().ordinal() | data.second() << 8; return data == null ? -1 : data.first().ordinal() | data.second() << 8;
} }

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
{ {
@ -58,8 +59,24 @@ public class ContainerEntityInventory extends Container
} }
} }
protected int getPlayerInventoryOffset() {
return this.entity instanceof EntityHorse horse && horse.isChested() ? (horse.isChested() ? this.entityInventory.getSizeInventory() : 2) : 0;
}
public IInventory getEntityInventory() { public IInventory getEntityInventory() {
return this.entityInventory.getSizeInventory() > 2 ? this.entityInventory : null; return this.entity instanceof EntityHorse horse && horse.isChested() ? this.entityInventory : null;
}
protected boolean canMergeStacks() {
return this.entity instanceof EntityHorse horse && horse.isChested();
}
protected int getMergeOffset() {
return 2;
}
protected int getMergeSize() {
return this.entityInventory.getSizeInventory() - 2;
} }
public boolean canInteractWith(EntityNPC playerIn) public boolean canInteractWith(EntityNPC playerIn)

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;
@ -156,6 +157,10 @@ public class ContainerMerchant extends Container
} }
} }
protected int getPlayerInventoryOffset() {
return 3;
}
public InventoryMerchant getMerchantInventory() public InventoryMerchant getMerchantInventory()
{ {
return this.merchantInventory; return this.merchantInventory;

View file

@ -44,6 +44,10 @@ public class ContainerPlayer extends Container {
} }
} }
protected int getPlayerInventoryOffset() {
return Equipment.ARMOR_SLOTS;
}
public ItemStack getSingleRecipe(ItemStack stack) { public ItemStack getSingleRecipe(ItemStack stack) {
return null; return null;
// this.baseCrafting.setInventorySlotContents(0, stack); // this.baseCrafting.setInventorySlotContents(0, stack);

View file

@ -202,6 +202,10 @@ public class ContainerRepair extends Container
} }
} }
protected int getPlayerInventoryOffset() {
return 3;
}
private void onChanged(IInventory inventoryIn) private void onChanged(IInventory inventoryIn)
{ {
this.detectAndSendChanges(); this.detectAndSendChanges();

View file

@ -61,6 +61,10 @@ public class ContainerTile extends Container
} }
} }
protected int getPlayerInventoryOffset() {
return this.tileInv.getSizeInventory();
}
public boolean canInteractWith(EntityNPC playerIn) public boolean canInteractWith(EntityNPC playerIn)
{ {
return this.tileInv.isUseableByPlayer(playerIn); return this.tileInv.isUseableByPlayer(playerIn);

View file

@ -25,6 +25,10 @@ public class ContainerWorkbench extends Container {
} }
} }
protected int getPlayerInventoryOffset() {
return 0;
}
public int getTier() { public int getTier() {
return this.block.getTier(); return this.block.getTier();
} }

View file

@ -15,17 +15,21 @@ public class CommandMore extends Command {
this.setParamsOptional(); this.setParamsOptional();
this.addFlag("all", 'a'); this.addFlag("all", 'a');
this.addInt("amount", 'n', 2, 100000000, 100);
this.setParamsRequired(); this.setParamsRequired();
this.addPlayerEntityList("players", 'p', true, UserPolicy.NON_ADMINS_OR_SELF); this.addPlayerEntityList("players", 'p', true, UserPolicy.NON_ADMINS_OR_SELF);
} }
private int addItems(ItemStack stack) { private int addItems(ItemStack stack, int amount) {
int diff = stack.getMaxStackSize() - stack.getSize(); int max = Math.min(stack.getMaxStackSize(), amount);
stack.setSize(stack.getMaxStackSize()); if(stack.getSize() >= max)
return 0;
int diff = max - stack.getSize();
stack.setSize(max);
return diff; return diff;
} }
public void exec(CommandEnvironment env, Executor exec, boolean all, List<EntityNPC> players) { public void exec(CommandEnvironment env, Executor exec, boolean all, int amount, List<EntityNPC> players) {
int done = 0; int done = 0;
int added = 0; int added = 0;
for(EntityNPC player : players) { for(EntityNPC player : players) {
@ -34,11 +38,11 @@ public class CommandMore extends Command {
for(int z = 0; z < player.getSizeInventory(); z++) { for(int z = 0; z < player.getSizeInventory(); z++) {
ItemStack item = player.getStackInSlot(z); ItemStack item = player.getStackInSlot(z);
if(item != null) if(item != null)
add += this.addItems(item); add += this.addItems(item, amount);
} }
} }
else if(player.getHeldItem() != null) { else if(player.getHeldItem() != null) {
add += this.addItems(player.getHeldItem()); add += this.addItems(player.getHeldItem(), amount);
} }
if(add > 0) { if(add > 0) {
exec.log("%d " + (add == 1 ? "Gegenstand" : "Gegenstände") + " wurde" + (add == 1 ? "" : "n") + " dem Inventar von %s hinzugefügt", add, player.getRawName()); exec.log("%d " + (add == 1 ? "Gegenstand" : "Gegenstände") + " wurde" + (add == 1 ? "" : "n") + " dem Inventar von %s hinzugefügt", add, player.getRawName());

View file

@ -2707,7 +2707,9 @@ public class Player extends User implements Executor, IPlayer
for(int z = 0; z < this.entity.getSizeInventory(); z++) { for(int z = 0; z < this.entity.getSizeInventory(); z++) {
ItemStack stack = this.entity.getStackInSlot(z); ItemStack stack = this.entity.getStackInSlot(z);
if(stack != null) { if(stack != null) {
stack.setSize(stack.getMaxStackSize()); int max = Math.min(stack.getMaxStackSize(), 100);
if(stack.getSize() < max)
stack.setSize(max);
stack.setRepairCost(0); stack.setRepairCost(0);
if(stack.getItem().getMaxDamage() > 0) if(stack.getItem().getMaxDamage() > 0)
stack.setItemDamage(0); stack.setItemDamage(0);
@ -2716,7 +2718,9 @@ public class Player extends User implements Executor, IPlayer
} }
else if(this.entity.getHeldItem() != null) { else if(this.entity.getHeldItem() != null) {
ItemStack stack = this.entity.getHeldItem(); ItemStack stack = this.entity.getHeldItem();
stack.setSize(stack.getMaxStackSize()); int max = Math.min(stack.getMaxStackSize(), 100);
if(stack.getSize() < max)
stack.setSize(max);
stack.setRepairCost(0); stack.setRepairCost(0);
if(stack.getItem().getMaxDamage() > 0) if(stack.getItem().getMaxDamage() > 0)
stack.setItemDamage(0); stack.setItemDamage(0);
@ -2943,6 +2947,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.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)