Add support for commands in classes other than the main one.
This commit is contained in:
parent
db76f97f6f
commit
1551562cee
@ -1,18 +1,34 @@
|
||||
package net.kelsoncraft.test;
|
||||
|
||||
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.text.Text;
|
||||
|
||||
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
||||
|
||||
// https://www.reddit.com/r/fabricmc/comments/r3bd47/fabric_execute_commands/
|
||||
|
||||
public class CommandTest {
|
||||
// This doesn't work at all in other classes.
|
||||
//
|
||||
// CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("test")
|
||||
// .executes(context -> {
|
||||
// // 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 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 CommandTest() {
|
||||
|
||||
}
|
||||
// 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;
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -73,8 +73,6 @@ public class KelsonCraftTest implements ModInitializer {
|
||||
// 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;
|
||||
})));
|
||||
|
||||
@ -84,6 +82,17 @@ public class KelsonCraftTest implements ModInitializer {
|
||||
context.getSource().sendFeedback(() -> Text.literal("Hello player %s"), false);
|
||||
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
|
||||
FuelRegistry.INSTANCE.add(CustomItems.CUSTOM_ITEM, 300);
|
||||
|
Loading…
Reference in New Issue
Block a user