Files
CustomNPC-Plus/src/main/java/noppes/npcs/attribute/player/PlayerAttributeTracker.java
T

114 lines
5.1 KiB
Java
Raw Normal View History

2025-02-26 16:13:43 -05:00
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.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import noppes.npcs.attribute.*;
public class PlayerAttributeTracker {
private final UUID playerId;
private PlayerAttributeMap playerAttributes = new PlayerAttributeMap();
2025-02-27 13:47:09 -05:00
public int extraHealth = 0;
public float maximumOutput = 0.0f;
2025-02-27 11:48:03 -05:00
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<>();
2025-02-26 16:13:43 -05:00
private PlayerEquipmentTracker equipmentTracker = new PlayerEquipmentTracker();
public PlayerAttributeTracker(UUID playerId) {
this.playerId = playerId;
// Initial recalc with base damage 0.
2025-02-26 22:49:26 -05:00
recalcAttributes(null);
2025-02-26 16:13:43 -05:00
}
public PlayerAttributeMap getPlayerAttributes() {
return playerAttributes;
}
2025-02-26 22:49:26 -05:00
public void recalcAttributes(EntityPlayer player) {
2025-02-26 16:13:43 -05:00
// Reset aggregated attributes and magic maps.
playerAttributes = new PlayerAttributeMap();
magicDamage.clear();
magicBoost.clear();
magicDefense.clear();
magicResistance.clear();
if (player != null) {
// Update equipment tracker.
PlayerEquipmentTracker currentEquip = new PlayerEquipmentTracker();
currentEquip.updateFrom(player);
2025-02-26 22:49:26 -05:00
ItemStack[] equipment = new ItemStack[] { currentEquip.heldItem, currentEquip.boots, currentEquip.leggings, currentEquip.chestplate, currentEquip.helmet };
for (ItemStack piece : equipment) {
2025-02-26 16:13:43 -05:00
if (piece != null) {
2025-02-27 11:48:03 -05:00
for (Entry<String, Float> entry : ItemAttributeHelper.readAttributes(piece).entrySet()) {
2025-02-26 16:13:43 -05:00
String key = entry.getKey();
2025-02-27 11:48:03 -05:00
float value = entry.getValue();
2025-02-26 16:13:43 -05:00
AttributeDefinition def = AttributeController.getAttribute(key);
if (def != null) {
IAttributeInstance inst = playerAttributes.getAttributeInstance(def);
if (inst == null) {
2025-02-27 11:48:03 -05:00
inst = playerAttributes.registerAttribute(def, 0.0f);
2025-02-26 16:13:43 -05:00
}
inst.setValue(inst.getValue() + value);
}
}
2025-02-26 22:49:26 -05:00
// Process Magic
// OFFENSIVE MAGIC
2025-02-27 11:48:03 -05:00
Map<Integer, Float> magicFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DAMAGE_KEY);
for (Entry<Integer, Float> entry : magicFlat.entrySet()) {
2025-02-26 22:49:26 -05:00
int magicId = entry.getKey();
2025-02-27 11:48:03 -05:00
float value = entry.getValue();
magicDamage.put(magicId, magicDamage.getOrDefault(magicId, 0.0f) + value);
2025-02-26 22:49:26 -05:00
}
2025-02-27 11:48:03 -05:00
Map<Integer, Float> magicPercent = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_BOOST_KEY);
for (Entry<Integer, Float> entry : magicPercent.entrySet()) {
2025-02-26 22:49:26 -05:00
int magicId = entry.getKey();
2025-02-27 11:48:03 -05:00
float value = entry.getValue();
magicBoost.put(magicId, magicBoost.getOrDefault(magicId, 0.0f) + value);
2025-02-26 22:49:26 -05:00
}
// DEFENSIVE MAGIC
2025-02-27 11:48:03 -05:00
Map<Integer, Float> magicDefFlat = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_DEFENSE_KEY);
for (Entry<Integer, Float> entry : magicDefFlat.entrySet()) {
2025-02-26 16:13:43 -05:00
int magicId = entry.getKey();
2025-02-27 11:48:03 -05:00
float value = entry.getValue();
magicDefense.put(magicId, magicDefense.getOrDefault(magicId, 0.0f) + value);
2025-02-26 16:13:43 -05:00
}
2025-02-27 11:48:03 -05:00
Map<Integer, Float> magicResist = ItemAttributeHelper.readMagicAttributeMap(piece, ModAttributes.MAGIC_RESISTANCE_KEY);
for (Entry<Integer, Float> entry : magicResist.entrySet()) {
2025-02-26 16:13:43 -05:00
int magicId = entry.getKey();
2025-02-27 11:48:03 -05:00
float value = entry.getValue();
magicResistance.put(magicId, magicResistance.getOrDefault(magicId, 0.0f) + value);
2025-02-26 16:13:43 -05:00
}
}
}
equipmentTracker = currentEquip;
2025-02-27 13:47:09 -05:00
maximumOutput = AttributeAttackHelper.calculateMaximumOutput(this);
2025-02-26 16:13:43 -05:00
}
}
/**
* 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)) {
2025-02-26 22:49:26 -05:00
recalcAttributes(player);
2025-02-26 16:13:43 -05:00
}
}
2025-02-27 11:48:03 -05:00
public float getAttributeValue(AttributeDefinition def) {
2025-02-26 16:13:43 -05:00
IAttributeInstance inst = playerAttributes.getAttributeInstance(def);
2025-02-27 11:48:03 -05:00
return inst != null ? inst.getValue() : 0.0f;
2025-02-26 16:13:43 -05:00
}
}