2022-01-01 18:14:37 +01:00
|
|
|
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;
|
|
|
|
|
2022-01-01 18:14:37 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-01-01 18:14:37 +01:00
|
|
|
|
|
|
|
static final class LightEmitter<T extends Entity & DynamicLightSource> {
|
|
|
|
@Nullable
|
|
|
|
private BlockPos lastPos;
|
|
|
|
|
|
|
|
private final T entity;
|
|
|
|
|
2023-09-02 19:34:57 +02:00
|
|
|
public LightEmitter(T entity) {
|
2022-01-01 18:14:37 +01:00
|
|
|
this.entity = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
2023-09-02 19:34:57 +02:00
|
|
|
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) { }
|
2022-01-01 18:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 19:34:57 +02:00
|
|
|
public void remove() {
|
2023-06-07 21:35:29 +02:00
|
|
|
LightSources.get(entity.getWorld()).removeLightSource(entity);
|
|
|
|
if (lastPos != null) {
|
2022-01-01 18:14:37 +01:00
|
|
|
try {
|
2023-06-03 13:40:54 +02:00
|
|
|
entity.getWorld().getLightingProvider().checkBlock(lastPos);
|
2022-01-01 18:14:37 +01:00
|
|
|
} catch (Exception ignored) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|