More Item Attribute Things

This commit is contained in:
Kam committed 2025-02-26 22:49:26 -05:00
1 parent 9b7af03de3
commit 50977543c0
13 files changed
+312 -162

No files matched your search

@@ -1,10 +1,19 @@
package noppes.npcs.attribute;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import noppes.npcs.client.ClientProxy;
import org.lwjgl.input.Keyboard;
import java.util.*;
import static noppes.npcs.attribute.AttributeDefinition.AttributeSection.INFO;
import static noppes.npcs.attribute.AttributeDefinition.AttributeSection.MODIFIER;
/**
* Provides helper functions to write/read item attributes to/from NBT.
@@ -12,7 +21,7 @@ import java.util.Map;
* and magic attributes are stored as a compound mapping (by magic ID) under specific keys.
*/
public class ItemAttributeHelper {
public static final String TAG_ROOT = "RPGCoreAttributes";
public static final String RPGItemAttributes = "RPGCoreAttributes";
/**
* Applies a non–magic attribute to an item.
@@ -24,13 +33,13 @@ public class ItemAttributeHelper {
}
NBTTagCompound root = item.stackTagCompound;
NBTTagCompound attrTag;
if (root.hasKey(TAG_ROOT)) {
attrTag = root.getCompoundTag(TAG_ROOT);
if (root.hasKey(RPGItemAttributes)) {
attrTag = root.getCompoundTag(RPGItemAttributes);
} else {
attrTag = new NBTTagCompound();
}
attrTag.setDouble(attributeKey, value);
root.setTag(TAG_ROOT, attrTag);
root.setTag(RPGItemAttributes, attrTag);
}
/**
@@ -39,12 +48,12 @@ public class ItemAttributeHelper {
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);
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(TAG_ROOT);
root.removeTag(RPGItemAttributes);
}
}
}
@@ -56,8 +65,8 @@ public class ItemAttributeHelper {
Map<String, Double> map = new HashMap<>();
if (item == null || item.stackTagCompound == null) return map;
NBTTagCompound root = item.stackTagCompound;
if (root.hasKey(TAG_ROOT)) {
NBTTagCompound attrTag = root.getCompoundTag(TAG_ROOT);
if (root.hasKey(RPGItemAttributes)) {
NBTTagCompound attrTag = root.getCompoundTag(RPGItemAttributes);
Set<String> keys = attrTag.func_150296_c();
for (String key : keys) {
map.put(key, attrTag.getDouble(key));
@@ -128,4 +137,114 @@ public class ItemAttributeHelper {
root.removeTag(attributeTag);
}
}
@SideOnly(Side.CLIENT)
public static List<String> getToolTip(List<String> original, NBTTagCompound compound) {
List<String> tooltip = original;
NBTTagCompound attrTag = compound.getCompoundTag(RPGItemAttributes);
if (Keyboard.isKeyDown(ClientProxy.NPCButton.getKeyCode())) {
List<String> newTooltips = new ArrayList<>();
if (!tooltip.isEmpty()) {
newTooltips.add((String) tooltip.get(0));
newTooltips.add("");
}
// Create lists for each section
List<String> baseList = new ArrayList<>();
List<String> modifierList = new ArrayList<>();
List<String> infoList = new ArrayList<>();
List<String> extraList = new ArrayList<>();
Iterator iterator = attrTag.func_150296_c().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
double value = attrTag.getDouble(key);
AttributeDefinition def = AttributeController.getAttribute(key);
// Default section is EXTRA if not defined
AttributeDefinition.AttributeSection section = def != null ? def.getSection() : AttributeDefinition.AttributeSection.EXTRA;
String line;
String translation = null;
if (key.contains(":")) {
if (StatCollector.canTranslate(key)) {
translation = StatCollector.translateToLocal(key);
}
} else {
if (StatCollector.canTranslate("rpgcore:attribute." + key)) {
translation = StatCollector.translateToLocal("rpgcore:attribute." + key);
}
}
if (translation == null && def != null) {
translation = def.getDisplayName();
} else if (translation == null) {
translation = key;
}
translation = EnumChatFormatting.AQUA + translation;
String formattedValue = formatDouble(value);
if (section == MODIFIER || section == INFO) {
String sign = (value >= 0) ? "+" : "";
String valueColor = (value >= 0) ? EnumChatFormatting.GREEN + "" : EnumChatFormatting.RED + "";
String valueString = valueColor + sign + formattedValue + EnumChatFormatting.GRAY;
switch (section) {
case MODIFIER:
line = valueString + " " + translation;
modifierList.add(line);
break;
case INFO:
line = valueString + " " + translation;
infoList.add(line);
break;
default:
line = translation + ": " + formattedValue;
break;
}
} else {
formattedValue = EnumChatFormatting.GRAY + formattedValue;
switch (section) {
case BASE:
line = translation + ": " + formattedValue;
baseList.add(line);
break;
case EXTRA:
line = translation + ": " + formattedValue;
extraList.add(line);
break;
default:
line = translation + ": " + formattedValue;
break;
}
}
}
if (!baseList.isEmpty()) {
newTooltips.add("");
newTooltips.addAll(baseList);
}
if (!modifierList.isEmpty()) {
newTooltips.add("");
newTooltips.addAll(modifierList);
}
if (!infoList.isEmpty()) {
newTooltips.add("");
newTooltips.addAll(infoList);
}
if (!extraList.isEmpty()) {
newTooltips.add("");
newTooltips.addAll(extraList);
}
tooltip = newTooltips;
} else {
// Inform the user to hold the NPCButton key for attribute details
String keyName = Keyboard.getKeyName(ClientProxy.NPCButton.getKeyCode());
tooltip.add(EnumChatFormatting.YELLOW + "" + EnumChatFormatting.ITALIC +
StatCollector.translateToLocal("rpgcore:tooltip").replace("%key%", keyName));
}
return tooltip;
}
/**
* Helper to format a double by removing trailing zeros.
*/
private static String formatDouble(double value) {
return new java.math.BigDecimal(Double.toString(value)).stripTrailingZeros().toPlainString();
}
}