change invntory logic
This commit is contained in:
parent
bfee96d5b5
commit
f93075f211
7 changed files with 48 additions and 8 deletions
|
@ -596,4 +596,37 @@ public abstract class Container
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ public class ContainerChest extends Container
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -18,8 +18,6 @@ public class ContainerEntityInventory extends Container
|
|||
{
|
||||
this.entityInventory = entityInv;
|
||||
this.entity = entity;
|
||||
int i = 3;
|
||||
int j = (i - 4) * 18;
|
||||
if(this.entity instanceof EntityHorse) {
|
||||
final EntityHorse horse = (EntityHorse)this.entity;
|
||||
this.addSlotToContainer(new Slot(entityInv, 0, 8, 18)
|
||||
|
@ -43,7 +41,7 @@ public class ContainerEntityInventory extends Container
|
|||
if (horse.isChested())
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
@ -59,6 +57,10 @@ public class ContainerEntityInventory extends Container
|
|||
this.addSlotToContainer(new SlotCommon(list, player, l, 8 + (l % 12) * 18, 84 + (l / 12) * 18));
|
||||
}
|
||||
}
|
||||
|
||||
public IInventory getEntityInventory() {
|
||||
return this.entityInventory.getSizeInventory() > 2 ? this.entityInventory : null;
|
||||
}
|
||||
|
||||
public boolean canInteractWith(EntityNPC playerIn)
|
||||
{
|
||||
|
@ -80,7 +82,7 @@ public class ContainerEntityInventory extends Container
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -203,7 +203,7 @@ public class ContainerMerchant extends Container
|
|||
|
||||
if (index == 2)
|
||||
{
|
||||
if (!this.mergeItemStack(itemstack1, 3, 39, true))
|
||||
if (!this.mergeItemStack(itemstack1, 3, 39, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -497,7 +497,7 @@ public class ContainerRepair extends Container
|
|||
|
||||
if (index == 2)
|
||||
{
|
||||
if (!this.mergeItemStack(itemstack1, 3, 39, true))
|
||||
if (!this.mergeItemStack(itemstack1, 3, 39, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -82,7 +82,7 @@ public class ContainerTile extends Container
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -955,6 +955,11 @@ public class Player extends User implements Executor, IPlayer
|
|||
--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();
|
||||
// if(!this.worldObj.client)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue