From e3d1f8c97381438f9b1a196e393d45c61010617a Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 1 Jan 2022 19:14:37 +0200 Subject: [PATCH] Fixed light sources not being removed after light-emitting entities are removed --- .../unicopia/entity/CastSpellEntity.java | 5 +- .../unicopia/entity/DynamicLightSource.java | 57 +++++++++++++++ .../unicopia/entity/FairyEntity.java | 8 ++- .../unicopia/entity/LightEmittingEntity.java | 70 ++++++------------- 4 files changed, 86 insertions(+), 54 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/entity/DynamicLightSource.java diff --git a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java index 61f4e579..e76b6361 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java @@ -25,7 +25,7 @@ import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.world.World; -public class CastSpellEntity extends Entity implements Caster, LightEmittingEntity { +public class CastSpellEntity extends LightEmittingEntity implements Caster { private static final TrackedData GRAVITY = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.FLOAT); private static final TrackedData> SPELL = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID); @@ -54,7 +54,6 @@ public class CastSpellEntity extends Entity implements Caster, Lig }; private final EntityReference owner = new EntityReference<>(); - private final LightEmitter emitter = new LightEmitter<>(this); private int orphanedTicks; @@ -101,8 +100,6 @@ public class CastSpellEntity extends Entity implements Caster, Lig orphanedTicks = 0; - emitter.tick(); - if (dataTracker.get(SPELL).filter(spellId -> { return getSpellSlot().forEach(spell -> { return spell.getUuid().equals(spellId) ? Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)) : Operation.SKIP; diff --git a/src/main/java/com/minelittlepony/unicopia/entity/DynamicLightSource.java b/src/main/java/com/minelittlepony/unicopia/entity/DynamicLightSource.java new file mode 100644 index 00000000..fd2b3a31 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/entity/DynamicLightSource.java @@ -0,0 +1,57 @@ +package com.minelittlepony.unicopia.entity; + +import org.jetbrains.annotations.Nullable; + +import net.minecraft.entity.Entity; +import net.minecraft.util.math.BlockPos; + +public interface DynamicLightSource { + int getLightLevel(); + + static final class LightEmitter { + @Nullable + private BlockPos lastPos; + + private final T entity; + + LightEmitter(T entity) { + this.entity = entity; + } + + @SuppressWarnings("deprecation") + void tick() { + if (entity.world.isClient) { + if (entity.isRemoved()) { + remove(); + return; + } + + int light = entity.getLightLevel(); + + if (light <= 0) { + return; + } + + BlockPos currentPos = entity.getBlockPos(); + + if (!currentPos.equals(lastPos) && entity.world.isChunkLoaded(currentPos)) { + try { + if (lastPos != null) { + entity.world.getLightingProvider().checkBlock(lastPos); + } + entity.world.getLightingProvider().addLightSource(currentPos, light); + lastPos = currentPos; + } catch (Exception ignored) { } + } + } + } + + void remove() { + if (entity.world.isClient && lastPos != null) { + try { + entity.world.getLightingProvider().checkBlock(lastPos); + } catch (Exception ignored) {} + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/unicopia/entity/FairyEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/FairyEntity.java index c8b93e00..f4018d77 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/FairyEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/FairyEntity.java @@ -42,7 +42,7 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import net.minecraft.world.event.GameEvent; -public class FairyEntity extends PathAwareEntity implements LightEmittingEntity, Owned { +public class FairyEntity extends PathAwareEntity implements DynamicLightSource, Owned { private final EntityReference owner = new EntityReference<>(); private final EntityReference assignment = new EntityReference<>(); @@ -215,6 +215,12 @@ public class FairyEntity extends PathAwareEntity implements LightEmittingEntity, return false; } + @Override + public void setRemoved(RemovalReason reason) { + super.setRemoved(reason); + emitter.remove(); + } + @Override public void onRemoved() { super.onRemoved(); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/LightEmittingEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/LightEmittingEntity.java index ed687b32..f2b29f43 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/LightEmittingEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/LightEmittingEntity.java @@ -1,59 +1,31 @@ package com.minelittlepony.unicopia.entity; import net.minecraft.entity.Entity; -import net.minecraft.util.math.BlockPos; +import net.minecraft.entity.EntityType; +import net.minecraft.world.World; -public interface LightEmittingEntity { +public abstract class LightEmittingEntity extends Entity implements DynamicLightSource { + private final LightEmitter emitter = new LightEmitter<>(this); - int getLightLevel(); + public LightEmittingEntity(EntityType type, World world) { + super(type, world); + } - static class LightEmitter { - private BlockPos lastPos; + @Override + public void tick() { + super.tick(); + emitter.tick(); + } - private final T entity; + @Override + public void setRemoved(RemovalReason reason) { + super.setRemoved(reason); + emitter.remove(); + } - public LightEmitter(T entity) { - this.entity = entity; - } - - @SuppressWarnings("deprecation") - public void tick() { - - if (entity.isRemoved()) { - remove(); - return; - } - - if (entity.world.isClient) { - int light = entity.getLightLevel(); - - if (light <= 0) { - return; - } - - BlockPos currentPos = entity.getBlockPos(); - - if (entity.world.isChunkLoaded(currentPos)) { - try { - if (lastPos != null && !currentPos.equals(lastPos)) { - entity.world.getLightingProvider().checkBlock(lastPos); - } - - entity.world.getLightingProvider().addLightSource(currentPos, light); - } catch (Exception ignored) { - } - - lastPos = currentPos; - } - } - } - - void remove() { - if (entity.world.isClient) { - try { - entity.world.getLightingProvider().checkBlock(entity.getBlockPos()); - } catch (Exception ignored) {} - } - } + @Override + public void onRemoved() { + super.onRemoved(); + emitter.remove(); } }