Files
NotEnoughItems/src/main/java/codechicken/nei/PositionedStack.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 lines
5.9 KiB
Java
Raw Normal View History

2014-04-29 10:54:20 +10:00
package codechicken.nei;
2023-09-25 00:35:24 +02:00
import java.util.ArrayList;
2025-01-25 21:16:28 +02:00
import java.util.Arrays;
import java.util.Comparator;
2023-09-25 00:35:24 +02:00
import java.util.List;
2025-01-25 21:16:28 +02:00
import java.util.stream.Collectors;
2023-09-25 00:35:24 +02:00
2014-04-29 10:54:20 +10:00
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
2025-01-25 21:16:28 +02:00
import codechicken.nei.api.ItemFilter;
import codechicken.nei.api.ItemInfo;
2026-04-06 20:39:41 +03:00
import codechicken.nei.recipe.Badge;
2025-01-25 21:16:28 +02:00
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.StackInfo;
2025-01-25 21:16:28 +02:00
2014-04-29 10:54:20 +10:00
/**
* Simply an {@link ItemStack} with position. Mainly used in the recipe handlers.
*/
public class PositionedStack {
2026-03-30 20:36:55 +03:00
/** Maximum chance value representing 100% probability (1 unit = 0.01%). */
public static final int CHANCE_FULL = 10_000;
2014-04-29 10:54:20 +10:00
public int relx;
public int rely;
2019-12-30 20:19:16 -08:00
public ItemStack[] items;
// compatibility dummy
2014-04-29 10:54:20 +10:00
public ItemStack item;
2026-03-30 20:36:55 +03:00
protected int chance = CHANCE_FULL;
2014-04-29 10:54:20 +10:00
private boolean permutated = false;
2021-08-08 11:56:59 -07:00
public PositionedStack(Object object, int x, int y, boolean genPerms) {
2014-04-29 10:54:20 +10:00
items = NEIServerUtils.extractRecipeItems(object);
relx = x;
rely = y;
if (genPerms) {
generatePermutations();
} else {
setPermutationToRender(0);
}
2014-04-29 10:54:20 +10:00
}
public PositionedStack(Object object, int x, int y) {
2014-04-29 10:54:20 +10:00
this(object, x, y, true);
}
2021-08-08 11:56:59 -07:00
public void generatePermutations() {
if (permutated) return;
2025-01-25 21:16:28 +02:00
List<ItemStack> stacks = new ArrayList<>();
for (ItemStack item : items) {
if (item == null || item.getItem() == null) continue;
if (item.getItemDamage() == Short.MAX_VALUE) {
2014-04-29 10:54:20 +10:00
List<ItemStack> permutations = ItemList.itemMap.get(item.getItem());
if (!permutations.isEmpty()) {
for (ItemStack stack : permutations) {
ItemStack toAdd = stack.copy();
toAdd.stackSize = item.stackSize;
stacks.add(toAdd);
}
} else {
2014-04-29 10:54:20 +10:00
ItemStack base = new ItemStack(item.getItem(), item.stackSize);
base.stackTagCompound = item.stackTagCompound;
stacks.add(base);
}
continue;
}
2014-04-29 10:54:20 +10:00
stacks.add(item.copy());
}
items = stacks.toArray(new ItemStack[0]);
if (items.length == 0) items = new ItemStack[] { new ItemStack(Blocks.fire) };
2014-04-29 10:54:20 +10:00
permutated = true;
setPermutationToRender(0);
}
2021-08-08 11:56:59 -07:00
public void setMaxSize(int i) {
for (ItemStack item : items) if (item.stackSize > i) item.stackSize = i;
2014-04-29 10:54:20 +10:00
}
2026-04-06 20:39:41 +03:00
public List<Badge> getBadges() {
return null;
}
2026-03-30 20:36:55 +03:00
public int getChance() {
return this.chance;
}
2026-04-06 20:39:41 +03:00
public void setChance(int chance) {
this.chance = chance;
}
2021-08-08 11:56:59 -07:00
public PositionedStack copy() {
PositionedStack pStack = new PositionedStack(
Arrays.stream(this.items).map(ItemStack::copy).toArray(ItemStack[]::new),
relx,
rely,
false);
2025-01-25 21:16:28 +02:00
pStack.permutated = this.permutated;
2026-04-04 00:03:15 +03:00
pStack.chance = this.chance;
2025-01-25 21:16:28 +02:00
return pStack;
}
public List<ItemStack> getFilteredPermutations() {
return getFilteredPermutations(null);
}
public List<ItemStack> getFilteredPermutations(ItemFilter additionalFilter) {
List<ItemStack> items = Arrays.asList(this.items);
2025-01-25 21:16:28 +02:00
items = filteringPermutations(items, item -> !ItemInfo.isHidden(item));
items = filteringPermutations(items, PresetsList.getItemFilter());
items = filteringPermutations(items, GuiRecipe.getSearchItemFilter());
items = filteringPermutations(items, additionalFilter);
2025-01-25 21:16:28 +02:00
items.sort(Comparator.comparing(FavoriteRecipes::containsManual).reversed());
2025-01-25 21:16:28 +02:00
return items;
}
private List<ItemStack> filteringPermutations(List<ItemStack> items, ItemFilter filter) {
if (filter == null) return items;
final List<ItemStack> filteredItems = items.stream().filter(filter::matches).collect(Collectors.toList());
return filteredItems.isEmpty() ? items : filteredItems;
2025-01-25 21:16:28 +02:00
}
public int getPermutationIndex(ItemStack stack) {
2025-01-25 21:16:28 +02:00
for (int index = 0; index < this.items.length; index++) {
if (NEIServerUtils.areStacksSameType(items[index], stack)) {
return index;
2025-01-25 21:16:28 +02:00
}
}
return -1;
}
public boolean setPermutationToRender(ItemStack ingredient) {
final int stackIndex = getPermutationIndex(ingredient);
if (stackIndex >= 0) {
setPermutationToRender(stackIndex);
}
return stackIndex >= 0;
2014-04-29 10:54:20 +10:00
}
2021-08-08 11:56:59 -07:00
public void setPermutationToRender(int index) {
2025-01-25 21:16:28 +02:00
this.item = this.items[index].copy();
2025-01-25 21:16:28 +02:00
if (this.item.getItem() == null) {
this.item = new ItemStack(Blocks.fire);
} else if (this.item.getItemDamage() == OreDictionary.WILDCARD_VALUE && this.item.getItem().isRepairable()) {
this.item.setItemDamage(0);
}
2014-04-29 10:54:20 +10:00
}
public boolean contains(int mx, int my) {
2025-11-05 00:10:32 +02:00
return mx >= this.relx - 1 && mx < this.relx + 17 && my >= this.rely - 1 && my < this.rely + 17;
}
2021-08-08 11:56:59 -07:00
public boolean contains(ItemStack ingredient) {
for (ItemStack item : items) if (NEIServerUtils.areStacksSameTypeCrafting(item, ingredient)) return true;
2014-04-29 10:54:20 +10:00
return false;
}
2022-02-08 16:29:51 +09:00
/**
* NBT-friendly version of {@link #contains(ItemStack)}
*/
2022-02-08 05:12:26 +09:00
public boolean containsWithNBT(ItemStack ingredient) {
for (ItemStack item : items) if (StackInfo.equalItemAndNBT(item, ingredient, true)) return true;
2022-02-08 05:12:26 +09:00
return false;
}
2021-08-08 11:56:59 -07:00
public boolean contains(Item ingred) {
for (ItemStack item : items) if (item.getItem() == ingred) return true;
2014-04-29 10:54:20 +10:00
return false;
}
2021-08-08 11:56:59 -07:00
@Override
public String toString() {
return "PositionedStack(output='" + item.toString() + "')";
}
2014-04-29 10:54:20 +10:00
}