diff --git a/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java index 2df81b752..0e43eedfa 100644 --- a/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java +++ b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java @@ -24,6 +24,7 @@ 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.DamageCalculator; import noppes.npcs.constants.EnumQuestType; import noppes.npcs.controllers.CustomEffectController; import noppes.npcs.controllers.PartyController; @@ -472,6 +473,12 @@ public class ScriptPlayerEventHandler { } if(event.source.getEntity() instanceof EntityPlayer) { + if(event.entityLiving instanceof EntityPlayer){ + event.ammount = DamageCalculator.calculateDamagePlayer((EntityPlayer) event.source.getEntity(), (EntityPlayer) event.entityLiving, event.ammount); + } else if (event.entityLiving instanceof EntityNPCInterface){ + + } + noppes.npcs.scripted.event.PlayerEvent.AttackEvent pevent1 = new noppes.npcs.scripted.event.PlayerEvent.AttackEvent((IPlayer)NpcAPI.Instance().getIEntity((EntityPlayer)event.source.getEntity()), event.entityLiving, event.ammount, event.source); cancel = cancel || EventHooks.onPlayerAttack(handler, pevent1); } diff --git a/src/main/java/noppes/npcs/attribute/AttributeValueType.java b/src/main/java/noppes/npcs/attribute/AttributeValueType.java index 26c35b6ff..c986bcd35 100644 --- a/src/main/java/noppes/npcs/attribute/AttributeValueType.java +++ b/src/main/java/noppes/npcs/attribute/AttributeValueType.java @@ -3,5 +3,5 @@ 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 double) + MAGIC // Represents a container of magic–based values (summed up as a float) } diff --git a/src/main/java/noppes/npcs/attribute/DamageCalculator.java b/src/main/java/noppes/npcs/attribute/DamageCalculator.java index c1923a01a..32d46f07b 100644 --- a/src/main/java/noppes/npcs/attribute/DamageCalculator.java +++ b/src/main/java/noppes/npcs/attribute/DamageCalculator.java @@ -1,7 +1,10 @@ package noppes.npcs.attribute; +import net.minecraft.entity.Entity; +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; @@ -9,51 +12,98 @@ import java.util.Random; public class DamageCalculator { private static final Random random = new Random(); - public static double calculateDamage(PlayerAttributeTracker attacker, PlayerAttributeTracker defender, double baseDamage) { + public static float calculateDamagePlayer(EntityPlayer attacker, EntityPlayer defender, float base){ + return calculateDamagePlayer(AttributeController.getTracker(attacker), AttributeController.getTracker(defender), base); + } + + public static float calculateDamagePlayer(PlayerAttributeTracker attacker, PlayerAttributeTracker defender, float baseDamage) { // Calculate physical damage - double mainAttack = attacker.getAttributeValue(ModAttributes.MAIN_ATTACK); - double mainBoost = attacker.getAttributeValue(ModAttributes.MAGIC_BOOST) + 1; - double physicalDamage = (baseDamage * mainBoost) + mainAttack; + float mainAttack = attacker.getAttributeValue(ModAttributes.MAIN_ATTACK); + float mainBoost = attacker.getAttributeValue(ModAttributes.MAGIC_BOOST) + 1; + float physicalDamage = (baseDamage * mainBoost) + mainAttack; // Calculate neutral damage - double neutralDamage = attacker.getAttributeValue(ModAttributes.NEUTRAL_ATTACK); - double neutralBoost = attacker.getAttributeValue(ModAttributes.NEUTRAL_BOOST) + 1; - double neutralTotal = neutralDamage * neutralBoost; + float neutralDamage = attacker.getAttributeValue(ModAttributes.NEUTRAL_ATTACK); + float neutralBoost = attacker.getAttributeValue(ModAttributes.NEUTRAL_BOOST) + 1; + float neutralTotal = neutralDamage * neutralBoost; // Calculate magic damage - double magicTotal = 0.0; + float magicTotal = 0.0f; MagicController magicController = MagicController.getInstance(); - for (Map.Entry entry : attacker.magicDamage.entrySet()) { + for (Map.Entry entry : attacker.magicDamage.entrySet()) { int magicId = entry.getKey(); if (magicController.getMagic(magicId) != null) { - double attackMagic = entry.getValue(); - double defenseMagic = defender.magicDefense.getOrDefault(magicId, 0.0); - double effectiveMagic = attackMagic - defenseMagic; + float attackMagic = entry.getValue(); + float defenseMagic = defender.magicDefense.getOrDefault(magicId, 0.0f); + float effectiveMagic = attackMagic - defenseMagic; - double boostMagic = attacker.magicBoost.getOrDefault(magicId, 0.0); - double resistanceMagic = defender.magicResistance.getOrDefault(magicId, 0.0); - double effectiveBoost = boostMagic - resistanceMagic; + float boostMagic = attacker.magicBoost.getOrDefault(magicId, 0.0f); + float resistanceMagic = defender.magicResistance.getOrDefault(magicId, 0.0f); + float effectiveBoost = boostMagic - resistanceMagic; - double magicDamageForMagic = effectiveMagic * (effectiveBoost + 1); + 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 - double totalDamage = physicalDamage + neutralTotal + magicTotal; + float totalDamage = physicalDamage + neutralTotal + magicTotal; - // Critical hit: if critical, double damage and add bonus - double criticalChance = attacker.getAttributeValue(ModAttributes.CRITICAL_CHANCE); - double criticalBonus = attacker.getAttributeValue(ModAttributes.CRITICAL_DAMAGE); - if (random.nextDouble() < (criticalChance / 100)) { + // 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.MAGIC_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 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; + } } diff --git a/src/main/java/noppes/npcs/attribute/IAttributeInstance.java b/src/main/java/noppes/npcs/attribute/IAttributeInstance.java index ed7d5669e..27269471d 100644 --- a/src/main/java/noppes/npcs/attribute/IAttributeInstance.java +++ b/src/main/java/noppes/npcs/attribute/IAttributeInstance.java @@ -5,6 +5,6 @@ package noppes.npcs.attribute; */ public interface IAttributeInstance { AttributeDefinition getAttribute(); - double getValue(); - void setValue(double value); + float getValue(); + void setValue(float value); } diff --git a/src/main/java/noppes/npcs/attribute/ItemAttributeHelper.java b/src/main/java/noppes/npcs/attribute/ItemAttributeHelper.java index 97dbfc7ce..fa66b9912 100644 --- a/src/main/java/noppes/npcs/attribute/ItemAttributeHelper.java +++ b/src/main/java/noppes/npcs/attribute/ItemAttributeHelper.java @@ -28,7 +28,7 @@ public class ItemAttributeHelper { /** * Applies a non–magic attribute to an item. */ - public static void applyAttribute(ItemStack item, String attributeKey, double value) { + public static void applyAttribute(ItemStack item, String attributeKey, Float value) { if (item == null) return; if (item.stackTagCompound == null) { item.stackTagCompound = new NBTTagCompound(); @@ -40,7 +40,7 @@ public class ItemAttributeHelper { } else { attrTag = new NBTTagCompound(); } - attrTag.setDouble(attributeKey, value); + attrTag.setFloat(attributeKey, value); root.setTag(RPGItemAttributes, attrTag); } @@ -63,15 +63,15 @@ public class ItemAttributeHelper { /** * Reads non–magic attributes from an item. */ - public static Map readAttributes(ItemStack item) { - Map map = new HashMap<>(); + public static Map readAttributes(ItemStack item) { + Map 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 keys = attrTag.func_150296_c(); for (String key : keys) { - map.put(key, attrTag.getDouble(key)); + map.put(key, attrTag.getFloat(key)); } } return map; @@ -81,8 +81,8 @@ public class ItemAttributeHelper { * 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 readMagicAttributeMap(ItemStack item, String attributeTag) { - Map map = new HashMap<>(); + public static Map readMagicAttributeMap(ItemStack item, String attributeTag) { + Map map = new HashMap<>(); if (item == null || item.stackTagCompound == null) return map; if (item.stackTagCompound.hasKey(attributeTag)) { NBTTagCompound magicMap = item.stackTagCompound.getCompoundTag(attributeTag); @@ -90,7 +90,7 @@ public class ItemAttributeHelper { for (String key : keys) { try { int magicId = Integer.parseInt(key); - map.put(magicId, magicMap.getDouble(key)); + map.put(magicId, magicMap.getFloat(key)); } catch (NumberFormatException e) { // Skip invalid key. } @@ -103,14 +103,14 @@ public class ItemAttributeHelper { * 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) { + 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, double value) { + public static void writeMagicAttribute(ItemStack item, String attributeTag, int magicId, Float value) { if (item == null) return; if (item.stackTagCompound == null) item.stackTagCompound = new NBTTagCompound(); @@ -120,7 +120,7 @@ public class ItemAttributeHelper { } else { magicMap = new NBTTagCompound(); } - magicMap.setDouble(String.valueOf(magicId), value); + magicMap.setFloat(String.valueOf(magicId), value); item.stackTagCompound.setTag(attributeTag, magicMap); } @@ -165,7 +165,7 @@ public class ItemAttributeHelper { || key.equals(ModAttributes.MAGIC_DEFENSE_KEY) || key.equals(ModAttributes.MAGIC_RESISTANCE_KEY)) { continue; } - double value = attrTag.getDouble(key); + 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); @@ -228,7 +228,7 @@ public class ItemAttributeHelper { String key = magicIter.next(); try { int magicId = Integer.parseInt(key); - double value = magicTag.getDouble(key); + Float value = magicTag.getFloat(key); Magic magic = MagicController.getInstance().getMagic(magicId); if (magic != null) { String magicDisplayName = magic.getDisplayName() + " " + EnumChatFormatting.GRAY + getMagicAppendix(magicKey); @@ -293,8 +293,8 @@ public class ItemAttributeHelper { * Formats the attribute line based on its section and value type. */ private static String formatAttributeLine(AttributeDefinition def, AttributeDefinition.AttributeSection section, - double value, String displayName) { - String formattedValue = formatDouble(value); + 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(); @@ -327,9 +327,9 @@ public class ItemAttributeHelper { } /** - * Helper to format a double by removing trailing zeros. + * Helper to format a Float by removing trailing zeros. */ - private static String formatDouble(double value) { - return new java.math.BigDecimal(Double.toString(value)).stripTrailingZeros().toPlainString(); + private static String formatFloat(Float value) { + return new java.math.BigDecimal(Float.toString(value)).stripTrailingZeros().toPlainString(); } } diff --git a/src/main/java/noppes/npcs/attribute/player/PlayerAttribute.java b/src/main/java/noppes/npcs/attribute/player/PlayerAttribute.java index 97d1d7233..41b3248e5 100644 --- a/src/main/java/noppes/npcs/attribute/player/PlayerAttribute.java +++ b/src/main/java/noppes/npcs/attribute/player/PlayerAttribute.java @@ -6,9 +6,9 @@ import noppes.npcs.attribute.IAttributeInstance; public class PlayerAttribute implements IAttributeInstance { private final AttributeDefinition attribute; - private double baseValue; + private float baseValue; - public PlayerAttribute(AttributeDefinition attribute, double baseValue) { + public PlayerAttribute(AttributeDefinition attribute, float baseValue) { this.attribute = attribute; this.baseValue = baseValue; } @@ -19,12 +19,12 @@ public class PlayerAttribute implements IAttributeInstance { } @Override - public double getValue() { + public float getValue() { return baseValue; } @Override - public void setValue(double value) { + public void setValue(float value) { this.baseValue = value; } } diff --git a/src/main/java/noppes/npcs/attribute/player/PlayerAttributeMap.java b/src/main/java/noppes/npcs/attribute/player/PlayerAttributeMap.java index 91f11aa43..7004466de 100644 --- a/src/main/java/noppes/npcs/attribute/player/PlayerAttributeMap.java +++ b/src/main/java/noppes/npcs/attribute/player/PlayerAttributeMap.java @@ -13,7 +13,7 @@ import java.util.Map; public class PlayerAttributeMap { private final Map map = new HashMap<>(); - public IAttributeInstance registerAttribute(AttributeDefinition attribute, double baseValue) { + public IAttributeInstance registerAttribute(AttributeDefinition attribute, float baseValue) { if (map.containsKey(attribute)) { throw new IllegalArgumentException("Attribute already registered: " + attribute.getKey()); } diff --git a/src/main/java/noppes/npcs/attribute/player/PlayerAttributeTracker.java b/src/main/java/noppes/npcs/attribute/player/PlayerAttributeTracker.java index 12a467712..8c4825e9d 100644 --- a/src/main/java/noppes/npcs/attribute/player/PlayerAttributeTracker.java +++ b/src/main/java/noppes/npcs/attribute/player/PlayerAttributeTracker.java @@ -14,10 +14,10 @@ public class PlayerAttributeTracker { private PlayerAttributeMap playerAttributes = new PlayerAttributeMap(); public final int extraHealth = 0; - public final Map magicDamage = new HashMap<>(); - public final Map magicBoost = new HashMap<>(); - public final Map magicDefense = new HashMap<>(); - public final Map magicResistance = new HashMap<>(); + public final Map magicDamage = new HashMap<>(); + public final Map magicBoost = new HashMap<>(); + public final Map magicDefense = new HashMap<>(); + public final Map magicResistance = new HashMap<>(); private PlayerEquipmentTracker equipmentTracker = new PlayerEquipmentTracker(); @@ -46,14 +46,14 @@ public class PlayerAttributeTracker { ItemStack[] equipment = new ItemStack[] { currentEquip.heldItem, currentEquip.boots, currentEquip.leggings, currentEquip.chestplate, currentEquip.helmet }; for (ItemStack piece : equipment) { if (piece != null) { - for (Entry entry : ItemAttributeHelper.readAttributes(piece).entrySet()) { + for (Entry entry : ItemAttributeHelper.readAttributes(piece).entrySet()) { String key = entry.getKey(); - double value = entry.getValue(); + float value = entry.getValue(); AttributeDefinition def = AttributeController.getAttribute(key); if (def != null) { IAttributeInstance inst = playerAttributes.getAttributeInstance(def); if (inst == null) { - inst = playerAttributes.registerAttribute(def, 0.0); + inst = playerAttributes.registerAttribute(def, 0.0f); } inst.setValue(inst.getValue() + value); } @@ -61,31 +61,31 @@ public class PlayerAttributeTracker { // Process Magic // OFFENSIVE MAGIC - Map magicFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DAMAGE_KEY); - for (Entry entry : magicFlat.entrySet()) { + Map magicFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DAMAGE_KEY); + for (Entry entry : magicFlat.entrySet()) { int magicId = entry.getKey(); - double value = entry.getValue(); - magicDamage.put(magicId, magicDamage.getOrDefault(magicId, 0.0) + value); + float value = entry.getValue(); + magicDamage.put(magicId, magicDamage.getOrDefault(magicId, 0.0f) + value); } - Map magicPercent = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_BOOST_KEY); - for (Entry entry : magicPercent.entrySet()) { + Map magicPercent = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_BOOST_KEY); + for (Entry entry : magicPercent.entrySet()) { int magicId = entry.getKey(); - double value = entry.getValue(); - magicBoost.put(magicId, magicBoost.getOrDefault(magicId, 0.0) + value); + float value = entry.getValue(); + magicBoost.put(magicId, magicBoost.getOrDefault(magicId, 0.0f) + value); } // DEFENSIVE MAGIC - Map magicDefFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DEFENSE_KEY); - for (Entry entry : magicDefFlat.entrySet()) { + Map magicDefFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DEFENSE_KEY); + for (Entry entry : magicDefFlat.entrySet()) { int magicId = entry.getKey(); - double value = entry.getValue(); - magicDefense.put(magicId, magicDefense.getOrDefault(magicId, 0.0) + value); + float value = entry.getValue(); + magicDefense.put(magicId, magicDefense.getOrDefault(magicId, 0.0f) + value); } - Map magicResist = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_RESISTANCE_KEY); - for (Entry entry : magicResist.entrySet()) { + Map magicResist = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_RESISTANCE_KEY); + for (Entry entry : magicResist.entrySet()) { int magicId = entry.getKey(); - double value = entry.getValue(); - magicResistance.put(magicId, magicResistance.getOrDefault(magicId, 0.0) + value); + float value = entry.getValue(); + magicResistance.put(magicId, magicResistance.getOrDefault(magicId, 0.0f) + value); } } } @@ -104,8 +104,8 @@ public class PlayerAttributeTracker { } } - public double getAttributeValue(AttributeDefinition def) { + public float getAttributeValue(AttributeDefinition def) { IAttributeInstance inst = playerAttributes.getAttributeInstance(def); - return inst != null ? inst.getValue() : 0.0; + return inst != null ? inst.getValue() : 0.0f; } }