mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-17 02:55:04 +00:00
224 lines
8.1 KiB
Java
224 lines
8.1 KiB
Java
package codechicken.nei.recipe;
|
|
|
|
import java.awt.Rectangle;
|
|
import java.lang.invoke.MethodHandle;
|
|
import java.lang.invoke.MethodHandles;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
|
import net.minecraft.client.gui.inventory.GuiCrafting;
|
|
import net.minecraft.inventory.Container;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.crafting.CraftingManager;
|
|
import net.minecraft.item.crafting.IRecipe;
|
|
import net.minecraft.item.crafting.ShapedRecipes;
|
|
import net.minecraftforge.oredict.ShapedOreRecipe;
|
|
|
|
import codechicken.nei.NEIClientUtils;
|
|
import codechicken.nei.NEIServerUtils;
|
|
import codechicken.nei.PositionedStack;
|
|
import codechicken.nei.api.DefaultOverlayRenderer;
|
|
import codechicken.nei.api.IOverlayHandler;
|
|
import codechicken.nei.api.IRecipeOverlayRenderer;
|
|
import codechicken.nei.api.IStackPositioner;
|
|
|
|
public class ShapedRecipeHandler extends TemplateRecipeHandler {
|
|
|
|
private static final MethodHandle SHAPED_ORE_RECIPE_WIDTH, SHAPED_ORE_RECIPE_HEIGHT;
|
|
|
|
static {
|
|
try {
|
|
final var lookup = MethodHandles.lookup();
|
|
final var widthField = ShapedOreRecipe.class.getDeclaredField("width");
|
|
final var heightField = ShapedOreRecipe.class.getDeclaredField("height");
|
|
|
|
widthField.setAccessible(true);
|
|
heightField.setAccessible(true);
|
|
|
|
SHAPED_ORE_RECIPE_WIDTH = lookup.unreflectGetter(widthField);
|
|
SHAPED_ORE_RECIPE_HEIGHT = lookup.unreflectGetter(heightField);
|
|
} catch (IllegalAccessException | NoSuchFieldException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public class CachedShapedRecipe extends CachedRecipe {
|
|
|
|
public ArrayList<PositionedStack> ingredients;
|
|
public PositionedStack result;
|
|
|
|
public CachedShapedRecipe(int width, int height, Object[] items, ItemStack out) {
|
|
result = new PositionedStack(out, 119, 24);
|
|
ingredients = new ArrayList<>();
|
|
setIngredients(width, height, items);
|
|
}
|
|
|
|
public CachedShapedRecipe(ShapedRecipes recipe) {
|
|
this(recipe.recipeWidth, recipe.recipeHeight, recipe.recipeItems, recipe.getRecipeOutput());
|
|
}
|
|
|
|
public void setIngredients(int width, int height, Object[] items) {
|
|
for (int x = 0; x < width; x++) {
|
|
for (int y = 0; y < height; y++) {
|
|
if (items[y * width + x] == null) continue;
|
|
|
|
PositionedStack stack = new PositionedStack(items[y * width + x], 25 + x * 18, 6 + y * 18, false);
|
|
stack.setMaxSize(1);
|
|
ingredients.add(stack);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<PositionedStack> getIngredients() {
|
|
return ingredients;
|
|
}
|
|
|
|
public PositionedStack getResult() {
|
|
return result;
|
|
}
|
|
|
|
public void computeVisuals() {
|
|
for (PositionedStack p : ingredients) p.generatePermutations();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadTransferRects() {
|
|
transferRects.add(new RecipeTransferRect(new Rectangle(84, 23, 24, 18), "crafting"));
|
|
}
|
|
|
|
@Override
|
|
public Class<? extends GuiContainer> getGuiClass() {
|
|
return GuiCrafting.class;
|
|
}
|
|
|
|
@Override
|
|
public String getRecipeName() {
|
|
return NEIClientUtils.translate("recipe.shaped");
|
|
}
|
|
|
|
@Override
|
|
public void loadCraftingRecipes(String outputId, Object... results) {
|
|
if (outputId.equals("crafting") && getClass() == ShapedRecipeHandler.class) {
|
|
for (IRecipe irecipe : CraftingManager.getInstance().getRecipeList()) {
|
|
CachedShapedRecipe recipe = null;
|
|
|
|
if (irecipe instanceof ShapedRecipes shaped) {
|
|
recipe = new CachedShapedRecipe(shaped);
|
|
} else if (irecipe instanceof ShapedOreRecipe shaped) {
|
|
recipe = forgeShapedRecipe(shaped);
|
|
}
|
|
|
|
if (recipe != null) {
|
|
recipe.computeVisuals();
|
|
arecipes.add(recipe);
|
|
}
|
|
}
|
|
} else if (outputId.equals("crafting2x2") && getClass() == ShapedRecipeHandler.class) {
|
|
for (IRecipe irecipe : CraftingManager.getInstance().getRecipeList()) {
|
|
|
|
if (irecipe instanceof ShapedRecipes shaped && shaped.recipeWidth <= 2 && shaped.recipeHeight <= 2) {
|
|
CachedShapedRecipe recipe = new CachedShapedRecipe(shaped);
|
|
recipe.computeVisuals();
|
|
arecipes.add(recipe);
|
|
}
|
|
|
|
}
|
|
} else {
|
|
super.loadCraftingRecipes(outputId, results);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadCraftingRecipes(ItemStack result) {
|
|
for (IRecipe irecipe : CraftingManager.getInstance().getRecipeList()) {
|
|
if (NEIServerUtils.areStacksSameTypeCrafting(irecipe.getRecipeOutput(), result)) {
|
|
CachedShapedRecipe recipe = null;
|
|
if (irecipe instanceof ShapedRecipes) recipe = new CachedShapedRecipe((ShapedRecipes) irecipe);
|
|
else if (irecipe instanceof ShapedOreRecipe) recipe = forgeShapedRecipe((ShapedOreRecipe) irecipe);
|
|
|
|
if (recipe == null) continue;
|
|
|
|
recipe.computeVisuals();
|
|
arecipes.add(recipe);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadUsageRecipes(ItemStack ingredient) {
|
|
for (IRecipe irecipe : CraftingManager.getInstance().getRecipeList()) {
|
|
CachedShapedRecipe recipe = null;
|
|
if (irecipe instanceof ShapedRecipes) recipe = new CachedShapedRecipe((ShapedRecipes) irecipe);
|
|
else if (irecipe instanceof ShapedOreRecipe) recipe = forgeShapedRecipe((ShapedOreRecipe) irecipe);
|
|
|
|
if (recipe == null || !recipe.contains(recipe.ingredients, ingredient.getItem())) continue;
|
|
|
|
recipe.computeVisuals();
|
|
if (recipe.contains(recipe.ingredients, ingredient)) {
|
|
recipe.setIngredientPermutation(recipe.ingredients, ingredient);
|
|
arecipes.add(recipe);
|
|
}
|
|
}
|
|
}
|
|
|
|
public CachedShapedRecipe forgeShapedRecipe(ShapedOreRecipe recipe) {
|
|
int width, height;
|
|
try {
|
|
width = (int) SHAPED_ORE_RECIPE_WIDTH.invokeExact(recipe);
|
|
height = (int) SHAPED_ORE_RECIPE_HEIGHT.invokeExact(recipe);
|
|
} catch (Throwable e) {
|
|
throw new IllegalStateException("Unreachable");
|
|
}
|
|
|
|
Object[] items = recipe.getInput();
|
|
for (Object item : items) {
|
|
if (item instanceof List<?>ores && ores.isEmpty()) // ore handler, no ores
|
|
return null;
|
|
}
|
|
|
|
return new CachedShapedRecipe(width, height, items, recipe.getRecipeOutput());
|
|
}
|
|
|
|
@Override
|
|
public String getGuiTexture() {
|
|
return "textures/gui/container/crafting_table.png";
|
|
}
|
|
|
|
@Override
|
|
public String getOverlayIdentifier() {
|
|
return "crafting";
|
|
}
|
|
|
|
public boolean hasOverlay(GuiContainer gui, Container container, int recipe) {
|
|
return super.hasOverlay(gui, container, recipe)
|
|
|| isRecipe2x2(recipe) && RecipeInfo.hasDefaultOverlay(gui, "crafting2x2");
|
|
}
|
|
|
|
@Override
|
|
public IRecipeOverlayRenderer getOverlayRenderer(GuiContainer gui, int recipe) {
|
|
IRecipeOverlayRenderer renderer = super.getOverlayRenderer(gui, recipe);
|
|
if (renderer != null) return renderer;
|
|
|
|
IStackPositioner positioner = RecipeInfo.getStackPositioner(gui, "crafting2x2");
|
|
if (positioner == null) return null;
|
|
return new DefaultOverlayRenderer(getIngredientStacks(recipe), positioner);
|
|
}
|
|
|
|
@Override
|
|
public IOverlayHandler getOverlayHandler(GuiContainer gui, int recipe) {
|
|
IOverlayHandler handler = super.getOverlayHandler(gui, recipe);
|
|
if (handler != null) return handler;
|
|
|
|
return RecipeInfo.getOverlayHandler(gui, "crafting2x2");
|
|
}
|
|
|
|
public boolean isRecipe2x2(int recipe) {
|
|
for (PositionedStack stack : getIngredientStacks(recipe)) if (stack.relx > 43 || stack.rely > 24) return false;
|
|
|
|
return true;
|
|
}
|
|
}
|