Skip to content

Commit

Permalink
Added KeyBinds
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMasrozYTLIVE committed Jul 29, 2024
1 parent 92e2c28 commit 55c80de
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 46 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/just_s/ctpmod/CTPMod.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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());
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/just_s/ctpmod/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,7 +11,7 @@ public static int run(CommandContext<FabricClientCommandSource> 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
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/just_s/ctpmod/commands/RcCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +15,7 @@ public static int run(CommandContext<FabricClientCommandSource> ctx) {
));
return 1;
}
CTPMod.startReconnect(new Point(null, seconds, seconds));
CTPMod.startReconnect(new Point(null, seconds, seconds, ModifierKeyCode.unknown()));
return 1;
}
}
27 changes: 27 additions & 0 deletions src/main/java/net/just_s/ctpmod/config/ModMenuIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,40 @@
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 {
@Override
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<AbstractConfigListEntry> 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);
}
}
60 changes: 16 additions & 44 deletions src/main/java/net/just_s/ctpmod/config/Point.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,31 @@
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;
@ConfigEntry.Gui.Tooltip
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<String, String> 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.";
}
}
15 changes: 15 additions & 0 deletions src/main/java/net/just_s/ctpmod/util/KeybindRegistry.java
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
}

0 comments on commit 55c80de

Please sign in to comment.