finish #5: make keybinds run tiggers instead of runnables

This commit is contained in:
Daniella / Tove 2024-05-30 20:38:03 +02:00
parent 3aee5d0a85
commit 520638a9f0
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
7 changed files with 96 additions and 46 deletions

View file

@ -1,6 +1,5 @@
package com.baseband.client.configuration;
import com.baseband.client.util.config.KeyBind;
import de.tudbut.tools.ReflectUtil;
import java.lang.reflect.Field;
@ -33,10 +32,10 @@ public class Updater {
field.setDouble(o, handle.getContent().getDouble(id));
} else if (field.getType() == boolean.class) {
field.setBoolean(o, handle.getContent().getBoolean(id));
} else if (field.getType() == Integer.class) {
field.set(o, handle.getContent().getInteger(id));
} else if (field.getType().isEnum()) {
field.set(o, field.getType().getEnumConstants()[handle.getContent().getInteger(id)]);
} else if (field.getType() == KeyBind.class) {
((KeyBind) field.get(o)).key = handle.getContent().getInteger(id);
} else {
throw new IllegalStateException("Unhandled type of field: " + field.getType());
}
@ -52,15 +51,13 @@ public class Updater {
field.getType() == long.class ||
field.getType() == float.class ||
field.getType() == double.class ||
field.getType() == boolean.class) {
field.getType() == boolean.class ||
field.getType() == Integer.class) {
handle.getContent().set(id, field.get(o));
handle.updated(id);
} else if(field.getType().isEnum()) {
handle.getContent().set(id, ((Enum<?>) field.get(o)).ordinal());
handle.updated(id);
} else if(field.getType() == KeyBind.class) {
handle.getContent().set(id, ((KeyBind) field.get(o)).key);
handle.updated(id);
} else {
throw new IllegalStateException("Unhandled type of field: " + field.getType());
}

View file

@ -33,13 +33,13 @@ public class AnyGate {
if(!gCache.containsKey(o))
gCache.put(o, new HashMap<>());
HashMap<AnnotatedElement, AnyGate> map = gCache.get(o);
AnyGate gate = map.get(f);
if(gate == null) {
gate = new AnyGate(f, o);
map.put(f, gate);
gate.cacheAll();
AnyGate g = map.get(f);
if(g == null) {
g = new AnyGate(f, o);
map.put(f, g);
g.cacheAll();
}
return gate;
return g;
}
private AnyGate(@Nonnull AnnotatedElement member, @Nonnull Object o) {
@ -53,6 +53,30 @@ public class AnyGate {
cache.put(id.value(), TRUE);
}
public static AnyGate get(AnnotatedElement f, @Nonnull Object o, Gate gate, MultiGate multiGate) {
if(!gCache.containsKey(o))
gCache.put(o, new HashMap<>());
HashMap<AnnotatedElement, AnyGate> map = gCache.get(o);
AnyGate g = map.get(f);
if(g == null) {
g = new AnyGate(f, o, gate, multiGate);
map.put(f, g);
g.cacheAll();
}
return g;
}
private AnyGate(@Nonnull AnnotatedElement member, @Nonnull Object o, Gate gate, MultiGate multiGate) {
this.member = member;
this.gate = gate;
this.multiGate = multiGate;
this.o = o;
Marker id = member.getDeclaredAnnotation(Marker.class);
if(id != null)
cache.put(id.value(), TRUE);
}
private AnyGate() {
this.member = null;
this.gate = null;

View file

@ -0,0 +1,10 @@
package com.baseband.client.configuration.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface KeyBound {
String value() default "";
MultiGate allowChangeGate() default @MultiGate();
}

View file

@ -41,9 +41,6 @@ public abstract class Feature extends ToggleButton implements SetCommand {
@Marker(M_ENABLED)
public boolean enabled = defaultEnable();
@Config("Toggle")
public KeyBind toggleKey = new KeyBind(null, this::toggle, null);
public Feature() {
super("Uninit", null, "Enabled");
this.setLambda(this::updateEnabled);
@ -113,13 +110,25 @@ public abstract class Feature extends ToggleButton implements SetCommand {
Field[] fields = getClass().getFields();
for (Field f : fields) {
Config config = f.getDeclaredAnnotation(Config.class);
KeyBound keyBound = f.getDeclaredAnnotation(KeyBound.class);
Description descriptionAnnotation = f.getDeclaredAnnotation(Description.class);
String description = descriptionAnnotation == null ? null : descriptionAnnotation.value();
AnyGate gate = AnyGate.get(f, this);
if (config != null) {
BaseBand.registerUpdater(settings.linkWith(this, f).name(config.value()));
if (f.getType() == boolean.class) {
subComponents.add(new ToggleButton(config.value(), settings, config.value()).gate(gate).hover(description));
Component btn = new ToggleButton(config.value(), settings, config.value()).gate(gate).hover(description);
subComponents.add(btn);
if(keyBound != null) {
String keyBindString = config.value();
if(!keyBound.value().isEmpty())
keyBindString = keyBound.value() + " " + keyBindString;
String keyBindConfig = keyBindString + " Key";
KeyBind keyBind = new KeyBind(null, () -> btn.click(0, 0, 0), gate);
subComponents.add(new KeyButton(keyBindString, settings, keyBindConfig).gate(AnyGate.get(f, this, null, keyBound.allowChangeGate())).hover(description));
BaseBand.registerUpdater(settings.linkWith(keyBind, KeyBind.KEY_FIELD).name(keyBindConfig));
BaseBand.registerKeyBind(keyBind);
}
}
if (f.getType() == int.class) {
Range range = f.getDeclaredAnnotation(Range.class);
@ -146,14 +155,6 @@ public abstract class Feature extends ToggleButton implements SetCommand {
if (f.getType() == String.class) {
subComponents.add(new StringButton(config.value(), settings, config.value()).gate(gate).hover(description));
}
if (f.getType() == KeyBind.class) {
subComponents.add(new KeyButton(config.value(), settings, config.value()).gate(gate).hover(description));
try {
BaseBand.registerKeyBind((KeyBind) f.get(this));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
if(f.getType().isEnum()) {
subComponents.add(new EnumButton((Class<? extends Enum<?>>) f.getType(), config.value(), settings, config.value()).gate(gate).hover(description));
}
@ -162,6 +163,8 @@ public abstract class Feature extends ToggleButton implements SetCommand {
Method[] methods = getClass().getMethods();
for (Method m : methods) {
Trigger trigger = m.getDeclaredAnnotation(Trigger.class);
KeyBound keyBound = m.getDeclaredAnnotation(KeyBound.class);
String keyBindString = null;
Description descriptionAnnotation = m.getDeclaredAnnotation(Description.class);
String description = descriptionAnnotation == null ? null : descriptionAnnotation.value();
AnyGate gate = AnyGate.get(m, this);
@ -175,6 +178,27 @@ public abstract class Feature extends ToggleButton implements SetCommand {
throw new RuntimeException(e.getCause());
}
}).gate(gate).hover(description));
if(keyBound != null) {
keyBindString = trigger.value();
if(!keyBound.value().isEmpty())
keyBindString = keyBound.value() + " " + keyBindString;
}
}
else if(keyBound != null) {
keyBindString = keyBound.value();
}
if (keyBindString != null) {
String keyBindConfig = keyBindString + " Key";
KeyBind keyBind = new KeyBind(null, () -> {
try {
m.invoke(this);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}, gate);
subComponents.add(new KeyButton(keyBindString, settings, keyBindConfig).gate(AnyGate.get(m, this, null, keyBound.allowChangeGate())).hover(description));
BaseBand.registerUpdater(settings.linkWith(keyBind, KeyBind.KEY_FIELD).name(keyBindConfig));
BaseBand.registerKeyBind(keyBind);
}
}
@ -235,6 +259,7 @@ public abstract class Feature extends ToggleButton implements SetCommand {
toggle();
}
@KeyBound("Toggle")
public void toggle() {
enabled = !enabled;
updateEnabled();

View file

@ -8,7 +8,6 @@ import com.baseband.client.module.Feature;
import com.baseband.client.module.category.Chat;
import com.baseband.client.module.render.HUD;
import com.baseband.client.util.adapt.FieldFinder;
import com.baseband.client.util.config.KeyBind;
import com.baseband.client.util.interact.ChatUtil;
import com.baseband.client.util.misc.*;
import de.tudbut.tools.Hasher;
@ -42,6 +41,7 @@ public class ChatCrypt extends Feature {
@Config("Send")
@Description("Encrypt sent messages")
@KeyBound("Toggle")
public boolean send;
@Config("Channel")
@ -101,16 +101,6 @@ public class ChatCrypt extends Feature {
@Config("Password")
public String password = "CLICK HERE";
@Config("Toggle Send")
public KeyBind toggleSend = new KeyBind(null, () -> {
this.send = !this.send;
handle.poll("Send");
if(send)
BaseBand.notifyAll("Sent messages §a§lwill§r be encrypted");
else
BaseBand.notifyAll("Sent messages §cwill §lnot§r be encrypted");
}, this);
private Trypt trypt;
private final Pattern CHAT_PATTERN = Pattern.compile("(<.*?> )|(.*?: )");

View file

@ -2,6 +2,7 @@ package com.baseband.client.module.client;
import com.baseband.client.configuration.Configuration;
import com.baseband.client.configuration.annotation.Config;
import com.baseband.client.configuration.annotation.KeyBound;
import com.baseband.client.util.interact.ServerDataManager;
import com.baseband.client.util.misc.Description;
import com.baseband.client.event.events.PacketEvent;
@ -9,7 +10,6 @@ import com.baseband.client.event.events.PlayerDestroyEvent;
import com.baseband.client.gui.GuiTheme;
import com.baseband.client.BaseBand;
import com.baseband.client.module.Feature;
import com.baseband.client.util.config.KeyBind;
import com.baseband.client.util.interact.ChatUtil;
import com.baseband.client.util.misc.Trigger;
import net.minecraft.entity.EntityLivingBase;
@ -31,7 +31,8 @@ public class Client extends Feature {
@Config("Prefix")
public static String prefix = "&";
@Trigger("Clear targets now")
@Trigger("Clear targets")
@KeyBound
public void clearTargets() {
entityTarget = null;
playerTarget = null;
@ -49,9 +50,6 @@ public class Client extends Feature {
@Config("Theme")
public GuiTheme.Theme theme = GuiTheme.Theme.TTC;
@Config("Clear targets")
public KeyBind clearTargetsBind = new KeyBind(null, this::clearTargets, this);
@Trigger("Save config")
public void saveConfig() {
Configuration.save();

View file

@ -1,24 +1,30 @@
package com.baseband.client.util.config;
import com.baseband.client.module.Feature;
import com.baseband.client.configuration.annotation.AnyGate;
import com.baseband.client.util.adapt.FieldFinder;
import com.baseband.client.util.misc.Marker;
import org.lwjgl.input.Keyboard;
import java.lang.reflect.Field;
import static com.baseband.client.BaseBand.mc;
public class KeyBind {
public static final Field KEY_FIELD = FieldFinder.findMarked(KeyBind.class, 1);
@Marker(1)
public Integer key;
public boolean down = false;
public final Runnable onPress;
public final Feature dependsOn;
public final AnyGate gate;
public KeyBind(Integer key, Runnable onPress, Feature dependsOn) {
public KeyBind(Integer key, Runnable onPress, AnyGate gate) {
this.key = key;
this.onPress = onPress;
this.dependsOn = dependsOn;
this.gate = gate;
}
public void onTick() {
if(dependsOn != null && !dependsOn.enabled)
if(!gate.canPass())
return;
if(key != null && mc.currentScreen == null) {
if (Keyboard.isKeyDown(key)) {