Better Reorganization

This commit is contained in:
Kam committed 2025-02-27 15:54:42 -05:00
1 parent a328a635d7
commit da0489296e
38 files changed
+151 -144

No files matched your search

+2 -2
View File
@@ -13,7 +13,7 @@ import kamkeel.npcs.addon.AddonManager;
import kamkeel.npcs.command.CommandKamkeel;
import kamkeel.npcs.command.profile.CommandProfile;
import kamkeel.npcs.controllers.ProfileController;
import kamkeel.npcs.controllers.data.CNPCData;
import kamkeel.npcs.controllers.data.profile.CNPCData;
import kamkeel.npcs.developer.Developer;
import kamkeel.npcs.network.PacketHandler;
import net.minecraft.block.Block;
@@ -27,7 +27,7 @@ import net.minecraft.server.MinecraftServer;
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.MinecraftForge;
import nikedemos.markovnames.generators.*;
import noppes.npcs.attribute.AttributeController;
import kamkeel.npcs.controllers.AttributeController;
import noppes.npcs.compat.PixelmonHelper;
import noppes.npcs.config.ConfigMain;
import noppes.npcs.config.LoadConfiguration;
@@ -23,9 +23,8 @@ import net.minecraftforge.event.entity.player.*;
import net.minecraftforge.event.world.BlockEvent;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.item.IItemCustomizable;
import noppes.npcs.attribute.AttributeController;
import noppes.npcs.attribute.AttributeAttackHelper;
import noppes.npcs.attribute.player.PlayerAttributeTracker;
import kamkeel.npcs.controllers.AttributeController;
import kamkeel.npcs.util.AttributeAttackUtil;
import noppes.npcs.config.ConfigMain;
import noppes.npcs.constants.EnumQuestType;
import noppes.npcs.controllers.CustomEffectController;
@@ -478,7 +477,7 @@ public class ScriptPlayerEventHandler {
if(event.source.getEntity() instanceof EntityPlayer) {
float attackAmount = event.ammount;
if(ConfigMain.AttributesEnabled)
attackAmount = AttributeAttackHelper.calculateOutgoing((EntityPlayer) event.source.getEntity(), event.ammount);
attackAmount = AttributeAttackUtil.calculateOutgoing((EntityPlayer) event.source.getEntity(), event.ammount);
noppes.npcs.scripted.event.PlayerEvent.AttackEvent pevent1 = new noppes.npcs.scripted.event.PlayerEvent.AttackEvent((IPlayer)NpcAPI.Instance().getIEntity((EntityPlayer)event.source.getEntity()), event.entityLiving, attackAmount, event.source);
cancel = cancel || EventHooks.onPlayerAttack(handler, pevent1);
@@ -500,9 +499,9 @@ public class ScriptPlayerEventHandler {
// Player to Player Interaction
if(ConfigMain.AttributesEnabled){
if(event.entityLiving instanceof EntityPlayer && source instanceof EntityPlayer){
event.ammount = AttributeAttackHelper.calculateDamagePlayer((EntityPlayer) event.entityLiving, (EntityPlayer) source, event.ammount);
event.ammount = AttributeAttackUtil.calculateDamagePlayer((EntityPlayer) event.entityLiving, (EntityPlayer) source, event.ammount);
} else if (!(event.entityLiving instanceof EntityNPCInterface) && source instanceof EntityPlayer){
event.ammount = AttributeAttackHelper.calculateOutgoing((EntityPlayer) event.entityLiving, event.ammount);
event.ammount = AttributeAttackUtil.calculateOutgoing((EntityPlayer) event.entityLiving, event.ammount);
}
}
@@ -1,153 +0,0 @@
package noppes.npcs.attribute;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.attribute.player.PlayerAttributeTracker;
import noppes.npcs.controllers.MagicController;
import noppes.npcs.entity.EntityNPCInterface;
import java.util.Map;
import java.util.Random;
import static noppes.npcs.attribute.AttributeController.getTracker;
public class AttributeAttackHelper {
private static final Random random = new Random();
public static float calculateDamagePlayer(EntityPlayer attacker, EntityPlayer defender, float base){
return calculateDamagePlayer(getTracker(attacker), getTracker(defender), base);
}
public static float calculateDamagePlayer(PlayerAttributeTracker attacker, PlayerAttributeTracker defender, float baseDamage) {
// Calculate physical damage
float mainAttack = attacker.getAttributeValue(ModAttributes.MAIN_ATTACK);
float mainBoost = attacker.getAttributeValue(ModAttributes.MAIN_BOOST) + 1;
float physicalDamage = (baseDamage * mainBoost) + mainAttack;
// Calculate neutral damage
float neutralDamage = attacker.getAttributeValue(ModAttributes.NEUTRAL_ATTACK);
float neutralBoost = attacker.getAttributeValue(ModAttributes.NEUTRAL_BOOST) + 1;
float neutralTotal = neutralDamage * neutralBoost;
// Calculate magic damage
float magicTotal = 0.0f;
MagicController magicController = MagicController.getInstance();
for (Map.Entry<Integer, Float> entry : attacker.magicDamage.entrySet()) {
int magicId = entry.getKey();
if (magicController.getMagic(magicId) != null) {
float attackMagic = entry.getValue();
float defenseMagic = defender.magicDefense.getOrDefault(magicId, 0.0f);
float effectiveMagic = attackMagic - defenseMagic;
float boostMagic = attacker.magicBoost.getOrDefault(magicId, 0.0f);
float resistanceMagic = defender.magicResistance.getOrDefault(magicId, 0.0f);
float effectiveBoost = boostMagic - resistanceMagic;
float magicDamageForMagic = effectiveMagic * (effectiveBoost + 1);
// Ensure magic damage doesn't drop below 0 for this magic
magicTotal += Math.max(0, magicDamageForMagic);
}
}
// Sum all damage components
float totalDamage = physicalDamage + neutralTotal + magicTotal;
// Critical hit: if critical, float damage and add bonus
float criticalChance = attacker.getAttributeValue(ModAttributes.CRITICAL_CHANCE);
float criticalBonus = attacker.getAttributeValue(ModAttributes.CRITICAL_DAMAGE);
if (random.nextFloat() < (criticalChance / 100)) {
totalDamage = (totalDamage * 2) + criticalBonus;
}
return totalDamage;
}
public static float calculateDamageNPC(PlayerAttributeTracker attacker, EntityNPCInterface defender, float baseDamage) {
// Calculate physical damage
float mainAttack = attacker.getAttributeValue(ModAttributes.MAIN_ATTACK);
float mainBoost = attacker.getAttributeValue(ModAttributes.MAIN_BOOST) + 1;
float physicalDamage = (baseDamage * mainBoost) + mainAttack;
// Calculate neutral damage
float neutralDamage = attacker.getAttributeValue(ModAttributes.NEUTRAL_ATTACK);
float neutralBoost = attacker.getAttributeValue(ModAttributes.NEUTRAL_BOOST) + 1;
float neutralTotal = neutralDamage * neutralBoost;
// Calculate magic damage
// float magicTotal = 0.0;
// MagicController magicController = MagicController.getInstance();
// for (Map.Entry<Integer, Float> entry : attacker.magicDamage.entrySet()) {
// int magicId = entry.getKey();
// if (magicController.getMagic(magicId) != null) {
// float attackMagic = entry.getValue();
// float defenseMagic = defender.magicDefense.getOrDefault(magicId, 0.0);
// float effectiveMagic = attackMagic - defenseMagic;
//
// float boostMagic = attacker.magicBoost.getOrDefault(magicId, 0.0);
// float resistanceMagic = defender.magicResistance.getOrDefault(magicId, 0.0);
// float effectiveBoost = boostMagic - resistanceMagic;
//
// float magicDamageForMagic = effectiveMagic * (effectiveBoost + 1);
// // Ensure magic damage doesn't drop below 0 for this magic
// magicTotal += Math.max(0, magicDamageForMagic);
// }
// }
//
// // Sum all damage components
// float totalDamage = physicalDamage + neutralTotal + magicTotal;
//
// // Critical hit: if critical, float damage and add bonus
// float criticalChance = attacker.getAttributeValue(ModAttributes.CRITICAL_CHANCE);
// float criticalBonus = attacker.getAttributeValue(ModAttributes.CRITICAL_DAMAGE);
// if (random.nextFloat() < (criticalChance / 100)) {
// totalDamage = (totalDamage * 2) + criticalBonus;
// }
return 0;
}
/**
* Calculates a player's total damage when attacking an opponent with no defense.
* In this calculation, any defensive modifiers (such as magic defense or resistance)
* are assumed to be zero.
*
* @param attacker The attacker's attribute tracker.
* @return The total calculated damage.
*/
public static float calculateMaximumOutput(PlayerAttributeTracker attacker) {
// Neutral damage remains unchanged.
float neutralDamage = attacker.getAttributeValue(ModAttributes.NEUTRAL_ATTACK);
float neutralBoost = attacker.getAttributeValue(ModAttributes.NEUTRAL_BOOST) + 1;
float neutralTotal = neutralDamage * neutralBoost;
// For magic damage, we ignore any defensive values.
float magicTotal = 0.0f;
MagicController magicController = MagicController.getInstance();
for (Map.Entry<Integer, Float> entry : attacker.magicDamage.entrySet()) {
int magicId = entry.getKey();
if (magicController.getMagic(magicId) != null) {
float attackMagic = entry.getValue();
// No defender, so effective magic equals attack magic.
float effectiveMagic = attackMagic;
float boostMagic = attacker.magicBoost.getOrDefault(magicId, 0.0f);
// No resistance present.
float effectiveBoost = boostMagic;
float magicDamageForMagic = effectiveMagic * (effectiveBoost + 1);
magicTotal += Math.max(0, magicDamageForMagic);
}
}
return neutralTotal + magicTotal;
}
public static float calculateOutgoing(EntityPlayer entityPlayer, float baseDamage) {
PlayerAttributeTracker attacker = getTracker(entityPlayer);
float mainAttack = attacker.getAttributeValue(ModAttributes.MAIN_ATTACK);
float mainBoost = attacker.getAttributeValue(ModAttributes.MAIN_BOOST) + 1;
float physicalDamage = (baseDamage * mainBoost) + mainAttack;
return attacker.maximumOutput + physicalDamage;
}
}
@@ -1,66 +0,0 @@
package noppes.npcs.attribute;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.attribute.player.PlayerAttributeTracker;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* AttributeController is the central registry for attribute definitions.
* It ensures that keys are unique.
*/
public class AttributeController {
public AttributeController Instance;
public ModAttributes attributes;
// Tracking Player Attributes
private static final Map<UUID, PlayerAttributeTracker> trackers = new HashMap<>();
private static final Map<String, AttributeDefinition> definitions = new HashMap<>();
public AttributeController(){
this.Instance = this;
definitions.clear();
trackers.clear();
attributes = new ModAttributes();
}
public static AttributeDefinition registerAttribute(String key, String displayName, char colorCode, AttributeValueType valueType, AttributeDefinition.AttributeSection section) {
if (definitions.containsKey(key)) {
throw new IllegalArgumentException("Attribute already registered with key: " + key);
}
AttributeDefinition def = new AttributeDefinition(key, displayName, colorCode, valueType, section);
definitions.put(key, def);
return def;
}
public static AttributeDefinition getAttribute(String key) {
return definitions.get(key);
}
public static Collection<AttributeDefinition> getAllAttributes() {
return definitions.values();
}
public static PlayerAttributeTracker getTracker(EntityPlayer player) {
return trackers.computeIfAbsent(player.getUniqueID(), id -> new PlayerAttributeTracker(id));
}
public static void removeTracker(UUID playerId) {
trackers.remove(playerId);
}
/**
* Update all trackers (e.g. called every 10 ticks for all online players).
*/
public static void updateAllTrackers(Iterable<EntityPlayer> players) {
for (EntityPlayer player : players) {
PlayerAttributeTracker tracker = getTracker(player);
tracker.updateIfChanged(player);
}
}
}
@@ -1,44 +0,0 @@
package noppes.npcs.attribute;
public class AttributeDefinition {
private final String key; // e.g., "main_attack_flat"
private final String displayName; // Human‑readable name
private final char colorCode;
private final AttributeValueType attributeValueType;
private final AttributeSection section;
public AttributeDefinition(String key, String displayName, char colorCode, AttributeValueType attributeValueType, AttributeSection section) {
this.key = key;
this.displayName = displayName;
this.attributeValueType = attributeValueType;
this.section = section;
this.colorCode = colorCode;
}
public String getKey() {
return key;
}
public String getDisplayName() {
return displayName;
}
public AttributeValueType getValueType() {
return attributeValueType;
}
public AttributeSection getSection() {
return section;
}
public char getColorCode() {
return colorCode;
}
public enum AttributeSection {
BASE,
MODIFIER,
INFO,
EXTRA;
}
}
@@ -1,7 +0,0 @@
package noppes.npcs.attribute;
public enum AttributeValueType {
FLAT, // e.g., +10 Health or +3 Damage
PERCENT, // e.g., +0.25 means +25% bonus
MAGIC // Represents a container of magic–based values (summed up as a float)
}
@@ -1,10 +0,0 @@
package noppes.npcs.attribute;
/**
* IAttributeInstance represents an instance of an attribute (with a base value and applied modifiers).
*/
public interface ICustomAttribute {
AttributeDefinition getAttribute();
float getValue();
void setValue(float value);
}
@@ -1,335 +0,0 @@
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 net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import noppes.npcs.client.ClientProxy;
import noppes.npcs.controllers.MagicController;
import noppes.npcs.controllers.data.Magic;
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.
* 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 ItemAttributeHelper {
public static final String RPGItemAttributes = "RPGCoreAttributes";
/**
* Applies a non–magic attribute to an item.
*/
public static void applyAttribute(ItemStack item, String attributeKey, Float value) {
if (item == null) return;
if (item.stackTagCompound == null) {
item.stackTagCompound = new NBTTagCompound();
}
NBTTagCompound root = item.stackTagCompound;
NBTTagCompound attrTag;
if (root.hasKey(RPGItemAttributes)) {
attrTag = root.getCompoundTag(RPGItemAttributes);
} else {
attrTag = new NBTTagCompound();
}
attrTag.setFloat(attributeKey, value);
root.setTag(RPGItemAttributes, 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(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);
}
}
}
/**
* Reads non–magic attributes from an item.
*/
public static Map<String, Float> readAttributes(ItemStack item) {
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));
}
}
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, 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.
}
}
}
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, Float 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, Float value) {
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();
}
magicMap.setFloat(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);
}
}
@SideOnly(Side.CLIENT)
public static List<String> getToolTip(List<String> original, NBTTagCompound compound) {
List<String> tooltip = new ArrayList<>(original);
NBTTagCompound attrTag = compound.getCompoundTag(RPGItemAttributes);
if (Keyboard.isKeyDown(ClientProxy.NPCButton.getKeyCode())) {
List<String> newTooltips = new ArrayList<>();
if (!tooltip.isEmpty()) {
newTooltips.add(tooltip.get(0));
}
// Lists for non–magic attributes
List<String> baseList = new ArrayList<>();
List<String> modifierList = 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(ModAttributes.MAGIC_DAMAGE_KEY) || key.equals(ModAttributes.MAGIC_BOOST_KEY)
|| key.equals(ModAttributes.MAGIC_DEFENSE_KEY) || key.equals(ModAttributes.MAGIC_RESISTANCE_KEY)) {
continue;
}
Float value = attrTag.getFloat(key);
AttributeDefinition def = AttributeController.getAttribute(key);
AttributeDefinition.AttributeSection section = def != null ? def.getSection() : AttributeDefinition.AttributeSection.EXTRA;
String displayName = getTranslatedAttributeName(key, def);
String line = formatAttributeLine(def, section, value, displayName);
switch (section) {
case BASE:
baseList.add(line);
break;
case MODIFIER:
modifierList.add(line);
break;
case INFO:
infoList.add(line);
break;
default:
extraList.add(line);
break;
}
}
// Process magic attributes and merge them into the proper lists.
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(infoList));
newTooltips.addAll(buildSection(extraList));
tooltip = newTooltips;
} else {
String keyName = Keyboard.getKeyName(ClientProxy.NPCButton.getKeyCode());
tooltip.add(EnumChatFormatting.YELLOW + "" + EnumChatFormatting.ITALIC +
StatCollector.translateToLocal("rpgcore:tooltip").replace("%key%", keyName));
}
return tooltip;
}
/**
* 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.
*/
private static void processMagicAttributes(NBTTagCompound compound, List<String> baseList, List<String> modifierList,
List<String> infoList, List<String> extraList) {
String[] magicKeys = {
ModAttributes.MAGIC_DAMAGE_KEY,
ModAttributes.MAGIC_BOOST_KEY,
ModAttributes.MAGIC_DEFENSE_KEY,
ModAttributes.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.
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();
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:
baseList.add(line);
break;
case MODIFIER:
modifierList.add(line);
break;
case INFO:
infoList.add(line);
break;
default:
extraList.add(line);
break;
}
}
} catch (NumberFormatException e) {
// Skip invalid key.
}
}
}
}
}
@SideOnly(Side.CLIENT)
private static String getMagicAppendix(String type){
switch(type){
case ModAttributes.MAGIC_DEFENSE_KEY:
return StatCollector.translateToLocal("rpgcore:attribute.defense");
case ModAttributes.MAGIC_RESISTANCE_KEY:
return StatCollector.translateToLocal("rpgcore:attribute.resistance");
default:
return StatCollector.translateToLocal("rpgcore:attribute.damage");
}
}
/**
* Returns a translated attribute name.
*/
@SideOnly(Side.CLIENT)
private static String getTranslatedAttributeName(String key, AttributeDefinition def) {
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;
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);
if (section == AttributeDefinition.AttributeSection.MODIFIER || section == AttributeDefinition.AttributeSection.INFO) {
String sign = value >= 0 ? "+" : "";
String color = value >= 0 ? EnumChatFormatting.GREEN.toString() : EnumChatFormatting.RED.toString();
String valueString = color + sign + formattedValue;
if (def != null && def.getValueType() == AttributeValueType.PERCENT)
valueString += "%";
valueString += EnumChatFormatting.GRAY;
return valueString + " " + displayName;
} else {
if (def != null)
displayName = "\u00A7" + def.getColorCode() + displayName;
else
displayName = EnumChatFormatting.AQUA + displayName;
formattedValue = EnumChatFormatting.GRAY + formattedValue;
return displayName + "\u00A77: " + formattedValue;
}
}
/**
* 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()) {
section.add("");
section.addAll(lines);
}
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();
}
}
@@ -1,52 +0,0 @@
package noppes.npcs.attribute;
/**
* ModAttributes registers the core attributes (both non–magic and magic–based).
*/
public class ModAttributes {
public static final String HEALTH_KEY = "health";
public static final String MAIN_ATTACK_KEY = "main_attack";
public static final String MAIN_ATTACK_BOOST_KEY = "main_attack_boost";
public static final String NEUTRAL_KEY = "neutral";
public static final String NEUTRAL_BOOST_KEY = "neutral_boost";
public static final String CRITICAL_DAMAGE_KEY = "critical_damage";
public static final String CRITICAL_CHANCE_KEY = "critical_chance";
public static final String MAGIC_DAMAGE_KEY = "magic_damage";
public static final String MAGIC_BOOST_KEY = "magic_boost";
public static final String MAGIC_DEFENSE_KEY = "magic_defense";
public static final String MAGIC_RESISTANCE_KEY = "magic_resistance";
public static AttributeDefinition MAIN_ATTACK;
public static AttributeDefinition MAIN_BOOST;
public static AttributeDefinition NEUTRAL_ATTACK;
public static AttributeDefinition NEUTRAL_BOOST;
public static AttributeDefinition HEALTH;
public static AttributeDefinition CRITICAL_CHANCE;
public static AttributeDefinition CRITICAL_DAMAGE;
public static AttributeDefinition MAGIC_DAMAGE;
public static AttributeDefinition MAGIC_BOOST;
public static AttributeDefinition MAGIC_DEFENSE;
public static AttributeDefinition MAGIC_RESISTANCE;
public ModAttributes(){
HEALTH = AttributeController.registerAttribute(HEALTH_KEY, "Health", 'c', AttributeValueType.FLAT, AttributeDefinition.AttributeSection.BASE);
MAIN_ATTACK = AttributeController.registerAttribute(MAIN_ATTACK_KEY, "Main Attack Damage", '4', AttributeValueType.FLAT, AttributeDefinition.AttributeSection.BASE);
MAIN_BOOST = AttributeController.registerAttribute(MAIN_ATTACK_BOOST_KEY, "Main Attack Damage", '4', AttributeValueType.PERCENT, AttributeDefinition.AttributeSection.MODIFIER);
NEUTRAL_ATTACK = AttributeController.registerAttribute(NEUTRAL_KEY, "Neutral Damage", '6', AttributeValueType.FLAT, AttributeDefinition.AttributeSection.BASE);
NEUTRAL_BOOST = AttributeController.registerAttribute(NEUTRAL_BOOST_KEY, "Neutral Damage", '6', AttributeValueType.PERCENT, AttributeDefinition.AttributeSection.MODIFIER);
CRITICAL_CHANCE = AttributeController.registerAttribute(CRITICAL_CHANCE_KEY, "Critical Chance", '6', AttributeValueType.PERCENT, AttributeDefinition.AttributeSection.INFO);
CRITICAL_DAMAGE = AttributeController.registerAttribute(CRITICAL_DAMAGE_KEY, "Critical Damage", '6', AttributeValueType.FLAT, AttributeDefinition.AttributeSection.INFO);
MAGIC_DAMAGE = AttributeController.registerAttribute(MAGIC_DAMAGE_KEY, "Magic Damage", '3', AttributeValueType.MAGIC, AttributeDefinition.AttributeSection.BASE);
MAGIC_BOOST = AttributeController.registerAttribute(MAGIC_BOOST_KEY, "Magic Damage", '3', AttributeValueType.MAGIC, AttributeDefinition.AttributeSection.MODIFIER);
MAGIC_DEFENSE = AttributeController.registerAttribute(MAGIC_DEFENSE_KEY, "Magic Defense", 'b', AttributeValueType.MAGIC, AttributeDefinition.AttributeSection.BASE);
MAGIC_RESISTANCE = AttributeController.registerAttribute(MAGIC_RESISTANCE_KEY, "Magic Resistance", 'b', AttributeValueType.MAGIC, AttributeDefinition.AttributeSection.MODIFIER);
}
}
@@ -1,30 +0,0 @@
package noppes.npcs.attribute.player;
import noppes.npcs.attribute.AttributeDefinition;
import noppes.npcs.attribute.ICustomAttribute;
public class PlayerAttribute implements ICustomAttribute {
private final AttributeDefinition attribute;
private float baseValue;
public PlayerAttribute(AttributeDefinition attribute, float baseValue) {
this.attribute = attribute;
this.baseValue = baseValue;
}
@Override
public AttributeDefinition getAttribute() {
return attribute;
}
@Override
public float getValue() {
return baseValue;
}
@Override
public void setValue(float value) {
this.baseValue = value;
}
}
@@ -1,32 +0,0 @@
package noppes.npcs.attribute.player;
import noppes.npcs.attribute.AttributeDefinition;
import noppes.npcs.attribute.ICustomAttribute;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* CustomAttributeMap holds a set of attribute instances for an entity (or item).
*/
public class PlayerAttributeMap {
private final Map<AttributeDefinition, ICustomAttribute> map = new HashMap<>();
public ICustomAttribute registerAttribute(AttributeDefinition attribute, float baseValue) {
if (map.containsKey(attribute)) {
throw new IllegalArgumentException("Attribute already registered: " + attribute.getKey());
}
ICustomAttribute instance = new PlayerAttribute(attribute, baseValue);
map.put(attribute, instance);
return instance;
}
public ICustomAttribute getAttributeInstance(AttributeDefinition attribute) {
return map.get(attribute);
}
public Collection<ICustomAttribute> getAllAttributes() {
return map.values();
}
}
@@ -1,150 +0,0 @@
package noppes.npcs.attribute.player;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.Map.Entry;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import noppes.npcs.attribute.*;
public class PlayerAttributeTracker {
public static UUID healthUUID = UUID.fromString("48a0ad75-2cf8-4838-ad2f-9a0aadc57dfe");
private final UUID playerId;
private PlayerAttributeMap playerAttributes = new PlayerAttributeMap();
public float extraHealth = 0;
public float maximumOutput = 0.0f;
public final Map<Integer, Float> magicDamage = new HashMap<>();
public final Map<Integer, Float> magicBoost = new HashMap<>();
public final Map<Integer, Float> magicDefense = new HashMap<>();
public final Map<Integer, Float> magicResistance = new HashMap<>();
private PlayerEquipmentTracker equipmentTracker = new PlayerEquipmentTracker();
public PlayerAttributeTracker(UUID playerId) {
this.playerId = playerId;
// Initial recalc with base damage 0.
recalcAttributes(null);
}
public PlayerAttributeMap getPlayerAttributes() {
return playerAttributes;
}
public void recalcAttributes(EntityPlayer player) {
// Reset aggregated attributes and magic maps.
playerAttributes = new PlayerAttributeMap();
magicDamage.clear();
magicBoost.clear();
magicDefense.clear();
magicResistance.clear();
int newExtraHealth = 0;
if (player != null) {
// Update equipment tracker.
PlayerEquipmentTracker currentEquip = new PlayerEquipmentTracker();
currentEquip.updateFrom(player);
ItemStack[] equipment = new ItemStack[] { currentEquip.heldItem, currentEquip.boots, currentEquip.leggings, currentEquip.chestplate, currentEquip.helmet };
for (ItemStack piece : equipment) {
if (piece != null) {
for (Entry<String, Float> entry : ItemAttributeHelper.readAttributes(piece).entrySet()) {
String key = entry.getKey();
float value = entry.getValue();
AttributeDefinition def = AttributeController.getAttribute(key);
if (def != null) {
ICustomAttribute inst = playerAttributes.getAttributeInstance(def);
if (inst == null) {
inst = playerAttributes.registerAttribute(def, 0.0f);
}
inst.setValue(inst.getValue() + value);
}
}
// Process Magic
// OFFENSIVE MAGIC
Map<Integer, Float> magicFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DAMAGE_KEY);
for (Entry<Integer, Float> entry : magicFlat.entrySet()) {
int magicId = entry.getKey();
float value = entry.getValue();
magicDamage.put(magicId, magicDamage.getOrDefault(magicId, 0.0f) + value);
}
Map<Integer, Float> magicPercent = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_BOOST_KEY);
for (Entry<Integer, Float> entry : magicPercent.entrySet()) {
int magicId = entry.getKey();
float value = entry.getValue();
magicBoost.put(magicId, magicBoost.getOrDefault(magicId, 0.0f) + value);
}
// DEFENSIVE MAGIC
Map<Integer, Float> magicDefFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DEFENSE_KEY);
for (Entry<Integer, Float> entry : magicDefFlat.entrySet()) {
int magicId = entry.getKey();
float value = entry.getValue();
magicDefense.put(magicId, magicDefense.getOrDefault(magicId, 0.0f) + value);
}
Map<Integer, Float> magicResist = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_RESISTANCE_KEY);
for (Entry<Integer, Float> entry : magicResist.entrySet()) {
int magicId = entry.getKey();
float value = entry.getValue();
magicResistance.put(magicId, magicResistance.getOrDefault(magicId, 0.0f) + value);
}
}
}
equipmentTracker = currentEquip;
maximumOutput = AttributeAttackHelper.calculateMaximumOutput(this);
extraHealth = getAttributeValue(ModAttributes.HEALTH);
updatePlayerMaxHealth(player);
}
}
/**
* Updates the player's maximum health attribute by applying a custom attribute modifier.
* In this system, the aggregated HEALTH attribute is treated as bonus health on top of a base of 20.
*
* @param player The EntityPlayer whose max health should be updated.
*/
public void updatePlayerMaxHealth(EntityPlayer player) {
float bonusHealth = extraHealth;
IAttributeInstance maxHealthAttr = player.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.maxHealth);
try {
AttributeModifier old = maxHealthAttr.getModifier(healthUUID);
if (old != null) {
maxHealthAttr.removeModifier(old);
}
} catch (Exception ignored) {}
if (bonusHealth != 0) {
maxHealthAttr.applyModifier(new AttributeModifier(healthUUID, "RPGCoreHealthBonus", bonusHealth, 0));
}
// Ensure the player's current health does not exceed the new maximum.
float currentHealth = player.getHealth();
if (currentHealth > maxHealthAttr.getAttributeValue()) {
player.setHealth((float) maxHealthAttr.getAttributeValue());
}
}
/**
* Checks if the player's equipment has changed and recalculates attributes if needed.
*/
public void updateIfChanged(EntityPlayer player) {
PlayerEquipmentTracker currentEquip = new PlayerEquipmentTracker();
currentEquip.updateFrom(player);
if (!equipmentTracker.equals(player)) {
recalcAttributes(player);
}
}
public float getAttributeValue(AttributeDefinition def) {
ICustomAttribute inst = playerAttributes.getAttributeInstance(def);
return inst != null ? inst.getValue() : 0.0f;
}
}
@@ -1,66 +0,0 @@
package noppes.npcs.attribute.player;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.attribute.ItemAttributeHelper;
public class PlayerEquipmentTracker {
public ItemStack heldItem;
public ItemStack helmet;
public ItemStack chestplate;
public ItemStack leggings;
public ItemStack boots;
/**
* Updates the stored equipment based on the player's current held item and armor.
*/
public void updateFrom(EntityPlayer player) {
heldItem = copyIfNotNull(player.getHeldItem());
ItemStack[] armor = player.inventory.armorInventory;
// Armor inventory order: 0=boots, 1=leggings, 2=chestplate, 3=helmet.
boots = copyIfNotNull(armor[0]);
leggings = copyIfNotNull(armor[1]);
chestplate = copyIfNotNull(armor[2]);
helmet = copyIfNotNull(armor[3]);
}
/**
* Compares the stored equipment with the player's current equipment.
* Ignores changes like durability by comparing the item and the CNPCAttributes NBT only.
*/
public boolean equals(EntityPlayer player) {
if (player == null) return false;
if (!areItemStacksEquivalent(heldItem, player.getHeldItem())) return false;
ItemStack[] armor = player.inventory.armorInventory;
if (!areItemStacksEquivalent(boots, armor[0])) return false;
if (!areItemStacksEquivalent(leggings, armor[1])) return false;
if (!areItemStacksEquivalent(chestplate, armor[2])) return false;
if (!areItemStacksEquivalent(helmet, armor[3])) return false;
return true;
}
/**
* Returns a copy of the given ItemStack, or null if it is null.
*/
private ItemStack copyIfNotNull(ItemStack stack) {
return (stack == null) ? null : stack.copy();
}
/**
* Compares two ItemStacks for equivalence, ignoring durability differences.
* It checks if the items are the same and if the "CNPCAttributes" NBT compound is equal.
*/
private boolean areItemStacksEquivalent(ItemStack a, ItemStack b) {
if (a == b) return true;
if (a == null || b == null) return false;
if (a.getItem() != b.getItem()) return false;
NBTTagCompound tagA = a.stackTagCompound;
NBTTagCompound tagB = b.stackTagCompound;
String attrA = (tagA != null && tagA.hasKey(ItemAttributeHelper.RPGItemAttributes))
? tagA.getCompoundTag(ItemAttributeHelper.RPGItemAttributes).toString() : "";
String attrB = (tagB != null && tagB.hasKey(ItemAttributeHelper.RPGItemAttributes))
? tagB.getCompoundTag(ItemAttributeHelper.RPGItemAttributes).toString() : "";
return attrA.equals(attrB);
}
}
@@ -1,9 +1,9 @@
package noppes.npcs.client.gui.player.inventory;
import noppes.npcs.api.handler.data.ISlot;
import kamkeel.npcs.controllers.data.InfoEntry;
import kamkeel.npcs.controllers.data.Profile;
import kamkeel.npcs.controllers.data.Slot;
import kamkeel.npcs.controllers.data.profile.ProfileInfoEntry;
import kamkeel.npcs.controllers.data.profile.Profile;
import kamkeel.npcs.controllers.data.profile.Slot;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.packets.request.profile.*;
import net.minecraft.client.gui.GuiButton;
@@ -36,7 +36,7 @@ public class GuiProfiles extends GuiCNPCInventory implements ISubGuiListener, IC
private String rename;
private Profile profile;
private Slot slot;
private HashMap<Integer, List<InfoEntry>> slotInfoMap = new HashMap<>();
private HashMap<Integer, List<ProfileInfoEntry>> slotInfoMap = new HashMap<>();
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy HH:mm");
private int scrollY = 0; // Vertical scrolling position
@@ -182,8 +182,8 @@ public class GuiProfiles extends GuiCNPCInventory implements ISubGuiListener, IC
}
if (slotInfoMap.containsKey(slot.getId())) {
List<InfoEntry> infoEntries = slotInfoMap.get(slot.getId());
for (InfoEntry entry : infoEntries) {
List<ProfileInfoEntry> infoEntries = slotInfoMap.get(slot.getId());
for (ProfileInfoEntry entry : infoEntries) {
label = StatCollector.translateToLocal(entry.getLabel());
result = StatCollector.translateToLocal(entry.getResult());
labelColor = entry.getLabelColor();
@@ -2,7 +2,7 @@ package noppes.npcs.controllers.data;
import kamkeel.npcs.addon.DBCAddon;
import kamkeel.npcs.controllers.ProfileController;
import kamkeel.npcs.controllers.data.Profile;
import kamkeel.npcs.controllers.data.profile.Profile;
import kamkeel.npcs.network.packets.data.AchievementPacket;
import kamkeel.npcs.network.packets.data.ChatAlertPacket;
import kamkeel.npcs.network.packets.request.party.PartyInvitePacket;
@@ -2,7 +2,7 @@ package noppes.npcs.controllers.data;
import kamkeel.npcs.controllers.ProfileController;
import noppes.npcs.api.handler.data.ISlot;
import kamkeel.npcs.controllers.data.Profile;
import kamkeel.npcs.controllers.data.profile.Profile;
import kamkeel.npcs.network.packets.data.QuestCompletionPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
@@ -3,7 +3,7 @@ package noppes.npcs.mixin;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.attribute.ItemAttributeHelper;
import kamkeel.npcs.util.AttributeItemUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@@ -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 noppes.npcs.attribute.ItemAttributeHelper.RPGItemAttributes;
import static kamkeel.npcs.util.AttributeItemUtil.RPGItemAttributes;
@Mixin(ItemStack.class)
public abstract class MixinItemStack {
@@ -29,7 +29,7 @@ public abstract class MixinItemStack {
public void getAttributeTooltip(EntityPlayer player, boolean advanced, CallbackInfoReturnable<List<String>> cir) {
List<String> tooltip = cir.getReturnValue();
if (hasTagCompound() && getTagCompound().hasKey(RPGItemAttributes)) {
cir.setReturnValue(ItemAttributeHelper.getToolTip(tooltip, getTagCompound()));
cir.setReturnValue(AttributeItemUtil.getToolTip(tooltip, getTagCompound()));
}
}
}