Better Attribute Handling System + Requirement Registry System

This commit is contained in:
Kam committed 2025-02-28 17:14:26 -05:00
1 parent d0bd5ff210
commit 05041464c1
7 files changed
+172 -90

No files matched your search

@@ -0,0 +1,16 @@
package kamkeel.npcs.controllers.data.attribute.requirement;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
public interface IRequirementChecker {
public String getKey();
/**
* Checks the requirement in the provided NBT.
* Return false if the requirement isn’t met.
*/
boolean check(EntityPlayer player, NBTTagCompound nbt);
}
@@ -0,0 +1,22 @@
package kamkeel.npcs.controllers.data.attribute.requirement;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.controllers.data.PlayerData;
public class QuestCompletedRequirement implements IRequirementChecker {
@Override
public String getKey() {
return "cnpc_quest_completed";
}
@Override
public boolean check(EntityPlayer player, NBTTagCompound nbt) {
if(nbt.hasKey(getKey())) {
int questID = nbt.getInteger(getKey());
return PlayerData.get(player).questData.hasFinishedQuest(questID);
}
return true;
}
}
@@ -0,0 +1,32 @@
package kamkeel.npcs.controllers.data.attribute.requirement;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import java.util.HashMap;
import java.util.Map;
public class RequirementCheckerRegistry {
private static final Map<String, IRequirementChecker> checkers = new HashMap<>();
public static void registerChecker(String key, IRequirementChecker checker) {
if (!checkers.containsKey(key)) {
checkers.put(key, checker);
}
}
/**
* Loops over all registered requirement keys.
* For each key present in the NBT, its checker must return true.
*/
public static boolean checkRequirements(EntityPlayer player, NBTTagCompound nbt) {
for (Map.Entry<String, IRequirementChecker> entry : checkers.entrySet()) {
String reqKey = entry.getKey();
if (nbt.hasKey(reqKey)) {
if (!entry.getValue().check(player, nbt)) {
return false;
}
}
}
return true;
}
}
@@ -53,8 +53,6 @@ public class PlayerAttributeTracker {
magicBoost.clear();
magicDefense.clear();
magicResistance.clear();
int newExtraHealth = 0;
if (player != null) {
// Update equipment tracker.
PlayerEquipmentTracker currentEquip = new PlayerEquipmentTracker();
@@ -57,10 +57,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(AttributeItemUtil.RPGItemAttributes))
? tagA.getCompoundTag(AttributeItemUtil.RPGItemAttributes).toString() : "";
String attrB = (tagB != null && tagB.hasKey(AttributeItemUtil.RPGItemAttributes))
? tagB.getCompoundTag(AttributeItemUtil.RPGItemAttributes).toString() : "";
String attrA = (tagA != null && tagA.hasKey(AttributeItemUtil.TAG_RPGCORE))
? tagA.getCompoundTag(AttributeItemUtil.TAG_RPGCORE).toString() : "";
String attrB = (tagB != null && tagB.hasKey(AttributeItemUtil.TAG_RPGCORE))
? tagB.getCompoundTag(AttributeItemUtil.TAG_RPGCORE).toString() : "";
return attrA.equals(attrB);
}
}
@@ -19,11 +19,19 @@ import java.util.*;
/**
* Provides helper functions to write/read item attributes to/from NBT.
* 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.
* All data is now stored under a hierarchical structure:
*
* "RPGCore"
* ├─ "Attributes"
* ├─ "Magic"
* └─ "Requirements"
*/
public class AttributeItemUtil {
public static final String RPGItemAttributes = "RPGCoreAttributes";
// New hierarchical keys.
public static final String TAG_RPGCORE = "RPGCore";
public static final String TAG_ATTRIBUTES = "Attributes";
public static final String TAG_MAGIC = "Magic";
public static final String TAG_REQUIREMENTS = "Requirements";
/**
* Applies a non–magic attribute to an item.
@@ -34,14 +42,13 @@ public class AttributeItemUtil {
item.stackTagCompound = new NBTTagCompound();
}
NBTTagCompound root = item.stackTagCompound;
NBTTagCompound attrTag;
if (root.hasKey(RPGItemAttributes)) {
attrTag = root.getCompoundTag(RPGItemAttributes);
} else {
attrTag = new NBTTagCompound();
}
// Get (or create) the RPGCore compound.
NBTTagCompound rpgCore = root.hasKey(TAG_RPGCORE) ? root.getCompoundTag(TAG_RPGCORE) : new NBTTagCompound();
// Get (or create) the Attributes compound.
NBTTagCompound attrTag = rpgCore.hasKey(TAG_ATTRIBUTES) ? rpgCore.getCompoundTag(TAG_ATTRIBUTES) : new NBTTagCompound();
attrTag.setFloat(attributeKey, value);
root.setTag(RPGItemAttributes, attrTag);
rpgCore.setTag(TAG_ATTRIBUTES, attrTag);
root.setTag(TAG_RPGCORE, rpgCore);
}
public static void applyAttribute(ItemStack item, AttributeDefinition definition, float value) {
@@ -54,12 +61,17 @@ public class AttributeItemUtil {
public static void removeAttribute(ItemStack item, String attributeKey) {
if (item == null || item.stackTagCompound == null) return;
NBTTagCompound root = item.stackTagCompound;
if (root.hasKey(RPGItemAttributes)) {
NBTTagCompound attrTag = root.getCompoundTag(RPGItemAttributes);
attrTag.removeTag(attributeKey);
// If no attributes remain, remove the root tag.
if (attrTag.func_150296_c().isEmpty()) {
root.removeTag(RPGItemAttributes);
if (root.hasKey(TAG_RPGCORE)) {
NBTTagCompound rpgCore = root.getCompoundTag(TAG_RPGCORE);
if (rpgCore.hasKey(TAG_ATTRIBUTES)) {
NBTTagCompound attrTag = rpgCore.getCompoundTag(TAG_ATTRIBUTES);
attrTag.removeTag(attributeKey);
if (attrTag.func_150296_c().isEmpty()) {
rpgCore.removeTag(TAG_ATTRIBUTES);
} else {
rpgCore.setTag(TAG_ATTRIBUTES, attrTag);
}
root.setTag(TAG_RPGCORE, rpgCore);
}
}
}
@@ -71,11 +83,14 @@ public class AttributeItemUtil {
Map<String, Float> map = new HashMap<>();
if (item == null || item.stackTagCompound == null) return map;
NBTTagCompound root = item.stackTagCompound;
if (root.hasKey(RPGItemAttributes)) {
NBTTagCompound attrTag = root.getCompoundTag(RPGItemAttributes);
Set<String> keys = attrTag.func_150296_c();
for (String key : keys) {
map.put(key, attrTag.getFloat(key));
if (root.hasKey(TAG_RPGCORE)) {
NBTTagCompound rpgCore = root.getCompoundTag(TAG_RPGCORE);
if (rpgCore.hasKey(TAG_ATTRIBUTES)) {
NBTTagCompound attrTag = rpgCore.getCompoundTag(TAG_ATTRIBUTES);
Set<String> keys = attrTag.func_150296_c();
for (String key : keys) {
map.put(key, attrTag.getFloat(key));
}
}
}
return map;
@@ -83,20 +98,27 @@ public class AttributeItemUtil {
/**
* 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).
* The given attributeTag is the key under which the compound is stored (e.g., MAGIC_DAMAGE_KEY).
*/
public static Map<Integer, Float> readMagicAttributeMap(ItemStack item, String attributeTag) {
Map<Integer, Float> map = new HashMap<>();
if (item == null || item.stackTagCompound == null) return map;
if (item.stackTagCompound.hasKey(attributeTag)) {
NBTTagCompound magicMap = item.stackTagCompound.getCompoundTag(attributeTag);
Set<String> keys = magicMap.func_150296_c();
for (String key : keys) {
try {
int magicId = Integer.parseInt(key);
map.put(magicId, magicMap.getFloat(key));
} catch (NumberFormatException e) {
// Skip invalid key.
NBTTagCompound root = item.stackTagCompound;
if (root.hasKey(TAG_RPGCORE)) {
NBTTagCompound rpgCore = root.getCompoundTag(TAG_RPGCORE);
if (rpgCore.hasKey(TAG_MAGIC)) {
NBTTagCompound magicCompound = rpgCore.getCompoundTag(TAG_MAGIC);
if (magicCompound.hasKey(attributeTag)) {
NBTTagCompound magicMap = magicCompound.getCompoundTag(attributeTag);
Set<String> keys = magicMap.func_150296_c();
for (String key : keys) {
try {
int magicId = Integer.parseInt(key);
map.put(magicId, magicMap.getFloat(key));
} catch (NumberFormatException e) {
// Skip invalid key.
}
}
}
}
}
@@ -105,7 +127,6 @@ public class AttributeItemUtil {
/**
* 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, float value) {
writeMagicAttribute(item, attributeTag, magicId, value);
@@ -118,14 +139,17 @@ public class AttributeItemUtil {
if (item == null) return;
if (item.stackTagCompound == null)
item.stackTagCompound = new NBTTagCompound();
NBTTagCompound magicMap;
if (item.stackTagCompound.hasKey(attributeTag)) {
magicMap = item.stackTagCompound.getCompoundTag(attributeTag);
} else {
magicMap = new NBTTagCompound();
}
NBTTagCompound root = item.stackTagCompound;
// Get or create RPGCore compound.
NBTTagCompound rpgCore = root.hasKey(TAG_RPGCORE) ? root.getCompoundTag(TAG_RPGCORE) : new NBTTagCompound();
// Get or create Magic compound.
NBTTagCompound magicCompound = rpgCore.hasKey(TAG_MAGIC) ? rpgCore.getCompoundTag(TAG_MAGIC) : new NBTTagCompound();
// Get or create the specific magic map.
NBTTagCompound magicMap = magicCompound.hasKey(attributeTag) ? magicCompound.getCompoundTag(attributeTag) : new NBTTagCompound();
magicMap.setFloat(String.valueOf(magicId), value);
item.stackTagCompound.setTag(attributeTag, magicMap);
magicCompound.setTag(attributeTag, magicMap);
rpgCore.setTag(TAG_MAGIC, magicCompound);
root.setTag(TAG_RPGCORE, rpgCore);
}
/**
@@ -135,41 +159,46 @@ public class AttributeItemUtil {
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);
if (root.hasKey(TAG_RPGCORE)) {
NBTTagCompound rpgCore = root.getCompoundTag(TAG_RPGCORE);
if (rpgCore.hasKey(TAG_MAGIC)) {
NBTTagCompound magicCompound = rpgCore.getCompoundTag(TAG_MAGIC);
if (magicCompound.hasKey(attributeTag)) {
NBTTagCompound magicMap = magicCompound.getCompoundTag(attributeTag);
magicMap.removeTag(String.valueOf(magicId));
if (magicMap.func_150296_c().isEmpty())
magicCompound.removeTag(attributeTag);
else
magicCompound.setTag(attributeTag, magicMap);
rpgCore.setTag(TAG_MAGIC, magicCompound);
root.setTag(TAG_RPGCORE, rpgCore);
}
}
}
}
@SideOnly(Side.CLIENT)
public static List<String> getToolTip(List<String> original, NBTTagCompound compound) {
List<String> tooltip = new ArrayList<>(original);
NBTTagCompound attrTag = compound.getCompoundTag(RPGItemAttributes);
// Get the RPGCore compound and then the Attributes compound.
NBTTagCompound rpgCore = compound.hasKey(TAG_RPGCORE) ? compound.getCompoundTag(TAG_RPGCORE) : new NBTTagCompound();
NBTTagCompound attrTag = rpgCore.hasKey(TAG_ATTRIBUTES) ? rpgCore.getCompoundTag(TAG_ATTRIBUTES) : new NBTTagCompound();
if (Keyboard.isKeyDown(ClientProxy.NPCButton.getKeyCode())) {
List<String> newTooltips = new ArrayList<>();
if (!tooltip.isEmpty()) {
newTooltips.add(tooltip.get(0));
}
// Lists for non–magic attributes
// Lists for non–magic attributes.
List<String> baseList = new ArrayList<>();
List<String> modifierList = new ArrayList<>();
List<String> statsList = new ArrayList<>();
List<String> infoList = new ArrayList<>();
List<String> extraList = new ArrayList<>();
// Process non–magic attributes (order maintained)
Iterator<String> iter = attrTag.func_150296_c().iterator();
while (iter.hasNext()) {
String key = iter.next();
// Skip magic attribute keys; they are handled separately.
if(key.equals(CustomAttributes.MAGIC_DAMAGE_KEY) || key.equals(CustomAttributes.MAGIC_BOOST_KEY)
|| key.equals(CustomAttributes.MAGIC_DEFENSE_KEY) || key.equals(CustomAttributes.MAGIC_RESISTANCE_KEY)) {
continue;
}
// Process non–magic attributes.
Set<String> keys = attrTag.func_150296_c();
for (String key : keys) {
Float value = attrTag.getFloat(key);
AttributeDefinition def = AttributeController.getAttribute(key);
AttributeDefinition.AttributeSection section = def != null ? def.getSection() : AttributeDefinition.AttributeSection.EXTRA;
@@ -194,10 +223,9 @@ public class AttributeItemUtil {
}
}
// Process magic attributes and merge them into the proper lists.
// Process magic attributes.
processMagicAttributes(compound, baseList, modifierList, infoList, extraList);
// Append sections (adds an empty line before each if not empty)
newTooltips.addAll(buildSection(baseList));
newTooltips.addAll(buildSection(modifierList));
newTooltips.addAll(buildSection(statsList));
@@ -215,11 +243,15 @@ public class AttributeItemUtil {
/**
* Processes magic attributes from the compound.
* For each magic key (from ModAttributes), it retrieves the stored magic attributes,
* formats each line (using the magic's display name) and adds it to the proper section list.
* For each magic key, it retrieves the stored magic attributes from the "Magic" compound,
* formats each line and adds it to the proper section list.
*/
private static void processMagicAttributes(NBTTagCompound compound, List<String> baseList, List<String> modifierList,
List<String> infoList, List<String> extraList) {
if (!compound.hasKey(TAG_RPGCORE)) return;
NBTTagCompound rpgCore = compound.getCompoundTag(TAG_RPGCORE);
if (!rpgCore.hasKey(TAG_MAGIC)) return;
NBTTagCompound magicCompound = rpgCore.getCompoundTag(TAG_MAGIC);
String[] magicKeys = {
CustomAttributes.MAGIC_DAMAGE_KEY,
CustomAttributes.MAGIC_BOOST_KEY,
@@ -227,21 +259,18 @@ public class AttributeItemUtil {
CustomAttributes.MAGIC_RESISTANCE_KEY
};
for (String magicKey : magicKeys) {
if (compound.hasKey(magicKey)) {
NBTTagCompound magicTag = compound.getCompoundTag(magicKey);
// Use the attribute definition for this magic key to determine its section.
if (magicCompound.hasKey(magicKey)) {
NBTTagCompound magicTag = magicCompound.getCompoundTag(magicKey);
AttributeDefinition def = AttributeController.getAttribute(magicKey);
AttributeDefinition.AttributeSection section = def != null ? def.getSection() : AttributeDefinition.AttributeSection.EXTRA;
Iterator<String> magicIter = magicTag.func_150296_c().iterator();
while (magicIter.hasNext()) {
String key = magicIter.next();
Set<String> keys = magicTag.func_150296_c();
for (String key : keys) {
try {
int magicId = Integer.parseInt(key);
Float value = magicTag.getFloat(key);
Magic magic = MagicController.getInstance().getMagic(magicId);
if (magic != null) {
String magicDisplayName = magic.getDisplayName() + " " + EnumChatFormatting.GRAY + getMagicAppendix(magicKey);
// Format using the magic attribute's definition and the magic's display name.
String line = formatAttributeLine(def, section, value, magicDisplayName);
switch (section) {
case BASE:
@@ -278,17 +307,12 @@ public class AttributeItemUtil {
}
}
/**
* Returns a translated attribute name.
*/
@SideOnly(Side.CLIENT)
private static String getTranslatedAttributeName(String key, AttributeDefinition def) {
key = def != null ? def.getTranslationKey() : key;
String translation = null;
if (StatCollector.canTranslate(key))
translation = StatCollector.translateToLocal(key);
if (translation == null && def != null)
translation = def.getDisplayName();
else if (translation == null)
@@ -296,9 +320,6 @@ public class AttributeItemUtil {
return translation;
}
/**
* Formats the attribute line based on its section and value type.
*/
private static String formatAttributeLine(AttributeDefinition def, AttributeDefinition.AttributeSection section,
Float value, String displayName) {
String formattedValue = formatFloat(value);
@@ -332,10 +353,6 @@ public class AttributeItemUtil {
}
}
/**
* Builds a section from a list of tooltip lines.
* Adds an empty line before the section if it contains lines.
*/
private static List<String> buildSection(List<String> lines) {
List<String> section = new ArrayList<>();
if (!lines.isEmpty()) {
@@ -345,9 +362,6 @@ public class AttributeItemUtil {
return section;
}
/**
* Helper to format a Float by removing trailing zeros.
*/
private static String formatFloat(Float value) {
return new java.math.BigDecimal(Float.toString(value)).stripTrailingZeros().toPlainString();
}
@@ -11,7 +11,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.List;
import static kamkeel.npcs.util.AttributeItemUtil.RPGItemAttributes;
import static kamkeel.npcs.util.AttributeItemUtil.TAG_RPGCORE;
@Mixin(ItemStack.class)
public abstract class MixinItemStack {
@@ -28,7 +28,7 @@ public abstract class MixinItemStack {
@Inject(method = "getTooltip", at = @At("TAIL"), cancellable = true)
public void getAttributeTooltip(EntityPlayer player, boolean advanced, CallbackInfoReturnable<List<String>> cir) {
List<String> tooltip = cir.getReturnValue();
if (hasTagCompound() && getTagCompound().hasKey(RPGItemAttributes)) {
if (hasTagCompound() && getTagCompound().hasKey(TAG_RPGCORE)) {
cir.setReturnValue(AttributeItemUtil.getToolTip(tooltip, getTagCompound()));
}
}