Skip to content

Commit

Permalink
Clean up command registration
Browse files Browse the repository at this point in the history
  • Loading branch information
the-pink-hacker committed Jan 8, 2024
1 parent ba64903 commit 50df29b
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 41 deletions.
60 changes: 43 additions & 17 deletions src/main/java/com/thepinkhacker/commandsplus/CommandsPlus.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.thepinkhacker.commandsplus;

import com.mojang.brigadier.CommandDispatcher;
import com.thepinkhacker.commandsplus.command.argument.ArgumentTypeManager;
import com.thepinkhacker.commandsplus.server.command.*;
import com.thepinkhacker.commandsplus.server.dedicated.command.CPStopCommand;
import com.thepinkhacker.commandsplus.server.dedicated.command.CommandRegistrationCallbackDedicated;
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
import com.thepinkhacker.commandsplus.world.GameRuleManager;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
// Commands
ClearSpawnPointCommand.register(dispatcher);
DayLockCommand.register(dispatcher);
GameRulePresetCommand.register(dispatcher);
HeadCommand.register(dispatcher);
HealthCommand.register(dispatcher);
HungerCommand.register(dispatcher);
NameCommand.register(dispatcher);
RideCommand.register(dispatcher, registryAccess);
SetOwnerCommand.register(dispatcher);
ToggleDownfallCommand.register(dispatcher);

// Dedicated server
if (environment.dedicated) {
CPStopCommand.register(dispatcher);
}
registerCommands(
dispatcher,
registryAccess,
environment,
new CommandRegistrationCallback[] {
new ClearSpawnPointCommand(),
new DayLockCommand(),
new GameRulePresetCommand(),
new HeadCommand(),
new HealthCommand(),
new HungerCommand(),
new NameCommand(),
new RideCommand(),
new SetOwnerCommand(),
new ToggleDownfallCommand(),
new CPStopCommand(),
}
);

// Aliases
AliasUtils.createAlias(dispatcher, "gamemode", "gm");
AliasUtils.createAlias(dispatcher, "help", "?");

LOGGER.info("Registered commands");
LOGGER.info("Registered commands+.");
});
}

private static void registerCommands(
CommandDispatcher<net.minecraft.server.command.ServerCommandSource> dispatcher,
CommandRegistryAccess registryAccess,
CommandManager.RegistrationEnvironment environment,
CommandRegistrationCallback[] commands
) {
for (CommandRegistrationCallback command : commands) {
if (command instanceof CommandRegistrationCallbackDedicated) {
if (environment.dedicated) command.register(dispatcher, registryAccess, environment);
} else {
command.register(dispatcher, registryAccess, environment);
}
}
}

public static Identifier identifier(String id) {
return new Identifier(MOD_ID, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class ArgumentTypeManager {
public static void register() {
CommandsPlus.LOGGER.info("Registering argument types");
ArgumentTypeRegistry.registerArgumentType(
new Identifier(CommandsPlus.MOD_ID, "gamerule_preset"),
CommandsPlus.identifier("gamerule_preset"),
GameRulePresetArgumentType.class,
ConstantArgumentSerializer.of(GameRulePresetArgumentType::preset)
);
ArgumentTypeRegistry.registerArgumentType(
new Identifier(CommandsPlus.MOD_ID, "teleport_rule"),
CommandsPlus.identifier("teleport_rule"),
TeleportRuleArgumentType.class,
ConstantArgumentSerializer.of(TeleportRuleArgumentType::teleportRule)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -13,8 +15,9 @@
import java.util.ArrayList;
import java.util.Collection;

public class ClearSpawnPointCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
public class ClearSpawnPointCommand implements CommandRegistrationCallback {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
dispatcher.register(CommandManager.literal("clearspawnpoint")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.argument("targets", EntityArgumentType.players())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.world.GameRules;

public class DayLockCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
public class DayLockCommand implements CommandRegistrationCallback {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("daylock")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.argument("lock", BoolArgumentType.bool())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
import com.thepinkhacker.commandsplus.CommandsPlus;
import com.thepinkhacker.commandsplus.command.argument.GameRulePresetArgumentType;
import com.thepinkhacker.commandsplus.world.GameRulePreset;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.apache.commons.io.FilenameUtils;

import java.nio.file.Path;

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

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
dispatcher.register(CommandManager.literal("gamerulepreset")
.then(CommandManager.literal("save")
.requires(source -> source.hasPermissionLevel(4))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.block.entity.SkullBlockEntity;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.GameProfileArgumentType;
Expand All @@ -27,10 +29,11 @@
import java.util.ArrayList;
import java.util.Collection;

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

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("head")
.then(CommandManager.literal("give")
.requires(source -> source.hasPermissionLevel(2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
Expand All @@ -16,8 +18,9 @@
import java.util.ArrayList;
import java.util.Collection;

public class HealthCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
public class HealthCommand implements CommandRegistrationCallback {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("health")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.literal("set")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.entity.player.HungerManager;
import net.minecraft.server.command.CommandManager;
Expand All @@ -18,8 +20,9 @@
import java.util.Collection;
import java.util.Objects;

public class HungerCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
public class HungerCommand implements CommandRegistrationCallback {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("hunger")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.literal("set")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.ItemSlotArgumentType;
import net.minecraft.command.argument.MessageArgumentType;
Expand All @@ -16,8 +18,9 @@

import java.util.Collection;

public class NameCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
public class NameCommand implements CommandRegistrationCallback {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
dispatcher.register(CommandManager.literal("name")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.literal("item")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.thepinkhacker.commandsplus.command.argument.TeleportRuleArgumentType;
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.RegistryEntryArgumentType;
Expand All @@ -26,15 +27,16 @@

import java.util.Collection;

public class RideCommand {
public class RideCommand implements CommandRegistrationCallback {
private static final SimpleCommandExceptionType START_RIDING_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.start_riding.fail"));
private static final SimpleCommandExceptionType STOP_RIDING_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.stop_riding.fail"));
private static final SimpleCommandExceptionType EVICT_RIDERS_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.evict_riders.fail"));
private static final SimpleCommandExceptionType SUMMON_RIDER_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.summon_rider.fail"));
private static final SimpleCommandExceptionType SUMMON_RIDE_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.ride.summon_ride.fail"));
private static final SimpleCommandExceptionType FAILED_UUID_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.summon.failed.uuid"));

public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
/*
* Todo: Add optional fields from bedrock edition
* - nameTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.thepinkhacker.commandsplus.util.command.AliasUtils;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.TameableEntity;
Expand All @@ -15,8 +17,9 @@

import java.util.Collection;

public class SetOwnerCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
public class SetOwnerCommand implements CommandRegistrationCallback {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
LiteralCommandNode<ServerCommandSource> node = dispatcher.register(CommandManager.literal("setowner")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.argument("pets", EntityArgumentType.entities())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.thepinkhacker.commandsplus.server.command;

import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;

public class ToggleDownfallCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
public class ToggleDownfallCommand implements CommandRegistrationCallback {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
dispatcher.register(CommandManager.literal("toggledownfall")
.requires(source -> source.hasPermissionLevel(2))
.executes(context -> execute(context.getSource()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SignedMessage;
import net.minecraft.server.MinecraftServer;
Expand All @@ -12,11 +14,12 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;

public class CPStopCommand {
public class CPStopCommand implements CommandRegistrationCallbackDedicated {
private static volatile int timeLeft;
private static final SimpleCommandExceptionType FAILED_CANCEL = new SimpleCommandExceptionType(Text.translatable("commands.cpstop.cancel.fail"));

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
@Override
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
dispatcher.register(CommandManager.literal("cpstop")
.requires(source -> source.hasPermissionLevel(4))
.then(CommandManager.literal("cancel")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.thepinkhacker.commandsplus.server.dedicated.command;

import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;

@FunctionalInterface
public interface CommandRegistrationCallbackDedicated extends CommandRegistrationCallback {}

0 comments on commit 50df29b

Please sign in to comment.