Skip to content

Commit

Permalink
Change Permutation Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
slprime committed Jan 25, 2025
1 parent af55a6b commit 04f2f5a
Show file tree
Hide file tree
Showing 21 changed files with 453 additions and 251 deletions.
2 changes: 1 addition & 1 deletion src/main/java/codechicken/nei/ItemList.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private List<ItemStack> getPermutations(Item item) {
.filter(
stack -> stack.getItem() != null && stack.getItem().delegate.name() != null
&& !ItemInfo.isHidden(stack))
.collect(Collectors.toCollection(ArrayList::new));
.collect(Collectors.toList());
}

// For optimization it generate itemslist, permutations, orders & collapsibleitems
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/codechicken/nei/ItemStackAmount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package codechicken.nei;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

import codechicken.nei.recipe.StackInfo;

public class ItemStackAmount {

private final Map<NBTTagCompound, Long> itemMap = new LinkedHashMap<>();

public void putAll(ItemStackAmount amounts) {
for (Map.Entry<NBTTagCompound, Long> entry : amounts.itemMap.entrySet()) {
this.itemMap.put(entry.getKey(), entry.getValue() + this.itemMap.getOrDefault(entry.getKey(), 0L));
}
}

public void add(ItemStack item) {
add(item, (long) StackInfo.getAmount(item));
}

public void add(ItemStack stack, Long value) {
if (stack == null || stack.getItem() == null) return;
final NBTTagCompound key = StackInfo.itemStackToNBT(stack, false);

this.itemMap.put(key, value + this.itemMap.getOrDefault(key, 0L));
}

public Long get(ItemStack stack) {
if (stack == null || stack.getItem() == null) return null;
final NBTTagCompound key = StackInfo.itemStackToNBT(stack, false);

return this.itemMap.get(key);
}

public void put(ItemStack stack, long value) {
if (stack == null || stack.getItem() == null) return;
final NBTTagCompound key = StackInfo.itemStackToNBT(stack, false);

this.itemMap.put(key, value);
}

public long getOrDefault(ItemStack stack, long defaultAmount) {
final Long e = get(stack);

return e == null ? defaultAmount : e;
}

public void clear() {
this.itemMap.clear();
}

public Long remove(ItemStack stack) {
if (stack == null || stack.getItem() == null) return null;
final NBTTagCompound key = StackInfo.itemStackToNBT(stack, false);

return this.itemMap.remove(key);
}

public boolean removeIf(Predicate<Map.Entry<NBTTagCompound, Long>> predicate) {
return this.itemMap.entrySet().removeIf(predicate);
}

public Set<Map.Entry<NBTTagCompound, Long>> entrySet() {
return this.itemMap.entrySet();
}

public List<ItemStack> values() {
List<ItemStack> list = new ArrayList<>();

for (Map.Entry<NBTTagCompound, Long> entry : this.itemMap.entrySet()) {
list.add(StackInfo.loadFromNBT(entry.getKey(), Math.max(0, entry.getValue())));
}

return list;
}

public int size() {
return this.itemMap.size();
}

public boolean isEmpty() {
return this.itemMap.isEmpty();
}

public static ItemStackAmount of(ItemStackAmount map) {
ItemStackAmount result = new ItemStackAmount();
result.itemMap.putAll(map.itemMap);
return result;
}

public static ItemStackAmount of(ItemStackMap<Long> map) {
ItemStackAmount result = new ItemStackAmount();

for (ItemStackMap.Entry<Long> entry : map.entries()) {
result.put(entry.key, entry.value);
}

return result;
}

public static ItemStackAmount of(Iterable<ItemStack> items) {
ItemStackAmount result = new ItemStackAmount();

for (ItemStack stack : items) {
result.add(stack);
}

return result;
}

}
14 changes: 14 additions & 0 deletions src/main/java/codechicken/nei/ItemStackMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -222,6 +223,19 @@ public void put(ItemStack key, T value) {
map.put(key, value);
}

public T computeIfAbsent(ItemStack key, Function<ItemStack, ? extends T> mappingFunction) {
T value;
if ((value = get(key)) == null) {
T newValue;
if ((newValue = mappingFunction.apply(key)) != null) {
put(key, newValue);
return newValue;
}
}

return value;
}

public void clear() {
itemMap.clear();
size = 0;
Expand Down
63 changes: 25 additions & 38 deletions src/main/java/codechicken/nei/ItemsTooltipLineHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import static codechicken.lib.gui.GuiDraw.fontRenderer;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;

import org.lwjgl.opengl.GL11;
Expand All @@ -24,8 +19,8 @@

public class ItemsTooltipLineHandler implements ITooltipLineHandler {

protected static int MAX_COLUMNS = 11;
protected static int MARGIN_TOP = 2;
protected static final int MAX_COLUMNS = 11;
protected static final int MARGIN_TOP = 2;

protected String label;
protected EnumChatFormatting labelColor = EnumChatFormatting.GRAY;
Expand All @@ -37,7 +32,7 @@ public class ItemsTooltipLineHandler implements ITooltipLineHandler {
protected int rows = 0;
protected int length = 0;

public ItemsTooltipLineHandler(String label, List<ItemStack> items, boolean saveStackSize) {
public ItemsTooltipLineHandler(String label, List<ItemStack> items) {
this(label, items, true, 5);
}

Expand All @@ -59,7 +54,12 @@ public ItemsTooltipLineHandler(String label, List<ItemStack> items, boolean save
this.length,
Math.min(
this.columns * this.rows,
this.length > MAX_COLUMNS * maxRows ? (MAX_COLUMNS * maxRows - 1) : Integer.MAX_VALUE));
this.length > MAX_COLUMNS * maxRows ? (MAX_COLUMNS * maxRows) : Integer.MAX_VALUE));

if (this.items.size() > this.count) {
String text = "+" + (this.items.size() - this.count);
this.count -= (int) Math.ceil((float) (fontRenderer.getStringWidth(text) - 2) / ItemsGrid.SLOT_SIZE);
}
}

}
Expand All @@ -85,6 +85,7 @@ public void draw(int x, int y) {

fontRenderer.drawStringWithShadow(this.labelColor + this.label + ":", x, y, 0);

GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glPushMatrix();
RenderHelper.enableGUIStandardItemLighting();

Expand All @@ -100,49 +101,35 @@ public void draw(int x, int y) {
String stackSize = !this.saveStackSize || drawStack.stackSize == 0 ? ""
: ReadableNumberConverter.INSTANCE.toWideReadableForm(drawStack.stackSize);

GuiContainerManager
.drawItem(col * ItemsGrid.SLOT_SIZE, row * ItemsGrid.SLOT_SIZE, drawStack, true, stackSize);
drawItem(col * ItemsGrid.SLOT_SIZE, row * ItemsGrid.SLOT_SIZE, drawStack, stackSize);
}

if (this.count < this.items.size()) {
String text = "+" + (this.items.size() - this.count);

fontRenderer.drawStringWithShadow(
"+" + (this.items.size() - this.count),
(this.columns - 1) * ItemsGrid.SLOT_SIZE,
(this.rows - 1) * ItemsGrid.SLOT_SIZE + (ItemsGrid.SLOT_SIZE - fontRenderer.FONT_HEIGHT) / 2 - 1,
text,
MAX_COLUMNS * ItemsGrid.SLOT_SIZE - fontRenderer.getStringWidth(text) - 2,
(this.rows - 1) * ItemsGrid.SLOT_SIZE + (ItemsGrid.SLOT_SIZE - fontRenderer.FONT_HEIGHT) / 2,
0xee555555);

}

GL11.glDisable(GL12.GL_RESCALE_NORMAL);
GL11.glPopMatrix();
GL11.glPopAttrib();
}

private List<ItemStack> groupingItemStacks(List<ItemStack> items) {
final Map<String, Integer> count = new HashMap<>();
final Map<String, NBTTagCompound> unique = new LinkedHashMap<>();
final List<ItemStack> result = new ArrayList<>();

for (ItemStack stack : items) {
final NBTTagCompound nbTag = StackInfo.itemStackToNBT(stack, true);
if (nbTag == null) continue;

final String GUID = StackInfo.getItemStackGUID(stack);

if (!unique.containsKey(GUID)) {
count.put(GUID, nbTag.getInteger("Count"));
unique.put(GUID, nbTag);
} else {
count.put(GUID, count.get(GUID) + nbTag.getInteger("Count"));
}
}
protected void drawItem(int x, int y, ItemStack drawStack, String stackSize) {
GuiContainerManager.drawItem(x, y, drawStack, true, stackSize);
}

for (String GUID : unique.keySet()) {
ItemStack stack = StackInfo.loadFromNBT(unique.get(GUID), count.get(GUID));
private List<ItemStack> groupingItemStacks(List<ItemStack> items) {
final List<ItemStack> result = ItemStackAmount.of(items).values();

if (unique.get(GUID).hasKey("gtFluidName")) {
for (ItemStack stack : result) {
if (StackInfo.itemStackToNBT(stack).hasKey("gtFluidName")) {
stack.stackSize = 0;
}

result.add(stack);
}

return result;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/codechicken/nei/LRUCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package codechicken.nei;

import java.util.LinkedHashMap;
import java.util.Map;

// Simple LRUCache
public class LRUCache<K, V> extends LinkedHashMap<K, V> {

private final int capacity;

public LRUCache(int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > capacity;
}

}
Loading

0 comments on commit 04f2f5a

Please sign in to comment.