From 206063acf3a6e0edc0d16b07d206aa36207b42d4 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Tue, 5 Jul 2022 22:37:47 +0900 Subject: a --- .../internal/mixin/ClientModLoaderMixin.java | 5 +- .../internal/mixin/KeyboardListenerMixin.java | 2 +- .../internal/mixin/MouseHelperAccessor.java | 11 ++ .../oneconfig/internal/mixin/MouseHelperMixin.java | 2 +- .../oneconfig/internal/mixin/TimeTrackerMixin.java | 2 +- .../utils/commands/PlatformCommandManagerImpl.java | 123 +++++++++++++++++++++ 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperAccessor.java create mode 100644 versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java (limited to 'versions/1.16.2-forge/src/main/java') diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/ClientModLoaderMixin.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/ClientModLoaderMixin.java index 2bfbaef..b0caed6 100644 --- a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/ClientModLoaderMixin.java +++ b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/ClientModLoaderMixin.java @@ -2,6 +2,7 @@ package cc.polyfrost.oneconfig.internal.mixin; import cc.polyfrost.oneconfig.events.EventManager; import cc.polyfrost.oneconfig.events.event.InitializationEvent; +import cc.polyfrost.oneconfig.internal.OneConfig; import net.minecraftforge.fml.client.ClientModLoader; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -9,13 +10,13 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(value = ClientModLoader.class, remap = false) -public class ClientModLoaderMixin { //todo +public class ClientModLoaderMixin { @Inject(method = "begin", at = @At("HEAD")) private static void onBegin(CallbackInfo ci) { OneConfig.preLaunch(); } - @Inject(method = "finishModLoading", at = @At("HEAD")) + @Inject(method = "lambda$finishModLoading$9", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/GameSettings;loadOptions()V")) private static void onFinishModLoading(CallbackInfo ci) { EventManager.INSTANCE.post(new InitializationEvent()); OneConfig.init(); diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/KeyboardListenerMixin.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/KeyboardListenerMixin.java index 5cd10cf..c7e6327 100644 --- a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/KeyboardListenerMixin.java +++ b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/KeyboardListenerMixin.java @@ -9,7 +9,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(KeyboardListener.class) -public class KeyboardListenerMixin { //todo +public class KeyboardListenerMixin { @Inject(method = "onKeyEvent", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/ForgeHooksClient;fireKeyInput(IIII)V")) private void onKeyEvent(long windowPointer, int key, int scanCode, int action, int modifiers, CallbackInfo ci) { EventManager.INSTANCE.post(new KeyInputEvent()); diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperAccessor.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperAccessor.java new file mode 100644 index 0000000..d04ecb1 --- /dev/null +++ b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperAccessor.java @@ -0,0 +1,11 @@ +package cc.polyfrost.oneconfig.internal.mixin; + +import net.minecraft.client.MouseHelper; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(MouseHelper.class) +public interface MouseHelperAccessor { + @Accessor + double getAccumulatedScrollDelta(); +} diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperMixin.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperMixin.java index 95a5b0c..42dd283 100644 --- a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperMixin.java +++ b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/MouseHelperMixin.java @@ -9,7 +9,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(MouseHelper.class) -public class MouseHelperMixin { //todo +public class MouseHelperMixin { @Inject(method = "mouseButtonCallback", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/ForgeHooksClient;fireMouseInput(III)V")) private void onMouse(long handle, int button, int action, int mods, CallbackInfo ci) { EventManager.INSTANCE.post(new MouseInputEvent()); diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/TimeTrackerMixin.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/TimeTrackerMixin.java index e2d31fe..dd9920e 100644 --- a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/TimeTrackerMixin.java +++ b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/TimeTrackerMixin.java @@ -13,7 +13,7 @@ import java.util.function.IntSupplier; import java.util.function.LongSupplier; @Mixin(TimeTracker.class) -public class TimeTrackerMixin { //todo +public class TimeTrackerMixin { @Inject(method = "", at = @At("RETURN")) private void onStart(LongSupplier longSupplier, IntSupplier intSupplier, CallbackInfo ci) { EventManager.INSTANCE.post(new StartEvent()); diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java new file mode 100644 index 0000000..81bc9b2 --- /dev/null +++ b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java @@ -0,0 +1,123 @@ +package cc.polyfrost.oneconfig.utils.commands; + +import cc.polyfrost.oneconfig.libs.universal.ChatColor; +import cc.polyfrost.oneconfig.libs.universal.UChat; +import cc.polyfrost.oneconfig.utils.commands.annotations.Command; +import cc.polyfrost.oneconfig.utils.commands.annotations.Greedy; +import cc.polyfrost.oneconfig.utils.commands.arguments.ArgumentParser; +import cc.polyfrost.oneconfig.utils.commands.arguments.Arguments; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.concurrent.CompletableFuture; + +import static cc.polyfrost.oneconfig.utils.commands.CommandManager.METHOD_RUN_ERROR; + +public class PlatformCommandManagerImpl extends PlatformCommandManager { + + final HashMap, Pair, ArgumentType>> parsers = new HashMap<>(); // non-greedy, greedy + + @Override + void createCommand(CommandManager.InternalCommand root, Command annotation) { + LiteralArgumentBuilder builder = Commands.literal(annotation.value()); + if (!root.invokers.isEmpty()) { + builder.executes((context -> + { + try { + root.invokers.get(0).method.invoke(null); + return 1; + } catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException | + ExceptionInInitializerError e) { + UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + METHOD_RUN_ERROR); + return 0; + } + })); + } + if (annotation.helpCommand()) { + builder.then(Commands.literal("help").executes((context -> + { + UChat.chat(sendHelpCommand(root)); + return 1; + }))); + } + for (CommandManager.InternalCommand command : root.children) { + loopThroughChildren(command, builder, null); + } + } + + private void loopThroughChildren(CommandManager.InternalCommand command, LiteralArgumentBuilder root, LiteralArgumentBuilder builder) { + if (command.invokers.isEmpty() || command.children.isEmpty()) return; + if (builder == null) { + builder = root.then(Commands.literal(command.name)); + } else { + builder = builder.then(Commands.literal(command.name)); + } + for (CommandManager.InternalCommand.InternalCommandInvoker invoker : command.invokers) { + for (Parameter parameter : invoker.method.getParameters()) { + Pair, ArgumentType> pair = parsers.get(parameter.getType()); + builder.then(Commands.argument(parameter.getName(), parameter.isAnnotationPresent(Greedy.class) ? pair.getRight() : pair.getLeft())); + } + builder.executes((context -> + { + try { + ArrayList args = new ArrayList<>(invoker.method.getParameterCount()); + for (Parameter parameter: invoker.method.getParameters()) { + args.add(context.getArgument(parameter.getName(), Object.class)); + } + invoker.method.invoke(null, args); + return 1; + } catch (Exception e) { + e.printStackTrace(); + UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + METHOD_RUN_ERROR); + return 0; + } + })); + } + for (CommandManager.InternalCommand child : command.children) { + loopThroughChildren(child, root, builder); + } + } + + @Override + public void handleNewParser(ArgumentParser parser, Class clazz) { + parsers.put(clazz, new ImmutablePair, ArgumentType>(new ArgumentType() { + + @Override + public Object parse(StringReader reader) { + final String text = reader.getRemaining(); + reader.setCursor(reader.getTotalLength()); + return parser.parse(new Arguments(text.split("\\s+"), false)); + } + + @Override + public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { + return ArgumentType.super.listSuggestions(context, builder); + } + }, new ArgumentType() { + + @Override + public Object parse(StringReader reader) { + final String text = reader.getRemaining(); + reader.setCursor(reader.getTotalLength()); + return parser.parse(new Arguments(text.split("\\s+"), true)); + } + + @Override + public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { + return ArgumentType.super.listSuggestions(context, builder); + } + })); + } +} -- cgit