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

@@ -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);
}
}