This commit is contained in:
Jess H 2024-05-24 04:35:47 +01:00
commit ff8f698ea1
5 changed files with 34 additions and 19 deletions

View file

@ -1,6 +1,6 @@
package com.baseband.client.configuration.annotation;
import com.baseband.client.util.misc.FieldUtil;
import com.baseband.client.util.misc.FieldFinder;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -29,7 +29,7 @@ public class AnyGate {
if(gate == null) return true;
Class<?> clazz = o.getClass();
return FieldUtil.findMarked(clazz, gate.value()).getBoolean(o);
return FieldFinder.findMarked(clazz, gate.value()).getBoolean(o);
}
private boolean canPassMulti() throws IllegalAccessException {
@ -37,18 +37,18 @@ public class AnyGate {
Class<?> clazz = o.getClass();
for(int gate : multiGate.overrideOr()) {
if(FieldUtil.findMarked(clazz, gate).getBoolean(o))
if(FieldFinder.findMarked(clazz, gate).getBoolean(o))
return true;
}
for(int gate : multiGate.and()) {
if(!FieldUtil.findMarked(clazz, gate).getBoolean(o))
if(!FieldFinder.findMarked(clazz, gate).getBoolean(o))
return false;
}
boolean pass = multiGate.or().length == 0;
for(int gate : multiGate.or()) {
if(FieldUtil.findMarked(clazz, gate).getBoolean(o))
if(FieldFinder.findMarked(clazz, gate).getBoolean(o))
pass = true;
}
return pass;

View file

@ -1,6 +1,6 @@
package com.baseband.client.gui;
import com.baseband.client.util.misc.FieldUtil;
import com.baseband.client.util.misc.FieldFinder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiIngame;
@ -28,8 +28,7 @@ public class GuiBBIngame extends Gui {
}
public static void drawItem(int x, int y, float partialTicks, EntityPlayer player, ItemStack stack) {
Method m = FieldUtil.getMethods(GuiIngame.class, int.class, int.class, float.class, EntityPlayer.class, ItemStack.class)[0];
m.setAccessible(true);
Method m = FieldFinder.findUnmarkedMethod(GuiIngame.class, 0, int.class, int.class, float.class, EntityPlayer.class, ItemStack.class);
try {
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
GlStateManager.enableRescaleNormal();

View file

@ -4,7 +4,7 @@ import com.baseband.client.init.BaseBand;
import com.baseband.client.configuration.ConfigHandle;
import com.baseband.client.configuration.Configuration;
import com.baseband.client.module.category.*;
import com.baseband.client.util.misc.FieldUtil;
import com.baseband.client.util.misc.FieldFinder;
import com.baseband.client.util.config.Marker;
import javax.annotation.Nonnull;
@ -27,9 +27,9 @@ public enum Category {
this.annotationClass = annotationClass;
try {
handle = Configuration.register("Category/" + name.toLowerCase().replace(' ', '_'));
BaseBand.registerUpdater(handle.linkWith(this, FieldUtil.findMarked(getClass(), 1)).name("x"));
BaseBand.registerUpdater(handle.linkWith(this, FieldUtil.findMarked(getClass(), 2)).name("y"));
BaseBand.registerUpdater(handle.linkWith(this, FieldUtil.findMarked(getClass(), 3)).name("show"));
BaseBand.registerUpdater(handle.linkWith(this, FieldFinder.findMarked(getClass(), 1)).name("x"));
BaseBand.registerUpdater(handle.linkWith(this, FieldFinder.findMarked(getClass(), 2)).name("y"));
BaseBand.registerUpdater(handle.linkWith(this, FieldFinder.findMarked(getClass(), 3)).name("show"));
} catch (IllegalAccessException e) {
//:skollerolley:
throw new RuntimeException(e);

View file

@ -7,7 +7,7 @@ import com.baseband.client.init.BaseBand;
import com.baseband.client.configuration.ConfigHandle;
import com.baseband.client.configuration.Configuration;
import com.baseband.client.module.category.Command;
import com.baseband.client.util.misc.FieldUtil;
import com.baseband.client.util.misc.FieldFinder;
import com.baseband.client.util.config.KeyBind;
import com.baseband.client.util.config.Marker;
import com.baseband.client.util.config.SetCommand;
@ -153,7 +153,7 @@ public abstract class Feature extends ToggleButton implements SetCommand {
}
handle = settings;
BaseBand.registerUpdater(settings.linkWith(this, FieldUtil.findMarked(Feature.class, 1)).name("Enabled"));
BaseBand.registerUpdater(settings.linkWith(this, FieldFinder.findMarked(Feature.class, 1)).name("Enabled"));
if(category != Category.COMMAND) {
updateEnabled();

View file

@ -9,7 +9,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
public class FieldUtil {
public class FieldFinder {
@Nonnull
public static Field findMarked(Class<?> clazz, int id) {
@ -24,17 +24,33 @@ public class FieldUtil {
return null;
}
public static Method[] getMethods(Class<?> clazz, Class<?>... args) {
ArrayList<Method> methods = new ArrayList<>();
public static Field findUnmarked(Class<?> clazz, Class<?> type, int id) {
Field[] fields = clazz.getDeclaredFields();
int n = 0;
for (int i = 0; i < fields.length; i++) {
if(fields[i].getType() == type) {
if(n++ == id) {
ReflectUtil.forceAccessible(fields[i]);
return fields[i];
}
}
}
return null;
}
public static Method findUnmarkedMethod(Class<?> clazz, int id, Class<?>... args) {
Method[] declaredMethods = clazz.getDeclaredMethods();
int n = 0;
for (int i = 0 ; i < declaredMethods.length ; i++) {
Method m = declaredMethods[i];
if(Arrays.equals(m.getParameterTypes(), args)) {
methods.add(m);
if(n++ == id) {
ReflectUtil.forceAccessible(m);
return m;
}
}
}
return methods.toArray(new Method[0]);
return null;
}
}