Limit status effect stacking

This commit is contained in:
Sollace 2023-08-27 21:55:39 +01:00
parent 7c62e56d1d
commit d22140eccd
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
2 changed files with 18 additions and 11 deletions

View file

@ -121,10 +121,10 @@ public interface Toxics {
Toxic BAT_PONYS_DELIGHT = register("bat_ponys_delight", new Toxic.Builder(Ailment.INNERT)
.with(Race.BAT, Ailment.of(Toxicity.SAFE,
Toxin.of(StatusEffects.HEALTH_BOOST, 30, 2)
.and(Toxin.of(StatusEffects.JUMP_BOOST, 30, 1))
.and(Toxin.of(StatusEffects.SPEED, 30, 1))
.and(Toxin.of(StatusEffects.REGENERATION, 3, 3))
Toxin.of(StatusEffects.HEALTH_BOOST, 30, 60, 2, 6)
.and(Toxin.of(StatusEffects.JUMP_BOOST, 30, 60, 1, 6))
.and(Toxin.of(StatusEffects.SPEED, 30, 30, 1, 6))
.and(Toxin.of(StatusEffects.REGENERATION, 3, 30, 3, 6))
))
);

View file

@ -101,7 +101,12 @@ public interface Toxin extends Affliction {
}
static Toxin of(StatusEffect effect, int seconds, int amplifier) {
int ticks = seconds * 20;
return of(effect, seconds, -1, amplifier, -1);
}
static Toxin of(StatusEffect effect, int seconds, int maxSeconds, int amplifier, int maxAmplifier) {
final int ticks = seconds * 20;
final int maxTicks = maxSeconds * 20;
MutableText text = effect.getName().copy();
@ -112,13 +117,11 @@ public interface Toxin extends Affliction {
text = Text.translatable("potion.withDuration", text, StringHelper.formatTicks(ticks));
return of(text, (player, stack) -> {
StatusEffectInstance current = player.getStatusEffect(effect);
float health = player.getHealth();
if (current != null) {
player.addStatusEffect(new StatusEffectInstance(effect, ticks + current.getDuration(), amplifier + current.getAmplifier(), false, false, false));
} else {
player.addStatusEffect(new StatusEffectInstance(effect, ticks, amplifier, false, false, false));
}
StatusEffectInstance current = player.getStatusEffect(effect);
int t = applyLimit(ticks + (current == null ? 0 : current.getDuration()), maxTicks);
int a = applyLimit(amplifier + (current == null ? 0 : current.getAmplifier()), maxAmplifier);
player.addStatusEffect(new StatusEffectInstance(effect, t, a, false, false, false));
// keep original health
if (effect.getAttributeModifiers().containsKey(EntityAttributes.GENERIC_MAX_HEALTH)) {
player.setHealth(MathHelper.clamp(health, 0, player.getMaxHealth()));
@ -126,6 +129,10 @@ public interface Toxin extends Affliction {
});
}
private static int applyLimit(int value, int max) {
return max > 0 ? Math.min(value, max) : value;
}
interface Predicate {
static Predicate of(Text name, Affliction.Predicate predicate) {
return new Predicate() {