132 lines
5.8 KiB
Java
132 lines
5.8 KiB
Java
/*
|
|
*
|
|
* AttributeCommands
|
|
*
|
|
* This class handles all the possible in-game commands for the attribute system
|
|
*
|
|
*
|
|
*/
|
|
|
|
package jesse.keeblarcraft.Commands;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import com.mojang.brigadier.arguments.StringArgumentType;
|
|
import com.mojang.brigadier.context.CommandContext;
|
|
|
|
import jesse.keeblarcraft.Keeblarcraft;
|
|
import jesse.keeblarcraft.AttributeMgr.AttributeMgr;
|
|
import jesse.keeblarcraft.AttributeMgr.AttributeNodes.AbstractNode;
|
|
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
|
import net.minecraft.command.argument.EntityArgumentType;
|
|
import net.minecraft.server.command.CommandManager;
|
|
import net.minecraft.server.command.ServerCommandSource;
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
import net.minecraft.text.Text;
|
|
|
|
public class AttributeCommands {
|
|
public void RegisterCommands() {
|
|
|
|
// Command Root: "/attributes apply <username> <attribute name>"
|
|
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
|
|
var attributeNode = CommandManager.literal("attributes").build();
|
|
|
|
var applySubnode = CommandManager.literal("apply").build();
|
|
|
|
var playerArg = CommandManager.argument("targetPlayer", EntityArgumentType.player()).build();
|
|
|
|
var attributeNameArg = CommandManager.argument("attributeName", StringArgumentType.greedyString())
|
|
.executes(context -> ApplyAttribute
|
|
(
|
|
EntityArgumentType.getPlayer(context, "targetPlayer"),
|
|
StringArgumentType.getString(context, "attributeName"),
|
|
context)
|
|
)
|
|
.build();
|
|
|
|
// Build out the argument tree here
|
|
dispatcher.getRoot().addChild(attributeNode);
|
|
attributeNode.addChild(applySubnode);
|
|
applySubnode.addChild(playerArg);
|
|
playerArg.addChild(attributeNameArg);
|
|
});
|
|
|
|
// Command Root: "/attributes remove <username> <attribute name>"
|
|
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
|
|
var attributeNode = CommandManager.literal("attributes").build();
|
|
|
|
var applySubnode = CommandManager.literal("delete").build();
|
|
|
|
var playerArg = CommandManager.argument("targetPlayer", EntityArgumentType.player()).build();
|
|
|
|
var attributeNameArg = CommandManager.argument("attributeName", StringArgumentType.greedyString())
|
|
.executes(context -> DeleteAttribute
|
|
(
|
|
EntityArgumentType.getPlayer(context, "targetPlayer"),
|
|
StringArgumentType.getString(context, "attributeName"),
|
|
context)
|
|
)
|
|
.build();
|
|
|
|
// Build out the argument tree here
|
|
dispatcher.getRoot().addChild(attributeNode);
|
|
attributeNode.addChild(applySubnode);
|
|
applySubnode.addChild(playerArg);
|
|
playerArg.addChild(attributeNameArg);
|
|
});
|
|
|
|
// Command Root: "/attribute-test <attribute_name>"
|
|
/// TODO: Remove this in final version. This is purely debug
|
|
// CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
|
|
// dispatcher.register(CommandManager.literal("attribute-test")
|
|
// .then(CommandManager.argument("value", StringArgumentType.greedyString())
|
|
// .executes(context -> ApplyAttribute(StringArgumentType.getString(context, "value"), context))));
|
|
// });
|
|
|
|
// Command Root: "/list-attributes"
|
|
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
|
|
dispatcher.register(CommandManager.literal("list-attributes")
|
|
.executes(context -> ListAttributes(context)));
|
|
});
|
|
}
|
|
|
|
public int ApplyAttribute(ServerPlayerEntity targetPlayer, String attributeName, CommandContext<ServerCommandSource> context) {
|
|
int ret = -1;
|
|
|
|
System.out.println("Applying attribute");
|
|
if (context.getSource().isExecutedByPlayer()) {
|
|
System.out.println("Executed by player");
|
|
// String result = AttributeMgr.ApplyAttribute(targetPlayer.getUuidAsString(), attributeName);
|
|
String result = AttributeMgr.ApplyAttribute(context.getSource().getPlayer().getUuidAsString(), attributeName);
|
|
Keeblarcraft.LOGGER.info("[ApplyAttribute] -> " + result);
|
|
context.getSource().getPlayer().sendMessage(Text.of(result));
|
|
ret = 0;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public int DeleteAttribute(ServerPlayerEntity username, String attributeName, CommandContext<ServerCommandSource> context) {
|
|
int ret = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
}
|
|
|
|
public int ListAttributes(CommandContext<ServerCommandSource> context) {
|
|
int ret = -1;
|
|
|
|
if (context.getSource().isExecutedByPlayer()) {
|
|
ServerPlayerEntity player = context.getSource().getPlayer();
|
|
|
|
for (Entry<String, AbstractNode> entry : AttributeMgr.attributes.entrySet()) {
|
|
Keeblarcraft.LOGGER.debug("ATTR-LIST: " + entry.getKey() + " LINKS " + entry.getValue().GetNodeTitle());
|
|
player.sendMessage(Text.of(entry.getKey()));
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
}
|