Compare commits

..
Author SHA1 Message Date
Glease 51ea8bba34 Add final modifiers on those handlers fields
Also added an optional asm transformer to catch potential problems
2022-01-26 20:54:04 -08:00
Glease b0c6f358f7 Use copy on write lists for handler lists 2022-01-26 20:54:04 -08:00
miozune c73edaa72e Use String key for RecipeCatalyst instead of IRecipeHandler.class (#197)
* Use String key for RecipeCatalyst instead of IRecipeHandler.class

* deprecate old methods

* delete commented out code
2022-01-22 23:37:54 +01:00
11 changed files with 324 additions and 30 deletions

No files matched your search

+36 -2
View File
@@ -300,7 +300,7 @@ public class API
* @param stack the ingredient that can craft recipes (like a furnace or crafting table)
* @param handler the recipe category handled by the ingredient
*/
public static void addRecipeCatalyst(ItemStack stack, Class<? extends IRecipeHandler> handler) {
public static void addRecipeCatalyst(ItemStack stack, IRecipeHandler handler) {
addRecipeCatalyst(Collections.singletonList(stack), handler);
}
@@ -310,8 +310,42 @@ public class API
* @param stacks the ingredients that can craft recipes (like a furnace or crafting table)
* @param handler the recipe category handled by the ingredient
*/
public static void addRecipeCatalyst(List<ItemStack> stacks, IRecipeHandler handler) {
String handlerID = handler.getOverlayIdentifier();
if (handlerID != null) {
addRecipeCatalyst(stacks, handler.getOverlayIdentifier());
}
else {
NEIClientConfig.logger.warn(String.format("failed to add recipe catalyst, implement IRecipeHandler#getOverlayIdentifier for your handler %s", handler.getClass().getName()));
}
}
/**
* Adds an association between an ingredient and what it can craft. (i.e. Furnace ItemStack -> Smelting and Fuel Recipes)
* Allows players to see what ingredient they need to craft in order to make recipes from a recipe category.
* @param stack the ingredient that can craft recipes (like a furnace or crafting table)
* @param handlerID recipe category identifier (see {@link IRecipeHandler#getOverlayIdentifier()})
*/
public static void addRecipeCatalyst(ItemStack stack, String handlerID) {
addRecipeCatalyst(Collections.singletonList(stack), handlerID);
}
/**
* Adds an association between an ingredient and what it can craft. (i.e. Furnace ItemStack -> Smelting and Fuel Recipes)
* Allows players to see what ingredient they need to craft in order to make recipes from a recipe category.
* @param stacks the ingredients that can craft recipes (like a furnace or crafting table)
* @param handlerID recipe category identifier (see {@link IRecipeHandler#getOverlayIdentifier()})
*/
public static void addRecipeCatalyst(List<ItemStack> stacks, String handlerID) {
RecipeCatalysts.addRecipeCatalyst(stacks, handlerID);
}
@Deprecated
public static void addRecipeCatalyst(ItemStack stack, Class<? extends IRecipeHandler> handler) {
}
@Deprecated
public static void addRecipeCatalyst(List<ItemStack> stacks, Class<? extends IRecipeHandler> handler) {
RecipeCatalysts.addRecipeCatalyst(stacks, handler);
}
}
@@ -0,0 +1,38 @@
package codechicken.nei.asm;
import codechicken.lib.asm.ASMHelper;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import static org.objectweb.asm.Opcodes.POP;
import static org.objectweb.asm.Opcodes.PUTSTATIC;
public class GuiContainerManagerAPITransformer extends ClassVisitor {
private final String tname;
private boolean changed = false;
public GuiContainerManagerAPITransformer(int api, ClassVisitor cv, String tname) {
super(api, cv);
this.tname = tname;
}
@Override
public MethodVisitor visitMethod(int access, String mname, String mdesc, String signature, String[] exceptions) {
return new MethodVisitor(api, super.visitMethod(access, mname, mdesc, signature, exceptions)) {
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
if (opcode == PUTSTATIC && "codechicken/nei/guihook/GuiContainerManager".equals(owner) && name.endsWith("Handlers") && desc.equals("Ljava/util/LinkedList;")) {
ASMHelper.logger.warn("Found suspicious PUTSTATIC {} in {}#{}{}, replacing with POP!", name, tname, mname, mdesc);
changed = true;
super.visitInsn(POP);
} else {
super.visitFieldInsn(opcode, owner, name, desc);
}
}
};
}
public boolean isChanged() {
return changed;
}
}
@@ -41,6 +41,7 @@ public class NEITransformer implements IClassTransformer
ASMInit.init();
}
private final boolean paranoidMode = Boolean.getBoolean("nei.paranoid");
private final ModularASMTransformer transformer = new ModularASMTransformer();
private final Map<String, ASMBlock> asmblocks = ASMReader.loadResource("/assets/nei/asm/blocks.asm");
@@ -264,6 +265,14 @@ public class NEITransformer implements IClassTransformer
}
bytes = transformSubclasses(name, bytes);
}
if (paranoidMode && !tname.startsWith("codechicken.nei")) {
ClassReader cr = new ClassReader(bytes);
ClassWriter cw = new ClassWriter(0);
GuiContainerManagerAPITransformer cv = new GuiContainerManagerAPITransformer(ASM5, cw, tname);
cr.accept(cv, ClassReader.SKIP_DEBUG);
if (cv.isChanged())
bytes = cw.toByteArray();
}
bytes = transformer.transform(name, bytes);
} catch (Exception e) {
@@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Pattern;
import static codechicken.lib.gui.GuiDraw.drawMultilineTip;
@@ -35,11 +36,11 @@ public class GuiContainerManager
public GuiContainer window;
public static RenderItem drawItems = new RenderItem();
public static LinkedList<IContainerTooltipHandler> tooltipHandlers = new LinkedList<>();
public static LinkedList<IContainerInputHandler> inputHandlers = new LinkedList<>();
public static LinkedList<IContainerDrawHandler> drawHandlers = new LinkedList<>();
public static LinkedList<IContainerObjectHandler> objectHandlers = new LinkedList<>();
public static LinkedList<IContainerSlotClickHandler> slotClickHandlers = new LinkedList<>();
public static final LinkedList<IContainerTooltipHandler> tooltipHandlers = new HideousLinkedList<>(new CopyOnWriteArrayList<>());
public static final LinkedList<IContainerInputHandler> inputHandlers = new HideousLinkedList<>(new CopyOnWriteArrayList<>());
public static final LinkedList<IContainerDrawHandler> drawHandlers = new HideousLinkedList<>(new CopyOnWriteArrayList<>());
public static final LinkedList<IContainerObjectHandler> objectHandlers = new HideousLinkedList<>(new CopyOnWriteArrayList<>());
public static final LinkedList<IContainerSlotClickHandler> slotClickHandlers = new HideousLinkedList<>(new CopyOnWriteArrayList<>());
static {
addSlotClickHandler(new DefaultSlotClickHandler());
@@ -0,0 +1,180 @@
package codechicken.nei.guihook;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
/**
* A hideous LinkedList that is backed by an arbitrary list, for alternative characteristics while preserving
* binary compatibility...
* @param <E>
*/
final class HideousLinkedList<E> extends LinkedList<E> {
private final List<E> backing;
HideousLinkedList(List<E> backing) {
this.backing = backing;
}
@Override
public int size() {
return backing.size();
}
@Override
public boolean isEmpty() {
return backing.isEmpty();
}
@Override
public boolean contains(Object o) {
return backing.contains(o);
}
@Override
public Iterator<E> iterator() {
return backing.iterator();
}
@Override
public Object[] toArray() {
return backing.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return backing.toArray(a);
}
@Override
public boolean add(E e) {
return backing.add(e);
}
@Override
public boolean remove(Object o) {
return backing.remove(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return backing.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends E> c) {
return backing.addAll(c);
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
return backing.addAll(index, c);
}
@Override
public boolean removeAll(Collection<?> c) {
return backing.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return backing.retainAll(c);
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
backing.replaceAll(operator);
}
@Override
public void sort(Comparator<? super E> c) {
backing.sort(c);
}
@Override
public void clear() {
backing.clear();
}
@Override
public boolean equals(Object o) {
return backing.equals(o);
}
@Override
public int hashCode() {
return backing.hashCode();
}
@Override
public E get(int index) {
return backing.get(index);
}
@Override
public E set(int index, E element) {
return backing.set(index, element);
}
@Override
public void add(int index, E element) {
backing.add(index, element);
}
@Override
public E remove(int index) {
return backing.remove(index);
}
@Override
public int indexOf(Object o) {
return backing.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return backing.lastIndexOf(o);
}
@Override
public ListIterator<E> listIterator() {
return backing.listIterator();
}
@Override
public ListIterator<E> listIterator(int index) {
return backing.listIterator(index);
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
return backing.subList(fromIndex, toIndex);
}
@Override
public Spliterator<E> spliterator() {
return backing.spliterator();
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
return backing.removeIf(filter);
}
@Override
public Stream<E> stream() {
return backing.stream();
}
@Override
public Stream<E> parallelStream() {
return backing.parallelStream();
}
@Override
public void forEach(Consumer<? super E> action) {
backing.forEach(action);
}
}
@@ -507,7 +507,7 @@ public abstract class GuiRecipe extends GuiContainer implements IGuiContainerOve
if (result != null)
slotcontainer.addSlot(result, p.x, p.y);
List<PositionedStack> catalysts = RecipeCatalysts.getRecipeCatalysts(handler.getClass());
List<PositionedStack> catalysts = RecipeCatalysts.getRecipeCatalysts(handler);
for (PositionedStack catalyst : catalysts) {
int xOffset = -GuiRecipeCatalyst.ingredientSize + 1;
int yOffset = BG_TOP_Y + GuiRecipeCatalyst.fullBorder;
@@ -24,7 +24,7 @@ public class GuiRecipeCatalyst extends INEIGuiAdapter {
public void draw() {
if (guiRecipe == null) return;
int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler().getClass()).size();
int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler()).size();
if (catalystsSize == 0) return;
int availableHeight = RecipeCatalysts.getHeight();
@@ -49,7 +49,7 @@ public class GuiRecipeCatalyst extends INEIGuiAdapter {
public boolean hideItemPanelSlot(GuiContainer gui, int x, int y, int w, int h) {
if (!(gui instanceof GuiRecipe)) return false;
guiRecipe = (GuiRecipe) gui;
int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler().getClass()).size();
int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler()).size();
if (catalystsSize == 0) return false;
int availableHeight = RecipeCatalysts.getHeight();
@@ -89,6 +89,15 @@ public interface IRecipeHandler
* @return An instance of {@link IOverlayHandler} to be used for rendering the overlay of this specific recipe.
*/
public IOverlayHandler getOverlayHandler(GuiContainer gui, int recipe);
/**
* Simply works with the {@link codechicken.nei.api.DefaultOverlayRenderer}
* If the current container has been registered with this identifier, the question mark appears and an overlay guide can be drawn.
*
* @return The overlay identifier of this recipe type.
*/
public default String getOverlayIdentifier() {
return null;
}
/**
*
* @return The number of recipes that can fit on a page in the viewer (1 or 2)
@@ -12,39 +12,49 @@ import java.util.List;
import java.util.Map;
public class RecipeCatalysts {
private static final Map<Class<? extends IRecipeHandler>, List<ItemStack>> recipeCatalystMap = new HashMap<>();
private static Map<Class<? extends IRecipeHandler>, List<PositionedStack>> positionedRecipeCatalystMap = new HashMap<>();
private static final Map<String, List<ItemStack>> recipeCatalystMap = new HashMap<>();
private static Map<String, List<PositionedStack>> positionedRecipeCatalystMap = new HashMap<>();
private static int heightCache;
public static void addRecipeCatalyst(List<ItemStack> stacks, Class<? extends IRecipeHandler> handler) {
if (recipeCatalystMap.containsKey(handler)) {
recipeCatalystMap.get(handler).addAll(stacks);
public static void addRecipeCatalyst(List<ItemStack> stacks, String handlerID) {
if (handlerID == null) return;
if (recipeCatalystMap.containsKey(handlerID)) {
recipeCatalystMap.get(handlerID).addAll(stacks);
} else {
recipeCatalystMap.put(handler, new ArrayList<>(stacks));
// use ArrayList initializer to prevent UOE
recipeCatalystMap.put(handlerID, new ArrayList<>(stacks));
}
}
public static Map<Class<? extends IRecipeHandler>, List<PositionedStack>> getPositionedRecipeCatalystMap() {
public static Map<String, List<PositionedStack>> getPositionedRecipeCatalystMap() {
return positionedRecipeCatalystMap;
}
public static List<PositionedStack> getRecipeCatalysts(Class<? extends IRecipeHandler> handler) {
public static List<PositionedStack> getRecipeCatalysts(IRecipeHandler handler) {
return getRecipeCatalysts(handler.getOverlayIdentifier());
}
public static List<PositionedStack> getRecipeCatalysts(String handlerID) {
if (!NEIClientConfig.areJEIStyleTabsVisible() || !NEIClientConfig.areJEIStyleRecipeCatalystsVisible()) {
return Collections.emptyList();
}
return positionedRecipeCatalystMap.getOrDefault(handler, Collections.emptyList());
return positionedRecipeCatalystMap.getOrDefault(handlerID, Collections.emptyList());
}
public static boolean containsCatalyst(Class<? extends IRecipeHandler> handler, ItemStack candidate) {
return recipeCatalystMap.getOrDefault(handler, Collections.emptyList()).stream()
public static boolean containsCatalyst(IRecipeHandler handler, ItemStack candidate) {
return containsCatalyst(handler.getOverlayIdentifier(), candidate);
}
public static boolean containsCatalyst(String handlerID, ItemStack candidate) {
return recipeCatalystMap.getOrDefault(handlerID, Collections.emptyList()).stream()
.anyMatch(s -> NEIServerUtils.areStacksSameType(s, candidate));
}
public static void updatePosition(int availableHeight) {
if (availableHeight == heightCache) return;
Map<Class<? extends IRecipeHandler>, List<PositionedStack>> newMap = new HashMap<>();
for (Map.Entry<Class<? extends IRecipeHandler>, List<ItemStack>> entry : recipeCatalystMap.entrySet()) {
Map<String, List<PositionedStack>> newMap = new HashMap<>();
for (Map.Entry<String, List<ItemStack>> entry : recipeCatalystMap.entrySet()) {
List<ItemStack> catalysts = entry.getValue();
List<PositionedStack> newStacks = new ArrayList<>();
int rowCount = getRowCount(availableHeight, catalysts.size());
@@ -75,4 +85,18 @@ public class RecipeCatalysts {
return NEIServerUtils.divideCeil(catalystsSize, columnCount);
}
@Deprecated
public static void addRecipeCatalyst(List<ItemStack> stacks, Class<? extends IRecipeHandler> handler) {
}
@Deprecated
public static List<PositionedStack> getRecipeCatalysts(Class<? extends IRecipeHandler> handler) {
return Collections.emptyList();
}
@Deprecated
public static boolean containsCatalyst(Class<? extends IRecipeHandler> handler, ItemStack candidate) {
return false;
}
}
@@ -123,12 +123,10 @@ public class RecipeInfo
API.registerRecipeHandler(new ProfilerRecipeHandler(true));
API.registerUsageHandler(new ProfilerRecipeHandler(false));
API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), ShapedRecipeHandler.class);
API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), ShapelessRecipeHandler.class);
API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), FireworkRecipeHandler.class);
API.addRecipeCatalyst(new ItemStack(Blocks.furnace), FurnaceRecipeHandler.class);
API.addRecipeCatalyst(new ItemStack(Items.brewing_stand), BrewingRecipeHandler.class);
API.addRecipeCatalyst(new ItemStack(Blocks.furnace), FuelRecipeHandler.class);
API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), "crafting");
API.addRecipeCatalyst(new ItemStack(Blocks.furnace), new FurnaceRecipeHandler());
API.addRecipeCatalyst(new ItemStack(Items.brewing_stand), new BrewingRecipeHandler());
API.addRecipeCatalyst(new ItemStack(Blocks.furnace), new FuelRecipeHandler());
}
}
@@ -442,6 +442,7 @@ public abstract class TemplateRecipeHandler implements ICraftingHandler, IUsageH
*
* @return The overlay identifier of this recipe type.
*/
@Override
public String getOverlayIdentifier() {
return null;
}
@@ -559,7 +560,7 @@ public abstract class TemplateRecipeHandler implements ICraftingHandler, IUsageH
TemplateRecipeHandler handler = newInstance();
if (inputId.equals("item")) {
ItemStack candidate = (ItemStack) ingredients[0];
if (RecipeCatalysts.containsCatalyst(handler.getClass(), candidate)) {
if (RecipeCatalysts.containsCatalyst(handler, candidate)) {
for (RecipeTransferRect rect : transferRects) {
if (specifyTransferRect() == null || Objects.equals(rect.outputId, specifyTransferRect())) {
handler.loadCraftingRecipes(rect.outputId, rect.results);