Add support for commands in classes other than the main one.

This commit is contained in:
kelson8 2024-03-14 05:48:14 -04:00
parent db76f97f6f
commit 1551562cee
Signed by: kelson8
GPG Key ID: 3D738D9DD7239E13
2 changed files with 38 additions and 13 deletions

View File

@ -1,18 +1,34 @@
package net.kelsoncraft.test; package net.kelsoncraft.test;
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; 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.text.Text;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
// https://www.reddit.com/r/fabricmc/comments/r3bd47/fabric_execute_commands/
public class CommandTest { public class CommandTest {
// This doesn't work at all in other classes. // 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
// CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("test")
// .executes(context -> { public CommandTest() {
// // For versions below 1.19, replace "Text.literal" with "new LiteralText".
// // For versions below 1.20, remode "() ->" directly.
// context.getSource().sendFeedback(() -> Text.literal("Called /test with no arguments"), false);
//
//
// return 1;
// })));
} }
// 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;
})));
}
}

View File

@ -73,8 +73,6 @@ public class KelsonCraftTest implements ModInitializer {
// For versions below 1.19, replace "Text.literal" with "new LiteralText". // For versions below 1.19, replace "Text.literal" with "new LiteralText".
// For versions below 1.20, remode "() ->" directly. // For versions below 1.20, remode "() ->" directly.
context.getSource().sendFeedback(() -> Text.literal("Called /test with no arguments"), false); context.getSource().sendFeedback(() -> Text.literal("Called /test with no arguments"), false);
return 1; return 1;
}))); })));
@ -84,6 +82,17 @@ public class KelsonCraftTest implements ModInitializer {
context.getSource().sendFeedback(() -> Text.literal("Hello player %s"), false); context.getSource().sendFeedback(() -> Text.literal("Hello player %s"), false);
return 1; return 1;
}))); })));
// This should add the teleport command, I got the idea from this:
// https://github.com/Draylar/get-off-my-lawn/blob/1.17/src/main/java/draylar/goml/GetOffMyLawn.java#L30-L37
CommandTest.init();
// CommandTest commandTest = new CommandTest();
//I wonder how to implement a teleport command, and a try to put this in another class.
// CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("teleport")
// .executes(context -> {
// context.getSource().sendFeedback(() -> Text.literal("Teleport to %s"), false);
// return 1;
// })));
// This is a way to use the item as fuel // This is a way to use the item as fuel
FuelRegistry.INSTANCE.add(CustomItems.CUSTOM_ITEM, 300); FuelRegistry.INSTANCE.add(CustomItems.CUSTOM_ITEM, 300);