diff --git a/src/main/java/net/just_s/ctpmod/CTPMod.java b/src/main/java/net/just_s/ctpmod/CTPMod.java index 2a2e665..70f4379 100644 --- a/src/main/java/net/just_s/ctpmod/CTPMod.java +++ b/src/main/java/net/just_s/ctpmod/CTPMod.java @@ -23,9 +23,6 @@ import net.minecraft.client.MinecraftClient; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.function.Supplier; @Environment(EnvType.CLIENT) public class CTPMod implements ClientModInitializer { @@ -79,7 +76,7 @@ public void connectToServer(ServerInfo targetInfo) { ConnectScreen.connect(new MultiplayerScreen(new TitleScreen()), MC, ServerAddress.parse(targetInfo.address), targetInfo, false, (CookieStorage)null); } - public static Supplier generateFeedback(String message, Object... args) { + public static Text generateFeedback(String message, Object... args) { //Send message in chat that only user can see //§0 black §8 dark_gray §g minecoin_gold //§1 dark_blue §9 blue §f white @@ -90,7 +87,6 @@ public static Supplier generateFeedback(String message, Object... args) { for (int i = 0; i < args.length; i++) { message = message.replace("{" + i + "}", args[i].toString()); } - String finalMessage = message; - return () -> Text.of("§8[§6CatTeleport§8]§2 " + finalMessage); + return Text.of("§8[§6CatTeleport§8]§2 " + message); } } diff --git a/src/main/java/net/just_s/ctpmod/commands/AddCommand.java b/src/main/java/net/just_s/ctpmod/commands/AddCommand.java new file mode 100644 index 0000000..56cb25a --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/AddCommand.java @@ -0,0 +1,20 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; + +public class AddCommand { + 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)); + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "Point §f{0} §aadded§2 with period: §f{1}-{2}§2.", + waypointName, startPeriod, endPeriod + )); + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/commands/DeleteCommand.java b/src/main/java/net/just_s/ctpmod/commands/DeleteCommand.java new file mode 100644 index 0000000..8d8f341 --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/DeleteCommand.java @@ -0,0 +1,42 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; + +import java.util.concurrent.CompletableFuture; + +public class DeleteCommand { + public static CompletableFuture suggest(CommandContext ctx, SuggestionsBuilder builder) { + return TpCommand.suggest(ctx, builder); + } + + public static int run(CommandContext ctx) { + String pointName = ctx.getArgument("name", String.class); + + boolean deleted = false; + for(Point point : CTPMod.CONFIG.points) { + if(point.getName().equals(pointName)) { + deleted = true; + CTPMod.CONFIG.points.remove(point); + break; + } + } + + if (deleted) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "Point §f{0} §cdeleted§2.", + pointName + )); + } else { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "Point §f{0} §cwas not found!§2.", + pointName + )); + } + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/commands/ListCommand.java b/src/main/java/net/just_s/ctpmod/commands/ListCommand.java index 08a9d85..42b1875 100644 --- a/src/main/java/net/just_s/ctpmod/commands/ListCommand.java +++ b/src/main/java/net/just_s/ctpmod/commands/ListCommand.java @@ -1,31 +1,18 @@ package net.just_s.ctpmod.commands; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.just_s.ctpmod.CTPMod; import net.just_s.ctpmod.config.Point; -import net.minecraft.command.CommandRegistryAccess; -import net.minecraft.server.command.ServerCommandSource; import java.util.stream.Collectors; -import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; - public class ListCommand { -// public static void register(CommandDispatcher fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) { -// fabricClientCommandSourceCommandDispatcher.register( -// literal("ctp").then( -// literal("list").executes(ListCommand::run) -// ) -// ); -// } - - public static int run(CommandContext ctx) { + public static int run(CommandContext ctx) { String message = "list of loaded Points:\n" + CTPMod.CONFIG.points.stream().map(Point::toString).collect(Collectors.joining("\n")); ctx.getSource().sendFeedback(CTPMod.generateFeedback( message - ), false); + )); return 1; } } diff --git a/src/main/java/net/just_s/ctpmod/commands/RcCommand.java b/src/main/java/net/just_s/ctpmod/commands/RcCommand.java new file mode 100644 index 0000000..cfdba42 --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/RcCommand.java @@ -0,0 +1,20 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; + +public class RcCommand { + public static int run(CommandContext ctx) { + int seconds = ctx.getArgument("seconds", int.class); + if (CTPMod.MC.isInSingleplayer()) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining." + )); + return 1; + } + CTPMod.startReconnect(new Point(null, seconds, seconds)); + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/commands/TpCommand.java b/src/main/java/net/just_s/ctpmod/commands/TpCommand.java new file mode 100644 index 0000000..28e4b3b --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/TpCommand.java @@ -0,0 +1,42 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; + +import java.util.Optional; +import java.util.concurrent.CompletableFuture; + +public class TpCommand { + public static CompletableFuture suggest(CommandContext ignoredCtx, SuggestionsBuilder builder) { + for (Point point : CTPMod.CONFIG.points) { + builder.suggest(point.getName()); + } + return builder.buildFuture(); + } + + public static int run(CommandContext ctx) { + if (CTPMod.MC.isInSingleplayer()) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining." + )); + return 1; + } + String pointName = ctx.getArgument("name", String.class); + Optional point = CTPMod.CONFIG.points.stream().filter(p -> p.getName().equals(pointName)).findFirst(); + if (point.isEmpty()) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "§cThere is no §fPoint §cwith name \"§f{0}§c\".", + pointName + )); + return 0; + } + + //if everything is okay, only then start reconnect cycle: + CTPMod.startReconnect(point.get()); + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/util/CommandRegistry.java b/src/main/java/net/just_s/ctpmod/util/CommandRegistry.java index df19de9..0168b71 100644 --- a/src/main/java/net/just_s/ctpmod/util/CommandRegistry.java +++ b/src/main/java/net/just_s/ctpmod/util/CommandRegistry.java @@ -1,30 +1,56 @@ package net.just_s.ctpmod.util; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.tree.ArgumentCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; -import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; -import net.just_s.ctpmod.commands.ListCommand; +import net.just_s.ctpmod.commands.*; import net.minecraft.command.CommandRegistryAccess; -import net.minecraft.server.command.CommandManager; -import net.minecraft.server.command.ServerCommandSource; -import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; public class CommandRegistry { public static void registerCommands() { - CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { - LiteralCommandNode mainCommand = CommandManager - .literal("ctp") - .build(); + ClientCommandRegistrationCallback.EVENT.register((CommandDispatcher dispatcher, CommandRegistryAccess registryAccess) -> { + LiteralCommandNode mainCommand = literal("ctp").build(); - LiteralCommandNode listNode = CommandManager - .literal("list") - .executes(ListCommand::run) - .build(); + // List SubCommand + LiteralCommandNode listNode = literal("list").executes(ListCommand::run).build(); + + // RC SubCommand + LiteralCommandNode rcNode = literal("rc").build(); + rcNode.addChild(argument("seconds", IntegerArgumentType.integer(1)) + .executes(RcCommand::run).build()); + + // TP SubCommand + LiteralCommandNode tpNode = literal("tp").build(); + tpNode.addChild(argument("name", StringArgumentType.word()) + .suggests(TpCommand::suggest).executes(TpCommand::run).build()); + + // Delete SubCommand + LiteralCommandNode deleteNode = literal("delete").build(); + deleteNode.addChild(argument("name", StringArgumentType.word()) + .suggests(DeleteCommand::suggest).executes(DeleteCommand::run).build()); + + // Add SubCommand + LiteralCommandNode addNode = literal("add").build(); + ArgumentCommandNode nameArgument = argument("name", StringArgumentType.word()).build(); + ArgumentCommandNode startArgument = argument("startPeriod", IntegerArgumentType.integer(0)).build(); + startArgument.addChild(argument("endPeriod", IntegerArgumentType.integer(1)) + .executes(AddCommand::run).build()); + nameArgument.addChild(startArgument); + addNode.addChild(nameArgument); dispatcher.getRoot().addChild(mainCommand); mainCommand.addChild(listNode); + mainCommand.addChild(rcNode); + mainCommand.addChild(tpNode); + mainCommand.addChild(deleteNode); + mainCommand.addChild(addNode); }); } }