change invntory logic
This commit is contained in:
parent
c6ba1703a2
commit
9f452b410e
11 changed files with 118 additions and 9 deletions
|
@ -39,6 +39,9 @@ public abstract class Container
|
|||
else
|
||||
{
|
||||
this.crafters.add(listener);
|
||||
EntityNPC entity = listener.getPresentEntity();
|
||||
if(entity != null)
|
||||
this.resortStacks(entity);
|
||||
listener.sendContainer(this, this.getInventory());
|
||||
this.detectAndSendChanges();
|
||||
}
|
||||
|
@ -396,9 +399,46 @@ public abstract class Container
|
|||
|
||||
this.detectAndSendChanges();
|
||||
}
|
||||
|
||||
this.resortStacks(playerIn);
|
||||
|
||||
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
|
||||
|
|
|
@ -7,6 +7,7 @@ import common.collect.Lists;
|
|||
import common.entity.npc.EntityNPC;
|
||||
import common.item.ItemStack;
|
||||
import common.tileentity.TileEntityChest;
|
||||
import common.util.Equipment;
|
||||
|
||||
public class ContainerChest extends Container
|
||||
{
|
||||
|
@ -40,6 +41,22 @@ public class ContainerChest extends Container
|
|||
this.addSlotToContainer(new SlotCommon(list2, player, l, 8 + (l % 12) * 18 + xOffset, 85 + (l / 12) * 18 + yOffset));
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -13,6 +13,7 @@ import common.item.ItemStack;
|
|||
import common.item.material.ItemEnchantedBook;
|
||||
import common.network.IPlayer;
|
||||
import common.rng.Random;
|
||||
import common.util.Equipment;
|
||||
import common.util.LocalPos;
|
||||
import common.util.Pair;
|
||||
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) {
|
||||
return data == null ? -1 : data.first().ordinal() | data.second() << 8;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import common.entity.animal.EntityHorse;
|
|||
import common.entity.npc.EntityNPC;
|
||||
import common.init.Items;
|
||||
import common.item.ItemStack;
|
||||
import common.util.Equipment;
|
||||
|
||||
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() {
|
||||
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)
|
||||
|
|
|
@ -6,6 +6,7 @@ import common.collect.Lists;
|
|||
import common.entity.npc.EntityNPC;
|
||||
import common.item.ItemStack;
|
||||
import common.network.IPlayer;
|
||||
import common.util.Equipment;
|
||||
import common.village.MerchantRecipe;
|
||||
import common.world.World;
|
||||
|
||||
|
@ -155,6 +156,10 @@ public class ContainerMerchant extends Container
|
|||
this.addSlotToContainer(new SlotCommon(list, playerInventory, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
|
||||
}
|
||||
}
|
||||
|
||||
protected int getPlayerInventoryOffset() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public InventoryMerchant getMerchantInventory()
|
||||
{
|
||||
|
|
|
@ -43,6 +43,10 @@ public class ContainerPlayer extends Container {
|
|||
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
|
||||
}
|
||||
}
|
||||
|
||||
protected int getPlayerInventoryOffset() {
|
||||
return Equipment.ARMOR_SLOTS;
|
||||
}
|
||||
|
||||
public ItemStack getSingleRecipe(ItemStack stack) {
|
||||
return null;
|
||||
|
|
|
@ -201,6 +201,10 @@ public class ContainerRepair extends Container
|
|||
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)
|
||||
{
|
||||
|
|
|
@ -60,6 +60,10 @@ public class ContainerTile extends Container
|
|||
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 112 + (l / 12) * 18));
|
||||
}
|
||||
}
|
||||
|
||||
protected int getPlayerInventoryOffset() {
|
||||
return this.tileInv.getSizeInventory();
|
||||
}
|
||||
|
||||
public boolean canInteractWith(EntityNPC playerIn)
|
||||
{
|
||||
|
|
|
@ -24,6 +24,10 @@ public class ContainerWorkbench extends Container {
|
|||
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() {
|
||||
return this.block.getTier();
|
||||
|
|
|
@ -15,17 +15,21 @@ public class CommandMore extends Command {
|
|||
|
||||
this.setParamsOptional();
|
||||
this.addFlag("all", 'a');
|
||||
this.addInt("amount", 'n', 2, 100000000, 100);
|
||||
this.setParamsRequired();
|
||||
this.addPlayerEntityList("players", 'p', true, UserPolicy.NON_ADMINS_OR_SELF);
|
||||
}
|
||||
|
||||
private int addItems(ItemStack stack) {
|
||||
int diff = stack.getMaxStackSize() - stack.getSize();
|
||||
stack.setSize(stack.getMaxStackSize());
|
||||
private int addItems(ItemStack stack, int amount) {
|
||||
int max = Math.min(stack.getMaxStackSize(), amount);
|
||||
if(stack.getSize() >= max)
|
||||
return 0;
|
||||
int diff = max - stack.getSize();
|
||||
stack.setSize(max);
|
||||
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 added = 0;
|
||||
for(EntityNPC player : players) {
|
||||
|
@ -34,11 +38,11 @@ public class CommandMore extends Command {
|
|||
for(int z = 0; z < player.getSizeInventory(); z++) {
|
||||
ItemStack item = player.getStackInSlot(z);
|
||||
if(item != null)
|
||||
add += this.addItems(item);
|
||||
add += this.addItems(item, amount);
|
||||
}
|
||||
}
|
||||
else if(player.getHeldItem() != null) {
|
||||
add += this.addItems(player.getHeldItem());
|
||||
add += this.addItems(player.getHeldItem(), amount);
|
||||
}
|
||||
if(add > 0) {
|
||||
exec.log("%d " + (add == 1 ? "Gegenstand" : "Gegenstände") + " wurde" + (add == 1 ? "" : "n") + " dem Inventar von %s hinzugefügt", add, player.getRawName());
|
||||
|
|
|
@ -2707,7 +2707,9 @@ public class Player extends User implements Executor, IPlayer
|
|||
for(int z = 0; z < this.entity.getSizeInventory(); z++) {
|
||||
ItemStack stack = this.entity.getStackInSlot(z);
|
||||
if(stack != null) {
|
||||
stack.setSize(stack.getMaxStackSize());
|
||||
int max = Math.min(stack.getMaxStackSize(), 100);
|
||||
if(stack.getSize() < max)
|
||||
stack.setSize(max);
|
||||
stack.setRepairCost(0);
|
||||
if(stack.getItem().getMaxDamage() > 0)
|
||||
stack.setItemDamage(0);
|
||||
|
@ -2716,7 +2718,9 @@ public class Player extends User implements Executor, IPlayer
|
|||
}
|
||||
else if(this.entity.getHeldItem() != null) {
|
||||
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);
|
||||
if(stack.getItem().getMaxDamage() > 0)
|
||||
stack.setItemDamage(0);
|
||||
|
@ -2943,6 +2947,7 @@ public class Player extends User implements Executor, IPlayer
|
|||
return;
|
||||
slot.putStack(stack);
|
||||
}
|
||||
this.entity.openContainer.resortStacks(this.entity);
|
||||
this.entity.openContainer.detectAndSendChanges();
|
||||
this.entity.worldObj.playSoundAtEntity(this.entity, SoundEvent.POP, 0.2F);
|
||||
if(amount == 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue