Skip to content

Commit

Permalink
Lots of lunch box/lunch bag fixes
Browse files Browse the repository at this point in the history
Desyncs, dupes, crashes, you name it. This should address most of them hopefully.

Closes #137, #143, #145, #146
  • Loading branch information
squeek502 committed Jun 12, 2018
1 parent b9fe79e commit a6b4c82
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 184 deletions.
41 changes: 17 additions & 24 deletions java/squeek/spiceoflife/inventory/ContainerFoodContainer.java
Expand Up @@ -35,6 +35,20 @@ public void setFoodContainerItemStack(@Nonnull ItemStack itemStack)
foodContainerInventory.itemStackFoodContainer = itemStack;
}

@Override
protected void addHotbarSlot(InventoryPlayer playerInventory, int slotNum, int x, int y)
{
ItemStack stackInSlot = playerInventory.getStackInSlot(slotNum);
if (isFoodContainerWithUUID(stackInSlot, getUUID()))
{
addSlotToContainer(new SlotLocked(playerInventory, slotNum, x, y));
}
else
{
super.addHotbarSlot(playerInventory, slotNum, x, y);
}
}

@Nonnull
public ItemStack getItemStack()
{
Expand All @@ -52,8 +66,8 @@ public void onContainerClosed(EntityPlayer player)
setFoodContainerItemStack(findFoodContainerWithUUID(getUUID()));
}

if (getItemStack() != ItemStack.EMPTY)
((ItemFoodContainer) getItemStack().getItem()).setIsOpen(getItemStack(), false);
if (!getItemStack().isEmpty())
foodContainerInventory.itemFoodContainer.setIsOpen(getItemStack(), false);

super.onContainerClosed(player);
}
Expand All @@ -73,34 +87,13 @@ public ItemStack findFoodContainerWithUUID(UUID uuid)
return ItemStack.EMPTY;
}

@Nonnull
@Override
public ItemStack slotClick(int slotNum, int dragType, ClickType clickType, EntityPlayer player)
{
// make sure the correct ItemStack instance is always used when the player is moving
// the food container around while they have it open
ItemStack putDownStack = player.inventory.getItemStack();
ItemStack pickedUpStack = super.slotClick(slotNum, dragType, clickType, player);

if (isFoodContainerWithUUID(pickedUpStack, getUUID()))
{
setFoodContainerItemStack(pickedUpStack);
}
else if (slotNum >= 0 && isFoodContainerWithUUID(putDownStack, getUUID()) && isFoodContainerWithUUID(getSlot(slotNum).getStack(), getUUID()))
{
setFoodContainerItemStack(getSlot(slotNum).getStack());
}

return pickedUpStack;
}

public boolean isFoodContainerWithUUID(@Nonnull ItemStack itemStack, UUID uuid)
{
return !itemStack.isEmpty() && itemStack.getItem() instanceof ItemFoodContainer && ((ItemFoodContainer) itemStack.getItem()).getUUID(itemStack).equals(uuid);
}

public UUID getUUID()
{
return ((ItemFoodContainer) getItemStack().getItem()).getUUID(getItemStack());
return foodContainerInventory.itemFoodContainer.getUUID(getItemStack());
}
}
132 changes: 25 additions & 107 deletions java/squeek/spiceoflife/inventory/ContainerGeneric.java
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
Expand Down Expand Up @@ -106,10 +107,33 @@ protected void addPlayerInventorySlots(InventoryPlayer playerInventory, int xSta
// hotbar
for (int col = 0; col < 9; ++col)
{
this.addSlotToContainer(new Slot(playerInventory, col, xStart + col * 18, yStart + 58));
this.addHotbarSlot(playerInventory, col, xStart + col * 18, yStart + 58);
}
}

protected void addHotbarSlot(InventoryPlayer playerInventory, int slotNum, int x, int y)
{
this.addSlotToContainer(new Slot(playerInventory, slotNum, x, y));
}

@Override
@Nonnull
public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player)
{
// prevent swapping using number keys
if (clickTypeIn == ClickType.SWAP && dragType >= 0 && dragType < 9)
{
int hotbarSlotIndex = this.inventorySlots.size() - 9 + dragType;
Slot hotbarSlot = getSlot(hotbarSlotIndex);
Slot swapSlot = getSlot(slotId);
if (hotbarSlot instanceof SlotLocked || swapSlot instanceof SlotLocked)
{
return ItemStack.EMPTY;
}
}
return super.slotClick(slotId, dragType, clickTypeIn, player);
}

@Override
@Nonnull
public ItemStack transferStackInSlot(EntityPlayer player, int slotNum)
Expand Down Expand Up @@ -155,112 +179,6 @@ public ItemStack transferStackInSlot(EntityPlayer player, int slotNum)
return ItemStack.EMPTY;
}

public int getEffectiveMaxStackSizeForSlot(int slotNum, @Nonnull ItemStack itemStack)
{
int effectiveMaxStackSize = itemStack.getMaxStackSize();
if (slotNum < inventory.getSizeInventory())
effectiveMaxStackSize = Math.min(effectiveMaxStackSize, this.inventory.getInventoryStackLimit());
return effectiveMaxStackSize;
}

@Override
protected boolean mergeItemStack(@Nonnull ItemStack itemStack, int startSlotNum, int endSlotNum, boolean checkBackwards)
{
boolean didMerge = false;
int k = startSlotNum;

if (checkBackwards)
{
k = endSlotNum - 1;
}

Slot slot;
ItemStack itemstack1;

if (itemStack.isStackable())
{
while (itemStack.getCount() > 0 && (!checkBackwards && k < endSlotNum || checkBackwards && k >= startSlotNum))
{
slot = this.inventorySlots.get(k);
itemstack1 = slot.getStack();

if (itemstack1 != ItemStack.EMPTY && itemstack1.getItem() == itemStack.getItem() && (!itemStack.getHasSubtypes() || itemStack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(itemStack, itemstack1) && slot.isItemValid(itemStack))
{
int l = itemstack1.getCount() + itemStack.getCount();
int effectiveMaxStackSize = getEffectiveMaxStackSizeForSlot(k, itemStack);

if (l <= effectiveMaxStackSize)
{
itemStack.setCount(0);
itemstack1.setCount(l);
slot.onSlotChanged();
didMerge = true;
break;
}
else if (itemstack1.getCount() < effectiveMaxStackSize)
{
itemStack.setCount(itemStack.getCount() - (effectiveMaxStackSize - itemstack1.getCount()));
itemstack1.setCount(effectiveMaxStackSize);
slot.onSlotChanged();
didMerge = true;
break;
}
}

if (checkBackwards)
{
--k;
}
else
{
++k;
}
}
}

if (itemStack.getCount() > 0)
{
if (checkBackwards)
{
k = endSlotNum - 1;
}
else
{
k = startSlotNum;
}

while (!checkBackwards && k < endSlotNum || checkBackwards && k >= startSlotNum)
{
slot = this.inventorySlots.get(k);
itemstack1 = slot.getStack();

if (itemstack1 == ItemStack.EMPTY && slot.isItemValid(itemStack))
{
int effectiveMaxStackSize = getEffectiveMaxStackSizeForSlot(k, itemStack);
ItemStack transferedStack = itemStack.copy();
if (transferedStack.getCount() > effectiveMaxStackSize)
transferedStack.setCount(effectiveMaxStackSize);
slot.putStack(transferedStack);
slot.onSlotChanged();
itemStack.setCount(itemStack.getCount() - transferedStack.getCount());
didMerge = true;
break;
}

if (checkBackwards)
{
--k;
}
else
{
++k;
}
}
}

return didMerge;
}

@Override
public boolean canInteractWith(@Nonnull EntityPlayer player)
{
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void findMatchingClientItemStack()
{
ContainerFoodContainer openFoodContainer = (ContainerFoodContainer) player.openContainer;
ItemStack matchingFoodContainer = openFoodContainer.findFoodContainerWithUUID(itemFoodContainer.getUUID(itemStackFoodContainer));
if (matchingFoodContainer != ItemStack.EMPTY)
if (!matchingFoodContainer.isEmpty())
itemStackFoodContainer = matchingFoodContainer;
}
}
Expand Down
42 changes: 32 additions & 10 deletions java/squeek/spiceoflife/inventory/NBTInventory.java
Expand Up @@ -5,19 +5,24 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.InvWrapper;
import squeek.spiceoflife.helpers.InventoryHelper;
import squeek.spiceoflife.interfaces.ISaveable;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class NBTInventory implements ISaveable, IInventory
public class NBTInventory implements ISaveable, IInventory, ICapabilityProvider
{
protected NonNullList<ItemStack> inventoryItems;
protected INBTInventoryHaver inventoryHaver = null;
Expand Down Expand Up @@ -51,7 +56,7 @@ public NBTInventory(INBTInventoryHaver inventoryHaver)
/*
* IItemHandler compat
*/
public IItemHandlerModifiable getWrapper()
public IItemHandlerModifiable getItemHandler()
{
return new InvWrapper(this);
}
Expand All @@ -75,7 +80,7 @@ public boolean isInventoryEmpty()
{
for (ItemStack itemStack : inventoryItems)
{
if (itemStack != ItemStack.EMPTY)
if (!itemStack.isEmpty())
return false;
}
return true;
Expand All @@ -91,7 +96,7 @@ public boolean isInventoryFull()
{
for (ItemStack itemStack : inventoryItems)
{
if (itemStack == ItemStack.EMPTY || itemStack.getCount() < Math.min(getInventoryStackLimit(), itemStack.getMaxStackSize()))
if (itemStack.isEmpty() || itemStack.getCount() < Math.min(getInventoryStackLimit(), itemStack.getMaxStackSize()))
return false;
}
return true;
Expand Down Expand Up @@ -133,7 +138,7 @@ public ItemStack decrStackSize(int slotNum, int count)
{
ItemStack itemStack = getStackInSlot(slotNum);

if (itemStack != ItemStack.EMPTY)
if (!itemStack.isEmpty())
{
if (itemStack.getCount() <= count)
setInventorySlotContents(slotNum, ItemStack.EMPTY);
Expand Down Expand Up @@ -162,15 +167,15 @@ public void setInventorySlotContents(int slotNum, @Nonnull ItemStack itemStack)
if (!isValidSlotNum(slotNum))
return;

boolean wasEmpty = getStackInSlot(slotNum) == ItemStack.EMPTY;
boolean wasEmpty = getStackInSlot(slotNum).isEmpty();
inventoryItems.set(slotNum, itemStack);

if (itemStack != ItemStack.EMPTY && itemStack.getCount() > getInventoryStackLimit())
if (!itemStack.isEmpty() && itemStack.getCount() > getInventoryStackLimit())
itemStack.setCount(getInventoryStackLimit());

if (wasEmpty && itemStack != ItemStack.EMPTY)
if (wasEmpty && !itemStack.isEmpty())
onSlotFilled(slotNum);
else if (!wasEmpty && itemStack == ItemStack.EMPTY)
else if (!wasEmpty && itemStack.isEmpty())
onSlotEmptied(slotNum);

markDirty();
Expand Down Expand Up @@ -247,7 +252,7 @@ public void writeToNBTData(NBTTagCompound data)
{
ItemStack stack = getStackInSlot(slotNum);

if (stack != ItemStack.EMPTY)
if (!stack.isEmpty())
{
NBTTagCompound item = new NBTTagCompound();
item.setByte("Slot", (byte) slotNum);
Expand Down Expand Up @@ -295,4 +300,21 @@ public int getFieldCount()
public void clear()
{
}

@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing)
{
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
}

@Nullable
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing)
{
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
{
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(getItemHandler());
}
return null;
}
}
32 changes: 32 additions & 0 deletions java/squeek/spiceoflife/inventory/SlotLocked.java
@@ -0,0 +1,32 @@
package squeek.spiceoflife.inventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class SlotLocked extends Slot
{
public SlotLocked(IInventory inventory, int id, int x, int y)
{
super(inventory, id, x, y);
}

@Override
public boolean isItemValid(ItemStack itemStack)
{
return false;
}

@Override
public ItemStack onTake(EntityPlayer thePlayer, ItemStack stack)
{
return ItemStack.EMPTY;
}

@Override
public ItemStack decrStackSize(int amount)
{
return ItemStack.EMPTY;
}
}

0 comments on commit a6b4c82

Please sign in to comment.