Add Recipe Groups to GuiRecipe (#1049)

This commit is contained in:
slprime authored and GitHub committed 2026-09-27 07:39:27 +03:00
1 parent 3c472a02cb
commit a54c414595
16 files changed
+1464 -840

No files matched your search

@@ -1,121 +1,242 @@
package codechicken.nei.recipe;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Collections;
import java.util.List;
import net.minecraft.init.Items;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemDye;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.RecipeFireworks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import codechicken.lib.gui.GuiDraw;
import codechicken.nei.InventoryCraftingDummy;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.PositionedStack;
import codechicken.nei.guihook.GuiContainerManager;
public class FireworkRecipeHandler extends ShapelessRecipeHandler {
public class CachedFireworkRecipe extends CachedShapelessRecipe {
final LinkedList<Object> itemList = new LinkedList<>();
public final Object[] baseIngredients;
public final Object extraIngred;
public final int recipeType;
public final Object groupId;
public CachedFireworkRecipe(Object[] base, Object extra, int type) {
super(new ItemStack(Items.fireworks));
this.baseIngredients = base;
this.extraIngred = extra;
public CachedFireworkRecipe(List<ItemStack> ingredients, ItemStack result, int type, Object groupId) {
super(ingredients, result);
this.recipeType = type;
cycle();
}
public void cycle() {
itemList.clear();
itemList.addAll(Arrays.asList(baseIngredients));
int extras = (cycleticks / 40) % (10 - itemList.size());
for (int i = 0; i < extras; i++) itemList.add(extraIngred);
setIngredients(itemList);
List<PositionedStack> ingreds = getIngredients();
for (int i = 0; i < 9; i++)
inventoryCrafting.setInventorySlotContents(i, i < ingreds.size() ? ingreds.get(i).item : null);
if (!recipeFireworks.matches(inventoryCrafting, null)) throw new RuntimeException("Invalid Recipe?");
setResult(recipeFireworks.getCraftingResult(null));
this.groupId = groupId;
}
}
// indexed by the "Type" explosion tag
private static final ItemStack[] SHAPES = { null, new ItemStack(Items.fire_charge),
new ItemStack(Items.gold_nugget), new ItemStack(Items.skull, 1, Short.MAX_VALUE),
new ItemStack(Items.feather) };
private final InventoryCrafting inventoryCrafting = new InventoryCraftingDummy();
private final RecipeFireworks recipeFireworks = new RecipeFireworks();
public final ArrayList<CachedFireworkRecipe> mfireworks = new ArrayList<>();
private ItemStack usageIngredient = null;
public FireworkRecipeHandler() {
super();
stackorder = new int[][] { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 1, 2 },
{ 2, 2 } };
loadAllFireworks();
}
private void loadAllFireworks() {
// charges
Item[] shapes = new Item[] { null, Items.fire_charge, Items.gold_nugget, Items.feather, Items.skull };
Item[] effects = new Item[] { null, Items.diamond, Items.glowstone_dust };
for (Item shape : shapes)
for (Item effect : effects) genRecipe(Items.gunpowder, shape, effect, Items.dye, Items.dye, 0);
// fireworks
genRecipe(Items.gunpowder, Items.paper, Items.firework_charge, 2);
genRecipe(Items.gunpowder, Items.gunpowder, Items.paper, Items.firework_charge, 2);
genRecipe(Items.gunpowder, Items.gunpowder, Items.gunpowder, Items.paper, Items.firework_charge, 2);
// setup a valid charge to use for the recolour recipe
for (int i = 0; i < 9; i++) inventoryCrafting.setInventorySlotContents(i, null);
inventoryCrafting.setInventorySlotContents(0, new ItemStack(Items.gunpowder));
inventoryCrafting.setInventorySlotContents(1, new ItemStack(Items.dye));
recipeFireworks.matches(inventoryCrafting, null);
ItemStack charge = recipeFireworks.getCraftingResult(null);
genRecipe(charge, Items.dye, Items.dye, 1);
@Override
public Object getRecipeGroupId(int recipe) {
return this.arecipes.get(recipe) instanceof CachedFireworkRecipe firework ? firework.groupId : null;
}
private void genRecipe(Object... params) {
int numIngreds = 0;
for (int i = 0; i < params.length - 2; i++) if (params[i] != null) numIngreds++;
private void loadChargeRecipes() {
final List<List<ItemStack>> dyeSets = new ArrayList<>();
for (int i = 0; i < params.length - 1; i++)
if (params[i] instanceof Item) params[i] = new ItemStack((Item) params[i], 1, Short.MAX_VALUE);
for (int dye = 0; dye < ItemDye.field_150922_c.length; dye++) {
dyeSets.add(Collections.singletonList(new ItemStack(Items.dye, 1, dye)));
}
Object[] ingreds = new Object[numIngreds];
for (int i = 0, j = 0; i < params.length - 2; i++) if (params[i] != null) ingreds[j++] = params[i];
for (ItemStack shape : SHAPES) {
for (int effect = 0; effect < 4; effect++) {
addRecipes(getChargeBase(shape, (effect & 1) != 0, (effect & 2) != 0), dyeSets, 0);
}
}
mfireworks
.add(new CachedFireworkRecipe(ingreds, params[params.length - 2], (Integer) params[params.length - 1]));
final ItemStack charge = craft(Arrays.asList(new ItemStack(Items.gunpowder), new ItemStack(Items.dye)));
addRecipes(Collections.singletonList(charge), dyeSets, 1);
}
private void loadRocketRecipes() {
final ItemStack charge = craft(Arrays.asList(new ItemStack(Items.gunpowder), new ItemStack(Items.dye)));
final List<List<ItemStack>> chargeSets = new ArrayList<>();
for (int count = 0; count < 9; count++) {
chargeSets.add(Collections.nCopies(count, charge));
}
for (int flight = 1; flight <= 3; flight++) {
addRecipes(getRocketBase(flight), chargeSets, 2);
}
}
private void loadExactChargeRecipes(NBTTagCompound explosion) {
final byte type = explosion.getByte("Type");
if (explosion.hasKey("FadeColors")) {
final NBTTagCompound baseTag = new NBTTagCompound();
final ItemStack charge = new ItemStack(Items.firework_charge);
baseTag.setTag("Explosion", explosion.copy());
baseTag.getCompoundTag("Explosion").removeTag("FadeColors");
charge.setTagCompound(baseTag);
addRecipes(Collections.singletonList(charge), getDyeSets(explosion.getIntArray("FadeColors")), 1);
} else if (type >= 0 && type < SHAPES.length) {
addRecipes(
getChargeBase(SHAPES[type], explosion.getBoolean("Flicker"), explosion.getBoolean("Trail")),
getDyeSets(explosion.getIntArray("Colors")),
0);
}
}
private void loadExactRocketRecipes(NBTTagCompound fireworks) {
final NBTTagList explosions = fireworks.getTagList("Explosions", 10);
final List<ItemStack> charges = new ArrayList<>();
for (int i = 0; i < explosions.tagCount(); i++) {
final ItemStack charge = new ItemStack(Items.firework_charge);
charge.setTagCompound(new NBTTagCompound());
charge.getTagCompound().setTag("Explosion", explosions.getCompoundTagAt(i));
charges.add(charge);
}
addRecipes(getRocketBase(fireworks.getByte("Flight")), Collections.singletonList(charges), 2);
}
private static List<ItemStack> getChargeBase(ItemStack shape, boolean flicker, boolean trail) {
final List<ItemStack> base = new ArrayList<>();
base.add(new ItemStack(Items.gunpowder));
if (shape != null) {
base.add(shape);
}
if (flicker) {
base.add(new ItemStack(Items.glowstone_dust));
}
if (trail) {
base.add(new ItemStack(Items.diamond));
}
return base;
}
private static List<ItemStack> getRocketBase(int flight) {
final List<ItemStack> base = new ArrayList<>();
for (int i = 0; i < flight; i++) {
base.add(new ItemStack(Items.gunpowder));
}
base.add(new ItemStack(Items.paper));
return base;
}
private static List<List<ItemStack>> getDyeSets(int[] colors) {
final List<ItemStack> dyes = new ArrayList<>();
for (int color : colors) {
for (int dye = 0; dye < ItemDye.field_150922_c.length; dye++) {
if (ItemDye.field_150922_c[dye] == color) {
dyes.add(new ItemStack(Items.dye, 1, dye));
break;
}
}
}
if (dyes.isEmpty() || dyes.size() != colors.length) {
return Collections.emptyList();
}
if (Arrays.stream(colors).distinct().count() > 1) {
return Collections.singletonList(dyes);
}
final List<List<ItemStack>> dyeSets = new ArrayList<>();
for (int count = 1; count < 9; count++) {
dyeSets.add(Collections.nCopies(count, dyes.get(0)));
}
return dyeSets;
}
private void addRecipes(List<ItemStack> base, List<List<ItemStack>> extraSets, int type) {
final Object groupId = new Object();
for (List<ItemStack> extras : extraSets) {
final List<ItemStack> ingredients = new ArrayList<>(base);
ingredients.addAll(extras);
final ItemStack result = craft(ingredients);
if (result != null) {
final CachedFireworkRecipe recipe = new CachedFireworkRecipe(ingredients, result, type, groupId);
if (usageIngredient == null || recipe.contains(recipe.ingredients, usageIngredient)) {
arecipes.add(recipe);
}
}
}
}
private ItemStack craft(List<ItemStack> ingredients) {
if (ingredients.size() > 9) {
return null;
}
for (int i = 0; i < 9; i++) {
inventoryCrafting.setInventorySlotContents(i, i < ingredients.size() ? ingredients.get(i) : null);
}
return recipeFireworks.matches(inventoryCrafting, null) ? recipeFireworks.getCraftingResult(inventoryCrafting)
: null;
}
@Override
public void loadCraftingRecipes(ItemStack result) {
for (CachedFireworkRecipe recipe : mfireworks) {
if (recipe.result.item.getItem() == result.getItem()) {
recipe.cycle();
arecipes.add(recipe);
final NBTTagCompound tag = result.getTagCompound();
if (result.getItem() == Items.firework_charge) {
if (tag != null) {
loadExactChargeRecipes(tag.getCompoundTag("Explosion"));
}
if (arecipes.isEmpty()) {
loadChargeRecipes();
}
} else if (result.getItem() == Items.fireworks) {
if (tag != null) {
loadExactRocketRecipes(tag.getCompoundTag("Fireworks"));
}
if (arecipes.isEmpty()) {
loadRocketRecipes();
}
}
// show random recolouring recipes as well
}
@Override
public void loadCraftingRecipes(String outputId, Object... results) {
if (outputId.equals("crafting") && getClass() == FireworkRecipeHandler.class) {
arecipes.addAll(mfireworks);
loadChargeRecipes();
loadRocketRecipes();
} else {
super.loadCraftingRecipes(outputId, results);
}
@@ -123,20 +244,9 @@ public class FireworkRecipeHandler extends ShapelessRecipeHandler {
@Override
public void loadUsageRecipes(ItemStack ingredient) {
for (CachedFireworkRecipe recipe : mfireworks) {
if (recipe.contains(recipe.ingredients, ingredient)) {
recipe.cycle();
arecipes.add(recipe);
}
}
}
@Override
public void onUpdate() {
if (!NEIClientUtils.shiftKey()) {
cycleticks++;
if (cycleticks % 20 == 0) for (CachedRecipe crecipe : arecipes) ((CachedFireworkRecipe) crecipe).cycle();
}
usageIngredient = ingredient;
loadChargeRecipes();
loadRocketRecipes();
}
@Override
@@ -147,11 +257,8 @@ public class FireworkRecipeHandler extends ShapelessRecipeHandler {
@Override
public List<String> handleTooltip(GuiRecipe<?> gui, List<String> currenttip, int recipe) {
currenttip = super.handleTooltip(gui, currenttip, recipe);
Point mousepos = GuiDraw.getMousePosition();
Point relMouse = new Point(mousepos.x - gui.guiLeft, mousepos.y - gui.guiTop);
Point recipepos = gui.getRecipePosition(recipe);
if (currenttip.isEmpty() && GuiContainerManager.getStackMouseOver(gui) == null
&& new Rectangle(recipepos.x, recipepos.y, 166, 55).contains(relMouse))
&& new Rectangle(0, 0, 166, 55).contains(gui.getRecipeMousePosition(recipe)))
currenttip.add(
NEIClientUtils.translate(
"recipe.firework.tooltip" + ((CachedFireworkRecipe) arecipes.get(recipe)).recipeType));