Skip to content

Commit 50df29b

Browse files
Clean up command registration
1 parent ba64903 commit 50df29b

14 files changed

+105
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.thepinkhacker.commandsplus;
22

3+
import com.mojang.brigadier.CommandDispatcher;
34
import com.thepinkhacker.commandsplus.command.argument.ArgumentTypeManager;
45
import com.thepinkhacker.commandsplus.server.command.*;
56
import com.thepinkhacker.commandsplus.server.dedicated.command.CPStopCommand;
7+
import com.thepinkhacker.commandsplus.server.dedicated.command.CommandRegistrationCallbackDedicated;
68
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
79
import com.thepinkhacker.commandsplus.world.GameRuleManager;
810
import net.fabricmc.api.ModInitializer;
911
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
12+
import net.minecraft.command.CommandRegistryAccess;
13+
import net.minecraft.server.command.CommandManager;
14+
import net.minecraft.util.Identifier;
1015
import org.apache.logging.log4j.LogManager;
1116
import org.apache.logging.log4j.Logger;
1217

@@ -20,28 +25,49 @@ public void onInitialize() {
2025
GameRuleManager.register();
2126

2227
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
23-
// Commands
24-
ClearSpawnPointCommand.register(dispatcher);
25-
DayLockCommand.register(dispatcher);
26-
GameRulePresetCommand.register(dispatcher);
27-
HeadCommand.register(dispatcher);
28-
HealthCommand.register(dispatcher);
29-
HungerCommand.register(dispatcher);
30-
NameCommand.register(dispatcher);
31-
RideCommand.register(dispatcher, registryAccess);
32-
SetOwnerCommand.register(dispatcher);
33-
ToggleDownfallCommand.register(dispatcher);
34-
35-
// Dedicated server
36-
if (environment.dedicated) {
37-
CPStopCommand.register(dispatcher);
38-
}
28+
registerCommands(
29+
dispatcher,
30+
registryAccess,
31+
environment,
32+
new CommandRegistrationCallback[] {
33+
new ClearSpawnPointCommand(),
34+
new DayLockCommand(),
35+
new GameRulePresetCommand(),
36+
new HeadCommand(),
37+
new HealthCommand(),
38+
new HungerCommand(),
39+
new NameCommand(),
40+
new RideCommand(),
41+
new SetOwnerCommand(),
42+
new ToggleDownfallCommand(),
43+
new CPStopCommand(),
44+
}
45+
);
3946

4047
// Aliases
4148
AliasUtils.createAlias(dispatcher, "gamemode", "gm");
4249
AliasUtils.createAlias(dispatcher, "help", "?");
4350

44-
LOGGER.info("Registered commands");
51+
LOGGER.info("Registered commands+.");
4552
});
4653
}
54+
55+
private static void registerCommands(
56+
CommandDispatcher<net.minecraft.server.command.ServerCommandSource> dispatcher,
57+
CommandRegistryAccess registryAccess,
58+
CommandManager.RegistrationEnvironment environment,
59+
CommandRegistrationCallback[] commands
60+
) {
61+
for (CommandRegistrationCallback command : commands) {
62+
if (command instanceof CommandRegistrationCallbackDedicated) {
63+
if (environment.dedicated) command.register(dispatcher, registryAccess, environment);
64+
} else {
65+
command.register(dispatcher, registryAccess, environment);
66+
}
67+
}
68+
}
69+
70+
public static Identifier identifier(String id) {
71+
return new Identifier(MOD_ID, id);
72+
}
4773
}

src/main/java/com/thepinkhacker/commandsplus/command/argument/ArgumentTypeManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public class ArgumentTypeManager {
99
public static void register() {
1010
CommandsPlus.LOGGER.info("Registering argument types");
1111
ArgumentTypeRegistry.registerArgumentType(
12-
new Identifier(CommandsPlus.MOD_ID, "gamerule_preset"),
12+
CommandsPlus.identifier("gamerule_preset"),
1313
GameRulePresetArgumentType.class,
1414
ConstantArgumentSerializer.of(GameRulePresetArgumentType::preset)
1515
);
1616
ArgumentTypeRegistry.registerArgumentType(
17-
new Identifier(CommandsPlus.MOD_ID, "teleport_rule"),
17+
CommandsPlus.identifier("teleport_rule"),
1818
TeleportRuleArgumentType.class,
1919
ConstantArgumentSerializer.of(TeleportRuleArgumentType::teleportRule)
2020
);

src/main/java/com/thepinkhacker/commandsplus/server/command/ClearSpawnPointCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.mojang.brigadier.CommandDispatcher;
44
import com.mojang.brigadier.exceptions.CommandSyntaxException;
55
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
6+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
7+
import net.minecraft.command.CommandRegistryAccess;
68
import net.minecraft.command.argument.EntityArgumentType;
79
import net.minecraft.server.command.CommandManager;
810
import net.minecraft.server.command.ServerCommandSource;
@@ -13,8 +15,9 @@
1315
import java.util.ArrayList;
1416
import java.util.Collection;
1517

16-
public class ClearSpawnPointCommand {
17-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
18+
public class ClearSpawnPointCommand implements CommandRegistrationCallback {
19+
@Override
20+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
1821
dispatcher.register(CommandManager.literal("clearspawnpoint")
1922
.requires(source -> source.hasPermissionLevel(2))
2023
.then(CommandManager.argument("targets", EntityArgumentType.players())

src/main/java/com/thepinkhacker/commandsplus/server/command/DayLockCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import com.mojang.brigadier.arguments.BoolArgumentType;
55
import com.mojang.brigadier.tree.LiteralCommandNode;
66
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
7+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
8+
import net.minecraft.command.CommandRegistryAccess;
79
import net.minecraft.server.command.CommandManager;
810
import net.minecraft.server.command.ServerCommandSource;
911
import net.minecraft.text.Text;
1012
import net.minecraft.world.GameRules;
1113

12-
public class DayLockCommand {
13-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
14+
public class DayLockCommand implements CommandRegistrationCallback {
15+
@Override
16+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
1417
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("daylock")
1518
.requires(source -> source.hasPermissionLevel(2))
1619
.then(CommandManager.argument("lock", BoolArgumentType.bool())

src/main/java/com/thepinkhacker/commandsplus/server/command/GameRulePresetCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
import com.thepinkhacker.commandsplus.CommandsPlus;
77
import com.thepinkhacker.commandsplus.command.argument.GameRulePresetArgumentType;
88
import com.thepinkhacker.commandsplus.world.GameRulePreset;
9+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
10+
import net.minecraft.command.CommandRegistryAccess;
911
import net.minecraft.server.command.CommandManager;
1012
import net.minecraft.server.command.ServerCommandSource;
1113
import net.minecraft.text.Text;
1214
import org.apache.commons.io.FilenameUtils;
1315

1416
import java.nio.file.Path;
1517

16-
public class GameRulePresetCommand {
18+
public class GameRulePresetCommand implements CommandRegistrationCallback {
1719
private static final DynamicCommandExceptionType FAILED_TO_LOAD_EXCEPTION = new DynamicCommandExceptionType(preset -> Text.translatable("commands.gamerulepreset.load.fail", preset));
1820

19-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
21+
@Override
22+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
2023
dispatcher.register(CommandManager.literal("gamerulepreset")
2124
.then(CommandManager.literal("save")
2225
.requires(source -> source.hasPermissionLevel(4))

src/main/java/com/thepinkhacker/commandsplus/server/command/HeadCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
77
import com.mojang.brigadier.tree.LiteralCommandNode;
88
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
9+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
910
import net.minecraft.block.entity.SkullBlockEntity;
11+
import net.minecraft.command.CommandRegistryAccess;
1012
import net.minecraft.command.argument.BlockPosArgumentType;
1113
import net.minecraft.command.argument.EntityArgumentType;
1214
import net.minecraft.command.argument.GameProfileArgumentType;
@@ -27,10 +29,11 @@
2729
import java.util.ArrayList;
2830
import java.util.Collection;
2931

30-
public class HeadCommand {
32+
public class HeadCommand implements CommandRegistrationCallback {
3133
private static final SimpleCommandExceptionType GIVE_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.head.give.fail"));
3234

33-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
35+
@Override
36+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
3437
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("head")
3538
.then(CommandManager.literal("give")
3639
.requires(source -> source.hasPermissionLevel(2))

src/main/java/com/thepinkhacker/commandsplus/server/command/HealthCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
77
import com.mojang.brigadier.tree.LiteralCommandNode;
88
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
9+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
10+
import net.minecraft.command.CommandRegistryAccess;
911
import net.minecraft.command.argument.EntityArgumentType;
1012
import net.minecraft.entity.Entity;
1113
import net.minecraft.entity.LivingEntity;
@@ -16,8 +18,9 @@
1618
import java.util.ArrayList;
1719
import java.util.Collection;
1820

19-
public class HealthCommand {
20-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
21+
public class HealthCommand implements CommandRegistrationCallback {
22+
@Override
23+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
2124
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("health")
2225
.requires(source -> source.hasPermissionLevel(2))
2326
.then(CommandManager.literal("set")

src/main/java/com/thepinkhacker/commandsplus/server/command/HungerCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
88
import com.mojang.brigadier.tree.LiteralCommandNode;
99
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
10+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
11+
import net.minecraft.command.CommandRegistryAccess;
1012
import net.minecraft.command.argument.EntityArgumentType;
1113
import net.minecraft.entity.player.HungerManager;
1214
import net.minecraft.server.command.CommandManager;
@@ -18,8 +20,9 @@
1820
import java.util.Collection;
1921
import java.util.Objects;
2022

21-
public class HungerCommand {
22-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
23+
public class HungerCommand implements CommandRegistrationCallback {
24+
@Override
25+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
2326
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("hunger")
2427
.requires(source -> source.hasPermissionLevel(2))
2528
.then(CommandManager.literal("set")

src/main/java/com/thepinkhacker/commandsplus/server/command/NameCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.mojang.brigadier.CommandDispatcher;
44
import com.mojang.brigadier.exceptions.CommandSyntaxException;
55
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
6+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
7+
import net.minecraft.command.CommandRegistryAccess;
68
import net.minecraft.command.argument.EntityArgumentType;
79
import net.minecraft.command.argument.ItemSlotArgumentType;
810
import net.minecraft.command.argument.MessageArgumentType;
@@ -16,8 +18,9 @@
1618

1719
import java.util.Collection;
1820

19-
public class NameCommand {
20-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
21+
public class NameCommand implements CommandRegistrationCallback {
22+
@Override
23+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
2124
dispatcher.register(CommandManager.literal("name")
2225
.requires(source -> source.hasPermissionLevel(2))
2326
.then(CommandManager.literal("item")

src/main/java/com/thepinkhacker/commandsplus/server/command/RideCommand.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.mojang.brigadier.tree.LiteralCommandNode;
77
import com.thepinkhacker.commandsplus.command.argument.TeleportRuleArgumentType;
88
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
9+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
910
import net.minecraft.command.CommandRegistryAccess;
1011
import net.minecraft.command.argument.EntityArgumentType;
1112
import net.minecraft.command.argument.RegistryEntryArgumentType;
@@ -26,15 +27,16 @@
2627

2728
import java.util.Collection;
2829

29-
public class RideCommand {
30+
public class RideCommand implements CommandRegistrationCallback {
3031
private static final SimpleCommandExceptionType START_RIDING_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.start_riding.fail"));
3132
private static final SimpleCommandExceptionType STOP_RIDING_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.stop_riding.fail"));
3233
private static final SimpleCommandExceptionType EVICT_RIDERS_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.evict_riders.fail"));
3334
private static final SimpleCommandExceptionType SUMMON_RIDER_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.summon_rider.fail"));
3435
private static final SimpleCommandExceptionType SUMMON_RIDE_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.summon_ride.fail"));
3536
private static final SimpleCommandExceptionType FAILED_UUID_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.summon.failed.uuid"));
3637

37-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
38+
@Override
39+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
3840
/*
3941
* Todo: Add optional fields from bedrock edition
4042
* - nameTag

src/main/java/com/thepinkhacker/commandsplus/server/command/SetOwnerCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
66
import com.mojang.brigadier.tree.LiteralCommandNode;
77
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
8+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
9+
import net.minecraft.command.CommandRegistryAccess;
810
import net.minecraft.command.argument.EntityArgumentType;
911
import net.minecraft.entity.Entity;
1012
import net.minecraft.entity.passive.TameableEntity;
@@ -15,8 +17,9 @@
1517

1618
import java.util.Collection;
1719

18-
public class SetOwnerCommand {
19-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
20+
public class SetOwnerCommand implements CommandRegistrationCallback {
21+
@Override
22+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
2023
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("setowner")
2124
.requires(source -> source.hasPermissionLevel(2))
2225
.then(CommandManager.argument("pets", EntityArgumentType.entities())

src/main/java/com/thepinkhacker/commandsplus/server/command/ToggleDownfallCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.thepinkhacker.commandsplus.server.command;
22

33
import com.mojang.brigadier.CommandDispatcher;
4+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
5+
import net.minecraft.command.CommandRegistryAccess;
46
import net.minecraft.server.command.CommandManager;
57
import net.minecraft.server.command.ServerCommandSource;
68
import net.minecraft.server.world.ServerWorld;
79
import net.minecraft.text.Text;
810

9-
public class ToggleDownfallCommand {
10-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
11+
public class ToggleDownfallCommand implements CommandRegistrationCallback {
12+
@Override
13+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
1114
dispatcher.register(CommandManager.literal("toggledownfall")
1215
.requires(source -> source.hasPermissionLevel(2))
1316
.executes(context -> execute(context.getSource()))

src/main/java/com/thepinkhacker/commandsplus/server/dedicated/command/CPStopCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.mojang.brigadier.arguments.IntegerArgumentType;
55
import com.mojang.brigadier.exceptions.CommandSyntaxException;
66
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
7+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
8+
import net.minecraft.command.CommandRegistryAccess;
79
import net.minecraft.network.message.MessageType;
810
import net.minecraft.network.message.SignedMessage;
911
import net.minecraft.server.MinecraftServer;
@@ -12,11 +14,12 @@
1214
import net.minecraft.server.command.ServerCommandSource;
1315
import net.minecraft.text.Text;
1416

15-
public class CPStopCommand {
17+
public class CPStopCommand implements CommandRegistrationCallbackDedicated {
1618
private static volatile int timeLeft;
1719
private static final SimpleCommandExceptionType FAILED_CANCEL = new SimpleCommandExceptionType(Text.translatable("commands.cpstop.cancel.fail"));
1820

19-
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
21+
@Override
22+
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
2023
dispatcher.register(CommandManager.literal("cpstop")
2124
.requires(source -> source.hasPermissionLevel(4))
2225
.then(CommandManager.literal("cancel")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.thepinkhacker.commandsplus.server.dedicated.command;
2+
3+
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
4+
5+
@FunctionalInterface
6+
public interface CommandRegistrationCallbackDedicated extends CommandRegistrationCallback {}

0 commit comments

Comments
 (0)