From 55c80de7b5651d7f05050108cc06c29b0048b50b Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 29 Jul 2024 21:25:44 +0300 Subject: [PATCH] Added KeyBinds --- src/main/java/net/just_s/ctpmod/CTPMod.java | 9 +++ .../just_s/ctpmod/commands/AddCommand.java | 3 +- .../net/just_s/ctpmod/commands/RcCommand.java | 3 +- .../ctpmod/config/ModMenuIntegration.java | 27 +++++++++ .../java/net/just_s/ctpmod/config/Point.java | 60 +++++-------------- .../just_s/ctpmod/util/KeybindRegistry.java | 15 +++++ 6 files changed, 71 insertions(+), 46 deletions(-) create mode 100644 src/main/java/net/just_s/ctpmod/util/KeybindRegistry.java diff --git a/src/main/java/net/just_s/ctpmod/CTPMod.java b/src/main/java/net/just_s/ctpmod/CTPMod.java index 70f4379..168a68b 100644 --- a/src/main/java/net/just_s/ctpmod/CTPMod.java +++ b/src/main/java/net/just_s/ctpmod/CTPMod.java @@ -1,14 +1,19 @@ package net.just_s.ctpmod; import me.shedaniel.autoconfig.AutoConfig; +import me.shedaniel.autoconfig.gui.registry.api.GuiProvider; +import me.shedaniel.autoconfig.gui.registry.api.GuiRegistryAccess; import me.shedaniel.autoconfig.serializer.GsonConfigSerializer; +import me.shedaniel.clothconfig2.api.AbstractConfigListEntry; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.just_s.ctpmod.config.ModConfig; +import net.just_s.ctpmod.config.ModMenuIntegration; import net.just_s.ctpmod.config.Point; import net.just_s.ctpmod.util.CommandRegistry; +import net.just_s.ctpmod.util.KeybindRegistry; import net.just_s.ctpmod.util.ReconnectThread; import net.minecraft.client.gui.screen.*; import net.minecraft.client.gui.screen.multiplayer.ConnectScreen; @@ -22,6 +27,8 @@ import net.minecraft.client.MinecraftClient; +import java.lang.reflect.Field; +import java.util.List; import java.util.Objects; @Environment(EnvType.CLIENT) @@ -38,10 +45,12 @@ public class CTPMod implements ClientModInitializer { @Override public void onInitializeClient() { + ModMenuIntegration.registerCustomTypes(); AutoConfig.register(ModConfig.class, GsonConfigSerializer::new); CONFIG = AutoConfig.getConfigHolder(ModConfig.class).getConfig(); CommandRegistry.registerCommands(); + KeybindRegistry.registerKeyBinds(); ClientPlayConnectionEvents.JOIN.register((networkHandler, packetSender, client) -> currentServer = client.getCurrentServerEntry()); } diff --git a/src/main/java/net/just_s/ctpmod/commands/AddCommand.java b/src/main/java/net/just_s/ctpmod/commands/AddCommand.java index 56cb25a..908ff66 100644 --- a/src/main/java/net/just_s/ctpmod/commands/AddCommand.java +++ b/src/main/java/net/just_s/ctpmod/commands/AddCommand.java @@ -1,6 +1,7 @@ package net.just_s.ctpmod.commands; import com.mojang.brigadier.context.CommandContext; +import me.shedaniel.clothconfig2.api.ModifierKeyCode; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.just_s.ctpmod.CTPMod; import net.just_s.ctpmod.config.Point; @@ -10,7 +11,7 @@ public static int run(CommandContext ctx) { String waypointName = ctx.getArgument("name", String.class); int startPeriod = ctx.getArgument("startPeriod", int.class); int endPeriod = ctx.getArgument("endPeriod", int.class); - CTPMod.CONFIG.points.add(new Point(waypointName, startPeriod, endPeriod)); + CTPMod.CONFIG.points.add(new Point(waypointName, startPeriod, endPeriod, ModifierKeyCode.unknown())); ctx.getSource().sendFeedback(CTPMod.generateFeedback( "Point §f{0} §aadded§2 with period: §f{1}-{2}§2.", waypointName, startPeriod, endPeriod diff --git a/src/main/java/net/just_s/ctpmod/commands/RcCommand.java b/src/main/java/net/just_s/ctpmod/commands/RcCommand.java index cfdba42..edf6952 100644 --- a/src/main/java/net/just_s/ctpmod/commands/RcCommand.java +++ b/src/main/java/net/just_s/ctpmod/commands/RcCommand.java @@ -1,6 +1,7 @@ package net.just_s.ctpmod.commands; import com.mojang.brigadier.context.CommandContext; +import me.shedaniel.clothconfig2.api.ModifierKeyCode; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.just_s.ctpmod.CTPMod; import net.just_s.ctpmod.config.Point; @@ -14,7 +15,7 @@ public static int run(CommandContext ctx) { )); return 1; } - CTPMod.startReconnect(new Point(null, seconds, seconds)); + CTPMod.startReconnect(new Point(null, seconds, seconds, ModifierKeyCode.unknown())); return 1; } } diff --git a/src/main/java/net/just_s/ctpmod/config/ModMenuIntegration.java b/src/main/java/net/just_s/ctpmod/config/ModMenuIntegration.java index 7e21001..6119637 100644 --- a/src/main/java/net/just_s/ctpmod/config/ModMenuIntegration.java +++ b/src/main/java/net/just_s/ctpmod/config/ModMenuIntegration.java @@ -3,8 +3,19 @@ import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; import me.shedaniel.autoconfig.AutoConfig; +import me.shedaniel.autoconfig.gui.registry.api.GuiProvider; +import me.shedaniel.autoconfig.gui.registry.api.GuiRegistryAccess; +import me.shedaniel.autoconfig.util.Utils; +import me.shedaniel.clothconfig2.api.AbstractConfigListEntry; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import me.shedaniel.clothconfig2.api.ModifierKeyCode; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.text.Text; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; @Environment(EnvType.CLIENT) public class ModMenuIntegration implements ModMenuApi { @@ -12,4 +23,20 @@ public class ModMenuIntegration implements ModMenuApi { public ConfigScreenFactory getModConfigScreenFactory() { return parent -> AutoConfig.getConfigScreen(ModConfig.class, parent).get(); } + + public static void registerCustomTypes() { + AutoConfig.getGuiRegistry(ModConfig.class).registerTypeProvider((i18n, field, config, defaults, registry) -> { + Point point = (Point) config; + ModifierKeyCode keyCode = Utils.getUnsafely(field, point, ModifierKeyCode.unknown()); + + List list = new ArrayList<>(); + ConfigEntryBuilder builder = ConfigEntryBuilder.create(); + list.add(builder.startModifierKeyCodeField(Text.translatable(i18n), keyCode) + .setModifierSaveConsumer(point::setKeybind) + .setAllowMouse(false) + .build()); + + return list; + }, ModifierKeyCode.class); + } } diff --git a/src/main/java/net/just_s/ctpmod/config/Point.java b/src/main/java/net/just_s/ctpmod/config/Point.java index 74bf733..a3914cc 100644 --- a/src/main/java/net/just_s/ctpmod/config/Point.java +++ b/src/main/java/net/just_s/ctpmod/config/Point.java @@ -1,12 +1,19 @@ package net.just_s.ctpmod.config; -import com.google.gson.Gson; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; import me.shedaniel.autoconfig.annotation.ConfigEntry; - -import javax.annotation.Nullable; -import java.util.HashMap; -import java.util.Map; - +import me.shedaniel.clothconfig2.api.ModifierKeyCode; +import net.minecraft.client.option.KeyBinding; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@SuperBuilder public class Point { @ConfigEntry.Gui.Tooltip private String name; @@ -14,46 +21,11 @@ public class Point { private int startPeriod; @ConfigEntry.Gui.Tooltip private int endPeriod; - - public Point() {} - - public Point(String name, int startPeriod, int endPeriod) { - this.name = name; - this.startPeriod = startPeriod; - this.endPeriod = endPeriod; - } - - public String getName() {return this.name;} - - public void setName(String newName) {this.name = newName;} - - public String toJson() { - Map point = new HashMap<>(); - point.put("name", this.name); - point.put("startPeriod", "" + this.startPeriod); - point.put("endPeriod", "" + this.endPeriod); - Gson gson = new Gson(); - return gson.toJson(point); - } + @ConfigEntry.Gui.Tooltip + private ModifierKeyCode keybind; @Override public String toString() { - return "Point §f" + name + " §2with period: §f" + startPeriod + "-" + endPeriod + "§2."; - } - - public void setStartPeriod(int value) { - this.startPeriod = value; - } - - public void setEndPeriod(int value) { - this.endPeriod = value; - } - - public int getStartPeriod() { - return this.startPeriod; - } - - public int getEndPeriod() { - return this.endPeriod; + return "Point §f" + name + " §2with period: §f" + startPeriod + "-" + endPeriod + "§2 and keybind:§f" + keybind.toString() + "§2."; } } diff --git a/src/main/java/net/just_s/ctpmod/util/KeybindRegistry.java b/src/main/java/net/just_s/ctpmod/util/KeybindRegistry.java new file mode 100644 index 0000000..8d8e0f8 --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/util/KeybindRegistry.java @@ -0,0 +1,15 @@ +package net.just_s.ctpmod.util; + +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; + +public class KeybindRegistry { + public static void registerKeyBinds() { + ClientTickEvents.END_CLIENT_TICK.register(client -> { + for(Point point : CTPMod.CONFIG.points) { + if(point.getKeybind().matchesCurrentKey()) CTPMod.startReconnect(point); + } + }); + } +}