feat(health): honor native ceilings without capping CON
This commit is contained in:
1 parent
9771d1e7c3
commit
db87505142
5 files changed
+165
-2
No files matched your search
@@ -19,6 +19,7 @@ public final class CharacterProgress {
|
||||
|
||||
private final UUID characterId;
|
||||
private final ProgressionRules rules;
|
||||
private final BigDecimal nativeHealthLimit;
|
||||
private final EnumMap<Stat, BigInteger> stats = new EnumMap<>(Stat.class);
|
||||
private int level = 1;
|
||||
private BigInteger experience = BigInteger.ZERO;
|
||||
@@ -26,11 +27,19 @@ public final class CharacterProgress {
|
||||
private BigInteger revision = BigInteger.ZERO;
|
||||
private BigDecimal health = BigDecimal.TEN;
|
||||
|
||||
/** Uses the pinned runtime's default ceiling. Runtime adapters must supply their actual limit. */
|
||||
public CharacterProgress(UUID characterId, ProgressionRules rules) {
|
||||
this(characterId, rules, new BigDecimal("1024"));
|
||||
}
|
||||
|
||||
public CharacterProgress(UUID characterId, ProgressionRules rules, BigDecimal nativeHealthLimit) {
|
||||
this.characterId = Objects.requireNonNull(characterId);
|
||||
this.rules = Objects.requireNonNull(rules);
|
||||
this.nativeHealthLimit = Objects.requireNonNull(nativeHealthLimit);
|
||||
if (nativeHealthLimit.signum() <= 0) throw new IllegalArgumentException("Native health limit must be positive");
|
||||
this.points = rules.startingPoints();
|
||||
for (Stat stat : Stat.values()) stats.put(stat, BigInteger.ZERO);
|
||||
this.health = maximumHealth();
|
||||
}
|
||||
|
||||
public synchronized Snapshot snapshot() {
|
||||
@@ -47,9 +56,10 @@ public final class CharacterProgress {
|
||||
if (!revision.equals(expectedRevision)) return Purchase.STALE_REVISION;
|
||||
if (health.signum() == 0) return Purchase.NOT_ALIVE;
|
||||
if (points.signum() == 0) return Purchase.NO_POINTS;
|
||||
BigDecimal previousMaximum = maximumHealth();
|
||||
stats.put(stat, stats.get(stat).add(BigInteger.ONE));
|
||||
points = points.subtract(BigInteger.ONE);
|
||||
if (stat == Stat.CON) health = health.add(BigDecimal.ONE);
|
||||
if (stat == Stat.CON) health = health.add(maximumHealth().subtract(previousMaximum));
|
||||
revision = revision.add(BigInteger.ONE);
|
||||
return Purchase.APPLIED;
|
||||
}
|
||||
@@ -80,6 +90,6 @@ public final class CharacterProgress {
|
||||
}
|
||||
|
||||
private BigDecimal maximumHealth() {
|
||||
return new BigDecimal(stats.get(Stat.CON).add(BigInteger.TEN));
|
||||
return new BigDecimal(stats.get(Stat.CON).add(BigInteger.TEN)).min(nativeHealthLimit);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package games.dmg.fantasystick.session;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import org.bukkit.attribute.Attribute;
|
||||
|
||||
/**
|
||||
* Read-only access to the pinned runtime's health range; never changes server settings or attributes.
|
||||
* Call on the server thread after registry initialization. The API exposes no range getter, so use the
|
||||
* supported runtime's public Craft/native accessors and fail explicitly if they are incompatible.
|
||||
* This reads the current range on each call, not a guessed default or cached configuration value.
|
||||
* Native session/health application is separate and must revalidate its context before mutation.
|
||||
*/
|
||||
public final class NativeHealthLimit {
|
||||
private NativeHealthLimit() { }
|
||||
|
||||
public static BigDecimal read() {
|
||||
try {
|
||||
Object range = Attribute.MAX_HEALTH.getClass().getMethod("getHandle").invoke(Attribute.MAX_HEALTH);
|
||||
double maximum = ((Number) range.getClass().getMethod("getMaxValue").invoke(range)).doubleValue();
|
||||
if (!Double.isFinite(maximum) || maximum <= 0
|
||||
|| !Float.isFinite((float) maximum) || (float) maximum <= 0) {
|
||||
throw new IllegalStateException("Native maximum health must be positive and representable as Minecraft health");
|
||||
}
|
||||
return BigDecimal.valueOf(maximum);
|
||||
} catch (ReflectiveOperationException | ClassCastException failure) {
|
||||
throw new IllegalStateException("Cannot read the supported native maximum-health range", failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user