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;
|
2025-04-24 18:48:40 +03:00
|
|
|
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;
|
|
|
|
|
import codechicken.nei.recipe.GuiRecipe;
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
/**
|
|
|
|
|
* Simply an {@link ItemStack} with position. Mainly used in the recipe handlers.
|
|
|
|
|
*/
|
2022-06-24 12:32:13 +01:00
|
|
|
public class PositionedStack {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
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;
|
2022-06-24 12:32:13 +01:00
|
|
|
// compatibility dummy
|
2014-04-29 10:54:20 +10:00
|
|
|
public ItemStack item;
|
2022-06-24 12:32:13 +01:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
private boolean permutated = false;
|
2022-06-24 12:32:13 +01:00
|
|
|
|
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;
|
2022-06-24 12:32:13 +01:00
|
|
|
|
2024-02-04 14:09:24 +02:00
|
|
|
if (genPerms) {
|
|
|
|
|
generatePermutations();
|
|
|
|
|
} else {
|
|
|
|
|
setPermutationToRender(0);
|
|
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2022-06-24 12:32:13 +01: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() {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (permutated) return;
|
|
|
|
|
|
2025-01-25 21:16:28 +02:00
|
|
|
List<ItemStack> stacks = new ArrayList<>();
|
2022-06-24 12:32:13 +01:00
|
|
|
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());
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!permutations.isEmpty()) {
|
2022-12-27 03:14:45 +09:00
|
|
|
for (ItemStack stack : permutations) {
|
|
|
|
|
ItemStack toAdd = stack.copy();
|
|
|
|
|
toAdd.stackSize = item.stackSize;
|
|
|
|
|
stacks.add(toAdd);
|
|
|
|
|
}
|
2022-06-24 12:32:13 +01:00
|
|
|
} 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;
|
|
|
|
|
}
|
2022-06-24 12:32:13 +01:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
stacks.add(item.copy());
|
|
|
|
|
}
|
|
|
|
|
items = stacks.toArray(new ItemStack[0]);
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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) {
|
2022-06-24 12:32:13 +01:00
|
|
|
for (ItemStack item : items) if (item.stackSize > i) item.stackSize = i;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2022-06-24 12:32:13 +01:00
|
|
|
|
2021-08-08 11:56:59 -07:00
|
|
|
public PositionedStack copy() {
|
2025-02-08 20:08:36 +02:00
|
|
|
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;
|
|
|
|
|
return pStack;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ItemStack> getFilteredPermutations() {
|
|
|
|
|
return getFilteredPermutations(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ItemStack> getFilteredPermutations(ItemFilter additionalFilter) {
|
2025-04-24 18:48:40 +03:00
|
|
|
List<ItemStack> items = Arrays.asList(this.items);
|
2025-01-25 21:16:28 +02:00
|
|
|
|
2025-04-24 18:48:40 +03: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
|
|
|
|
2025-04-24 18:48:40 +03:00
|
|
|
items.sort(Comparator.comparing(FavoriteRecipes::contains).reversed());
|
2025-01-25 21:16:28 +02:00
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-24 18:48:40 +03:00
|
|
|
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 boolean setPermutationToRender(ItemStack ingredient) {
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index < this.items.length; index++) {
|
|
|
|
|
if (NEIServerUtils.areStacksSameTypeCraftingWithNBT(this.items[index], ingredient)) {
|
|
|
|
|
setPermutationToRender(index);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2022-06-24 12:32:13 +01: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();
|
2024-02-04 14:09:24 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:56:59 -07:00
|
|
|
public boolean contains(ItemStack ingredient) {
|
2022-06-24 12:32:13 +01:00
|
|
|
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) {
|
2022-06-24 12:32:13 +01:00
|
|
|
for (ItemStack item : items) if (NEIServerUtils.areStacksSameTypeCraftingWithNBT(item, ingredient)) 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) {
|
2022-06-24 12:32:13 +01:00
|
|
|
for (ItemStack item : items) if (item.getItem() == ingred) return true;
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-24 12:32:13 +01:00
|
|
|
|
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
|
|
|
}
|