add event system and module exclusion, plus common tick event

This commit is contained in:
jane 2021-03-07 20:10:33 -05:00
parent bc0435cee1
commit a0b4103e4d
12 changed files with 133 additions and 6 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -0,0 +1,5 @@
package pm.j4.kerosene.util.event;
public enum Event {
TICK
}

View File

@ -0,0 +1,5 @@
package pm.j4.kerosene.util.event;
public class EventContext {
public static EventContext NoEventContext = new EventContext();
}

View File

@ -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<Event, Set<ModuleBase>> 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<ModuleBase> modules = listeners.get(event);
if(!modules.contains(module)) {
modules.add(module);
}
}
else {
Set<ModuleBase> 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);
}
);
}
}
}

View File

@ -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();
}

View File

@ -0,0 +1,5 @@
package pm.j4.kerosene.util.event;
public @interface Listeners {
public Listener[] value();
}

View File

@ -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<ConfigHolder> 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.
*

View File

@ -7,8 +7,8 @@
],
"client": [
"EntryListWidgetAccessor",
"OptionsMenuMixin",
"ChatMessageMixin"
"OptionsScreenMixin",
"ClientPlayerEntityMixin"
],
"injectors": {
"defaultRequire": 1