forked from JustS-js/CatTeleportMod-Fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce92622
commit 76f33a1
Showing
7 changed files
with
167 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/java/net/just_s/ctpmod/commands/DeleteCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
src/main/java/net/just_s/ctpmod/util/CommandRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |