110 lines
4.7 KiB
Java
110 lines
4.7 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) -> {
|
||
|
dispatcher.register(CommandManager.literal("attributes")
|
||
|
.then(CommandManager.literal("apply")
|
||
|
.then(CommandManager.argument("username", EntityArgumentType.player()))
|
||
|
.then(CommandManager.argument("attributeName", StringArgumentType.string()))
|
||
|
.executes(context -> ApplyAttribute(
|
||
|
EntityArgumentType.getPlayer(context, "username"),
|
||
|
StringArgumentType.getString(context, "attributeName"),
|
||
|
context
|
||
|
))));
|
||
|
});
|
||
|
|
||
|
// Command Root: "/attributes remove <username> <attribute name>"
|
||
|
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
|
||
|
dispatcher.register(CommandManager.literal("attributes")
|
||
|
.then(CommandManager.literal("delete")
|
||
|
.then(CommandManager.argument("username", EntityArgumentType.player()))
|
||
|
.then(CommandManager.argument("attributeName", StringArgumentType.greedyString()))
|
||
|
.executes(context -> DeleteAttribute(
|
||
|
EntityArgumentType.getPlayer(context, "username"),
|
||
|
StringArgumentType.getString(context, "attributeName"),
|
||
|
context
|
||
|
))));
|
||
|
});
|
||
|
|
||
|
// 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;
|
||
|
}
|
||
|
}
|