Skip to content

Commit

Permalink
Finished commands refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMasrozYTLIVE committed Jul 29, 2024
1 parent ce92622 commit 76f33a1
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 34 deletions.
8 changes: 2 additions & 6 deletions src/main/java/net/just_s/ctpmod/CTPMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Text> 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
Expand All @@ -90,7 +87,6 @@ public static Supplier<Text> 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);
}
}
20 changes: 20 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -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<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));
ctx.getSource().sendFeedback(CTPMod.generateFeedback(
"Point §f{0} §aadded§2 with period: §f{1}-{2}§2.",
waypointName, startPeriod, endPeriod
));
return 1;
}
}
42 changes: 42 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -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<Suggestions> suggest(CommandContext<FabricClientCommandSource> ctx, SuggestionsBuilder builder) {
return TpCommand.suggest(ctx, builder);
}

public static int run(CommandContext<FabricClientCommandSource> 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;
}
}
17 changes: 2 additions & 15 deletions src/main/java/net/just_s/ctpmod/commands/ListCommand.java
Original file line number Diff line number Diff line change
@@ -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<FabricClientCommandSource> fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) {
// fabricClientCommandSourceCommandDispatcher.register(
// literal("ctp").then(
// literal("list").executes(ListCommand::run)
// )
// );
// }

public static int run(CommandContext<ServerCommandSource> ctx) {
public static int run(CommandContext<FabricClientCommandSource> 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;
}
}
20 changes: 20 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/RcCommand.java
Original file line number Diff line number Diff line change
@@ -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<FabricClientCommandSource> 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;
}
}
42 changes: 42 additions & 0 deletions src/main/java/net/just_s/ctpmod/commands/TpCommand.java
Original file line number Diff line number Diff line change
@@ -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<Suggestions> suggest(CommandContext<FabricClientCommandSource> ignoredCtx, SuggestionsBuilder builder) {
for (Point point : CTPMod.CONFIG.points) {
builder.suggest(point.getName());
}
return builder.buildFuture();
}

public static int run(CommandContext<FabricClientCommandSource> 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> 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;
}
}
52 changes: 39 additions & 13 deletions src/main/java/net/just_s/ctpmod/util/CommandRegistry.java
Original file line number Diff line number Diff line change
@@ -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<ServerCommandSource> mainCommand = CommandManager
.literal("ctp")
.build();
ClientCommandRegistrationCallback.EVENT.register((CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) -> {
LiteralCommandNode<FabricClientCommandSource> mainCommand = literal("ctp").build();

LiteralCommandNode<ServerCommandSource> listNode = CommandManager
.literal("list")
.executes(ListCommand::run)
.build();
// List SubCommand
LiteralCommandNode<FabricClientCommandSource> listNode = literal("list").executes(ListCommand::run).build();

// RC SubCommand
LiteralCommandNode<FabricClientCommandSource> rcNode = literal("rc").build();
rcNode.addChild(argument("seconds", IntegerArgumentType.integer(1))
.executes(RcCommand::run).build());

// TP SubCommand
LiteralCommandNode<FabricClientCommandSource> tpNode = literal("tp").build();
tpNode.addChild(argument("name", StringArgumentType.word())
.suggests(TpCommand::suggest).executes(TpCommand::run).build());

// Delete SubCommand
LiteralCommandNode<FabricClientCommandSource> deleteNode = literal("delete").build();
deleteNode.addChild(argument("name", StringArgumentType.word())
.suggests(DeleteCommand::suggest).executes(DeleteCommand::run).build());

// Add SubCommand
LiteralCommandNode<FabricClientCommandSource> addNode = literal("add").build();
ArgumentCommandNode<FabricClientCommandSource, String> nameArgument = argument("name", StringArgumentType.word()).build();
ArgumentCommandNode<FabricClientCommandSource, Integer> 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);
});
}
}

0 comments on commit 76f33a1

Please sign in to comment.