Attribute Command and Player Equipment Tracking

This commit is contained in:
Kam committed 2025-02-25 17:14:55 -05:00
1 parent f1b77fe94f
commit 6903726300
7 files changed
+394 -129

No files matched your search

@@ -0,0 +1,211 @@
package kamkeel.npcs.command;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.attribute.AttributeController;
import noppes.npcs.attribute.AttributeDefinition;
import noppes.npcs.attribute.AttributeValueType;
import noppes.npcs.attribute.ItemAttributeHelper;
import noppes.npcs.controllers.MagicController;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class AttributeCommand extends CommandKamkeelBase {
@Override
public String getCommandName() {
return "attribute";
}
@Override
public String getDescription() {
return "Manage attributes on your held item";
}
@SubCommand(
name = "list",
usage = "[page]",
desc = "Lists all available attributes"
)
public void list(ICommandSender sender, String[] args) throws CommandException {
// Get all registered attributes.
List<AttributeDefinition> defs = new ArrayList<AttributeDefinition>(AttributeController.getAllAttributes());
if(defs.isEmpty()){
sendError(sender, "No attributes found.");
return;
}
// Sort attributes by display name.
Collections.sort(defs, new Comparator<AttributeDefinition>() {
@Override
public int compare(AttributeDefinition a, AttributeDefinition b) {
return a.getKey().compareToIgnoreCase(b.getKey());
}
});
// Determine which page to show.
int page = 1;
if(args.length > 0) {
try {
page = Integer.parseInt(args[0]);
} catch(NumberFormatException e) {
// If parsing fails, default to page 1.
}
}
int perPage = 10;
int total = defs.size();
int totalPages = (int)Math.ceil(total / (double) perPage);
if(page < 1) page = 1;
if(page > totalPages) page = totalPages;
int startIndex = (page - 1) * perPage;
int endIndex = Math.min(startIndex + perPage, total);
sendResult(sender, "--------------------");
sendResult(sender, "Attributes (Page " + page + "/" + totalPages + "):");
for (int i = startIndex; i < endIndex; i++) {
AttributeDefinition def = defs.get(i);
// First line: numbered display name.
sendResult(sender, (i + 1) + ". " + def.getDisplayName());
// Second line: key and value type.
sendResult(sender, " - " + def.getKey() + ", " + def.getValueType());
}
sendResult(sender, "--------------------");
}
@SubCommand(
name = "apply",
usage = "<attribute> <value> [magicID]",
desc = "Applies an attribute to your held item. For magic attributes, provide a magicID."
)
public void apply(ICommandSender sender, String[] args) throws CommandException {
if(!(sender instanceof EntityPlayer)){
sendError(sender, "Only players can use this command.");
return;
}
EntityPlayer player = (EntityPlayer)sender;
ItemStack held = player.getHeldItem();
if(held == null){
sendError(sender, "You are not holding an item.");
return;
}
String attrKey = args[0];
AttributeDefinition def = AttributeController.getAttribute(attrKey);
if(def == null){
sendError(sender, "Unknown attribute: " + attrKey);
return;
}
// Check if this is a magic attribute.
if(def.getValueType() == AttributeValueType.MAGIC) {
if(args.length < 3){
sendError(sender, "Usage for magic attribute: apply <attribute> <magicID> <value>");
return;
}
int magicId;
double value;
try {
value = Double.parseDouble(args[1]);
magicId = Integer.parseInt(args[2]);
} catch(NumberFormatException e) {
sendError(sender, "Invalid number format for magicID or value.");
return;
}
if(MagicController.getInstance().getMagic(magicId) == null){
sendError(sender, "No Magic Found for Magic ID %d", magicId);
return;
}
ItemAttributeHelper.writeMagicAttribute(held, attrKey, magicId, value);
sendResult(sender, "Applied magic attribute " + attrKey + " with magicID " + magicId + " and value " + value + " to held item.");
} else {
if(args.length < 2){
sendError(sender, "Usage: <attribute> <value>");
return;
}
double value;
try {
value = Double.parseDouble(args[1]);
} catch(NumberFormatException e) {
sendError(sender, "Invalid number format for value.");
return;
}
ItemAttributeHelper.applyAttribute(held, attrKey, value);
sendResult(sender, "Applied attribute " + attrKey + " with value " + value + " to held item.");
}
}
@SubCommand(
name = "remove",
usage = "<attribute> [magicID)",
desc = "Removes an attribute from your held item. For magic attributes, provide a magicID."
)
public void remove(ICommandSender sender, String[] args) throws CommandException {
if(!(sender instanceof EntityPlayer)){
sendError(sender, "Only players can use this command.");
return;
}
EntityPlayer player = (EntityPlayer)sender;
ItemStack held = player.getHeldItem();
if(held == null){
sendError(sender, "You are not holding an item.");
return;
}
String attrKey = args[0];
AttributeDefinition def = AttributeController.getAttribute(attrKey);
if(def == null){
sendError(sender, "Unknown attribute: " + attrKey);
return;
}
if(def.getValueType() == AttributeValueType.MAGIC) {
if(args.length < 2){
sendError(sender, "Usage for magic attribute: remove <attribute> <magicID>");
return;
}
int magicId;
try {
magicId = Integer.parseInt(args[1]);
} catch(NumberFormatException e) {
sendError(sender, "Invalid magicID format.");
return;
}
if(MagicController.getInstance().getMagic(magicId) == null){
sendError(sender, "No Magic Found for Magic ID %d", magicId);
return;
}
ItemAttributeHelper.removeMagicAttribute(held, attrKey, magicId);
sendResult(sender, "Removed magic attribute " + attrKey + " (magicID " + magicId + ") from held item.");
} else {
ItemAttributeHelper.removeAttribute(held, attrKey);
sendResult(sender, "Removed attribute " + attrKey + " from held item.");
}
}
@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args) {
// For the first parameter of the subcommand (apply or remove), list available attribute keys.
if(args.length == 1) {
List<String> keys = new ArrayList<>();
for (AttributeDefinition def : AttributeController.getAllAttributes()) {
keys.add(def.getKey());
}
return CommandBase.getListOfStringsMatchingLastWord(args, keys.toArray(new String[keys.size()]));
}
// If the attribute is magic and we are completing the magicID parameter, you could return dummy numbers.
return super.addTabCompletionOptions(sender, args);
}
}
@@ -16,110 +16,111 @@ import java.util.Map;
public class CommandKamkeel extends CommandBase{
public Map<String, CommandKamkeelBase> map = new HashMap<String, CommandKamkeelBase>();
public HelpCommand help = new HelpCommand(this);
public String[] alias = {"kam"};
public Map<String, CommandKamkeelBase> map = new HashMap<String, CommandKamkeelBase>();
public HelpCommand help = new HelpCommand(this);
public String[] alias = {"kam"};
public CommandKamkeel(){
registerCommand(help);
registerCommand(new ScriptCommand());
registerCommand(new SlayCommand());
registerCommand(new QuestCommand());
registerCommand(new QuestCategoryCommand());
registerCommand(new DialogCommand());
registerCommand(new DialogCategoryCommand());
registerCommand(new FactionCommand());
registerCommand(new NpcCommand());
registerCommand(new CloneCommand());
registerCommand(new ConfigCommand());
registerCommand(new AnimationCommand());
registerCommand(new OverlayCommand());
registerCommand(new CommandCommand());
public CommandKamkeel(){
registerCommand(help);
registerCommand(new ScriptCommand());
registerCommand(new SlayCommand());
registerCommand(new QuestCommand());
registerCommand(new QuestCategoryCommand());
registerCommand(new DialogCommand());
registerCommand(new DialogCategoryCommand());
registerCommand(new FactionCommand());
registerCommand(new NpcCommand());
registerCommand(new CloneCommand());
registerCommand(new ConfigCommand());
registerCommand(new AnimationCommand());
registerCommand(new OverlayCommand());
registerCommand(new CommandCommand());
registerCommand(new EffectCommand());
}
registerCommand(new AttributeCommand());
}
public void registerCommand(CommandKamkeelBase command){
String name = command.getCommandName().toLowerCase();
if(map.containsKey(name))
throw new CustomNPCsException("Already a subcommand with the name: " + name);
map.put(name, command);
}
public void registerCommand(CommandKamkeelBase command){
String name = command.getCommandName().toLowerCase();
if(map.containsKey(name))
throw new CustomNPCsException("Already a subcommand with the name: " + name);
map.put(name, command);
}
@Override
public String getCommandName() {
return "kamkeel";
}
@Override
public String getCommandName() {
return "kamkeel";
}
@Override
public List getCommandAliases()
{
return Arrays.asList(alias);
}
@Override
public List getCommandAliases()
{
return Arrays.asList(alias);
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "Use as /kamkeel subcommand";
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "Use as /kamkeel subcommand";
}
@Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
if(args.length == 0){
help.processCommand(sender, args);
return;
}
@Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
if(args.length == 0){
help.processCommand(sender, args);
return;
}
CommandKamkeelBase command = getCommand(args);
if(command == null)
throw new CommandException("Unknown command " + args[0]);
CommandKamkeelBase command = getCommand(args);
if(command == null)
throw new CommandException("Unknown command " + args[0]);
args = Arrays.copyOfRange(args, 1, args.length);
if(command.subcommands.isEmpty() || !command.runSubCommands()){
if(!canSendCommand(sender, command))
throw new CommandException("You are not allowed to use this command: " + command);
command.canRun(sender, command.getUsage(), args);
command.processCommand(sender, args);
return;
}
args = Arrays.copyOfRange(args, 1, args.length);
if(command.subcommands.isEmpty() || !command.runSubCommands()){
if(!canSendCommand(sender, command))
throw new CommandException("You are not allowed to use this command: " + command);
command.canRun(sender, command.getUsage(), args);
command.processCommand(sender, args);
return;
}
if(args.length == 0){
help.processCommand(sender, new String[]{command.getCommandName()});
return;
}
if(args.length == 0){
help.processCommand(sender, new String[]{command.getCommandName()});
return;
}
command.processSubCommand(sender, args[0], Arrays.copyOfRange(args, 1, args.length));
}
command.processSubCommand(sender, args[0], Arrays.copyOfRange(args, 1, args.length));
}
@Override
@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args){
if(args.length == 1)
return CommandBase.getListOfStringsMatchingLastWord(args, map.keySet().toArray(new String[map.size()]));
CommandKamkeelBase command = getCommand(args);
if(command == null)
return null;
if(args.length == 2 && command.runSubCommands())
return CommandBase.getListOfStringsMatchingLastWord(args, command.subcommands.keySet().toArray(new String[command.subcommands.keySet().size()]));
String[] useArgs = command.getUsage().split(" ");
if(command.runSubCommands()) {
Method m = command.subcommands.get(args[1].toLowerCase());
if(m != null) {
useArgs = m.getAnnotation(CommandKamkeelBase.SubCommand.class).usage().split(" ");
}
}
if(args.length <= useArgs.length + 2) {
if(args.length - 3 >= 0){
String usage = useArgs[args.length - 3];
if(usage.equals("<player>") || usage.equals("[player]")) {
return CommandBase.getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames());
}
}
}
return command.addTabCompletionOptions(sender, Arrays.copyOfRange(args, 1, args.length));
if(args.length == 1)
return CommandBase.getListOfStringsMatchingLastWord(args, map.keySet().toArray(new String[map.size()]));
CommandKamkeelBase command = getCommand(args);
if(command == null)
return null;
if(args.length == 2 && command.runSubCommands())
return CommandBase.getListOfStringsMatchingLastWord(args, command.subcommands.keySet().toArray(new String[command.subcommands.keySet().size()]));
String[] useArgs = command.getUsage().split(" ");
if(command.runSubCommands()) {
Method m = command.subcommands.get(args[1].toLowerCase());
if(m != null) {
useArgs = m.getAnnotation(CommandKamkeelBase.SubCommand.class).usage().split(" ");
}
}
if(args.length <= useArgs.length + 2) {
if(args.length - 3 >= 0){
String usage = useArgs[args.length - 3];
if(usage.equals("<player>") || usage.equals("[player]")) {
return CommandBase.getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames());
}
}
}
return command.addTabCompletionOptions(sender, Arrays.copyOfRange(args, 1, args.length));
}
public CommandKamkeelBase getCommand(String[] args){
if(args.length == 0)
return null;
return map.get(args[0].toLowerCase());
if(args.length == 0)
return null;
return map.get(args[0].toLowerCase());
}
@Override
@@ -127,28 +128,28 @@ public class CommandKamkeel extends CommandBase{
return 2;
}
public static String getCommandPermission(String command){
return "cnpc.kamkeel." + command.toLowerCase();
}
public static String getCommandPermission(String command){
return "cnpc.kamkeel." + command.toLowerCase();
}
public static String getUniversalPermission(){
return "cnpc.kamkeel.*";
}
public static String getUniversalPermission(){
return "cnpc.kamkeel.*";
}
public static boolean canSendCommand(ICommandSender sender, CommandKamkeelBase command){
if(sender.canCommandSenderUseCommand(command.getRequiredPermissionLevel(), getUniversalPermission())){
return true;
}
public static boolean canSendCommand(ICommandSender sender, CommandKamkeelBase command){
if(sender.canCommandSenderUseCommand(command.getRequiredPermissionLevel(), getUniversalPermission())){
return true;
}
if(sender.canCommandSenderUseCommand(command.getRequiredPermissionLevel(), getCommandPermission(command.getCommandName()))){
return true;
}
if(sender.canCommandSenderUseCommand(command.getRequiredPermissionLevel(), getCommandPermission(command.getCommandName()))){
return true;
}
if(sender instanceof EntityPlayer){
return CustomNpcsPermissions.hasCustomPermission((EntityPlayer) sender, getCommandPermission(command.getCommandName()));
}
if(sender instanceof EntityPlayer){
return CustomNpcsPermissions.hasCustomPermission((EntityPlayer) sender, getCommandPermission(command.getCommandName()));
}
return false;
}
return false;
}
}
@@ -6,7 +6,7 @@ package noppes.npcs.attribute;
* once in the AttributeController.
*/
public class AttributeDefinition {
private final String key; // e.g., "rpgcore:main_attack_flat"
private final String key; // e.g., "main_attack_flat"
private final String displayName; // Human‑readable name
private final AttributeValueType attributeValueType;
@@ -24,7 +24,7 @@ public class AttributeDefinition {
return displayName;
}
public AttributeValueType getOperation() {
public AttributeValueType getValueType() {
return attributeValueType;
}
}
@@ -1,17 +1,17 @@
package noppes.npcs.attribute;
public final class AttributeKeys {
public static final String MAIN_ATTACK_FLAT = "rpgcore:main_attack_flat";
public static final String MAIN_ATTACK_PERCENT = "rpgcore:main_attack_percent";
public static final String HEALTH = "rpgcore:health";
public static final String CRITICAL_CHANCE_PERCENT = "rpgcore:critical_chance_percent";
public static final String CRITICAL_DAMAGE_FLAT = "rpgcore:critical_damage_flat";
public static final String MAIN_ATTACK_FLAT = "main_attack_flat";
public static final String MAIN_ATTACK_PERCENT = "main_attack_percent";
public static final String HEALTH = "health";
public static final String CRITICAL_CHANCE_PERCENT = "critical_chance_percent";
public static final String CRITICAL_DAMAGE_FLAT = "critical_damage_flat";
// Magic–based attribute keys:
public static final String MAGIC_DAMAGE_FLAT = "rpgcore:magic_damage_flat";
public static final String MAGIC_DAMAGE_PERCENT = "rpgcore:magic_damage_percent";
public static final String MAGIC_DEFENSE_FLAT = "rpgcore:magic_defense_flat";
public static final String MAGIC_RESISTANCE_PERCENT = "rpgcore:magic_resistance_percent";
public static final String MAGIC_DAMAGE_FLAT = "magic_damage_flat";
public static final String MAGIC_DAMAGE_PERCENT = "magic_damage_percent";
public static final String MAGIC_DEFENSE_FLAT = "magic_defense_flat";
public static final String MAGIC_RESISTANCE_PERCENT = "magic_resistance_percent";
private AttributeKeys() { }
}
@@ -11,9 +11,12 @@ import java.util.Map;
* Non–magic attributes are stored under a compound tag (TAG_ROOT),
* and magic attributes are stored as a compound mapping (by magic ID) under specific keys.
*/
public class CNPCItemAttributeHelper {
public static final String TAG_ROOT = "CNPCAttributes";
public class ItemAttributeHelper {
public static final String TAG_ROOT = "RPGCoreAttributes";
/**
* Applies a non–magic attribute to an item.
*/
public static void applyAttribute(ItemStack item, String attributeKey, double value) {
if (item == null) return;
if (item.stackTagCompound == null) {
@@ -30,6 +33,25 @@ public class CNPCItemAttributeHelper {
root.setTag(TAG_ROOT, attrTag);
}
/**
* Removes a non–magic attribute from an item.
*/
public static void removeAttribute(ItemStack item, String attributeKey) {
if (item == null || item.stackTagCompound == null) return;
NBTTagCompound root = item.stackTagCompound;
if (root.hasKey(TAG_ROOT)) {
NBTTagCompound attrTag = root.getCompoundTag(TAG_ROOT);
attrTag.removeTag(attributeKey);
// If no attributes remain, remove the root tag.
if (attrTag.func_150296_c().isEmpty()) {
root.removeTag(TAG_ROOT);
}
}
}
/**
* Reads non–magic attributes from an item.
*/
public static Map<String, Double> readAttributes(ItemStack item) {
Map<String, Double> map = new HashMap<>();
if (item == null || item.stackTagCompound == null) return map;
@@ -44,6 +66,10 @@ public class CNPCItemAttributeHelper {
return map;
}
/**
* Reads a magic attribute map from an item.
* The given attributeTag is the key under which the compound is stored (e.g., MAGIC_DAMAGE_FLAT).
*/
public static Map<Integer, Double> readMagicAttributeMap(ItemStack item, String attributeTag) {
Map<Integer, Double> map = new HashMap<>();
if (item == null || item.stackTagCompound == null) return map;
@@ -62,6 +88,17 @@ public class CNPCItemAttributeHelper {
return map;
}
/**
* Applies (writes) a magic attribute to an item.
* This is essentially the same as writeMagicAttribute.
*/
public static void applyMagicAttribute(ItemStack item, String attributeTag, int magicId, double value) {
writeMagicAttribute(item, attributeTag, magicId, value);
}
/**
* Writes a magic attribute value to the given attributeTag.
*/
public static void writeMagicAttribute(ItemStack item, String attributeTag, int magicId, double value) {
if (item == null) return;
if (item.stackTagCompound == null)
@@ -75,4 +112,20 @@ public class CNPCItemAttributeHelper {
magicMap.setDouble(String.valueOf(magicId), value);
item.stackTagCompound.setTag(attributeTag, magicMap);
}
/**
* Removes a magic attribute value from the given attributeTag.
*/
public static void removeMagicAttribute(ItemStack item, String attributeTag, int magicId) {
if (item == null || item.stackTagCompound == null)
return;
NBTTagCompound root = item.stackTagCompound;
if (root.hasKey(attributeTag)) {
NBTTagCompound magicMap = root.getCompoundTag(attributeTag);
magicMap.removeTag(String.valueOf(magicId));
// If the magic map is empty, remove the tag.
if (magicMap.func_150296_c().isEmpty())
root.removeTag(attributeTag);
}
}
}
@@ -61,7 +61,7 @@ public class PlayerAttributeTracker {
ItemStack held = currentEquip.heldItem;
if (held != null) {
// Process non-magic attributes.
for (Entry<String, Double> entry : CNPCItemAttributeHelper.readAttributes(held).entrySet()) {
for (Entry<String, Double> entry : ItemAttributeHelper.readAttributes(held).entrySet()) {
String key = entry.getKey();
double value = entry.getValue();
AttributeDefinition def = AttributeController.getAttribute(key);
@@ -74,13 +74,13 @@ public class PlayerAttributeTracker {
}
}
// Process magic offense.
Map<Integer, Double> magicFlat = CNPCItemAttributeHelper.readMagicAttributeMap(held, AttributeKeys.MAGIC_DAMAGE_FLAT);
Map<Integer, Double> magicFlat = ItemAttributeHelper.readMagicAttributeMap(held, AttributeKeys.MAGIC_DAMAGE_FLAT);
for (Entry<Integer, Double> entry : magicFlat.entrySet()) {
int magicId = entry.getKey();
double value = entry.getValue();
aggregatedMagicDamageFlat.put(magicId, aggregatedMagicDamageFlat.getOrDefault(magicId, 0.0) + value);
}
Map<Integer, Double> magicPercent = CNPCItemAttributeHelper.readMagicAttributeMap(held, AttributeKeys.MAGIC_DAMAGE_PERCENT);
Map<Integer, Double> magicPercent = ItemAttributeHelper.readMagicAttributeMap(held, AttributeKeys.MAGIC_DAMAGE_PERCENT);
for (Entry<Integer, Double> entry : magicPercent.entrySet()) {
int magicId = entry.getKey();
double value = entry.getValue();
@@ -93,7 +93,7 @@ public class PlayerAttributeTracker {
for (ItemStack piece : armor) {
if (piece != null) {
// Process non-magic attributes.
for (Entry<String, Double> entry : CNPCItemAttributeHelper.readAttributes(piece).entrySet()) {
for (Entry<String, Double> entry : ItemAttributeHelper.readAttributes(piece).entrySet()) {
String key = entry.getKey();
double value = entry.getValue();
AttributeDefinition def = AttributeController.getAttribute(key);
@@ -106,13 +106,13 @@ public class PlayerAttributeTracker {
}
}
// Process magic defense.
Map<Integer, Double> magicDefFlat = CNPCItemAttributeHelper.readMagicAttributeMap(piece, AttributeKeys.MAGIC_DEFENSE_FLAT);
Map<Integer, Double> magicDefFlat = ItemAttributeHelper.readMagicAttributeMap(piece, AttributeKeys.MAGIC_DEFENSE_FLAT);
for (Entry<Integer, Double> entry : magicDefFlat.entrySet()) {
int magicId = entry.getKey();
double value = entry.getValue();
aggregatedMagicDefenseFlat.put(magicId, aggregatedMagicDefenseFlat.getOrDefault(magicId, 0.0) + value);
}
Map<Integer, Double> magicResist = CNPCItemAttributeHelper.readMagicAttributeMap(piece, AttributeKeys.MAGIC_RESISTANCE_PERCENT);
Map<Integer, Double> magicResist = ItemAttributeHelper.readMagicAttributeMap(piece, AttributeKeys.MAGIC_RESISTANCE_PERCENT);
for (Entry<Integer, Double> entry : magicResist.entrySet()) {
int magicId = entry.getKey();
double value = entry.getValue();
@@ -56,10 +56,10 @@ public class PlayerEquipmentTracker {
if (a.getItem() != b.getItem()) return false;
NBTTagCompound tagA = a.stackTagCompound;
NBTTagCompound tagB = b.stackTagCompound;
String attrA = (tagA != null && tagA.hasKey(CNPCItemAttributeHelper.TAG_ROOT))
? tagA.getCompoundTag(CNPCItemAttributeHelper.TAG_ROOT).toString() : "";
String attrB = (tagB != null && tagB.hasKey(CNPCItemAttributeHelper.TAG_ROOT))
? tagB.getCompoundTag(CNPCItemAttributeHelper.TAG_ROOT).toString() : "";
String attrA = (tagA != null && tagA.hasKey(ItemAttributeHelper.TAG_ROOT))
? tagA.getCompoundTag(ItemAttributeHelper.TAG_ROOT).toString() : "";
String attrB = (tagB != null && tagB.hasKey(ItemAttributeHelper.TAG_ROOT))
? tagB.getCompoundTag(ItemAttributeHelper.TAG_ROOT).toString() : "";
return attrA.equals(attrB);
}
}