Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import aztech.modern_industrialization.compat.viewer.ReiDraggable;
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.fluid.FluidVariant;
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.item.ItemVariant;
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.storage.base.ResourceAmount;
import aztech.modern_industrialization.util.Simulation;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -50,6 +51,8 @@
public class ConfigurableItemStack extends AbstractConfigurableStack<Item, ItemVariant> implements ItemAccess {
private int adjustedCapacity = 64;

private @Nullable ItemStack cachedItemStack;

public ConfigurableItemStack() {}

public ConfigurableItemStack(CompoundTag compound, HolderLookup.Provider registries) {
Expand Down Expand Up @@ -191,6 +194,32 @@ public ItemVariant getVariant() {
return getResource();
}

@Override
public ItemStack toStack() {
if (cachedItemStack == null) {
cachedItemStack = ItemAccess.super.toStack();
}
return cachedItemStack;
}

@Override
public void setAmount(long amount) {
cachedItemStack = null;
super.setAmount(amount);
}

@Override
public void setKey(ItemVariant key) {
cachedItemStack = null;
super.setKey(key);
}

@Override
public void revertToSnapshot(ResourceAmount<ItemVariant> ra) {
cachedItemStack = null;
super.revertToSnapshot(ra);
}

public class ConfigurableItemSlot extends HackySlot implements ReiDraggable, BackgroundRenderedSlot {
private final Predicate<ItemStack> insertPredicate;
private final Runnable markDirty;
Expand Down Expand Up @@ -231,6 +260,7 @@ protected ItemStack getRealStack() {
protected void setRealStack(ItemStack stack) {
key = ItemVariant.of(stack);
amount = stack.getCount();
cachedItemStack = null;
notifyListeners();
markDirty.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import aztech.modern_industrialization.thirdparty.fabrictransfer.api.item.ItemVariant;
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.transaction.Transaction;
import com.google.common.primitives.Ints;
import java.util.List;
import java.util.Set;
import net.minecraft.world.item.Item;
Expand All @@ -49,7 +48,7 @@ public int getSlots() {

@Override
public ItemStack getStackInSlot(int slot) {
return stacks.get(slot).getVariant().toStack(Ints.saturatedCast(stacks.get(slot).getAmount()));
return stacks.get(slot).toStack();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,32 @@
import com.google.common.primitives.Ints;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.IItemHandler;
import org.jspecify.annotations.Nullable;

public final class SlotItemHandler implements IItemHandler {
private final SingleSlotStorage<ItemVariant> storage;

private @Nullable ItemStack cachedItemStack;

public SlotItemHandler(SingleSlotStorage<ItemVariant> storage) {
this.storage = storage;
}

public SingleSlotStorage<ItemVariant> storage() {
return storage;
}

public record SlotItemHandler(SingleSlotStorage<ItemVariant> storage) implements IItemHandler {
@Override
public int getSlots() {
return 1;
}

@Override
public ItemStack getStackInSlot(int slot) {
return storage.getResource().toStack(Ints.saturatedCast(storage.getAmount()));
if (cachedItemStack == null) {
cachedItemStack = storage.getResource().toStack(Ints.saturatedCast(storage.getAmount()));
}
return cachedItemStack;
}

@Override
Expand All @@ -51,6 +67,9 @@ public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
var inserted = storage.insert(ItemVariant.of(stack), stack.getCount(), tx);
if (!simulate) {
tx.commit();
if (inserted > 0) {
cachedItemStack = null;
}
}
return inserted == 0 ? stack : stack.copyWithCount(stack.getCount() - (int) inserted);
}
Expand All @@ -69,6 +88,9 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) {
var extracted = storage.extract(resource, amount, tx);
if (!simulate) {
tx.commit();
if (extracted > 0) {
cachedItemStack = null;
}
}
return extracted == 0 ? ItemStack.EMPTY : resource.toStack((int) extracted);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static ItemVariant of(ItemStack stack) {
if (stack.isComponentsPatchEmpty() || stack.isEmpty()) {
return of(stack.getItem());
} else {
return new ItemVariantImpl(stack);
return new ItemVariantImpl(stack.copyWithCount(1)); // defensive copy
}
}

Expand All @@ -61,7 +61,7 @@ public static ItemVariant of(ItemStack stack) {
private final int hashCode;

private ItemVariantImpl(ItemStack stack) {
this.stack = stack.copyWithCount(1); // defensive copy
this.stack = stack;
hashCode = ItemStack.hashItemAndComponents(stack);
}

Expand Down
Loading