Skip to content

Commit

Permalink
3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CodexAdrian committed Mar 22, 2024
1 parent 1272d21 commit 44e37f7
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 23 deletions.
4 changes: 2 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 3.1.0

- Cross platform support for Item inventories
- Improved Item API
- Added support for custom capabilities
-
- 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)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package earth.terrarium.botarium.common.generic.util;
package earth.terrarium.botarium.common.generic.utils;

import net.minecraft.world.Clearable;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +15,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;

public class SimpleItemContainer implements ItemContainer, Serializable, Snapshotable<ItemSnapshot> {
public class SimpleItemContainer implements ItemContainer, ItemContainerExtras, Serializable, Snapshotable<ItemSnapshot> {
private final NonNullList<ItemStack> stacks;
private Runnable onUpdate = () -> {};

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand All @@ -182,6 +189,7 @@ public SimpleItemSnapshot() {

@Override
public void loadSnapshot() {
SimpleItemContainer.this.stacks.clear();
SimpleItemContainer.this.deserialize(tag);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T extends ItemContainer & ItemContainerExtras> 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);
}
}
2 changes: 1 addition & 1 deletion common/src/test/java/testmod/ManaContainer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions common/src/test/java/testmod/TestBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ public int getSlotLimit(int slot) {

@Override
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
Iterator<StorageView<ItemVariant>> it = storage.iterator();
for (int i = 0; i < slot; i++) {
it.next();
}
return it.next().getResource().matches(stack);
return true;
}

@Override
Expand Down

0 comments on commit 44e37f7

Please sign in to comment.