From a0b4103e4d298d652d852851f516d9f8e01ea7d5 Mon Sep 17 00:00:00 2001 From: Jane Petrovna Date: Sun, 7 Mar 2021 20:10:33 -0500 Subject: [PATCH] add event system and module exclusion, plus common tick event --- gradle.properties | 2 +- ...ixin.java => ClientPlayerEntityMixin.java} | 13 +++++- ...MenuMixin.java => OptionsScreenMixin.java} | 4 +- .../pm/j4/kerosene/util/config/Disables.java | 12 +++++ .../j4/kerosene/util/config/DisablesList.java | 9 ++++ .../java/pm/j4/kerosene/util/event/Event.java | 5 ++ .../j4/kerosene/util/event/EventContext.java | 5 ++ .../j4/kerosene/util/event/EventManager.java | 46 +++++++++++++++++++ .../pm/j4/kerosene/util/event/Listener.java | 9 ++++ .../pm/j4/kerosene/util/event/Listeners.java | 5 ++ .../j4/kerosene/util/module/ModuleBase.java | 25 ++++++++++ src/main/resources/kerosene.mixins.json | 4 +- 12 files changed, 133 insertions(+), 6 deletions(-) rename src/main/java/pm/j4/kerosene/mixin/{ChatMessageMixin.java => ClientPlayerEntityMixin.java} (71%) rename src/main/java/pm/j4/kerosene/mixin/{OptionsMenuMixin.java => OptionsScreenMixin.java} (92%) create mode 100644 src/main/java/pm/j4/kerosene/util/config/Disables.java create mode 100644 src/main/java/pm/j4/kerosene/util/config/DisablesList.java create mode 100644 src/main/java/pm/j4/kerosene/util/event/Event.java create mode 100644 src/main/java/pm/j4/kerosene/util/event/EventContext.java create mode 100644 src/main/java/pm/j4/kerosene/util/event/EventManager.java create mode 100644 src/main/java/pm/j4/kerosene/util/event/Listener.java create mode 100644 src/main/java/pm/j4/kerosene/util/event/Listeners.java diff --git a/gradle.properties b/gradle.properties index 1e01ed9..cac19f0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.11.2 # Mod Properties - mod_version = 0.2.1.0-SNAPSHOT + mod_version = 0.2.1.2-SNAPSHOT maven_group = pm.j4 archives_base_name = kerosene diff --git a/src/main/java/pm/j4/kerosene/mixin/ChatMessageMixin.java b/src/main/java/pm/j4/kerosene/mixin/ClientPlayerEntityMixin.java similarity index 71% rename from src/main/java/pm/j4/kerosene/mixin/ChatMessageMixin.java rename to src/main/java/pm/j4/kerosene/mixin/ClientPlayerEntityMixin.java index 63a9bc8..2ca25ea 100644 --- a/src/main/java/pm/j4/kerosene/mixin/ChatMessageMixin.java +++ b/src/main/java/pm/j4/kerosene/mixin/ClientPlayerEntityMixin.java @@ -10,9 +10,12 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import pm.j4.kerosene.modules.bindings.ChatCommands; import pm.j4.kerosene.util.data.ChatCommand; +import pm.j4.kerosene.util.event.Event; +import pm.j4.kerosene.util.event.EventContext; +import pm.j4.kerosene.util.event.EventManager; @Mixin(ClientPlayerEntity.class) -public class ChatMessageMixin { +public class ClientPlayerEntityMixin { @Inject( at = @At("HEAD"), method = "sendChatMessage", @@ -35,4 +38,12 @@ public class ChatMessageMixin { ci.cancel(); } } + + @Inject(at =@At(value = "INVOKE", + target = "Lnet/minecraft/client/network/AbstractClientPlayerEntity;tick()V", + ordinal = 0), + method = "tick()V") + private void onTick(CallbackInfo ci) { + EventManager.fire(Event.TICK, EventContext.NoEventContext); + } } diff --git a/src/main/java/pm/j4/kerosene/mixin/OptionsMenuMixin.java b/src/main/java/pm/j4/kerosene/mixin/OptionsScreenMixin.java similarity index 92% rename from src/main/java/pm/j4/kerosene/mixin/OptionsMenuMixin.java rename to src/main/java/pm/j4/kerosene/mixin/OptionsScreenMixin.java index 21b8db6..815fea0 100644 --- a/src/main/java/pm/j4/kerosene/mixin/OptionsMenuMixin.java +++ b/src/main/java/pm/j4/kerosene/mixin/OptionsScreenMixin.java @@ -15,14 +15,14 @@ import pm.j4.kerosene.gui.POptionsScreen; * The type Options menu mixin. */ @Mixin(OptionsScreen.class) -public class OptionsMenuMixin extends Screen { +public class OptionsScreenMixin extends Screen { /** * Instantiates a new Options menu mixin. * * @param title the title */ - protected OptionsMenuMixin(Text title) { + protected OptionsScreenMixin(Text title) { super(title); } diff --git a/src/main/java/pm/j4/kerosene/util/config/Disables.java b/src/main/java/pm/j4/kerosene/util/config/Disables.java new file mode 100644 index 0000000..bd388bb --- /dev/null +++ b/src/main/java/pm/j4/kerosene/util/config/Disables.java @@ -0,0 +1,12 @@ +package pm.j4.kerosene.util.config; + +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import pm.j4.kerosene.util.module.ModuleBase; + +@Retention(RetentionPolicy.RUNTIME) +@Repeatable(DisablesList.class) +public @interface Disables { + public String value(); +} diff --git a/src/main/java/pm/j4/kerosene/util/config/DisablesList.java b/src/main/java/pm/j4/kerosene/util/config/DisablesList.java new file mode 100644 index 0000000..1f22f9e --- /dev/null +++ b/src/main/java/pm/j4/kerosene/util/config/DisablesList.java @@ -0,0 +1,9 @@ +package pm.j4.kerosene.util.config; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface DisablesList { + Disables[] value(); +} diff --git a/src/main/java/pm/j4/kerosene/util/event/Event.java b/src/main/java/pm/j4/kerosene/util/event/Event.java new file mode 100644 index 0000000..18da7bc --- /dev/null +++ b/src/main/java/pm/j4/kerosene/util/event/Event.java @@ -0,0 +1,5 @@ +package pm.j4.kerosene.util.event; + +public enum Event { + TICK +} diff --git a/src/main/java/pm/j4/kerosene/util/event/EventContext.java b/src/main/java/pm/j4/kerosene/util/event/EventContext.java new file mode 100644 index 0000000..e640a99 --- /dev/null +++ b/src/main/java/pm/j4/kerosene/util/event/EventContext.java @@ -0,0 +1,5 @@ +package pm.j4.kerosene.util.event; + +public class EventContext { + public static EventContext NoEventContext = new EventContext(); +} \ No newline at end of file diff --git a/src/main/java/pm/j4/kerosene/util/event/EventManager.java b/src/main/java/pm/j4/kerosene/util/event/EventManager.java new file mode 100644 index 0000000..0d0d999 --- /dev/null +++ b/src/main/java/pm/j4/kerosene/util/event/EventManager.java @@ -0,0 +1,46 @@ +package pm.j4.kerosene.util.event; + +import java.lang.annotation.Annotation; +import java.util.*; +import pm.j4.kerosene.util.config.Disables; +import pm.j4.kerosene.util.config.DisablesList; +import pm.j4.kerosene.util.module.ModuleBase; + +public class EventManager { + private static Map> listeners = new HashMap<>(); + + public static void register(ModuleBase module) { + try { + Annotation moduleAnnotation = module.getClass().getAnnotation(Listener.class); + if (moduleAnnotation != null) { + Listener[] annotatedListeners = ((Listeners)moduleAnnotation).value(); + for (Listener listener : annotatedListeners) { + Event event = listener.value(); + if (listeners.containsKey(event)) { + Set modules = listeners.get(event); + if(!modules.contains(module)) { + modules.add(module); + } + } + else { + Set modules = Collections.singleton(module); + listeners.put(event, modules); + } + } + } + } + catch (Exception e) { + System.out.println(e); + } + } + + public static void fire(Event event, EventContext context) { + if (listeners.containsKey(event)) { + listeners.get(event).forEach( + listener -> { + listener.receiveEvent(event, context); + } + ); + } + } +} diff --git a/src/main/java/pm/j4/kerosene/util/event/Listener.java b/src/main/java/pm/j4/kerosene/util/event/Listener.java new file mode 100644 index 0000000..910b242 --- /dev/null +++ b/src/main/java/pm/j4/kerosene/util/event/Listener.java @@ -0,0 +1,9 @@ +package pm.j4.kerosene.util.event; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface Listener { + public Event value(); +} diff --git a/src/main/java/pm/j4/kerosene/util/event/Listeners.java b/src/main/java/pm/j4/kerosene/util/event/Listeners.java new file mode 100644 index 0000000..377c27e --- /dev/null +++ b/src/main/java/pm/j4/kerosene/util/event/Listeners.java @@ -0,0 +1,5 @@ +package pm.j4.kerosene.util.event; + +public @interface Listeners { + public Listener[] value(); +} diff --git a/src/main/java/pm/j4/kerosene/util/module/ModuleBase.java b/src/main/java/pm/j4/kerosene/util/module/ModuleBase.java index 4ca883d..eb7ffe5 100644 --- a/src/main/java/pm/j4/kerosene/util/module/ModuleBase.java +++ b/src/main/java/pm/j4/kerosene/util/module/ModuleBase.java @@ -1,6 +1,7 @@ package pm.j4.kerosene.util.module; import com.google.gson.JsonElement; +import java.lang.annotation.Annotation; import java.util.*; import java.util.stream.Collectors; import net.minecraft.client.MinecraftClient; @@ -12,7 +13,12 @@ import pm.j4.kerosene.modules.bindings.BindingManager; import pm.j4.kerosene.modules.bindings.ChatCommands; import pm.j4.kerosene.util.config.ConfigHolder; import pm.j4.kerosene.util.config.ConfigManager; +import pm.j4.kerosene.util.config.Disables; +import pm.j4.kerosene.util.config.DisablesList; import pm.j4.kerosene.util.data.ChatCommand; +import pm.j4.kerosene.util.event.Event; +import pm.j4.kerosene.util.event.EventContext; +import pm.j4.kerosene.util.event.EventManager; import pm.j4.kerosene.util.module.option.ConfigurationOption; /** @@ -47,6 +53,7 @@ public abstract class ModuleBase { */ public void init() { BindingManager.registerBindings(this); + EventManager.register(this); ChatCommands.registerCommands(this.getChatCommands()); } @@ -64,6 +71,21 @@ public abstract class ModuleBase { */ public void toggle() { Optional config = ConfigManager.getConfig(this.getParent()); + try { + Annotation moduleAnnotation = this.getClass().getAnnotation(DisablesList.class); + if (moduleAnnotation != null) { + Disables[] modules = ((DisablesList)moduleAnnotation).value(); + System.out.println(modules); + for (int i = 0; i < modules.length; i++) { + System.out.println("disabling " + modules[i].value() + " as a requirement for " + this.getModuleName()); + int index = i; + config.ifPresent(configHolder -> configHolder.disableModule(modules[index].value())); + } + } + } + catch (Exception e) { + System.out.println(e); + } config.ifPresent(configHolder -> configHolder.toggleModule(this.moduleName)); } @@ -295,6 +317,9 @@ public abstract class ModuleBase { return entries; } + public void receiveEvent(Event event, EventContext context) {} + + // TODO evaluate if necessary /** * Equals boolean. * diff --git a/src/main/resources/kerosene.mixins.json b/src/main/resources/kerosene.mixins.json index d043c3f..c22b23f 100644 --- a/src/main/resources/kerosene.mixins.json +++ b/src/main/resources/kerosene.mixins.json @@ -7,8 +7,8 @@ ], "client": [ "EntryListWidgetAccessor", - "OptionsMenuMixin", - "ChatMessageMixin" + "OptionsScreenMixin", + "ClientPlayerEntityMixin" ], "injectors": { "defaultRequire": 1