Custom Attributes can now modify health

This commit is contained in:
Kam committed 2025-02-27 14:29:04 -05:00
1 parent 4e0359902d
commit baf06e7162
4 files changed
+48 -12

No files matched your search

@@ -3,7 +3,7 @@ package noppes.npcs.attribute;
/**
* IAttributeInstance represents an instance of an attribute (with a base value and applied modifiers).
*/
public interface IAttributeInstance {
public interface ICustomAttribute {
AttributeDefinition getAttribute();
float getValue();
void setValue(float value);
@@ -2,9 +2,9 @@ package noppes.npcs.attribute.player;
import noppes.npcs.attribute.AttributeDefinition;
import noppes.npcs.attribute.IAttributeInstance;
import noppes.npcs.attribute.ICustomAttribute;
public class PlayerAttribute implements IAttributeInstance {
public class PlayerAttribute implements ICustomAttribute {
private final AttributeDefinition attribute;
private float baseValue;
@@ -1,7 +1,7 @@
package noppes.npcs.attribute.player;
import noppes.npcs.attribute.AttributeDefinition;
import noppes.npcs.attribute.IAttributeInstance;
import noppes.npcs.attribute.ICustomAttribute;
import java.util.Collection;
import java.util.HashMap;
@@ -11,22 +11,22 @@ import java.util.Map;
* CustomAttributeMap holds a set of attribute instances for an entity (or item).
*/
public class PlayerAttributeMap {
private final Map<AttributeDefinition, IAttributeInstance> map = new HashMap<>();
private final Map<AttributeDefinition, ICustomAttribute> map = new HashMap<>();
public IAttributeInstance registerAttribute(AttributeDefinition attribute, float baseValue) {
public ICustomAttribute registerAttribute(AttributeDefinition attribute, float baseValue) {
if (map.containsKey(attribute)) {
throw new IllegalArgumentException("Attribute already registered: " + attribute.getKey());
}
IAttributeInstance instance = new PlayerAttribute(attribute, baseValue);
ICustomAttribute instance = new PlayerAttribute(attribute, baseValue);
map.put(attribute, instance);
return instance;
}
public IAttributeInstance getAttributeInstance(AttributeDefinition attribute) {
public ICustomAttribute getAttributeInstance(AttributeDefinition attribute) {
return map.get(attribute);
}
public Collection<IAttributeInstance> getAllAttributes() {
public Collection<ICustomAttribute> getAllAttributes() {
return map.values();
}
}
@@ -4,16 +4,23 @@ 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 int extraHealth = 0;
public float extraHealth = 0;
public float maximumOutput = 0.0f;
public final Map<Integer, Float> magicDamage = new HashMap<>();
@@ -41,6 +48,7 @@ public class PlayerAttributeTracker {
magicDefense.clear();
magicResistance.clear();
int newExtraHealth = 0;
if (player != null) {
// Update equipment tracker.
PlayerEquipmentTracker currentEquip = new PlayerEquipmentTracker();
@@ -53,7 +61,7 @@ public class PlayerAttributeTracker {
float value = entry.getValue();
AttributeDefinition def = AttributeController.getAttribute(key);
if (def != null) {
IAttributeInstance inst = playerAttributes.getAttributeInstance(def);
ICustomAttribute inst = playerAttributes.getAttributeInstance(def);
if (inst == null) {
inst = playerAttributes.registerAttribute(def, 0.0f);
}
@@ -93,6 +101,34 @@ public class PlayerAttributeTracker {
}
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());
}
}
@@ -108,7 +144,7 @@ public class PlayerAttributeTracker {
}
public float getAttributeValue(AttributeDefinition def) {
IAttributeInstance inst = playerAttributes.getAttributeInstance(def);
ICustomAttribute inst = playerAttributes.getAttributeInstance(def);
return inst != null ? inst.getValue() : 0.0f;
}
}