Unicopia/src/main/java/com/minelittlepony/unicopia/entity/DynamicLightSource.java

62 lines
1.7 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
import org.jetbrains.annotations.Nullable;
2023-06-07 21:35:29 +02:00
import com.minelittlepony.unicopia.server.world.LightSources;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
public interface DynamicLightSource {
2022-03-26 20:34:15 +01:00
default int getLightLevel() {
return 0;
}
static final class LightEmitter<T extends Entity & DynamicLightSource> {
@Nullable
private BlockPos lastPos;
private final T entity;
public LightEmitter(T entity) {
this.entity = entity;
}
@SuppressWarnings("deprecation")
public void tick() {
2023-06-07 21:35:29 +02:00
if (entity.isRemoved()) {
remove();
return;
}
int light = entity.getLightLevel();
if (light <= 0) {
return;
}
BlockPos currentPos = entity.getBlockPos();
if (!currentPos.equals(lastPos) && entity.getWorld().isChunkLoaded(currentPos)) {
LightSources.get(entity.getWorld()).addLightSource(entity);
try {
if (lastPos != null) {
entity.getWorld().getLightingProvider().checkBlock(lastPos);
entity.getWorld().getLightingProvider().checkBlock(currentPos);
}
lastPos = currentPos;
} catch (Exception ignored) { }
}
}
public void remove() {
2023-06-07 21:35:29 +02:00
LightSources.get(entity.getWorld()).removeLightSource(entity);
if (lastPos != null) {
try {
2023-06-03 13:40:54 +02:00
entity.getWorld().getLightingProvider().checkBlock(lastPos);
} catch (Exception ignored) {}
}
}
}
}