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

117 lines
5.0 KiB
Java

package net.kelsoncraft.test;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.kelsoncraft.test.util.Messages;
import net.minecraft.block.PlayerSkullBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SentMessage;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
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() {
}
// I'm not sure how to use this.
// private String getPlayer() {
//
// PlayerEntity playerEntity = new PlayerEntity() {
// @Override
// public boolean isSpectator() {
// return false;
// }
//
// @Override
// public boolean isCreative() {
// return false;
// }
// }
// return "";
// }
// This may crash the server.
// PlayerEntity player;
// This might be fun.
// PlayerSkullBlock playerSkullBlock = new PlayerSkullBlock();
// PlayerInventory playerInventory = new PlayerInventory(player);
// CraftingInventory craftingInventory = new CraftingInventory();
// 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;
})));
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("test1")
.executes(context -> {
// // I don't know how to send a chat message with the below code. I need a message params
// SentMessage sentMessage = new SentMessage() {
// @Override
// public Text getContent() {
// return null;
// }
//
// @Override
// public void send(ServerPlayerEntity sender, boolean filterMaskEnabled, MessageType.Parameters params) {
//
// }
// };
// context.getSource().sendChatMessage(sentMessage, false, );
context.getSource().sendFeedback(() -> Text.literal("You will be exterminated!"), false);
return 1;
})));
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("craft")
.executes(context -> {
// // I don't know how to send a chat message with the below code. I need a message params
// SentMessage sentMessage = new SentMessage() {
// @Override
// public Text getContent() {
// return null;
// }
//
// @Override
// public void send(ServerPlayerEntity sender, boolean filterMaskEnabled, MessageType.Parameters params) {
//
// }
// };
// context.getSource().sendChatMessage(sentMessage, false, );
return 1;
})));
}
}