mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-03-23 03:27:12 +01:00
Fixed light sources not being removed after light-emitting entities are removed
This commit is contained in:
parent
63f70faab1
commit
e3d1f8c973
4 changed files with 86 additions and 54 deletions
|
@ -25,7 +25,7 @@ import net.minecraft.text.Text;
|
||||||
import net.minecraft.text.TranslatableText;
|
import net.minecraft.text.TranslatableText;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public class CastSpellEntity extends Entity implements Caster<LivingEntity>, LightEmittingEntity {
|
public class CastSpellEntity extends LightEmittingEntity implements Caster<LivingEntity> {
|
||||||
private static final TrackedData<Float> GRAVITY = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
private static final TrackedData<Float> GRAVITY = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
||||||
private static final TrackedData<Optional<UUID>> SPELL = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID);
|
private static final TrackedData<Optional<UUID>> SPELL = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID);
|
||||||
|
|
||||||
|
@ -54,7 +54,6 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity>, Lig
|
||||||
};
|
};
|
||||||
|
|
||||||
private final EntityReference<LivingEntity> owner = new EntityReference<>();
|
private final EntityReference<LivingEntity> owner = new EntityReference<>();
|
||||||
private final LightEmitter<?> emitter = new LightEmitter<>(this);
|
|
||||||
|
|
||||||
private int orphanedTicks;
|
private int orphanedTicks;
|
||||||
|
|
||||||
|
@ -101,8 +100,6 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity>, Lig
|
||||||
|
|
||||||
orphanedTicks = 0;
|
orphanedTicks = 0;
|
||||||
|
|
||||||
emitter.tick();
|
|
||||||
|
|
||||||
if (dataTracker.get(SPELL).filter(spellId -> {
|
if (dataTracker.get(SPELL).filter(spellId -> {
|
||||||
return getSpellSlot().forEach(spell -> {
|
return getSpellSlot().forEach(spell -> {
|
||||||
return spell.getUuid().equals(spellId) ? Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)) : Operation.SKIP;
|
return spell.getUuid().equals(spellId) ? Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)) : Operation.SKIP;
|
||||||
|
|
|
@ -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<T extends Entity & DynamicLightSource> {
|
||||||
|
@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) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -42,7 +42,7 @@ import net.minecraft.util.math.Vec3d;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraft.world.event.GameEvent;
|
import net.minecraft.world.event.GameEvent;
|
||||||
|
|
||||||
public class FairyEntity extends PathAwareEntity implements LightEmittingEntity, Owned<LivingEntity> {
|
public class FairyEntity extends PathAwareEntity implements DynamicLightSource, Owned<LivingEntity> {
|
||||||
private final EntityReference<LivingEntity> owner = new EntityReference<>();
|
private final EntityReference<LivingEntity> owner = new EntityReference<>();
|
||||||
|
|
||||||
private final EntityReference<LivingEntity> assignment = new EntityReference<>();
|
private final EntityReference<LivingEntity> assignment = new EntityReference<>();
|
||||||
|
@ -215,6 +215,12 @@ public class FairyEntity extends PathAwareEntity implements LightEmittingEntity,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRemoved(RemovalReason reason) {
|
||||||
|
super.setRemoved(reason);
|
||||||
|
emitter.remove();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRemoved() {
|
public void onRemoved() {
|
||||||
super.onRemoved();
|
super.onRemoved();
|
||||||
|
|
|
@ -1,59 +1,31 @@
|
||||||
package com.minelittlepony.unicopia.entity;
|
package com.minelittlepony.unicopia.entity;
|
||||||
|
|
||||||
import net.minecraft.entity.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<T extends Entity & LightEmittingEntity> {
|
|
||||||
private BlockPos lastPos;
|
|
||||||
|
|
||||||
private final T entity;
|
|
||||||
|
|
||||||
public LightEmitter(T entity) {
|
|
||||||
this.entity = entity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@Override
|
||||||
public void tick() {
|
public void tick() {
|
||||||
|
super.tick();
|
||||||
if (entity.isRemoved()) {
|
emitter.tick();
|
||||||
remove();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entity.world.isClient) {
|
@Override
|
||||||
int light = entity.getLightLevel();
|
public void setRemoved(RemovalReason reason) {
|
||||||
|
super.setRemoved(reason);
|
||||||
if (light <= 0) {
|
emitter.remove();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockPos currentPos = entity.getBlockPos();
|
@Override
|
||||||
|
public void onRemoved() {
|
||||||
if (entity.world.isChunkLoaded(currentPos)) {
|
super.onRemoved();
|
||||||
try {
|
emitter.remove();
|
||||||
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) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue