diff --git a/changelog.md b/changelog.md index d1088121..94b53264 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # 3.1.0 - Cross platform support for Item inventories -- Improved Item API - Added support for custom capabilities -- \ No newline at end of file +- Support for entity capabilities (Item Container only for now while in testing) +- New API format for containers (Feedback is welcome! For ItemApi only for now) \ No newline at end of file diff --git a/common/src/main/java/earth/terrarium/botarium/common/generic/util/AmountBasedContainer.java b/common/src/main/java/earth/terrarium/botarium/common/generic/utils/AmountBasedContainer.java similarity index 86% rename from common/src/main/java/earth/terrarium/botarium/common/generic/util/AmountBasedContainer.java rename to common/src/main/java/earth/terrarium/botarium/common/generic/utils/AmountBasedContainer.java index a9ae9a95..7e965260 100644 --- a/common/src/main/java/earth/terrarium/botarium/common/generic/util/AmountBasedContainer.java +++ b/common/src/main/java/earth/terrarium/botarium/common/generic/utils/AmountBasedContainer.java @@ -1,4 +1,4 @@ -package earth.terrarium.botarium.common.generic.util; +package earth.terrarium.botarium.common.generic.utils; import net.minecraft.world.Clearable; diff --git a/common/src/main/java/earth/terrarium/botarium/common/generic/util/StackBasedContainer.java b/common/src/main/java/earth/terrarium/botarium/common/generic/utils/StackBasedContainer.java similarity index 90% rename from common/src/main/java/earth/terrarium/botarium/common/generic/util/StackBasedContainer.java rename to common/src/main/java/earth/terrarium/botarium/common/generic/utils/StackBasedContainer.java index 1d8eeb46..510ed863 100644 --- a/common/src/main/java/earth/terrarium/botarium/common/generic/util/StackBasedContainer.java +++ b/common/src/main/java/earth/terrarium/botarium/common/generic/utils/StackBasedContainer.java @@ -1,4 +1,4 @@ -package earth.terrarium.botarium.common.generic.util; +package earth.terrarium.botarium.common.generic.utils; import net.minecraft.world.Clearable; import org.jetbrains.annotations.NotNull; diff --git a/common/src/main/java/earth/terrarium/botarium/common/item/base/ItemContainerExtras.java b/common/src/main/java/earth/terrarium/botarium/common/item/base/ItemContainerExtras.java new file mode 100644 index 00000000..dbc47e9f --- /dev/null +++ b/common/src/main/java/earth/terrarium/botarium/common/item/base/ItemContainerExtras.java @@ -0,0 +1,7 @@ +package earth.terrarium.botarium.common.item.base; + +import net.minecraft.world.item.ItemStack; + +public interface ItemContainerExtras { + void setStackInSlot(int slot, ItemStack stack); +} diff --git a/common/src/main/java/earth/terrarium/botarium/common/item/impl/SimpleItemContainer.java b/common/src/main/java/earth/terrarium/botarium/common/item/impl/SimpleItemContainer.java index fe8042e2..e1d87ba6 100644 --- a/common/src/main/java/earth/terrarium/botarium/common/item/impl/SimpleItemContainer.java +++ b/common/src/main/java/earth/terrarium/botarium/common/item/impl/SimpleItemContainer.java @@ -1,6 +1,7 @@ package earth.terrarium.botarium.common.item.impl; import earth.terrarium.botarium.common.item.base.ItemContainer; +import earth.terrarium.botarium.common.item.base.ItemContainerExtras; import earth.terrarium.botarium.common.item.base.ItemSnapshot; import earth.terrarium.botarium.util.Serializable; import earth.terrarium.botarium.util.Snapshotable; @@ -14,7 +15,7 @@ import net.minecraft.world.level.block.entity.BlockEntity; import org.jetbrains.annotations.NotNull; -public class SimpleItemContainer implements ItemContainer, Serializable, Snapshotable { +public class SimpleItemContainer implements ItemContainer, ItemContainerExtras, Serializable, Snapshotable { private final NonNullList stacks; private Runnable onUpdate = () -> {}; @@ -82,27 +83,28 @@ public boolean isItemValid(int slot, @NotNull ItemStack stack) { break; } } - return initial.copyWithCount(insertedAmount); + return insertedAmount == 0 ? ItemStack.EMPTY : initial.copyWithCount(insertedAmount); } @Override public @NotNull ItemStack insertIntoSlot(int slot, @NotNull ItemStack stack, boolean simulate) { if (stack.isEmpty() || !isItemValid(slot, stack)) return ItemStack.EMPTY; - ItemStack itemStack = stacks.get(slot); + ItemStack itemStack = stacks.get(slot).copy(); if (itemStack.isEmpty()) { int amount = Math.min(stack.getCount(), getSlotLimit(slot)); - itemStack = stack.copyWithCount(amount); if (!simulate) { - stacks.set(slot, itemStack); + stacks.set(slot, stack.copyWithCount(amount)); } - return itemStack; + return stack.copyWithCount(amount); } else if (ItemStack.isSameItemSameTags(stack, itemStack)) { - int amount = Math.min(stack.getCount(), itemStack.getMaxStackSize() - itemStack.getCount()); - if (!simulate) { - itemStack.grow(amount); - stacks.set(slot, itemStack); + int amount = Math.min(stack.getCount(), getSlotLimit(slot) - itemStack.getCount()); + if (amount > 0) { + if (!simulate) { + itemStack.grow(amount); + stacks.set(slot, itemStack); + } + return stack.copyWithCount(amount); } - return stack.copyWithCount(amount); } return ItemStack.EMPTY; } @@ -173,6 +175,11 @@ public void update() { onUpdate.run(); } + @Override + public void setStackInSlot(int slot, ItemStack stack) { + stacks.set(slot, stack); + } + public class SimpleItemSnapshot implements ItemSnapshot { CompoundTag tag; @@ -182,6 +189,7 @@ public SimpleItemSnapshot() { @Override public void loadSnapshot() { + SimpleItemContainer.this.stacks.clear(); SimpleItemContainer.this.deserialize(tag); } } diff --git a/common/src/main/java/earth/terrarium/botarium/common/item/utils/SlotItemContainer.java b/common/src/main/java/earth/terrarium/botarium/common/item/utils/SlotItemContainer.java new file mode 100644 index 00000000..9692a1fa --- /dev/null +++ b/common/src/main/java/earth/terrarium/botarium/common/item/utils/SlotItemContainer.java @@ -0,0 +1,78 @@ +package earth.terrarium.botarium.common.item.utils; + +import earth.terrarium.botarium.common.item.base.ItemContainer; +import earth.terrarium.botarium.common.item.base.ItemContainerExtras; +import net.minecraft.world.Container; +import net.minecraft.world.SimpleContainer; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.NotNull; + +public class SlotItemContainer extends Slot { + private static final Container EMPTY = new SimpleContainer(0); + private final T container; + + public SlotItemContainer(T container, int slot, int x, int y) { + super(EMPTY, slot, x, y); + this.container = container; + } + + @Override + public boolean mayPlace(@NotNull ItemStack stack) { + if (stack.isEmpty()) + return false; + return container.isItemValid(index, stack); + } + + @Override + @NotNull + public ItemStack getItem() { + return container.getStackInSlot(index); + } + + // Override if your IItemHandler does not implement IItemHandlerModifiable + @Override + public void set(@NotNull ItemStack stack) { + container.setStackInSlot(index, stack); + this.setChanged(); + } + + // Override if your IItemHandler does not implement IItemHandlerModifiable + // @Override + public void initialize(ItemStack stack) { + container.setStackInSlot(index, stack); + this.setChanged(); + } + + @Override + public void onQuickCraft(@NotNull ItemStack oldStackIn, @NotNull ItemStack newStackIn) { + } + + @Override + public int getMaxStackSize() { + return container.getSlotLimit(this.index); + } + + @Override + public int getMaxStackSize(@NotNull ItemStack stack) { + int maxInput = stack.getMaxStackSize(); + ItemStack maxAdd = stack.copyWithCount(maxInput); + ItemStack currentStack = container.getStackInSlot(index); + container.setStackInSlot(index, ItemStack.EMPTY); + ItemStack remainder = container.insertIntoSlot(index, maxAdd, true); + container.setStackInSlot(index, currentStack); + return maxInput - remainder.getCount(); + } + + @Override + public boolean mayPickup(Player playerIn) { + return !container.extractFromSlot(index, 1, true).isEmpty(); + } + + @Override + @NotNull + public ItemStack remove(int amount) { + return container.extractFromSlot(index, amount, false); + } +} diff --git a/common/src/test/java/testmod/ManaContainer.java b/common/src/test/java/testmod/ManaContainer.java index 7b00af04..198e48bd 100644 --- a/common/src/test/java/testmod/ManaContainer.java +++ b/common/src/test/java/testmod/ManaContainer.java @@ -1,6 +1,6 @@ package testmod; -import earth.terrarium.botarium.common.generic.util.AmountBasedContainer; +import earth.terrarium.botarium.common.generic.utils.AmountBasedContainer; import earth.terrarium.botarium.util.Serializable; import net.minecraft.nbt.CompoundTag; import net.minecraft.util.Mth; diff --git a/common/src/test/java/testmod/TestBlockEntity.java b/common/src/test/java/testmod/TestBlockEntity.java index fdfe0393..6dcc8f7d 100644 --- a/common/src/test/java/testmod/TestBlockEntity.java +++ b/common/src/test/java/testmod/TestBlockEntity.java @@ -90,8 +90,8 @@ public void tick() { ItemStack itemStack = container.extractItem(1, true); ItemStack inserted = thisContainer.insertItem(itemStack, true); if (!itemStack.isEmpty() && itemStack.getCount() == inserted.getCount()) { - ItemStack itemStack1 = container.extractItem(1, false); - thisContainer.insertItem(itemStack1, false); + container.extractItem(1, false); + thisContainer.insertItem(inserted, false); } } } diff --git a/fabric/src/main/java/earth/terrarium/botarium/fabric/item/PlatformItemContainer.java b/fabric/src/main/java/earth/terrarium/botarium/fabric/item/PlatformItemContainer.java index 136ca3dd..7f10d1a8 100644 --- a/fabric/src/main/java/earth/terrarium/botarium/fabric/item/PlatformItemContainer.java +++ b/fabric/src/main/java/earth/terrarium/botarium/fabric/item/PlatformItemContainer.java @@ -56,11 +56,7 @@ public int getSlotLimit(int slot) { @Override public boolean isItemValid(int slot, @NotNull ItemStack stack) { - Iterator> it = storage.iterator(); - for (int i = 0; i < slot; i++) { - it.next(); - } - return it.next().getResource().matches(stack); + return true; } @Override