Minor refactoring

This commit is contained in:
Frank 2021-08-20 03:16:20 +02:00
parent 290bed1ffe
commit 6e2a539232

View file

@ -11,16 +11,15 @@ import java.lang.reflect.Proxy;
import java.util.Map; import java.util.Map;
class ModMenuScreenFactoryImpl { class ModMenuScreenFactoryImpl {
static class ScreenFactoryInvocationHandler implements InvocationHandler { record ScreenFactoryInvocationHandler(ModMenuScreenFactory act) implements InvocationHandler {
private final ModMenuScreenFactory act;
public ScreenFactoryInvocationHandler(ModMenuScreenFactory act) {
this.act = act;
}
@Override @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return act.create((Screen)args[0]); return switch (method.getName()) {
case "toString" -> "";
case "equals" -> false;
case "hashCode" -> 0;
default -> act.create((Screen) args[0]);
};
} }
} }
@ -31,6 +30,7 @@ class ModMenuScreenFactoryImpl {
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
return null;
} }
Object o = Proxy.newProxyInstance( Object o = Proxy.newProxyInstance(
@ -73,7 +73,7 @@ public abstract class ModMenuIntegration {
* @return A Proxy that conforms to the ModMenu spec * @return A Proxy that conforms to the ModMenu spec
*/ */
public static ModMenuApiMarker createEntrypoint(ModMenuIntegration target) { public static ModMenuApiMarker createEntrypoint(ModMenuIntegration target) {
Class<?> iModMenuAPI = null; Class<?> iModMenuAPI;
//Class<?> iModMenuAPIMarker = null; //Class<?> iModMenuAPIMarker = null;
try { try {
iModMenuAPI = Class.forName("com.terraformersmc.modmenu.api.ModMenuApi"); iModMenuAPI = Class.forName("com.terraformersmc.modmenu.api.ModMenuApi");
@ -81,7 +81,7 @@ public abstract class ModMenuIntegration {
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
return (ModMenuApiMarker)new Object(); return null;
} }
Object o = Proxy.newProxyInstance( Object o = Proxy.newProxyInstance(
@ -108,7 +108,7 @@ public abstract class ModMenuIntegration {
/** /**
* A Helper class to make a BCLib-Factory conform to the ModMenu-Factory Interface. * A Helper class to make a BCLib-Factory conform to the ModMenu-Factory Interface.
* @param factory * @param factory A BCLib-Type Factory, ie. {@code GridScreen::new }
* @return A ModMenu Factory for a Screen * @return A ModMenu Factory for a Screen
*/ */
final protected ModMenuScreenFactory createFactory(ModMenuScreenFactory factory){ final protected ModMenuScreenFactory createFactory(ModMenuScreenFactory factory){
@ -156,28 +156,21 @@ public abstract class ModMenuIntegration {
* The Interface matches {@code com.terraformersmc.modmenu.api.ConfigScreenFactory} * The Interface matches {@code com.terraformersmc.modmenu.api.ConfigScreenFactory}
*/ */
@FunctionalInterface @FunctionalInterface
public static interface ModMenuScreenFactory { public interface ModMenuScreenFactory {
Screen create(Screen parent); Screen create(Screen parent);
} }
static class BCLibModMenuInvocationHandler implements InvocationHandler { record BCLibModMenuInvocationHandler(ModMenuIntegration target) implements InvocationHandler {
private final ModMenuIntegration target;
public BCLibModMenuInvocationHandler(ModMenuIntegration target) {
this.target = target;
}
@Override @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getModConfigScreenFactory".equals(method.getName())){ return switch (method.getName()) {
return target.getModConfigScreenFactory(); case "getModConfigScreenFactory" -> target.getModConfigScreenFactory();
} else if ("getProvidedConfigScreenFactories".equals(method.getName())){ case "getProvidedConfigScreenFactories" -> target.getProvidedConfigScreenFactories();
return target.getProvidedConfigScreenFactories(); case "toString" -> target.toString();
} else if ("toString".equals(method.getName())){ case "equals" -> target.equals(args[0]);
return target.toString(); case "hashCode" -> target.hashCode();
} else { default -> null;
return null; };
}
} }
} }
} }