From 52bba0323d4cbbf47c786a4c71c2307109ece0ad Mon Sep 17 00:00:00 2001 From: bruberu <80226372+bruberu@users.noreply.github.com> Date: Mon, 6 Jan 2025 19:21:26 -0600 Subject: [PATCH] feat: stop all redirections, and comment heavily --- .../common/covers/CoverRestrictive.java | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/main/java/supersymmetry/common/covers/CoverRestrictive.java b/src/main/java/supersymmetry/common/covers/CoverRestrictive.java index 79e3e6d08..52b4d5575 100644 --- a/src/main/java/supersymmetry/common/covers/CoverRestrictive.java +++ b/src/main/java/supersymmetry/common/covers/CoverRestrictive.java @@ -9,21 +9,21 @@ import gregtech.api.cover.CoverDefinition; import gregtech.api.cover.CoverableView; import gregtech.api.util.ItemStackHashStrategy; -import it.unimi.dsi.fastutil.objects.Object2IntArrayMap; -import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap; +import it.unimi.dsi.fastutil.ints.IntArraySet; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArraySet; +import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet; import net.minecraft.item.ItemStack; import net.minecraft.util.BlockRenderLayer; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.ItemHandlerHelper; import org.jetbrains.annotations.NotNull; -import supersymmetry.api.recipes.catalysts.CatalystInfo; import supersymmetry.client.renderer.textures.SusyTextures; -import java.util.Map; +import java.util.Set; public class CoverRestrictive extends CoverBase { @@ -66,35 +66,41 @@ public void renderCover(@NotNull CCRenderState renderState, @NotNull Matrix4 tra } protected static class ItemHandlerRestrictive extends ItemHandlerDelegate { - private final Map map = new Object2IntOpenCustomHashMap<>(ItemStackHashStrategy.comparingAllButCount()); + private final Set set = new ObjectOpenCustomHashSet<>(ItemStackHashStrategy.comparingAllButCount()); public ItemHandlerRestrictive(IItemHandler delegate) { super(delegate); } + @NotNull @Override public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) { - if (stack.isEmpty() || stack.isItemEqual(getStackInSlot(slot))) { + // Check if the current slot already has the item (by checking if it stacks). If not: + // Check if it happens to be somewhere else. This is cached in a set. + // If it is, we reject the stack, but otherwise, we let it through. + // (The whole point is to prevent more than one slot from automatically filling up.) + + if (!getStackInSlot(slot).isEmpty() && ItemHandlerHelper.canItemStacksStack(stack, getStackInSlot(slot))) { return super.insertItem(slot, stack, simulate); } - // Makes things more efficient for common items. - if (map.containsKey(stack)) { - int location = map.get(stack); - if (stack.isItemEqual(getStackInSlot(location))) { - return super.insertItem(location, stack, simulate); + if (getStackInSlot(slot).isEmpty()) { + if (set.contains(stack)) { + return stack; } else { - map.remove(stack); - } - } - // If it's not already in the map of what goes where, we search if it happens to be anywhere already, for some reason. - for (int i = 0; i < getSlots(); i++) { - if (i != slot && stack.isItemEqual(getStackInSlot(i))) { - map.put(stack, i); - return super.insertItem(i, stack, simulate); + // If it's not already in the set of what goes where, we search if it happens to be anywhere already, for some reason. + for (int i = 0; i < getSlots(); i++) { + if (i != slot && ItemHandlerHelper.canItemStacksStack(stack, getStackInSlot(slot))) { + set.add(stack); + return stack; + } + } + // OK, we let it through now that we know it's not anywhere else. + return super.insertItem(slot, stack, simulate); } } - return super.insertItem(slot, stack, simulate); + // It simply wouldn't even fit in that slot anyway. + return stack; } } }