KelsonCraftFabricTest/src/main/java/net/kelsoncraft/test/TestCommands.java

43 lines
1.9 KiB
Java
Raw Normal View History

package net.kelsoncraft.test;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.kelsoncraft.test.util.Messages;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
// https://www.reddit.com/r/fabricmc/comments/r3bd47/fabric_execute_commands/
public class TestCommands {
// This idea came from here:
// https://github.com/Draylar/get-off-my-lawn/blob/1.17/src/main/java/draylar/goml/command/ClaimCommand.java#L32-L98
public TestCommands() {
}
// This works like this, now I know how to add commands to other classes, add them like this then initialize them in the main class.
public static void init() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("teleport")
.executes(context -> {
context.getSource().sendFeedback(() -> Text.literal("Teleport to %s"), false);
return 1;
})));
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("lightning")
.executes(context -> {
ServerCommandSource serverCommandSource = context.getSource();
// String playerName = serverCommandSource.getDisplayName().toString();
String playerName = serverCommandSource.getName();
String lightningMsg = String.format(Messages.Kbp_Fabric() + "Strike lightning at your location %s.", playerName);
context.getSource().sendFeedback(() -> Text.literal(lightningMsg), false);
return 1;
})));
}
}