From 6477ede022abccc21e432d1e0aba0b3150c31848 Mon Sep 17 00:00:00 2001 From: KAMKEEL <28842281+KAMKEEL@users.noreply.github.com> Date: Tue, 11 Aug 2026 03:31:38 -0400 Subject: [PATCH] Make hazard radius and barrier duration accurate - Hazards only checked who was inside once per damage interval, so the boundary where damage started and stopped drifted by however far an entity moved in between - several blocks at sprint speed. Membership is now checked every tick and damage accrues per entity from the time it actually spends inside. - Barriers counted elapsed ticks instead of holding a deadline, so every tick the entity missed extended it past its configured duration. They now expire on an absolute world time like the other energy entities. --- .../npcs/entity/EntityAbilityZone.java | 34 +++++++++++++------ .../npcs/entity/EntityEnergyBarrier.java | 19 +++++++---- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/main/java/kamkeel/npcs/entity/EntityAbilityZone.java b/src/main/java/kamkeel/npcs/entity/EntityAbilityZone.java index 7913386c6..2a4632e77 100644 --- a/src/main/java/kamkeel/npcs/entity/EntityAbilityZone.java +++ b/src/main/java/kamkeel/npcs/entity/EntityAbilityZone.java @@ -23,8 +23,10 @@ import noppes.npcs.NpcDamageSource; import noppes.npcs.entity.EntityNPCInterface; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -170,8 +172,9 @@ public class EntityAbilityZone extends Entity implements IEntityAdditionalSpawnD private transient Set triggeredEntities = new HashSet<>(); private transient int triggerFlashTick = -1; - private transient int ticksSinceDamage = 0; private transient Set damagedThisTick = new HashSet<>(); + /** Ticks each entity has spent inside the zone since it was last damaged, by entity id. */ + private transient Map ticksInZone = new HashMap<>(); private transient Ability sourceAbility = null; @@ -216,7 +219,6 @@ public class EntityAbilityZone extends Entity implements IEntityAdditionalSpawnD this.ownerEntityId = owner != null ? owner.getEntityId() : -1; double groundY = Ability.findGroundLevel(worldObj, x, y, z) + GROUND_OFFSET; this.setPosition(x, groundY, z); - this.ticksSinceDamage = damageInterval; } /** @@ -309,7 +311,6 @@ public class EntityAbilityZone extends Entity implements IEntityAdditionalSpawnD zone.effects.add(e.copy()); } } - zone.ticksSinceDamage = damageInterval; return zone; } @@ -495,13 +496,10 @@ public class EntityAbilityZone extends Entity implements IEntityAdditionalSpawnD private void tickHazard() { damagedThisTick.clear(); - ticksSinceDamage++; - - if (ticksSinceDamage < damageInterval) { - return; - } - ticksSinceDamage = 0; + // Membership is checked every tick, not once per damage interval. Sampling on the + // interval alone made the effective boundary drift by however far an entity moved in + // between, which on a large radius reads as the radius itself being wrong. AxisAlignedBB searchBox = AxisAlignedBB.getBoundingBox( posX - radius, posY - 0.5, posZ - radius, posX + radius, posY + zoneHeight, posZ + radius @@ -511,19 +509,35 @@ public class EntityAbilityZone extends Entity implements IEntityAdditionalSpawnD @SuppressWarnings("unchecked") List entities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, searchBox); + Set insideThisTick = new HashSet<>(); for (EntityLivingBase entity : entities) { if (owner != null && entity == owner && !affectsCaster) continue; if (damagedThisTick.contains(entity.getEntityId())) continue; if (entity != owner && owner instanceof EntityLivingBase && !AbilityTargetHelper.shouldAffect((EntityLivingBase) owner, entity, TargetFilter.ENEMIES, false)) continue; if (!isInZone(entity)) continue; + int entityId = entity.getEntityId(); + insideThisTick.add(entityId); + + // Entities that just entered are hit immediately, then once per interval of time + // actually spent inside. + Integer elapsed = ticksInZone.get(entityId); + int ticks = elapsed == null ? damageInterval : elapsed + 1; + if (ticks < damageInterval) { + ticksInZone.put(entityId, ticks); + continue; + } + ticksInZone.put(entityId, 0); + if (damagePerSecond > 0) { applyDamage(entity, owner, damagePerSecond); } applyEffects(entity); - damagedThisTick.add(entity.getEntityId()); + damagedThisTick.add(entityId); } + + ticksInZone.keySet().retainAll(insideThisTick); } private boolean isInZone(EntityLivingBase entity) { diff --git a/src/main/java/kamkeel/npcs/entity/EntityEnergyBarrier.java b/src/main/java/kamkeel/npcs/entity/EntityEnergyBarrier.java index 4ccb01269..8b5da0ad5 100644 --- a/src/main/java/kamkeel/npcs/entity/EntityEnergyBarrier.java +++ b/src/main/java/kamkeel/npcs/entity/EntityEnergyBarrier.java @@ -114,6 +114,8 @@ public abstract class EntityEnergyBarrier extends EntityEnergyAbility { protected EnergyBarrierData barrierData = new EnergyBarrierData(); protected float currentHealth; protected int ticksAlive = 0; + /** Absolute world time the barrier expires at, so ticks it misses cannot extend it. */ + protected long barrierDeathTime = -1; // ==================== DATA WATCHER INDICES ==================== protected static final int DW_HEALTH_PERCENT = 21; @@ -510,15 +512,16 @@ public abstract class EntityEnergyBarrier extends EntityEnergyAbility { } } - // Duration check - if (barrierData.useDuration && ticksAlive >= barrierData.durationTicks) { - onBarrierDestroyed(); - this.setDead(); - return true; + // Set the deadline on the first tick after charging, so lifetime starts when the + // barrier is actually up. Counting elapsed ticks instead would let any tick the + // entity misses - chunk unload, for one - extend it past its configured duration. + if (barrierDeathTime < 0 && !isCharging()) { + barrierDeathTime = worldObj.getTotalWorldTime() + + (barrierData.useDuration ? barrierData.durationTicks : BARRIER_HARD_LIFETIME_CAP); } - // Absolute hard lifetime cap (safety net for barriers with no duration) - if (ticksAlive > BARRIER_HARD_LIFETIME_CAP) { + // Duration check, and the hard lifetime cap for barriers with no duration + if (barrierDeathTime >= 0 && worldObj.getTotalWorldTime() >= barrierDeathTime) { onBarrierDestroyed(); this.setDead(); return true; @@ -865,6 +868,7 @@ public abstract class EntityEnergyBarrier extends EntityEnergyAbility { protected void writeBarrierBaseNBT(NBTTagCompound nbt) { writeEnergyBaseNBT(nbt); nbt.setInteger("TicksAlive", ticksAlive); + nbt.setLong("BarrierDeathTime", barrierDeathTime); nbt.setFloat("CurrentHealth", currentHealth); nbt.setBoolean("Charging", charging); nbt.setInteger("ChargeTick", chargeTick); @@ -879,6 +883,7 @@ public abstract class EntityEnergyBarrier extends EntityEnergyAbility { protected void readBarrierBaseNBT(NBTTagCompound nbt) { readEnergyBaseNBT(nbt); this.ticksAlive = nbt.getInteger("TicksAlive"); + this.barrierDeathTime = nbt.hasKey("BarrierDeathTime") ? nbt.getLong("BarrierDeathTime") : -1; this.currentHealth = nbt.getFloat("CurrentHealth"); if (Float.isNaN(currentHealth) || Float.isInfinite(currentHealth) || currentHealth < 0) currentHealth = barrierData.maxHealth; this.charging = nbt.hasKey("Charging") && nbt.getBoolean("Charging");