Skip to content

Commit

Permalink
feat: stop all redirections, and comment heavily
Browse files Browse the repository at this point in the history
  • Loading branch information
bruberu committed Jan 7, 2025
1 parent b17006a commit 52bba03
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/main/java/supersymmetry/common/covers/CoverRestrictive.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -66,35 +66,41 @@ public void renderCover(@NotNull CCRenderState renderState, @NotNull Matrix4 tra
}

protected static class ItemHandlerRestrictive extends ItemHandlerDelegate {
private final Map<ItemStack, Integer> map = new Object2IntOpenCustomHashMap<>(ItemStackHashStrategy.comparingAllButCount());
private final Set<ItemStack> 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;
}
}
}

0 comments on commit 52bba03

Please sign in to comment.