1
0
Fork 0

change invntory logic

This commit is contained in:
Sen 2025-09-05 14:15:32 +02:00
parent bfee96d5b5
commit f93075f211
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
7 changed files with 48 additions and 8 deletions

View file

@ -596,4 +596,37 @@ 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

@ -58,7 +58,7 @@ public class ContainerChest extends Container
if (index < this.chestSize) if (index < this.chestSize)
{ {
if (!this.mergeItemStack(itemstack1, this.chestSize, this.inventorySlots.size(), true)) if (!this.mergeItemStack(itemstack1, this.chestSize, this.inventorySlots.size(), false))
{ {
return null; return null;
} }

View file

@ -18,8 +18,6 @@ public class ContainerEntityInventory extends Container
{ {
this.entityInventory = entityInv; this.entityInventory = entityInv;
this.entity = entity; this.entity = entity;
int i = 3;
int j = (i - 4) * 18;
if(this.entity instanceof EntityHorse) { if(this.entity instanceof EntityHorse) {
final EntityHorse horse = (EntityHorse)this.entity; final EntityHorse horse = (EntityHorse)this.entity;
this.addSlotToContainer(new Slot(entityInv, 0, 8, 18) this.addSlotToContainer(new Slot(entityInv, 0, 8, 18)
@ -43,7 +41,7 @@ public class ContainerEntityInventory extends Container
if (horse.isChested()) if (horse.isChested())
{ {
List<SlotCommon> list = Lists.newArrayList(); List<SlotCommon> list = Lists.newArrayList();
for (int k = 0; k < i; ++k) for (int k = 0; k < 3; ++k)
{ {
for (int l = 0; l < 5; ++l) for (int l = 0; l < 5; ++l)
{ {
@ -60,6 +58,10 @@ public class ContainerEntityInventory extends Container
} }
} }
public IInventory getEntityInventory() {
return this.entityInventory.getSizeInventory() > 2 ? this.entityInventory : null;
}
public boolean canInteractWith(EntityNPC playerIn) public boolean canInteractWith(EntityNPC playerIn)
{ {
return this.entityInventory.isUseableByPlayer(playerIn) && this.entity.isEntityAlive() && this.entity.getDistanceToEntity(playerIn) < 8.0F; return this.entityInventory.isUseableByPlayer(playerIn) && this.entity.isEntityAlive() && this.entity.getDistanceToEntity(playerIn) < 8.0F;
@ -80,7 +82,7 @@ public class ContainerEntityInventory extends Container
if (index < this.entityInventory.getSizeInventory()) if (index < this.entityInventory.getSizeInventory())
{ {
if (!this.mergeItemStack(itemstack1, this.entityInventory.getSizeInventory(), this.inventorySlots.size(), true)) if (!this.mergeItemStack(itemstack1, this.entityInventory.getSizeInventory(), this.inventorySlots.size(), false))
{ {
return null; return null;
} }

View file

@ -203,7 +203,7 @@ public class ContainerMerchant extends Container
if (index == 2) if (index == 2)
{ {
if (!this.mergeItemStack(itemstack1, 3, 39, true)) if (!this.mergeItemStack(itemstack1, 3, 39, false))
{ {
return null; return null;
} }

View file

@ -497,7 +497,7 @@ public class ContainerRepair extends Container
if (index == 2) if (index == 2)
{ {
if (!this.mergeItemStack(itemstack1, 3, 39, true)) if (!this.mergeItemStack(itemstack1, 3, 39, false))
{ {
return null; return null;
} }

View file

@ -82,7 +82,7 @@ public class ContainerTile extends Container
if (index < this.tileInv.getSizeInventory()) if (index < this.tileInv.getSizeInventory())
{ {
if (!this.mergeItemStack(itemstack1, this.tileInv.getSizeInventory(), this.inventorySlots.size(), true)) if (!this.mergeItemStack(itemstack1, this.tileInv.getSizeInventory(), this.inventorySlots.size(), false))
{ {
return null; return null;
} }

View file

@ -955,6 +955,11 @@ public class Player extends User implements Executor, IPlayer
--this.entity.hurtResistance; --this.entity.hurtResistance;
} }
Container.removeEmptySlots(this.entity, 0, this.entity.getInventoryCapacity());
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)