2021-05-31 02:05:41 +02:00
|
|
|
package com.jozufozu.flywheel.light;
|
2021-03-23 08:08:31 +01:00
|
|
|
|
2021-09-05 01:40:40 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2021-08-29 23:40:46 +02:00
|
|
|
import java.util.Set;
|
2021-09-05 01:40:40 +02:00
|
|
|
import java.util.stream.Stream;
|
2021-04-08 19:22:11 +02:00
|
|
|
|
2021-05-11 20:02:43 +02:00
|
|
|
import com.jozufozu.flywheel.util.WeakHashSet;
|
2021-04-08 19:22:11 +02:00
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
import it.unimi.dsi.fastutil.longs.LongSet;
|
2021-03-23 08:08:31 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.SectionPos;
|
2021-03-24 14:54:24 +01:00
|
|
|
import net.minecraft.world.IBlockDisplayReader;
|
2021-03-23 08:08:31 +01:00
|
|
|
import net.minecraft.world.LightType;
|
|
|
|
|
2021-09-05 01:40:40 +02:00
|
|
|
/**
|
|
|
|
* Keeps track of what chunks/sections each listener is in so we can update exactly what needs to be updated.
|
|
|
|
*/
|
2021-03-23 08:08:31 +01:00
|
|
|
public class LightUpdater {
|
|
|
|
|
2021-09-05 01:40:40 +02:00
|
|
|
private static final Map<IBlockDisplayReader, LightUpdater> light = new HashMap<>();
|
|
|
|
public static LightUpdater get(IBlockDisplayReader world) {
|
|
|
|
return light.computeIfAbsent(world, LightUpdater::new);
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:40:40 +02:00
|
|
|
private final LightProvider provider;
|
|
|
|
|
|
|
|
private final WeakHashSet<IMovingListener> movingListeners = new WeakHashSet<>();
|
|
|
|
private final WeakContainmentMultiMap<ILightUpdateListener> sections = new WeakContainmentMultiMap<>();
|
|
|
|
private final WeakContainmentMultiMap<ILightUpdateListener> chunks = new WeakContainmentMultiMap<>();
|
2021-03-23 08:08:31 +01:00
|
|
|
|
2021-09-05 01:40:40 +02:00
|
|
|
public LightUpdater(IBlockDisplayReader world) {
|
|
|
|
provider = BasicProvider.get(world);
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
public void tick() {
|
2021-09-05 01:40:40 +02:00
|
|
|
for (IMovingListener listener : movingListeners) {
|
|
|
|
if (listener.update(provider)) {
|
2021-08-29 23:40:46 +02:00
|
|
|
addListener(listener);
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-29 23:40:46 +02:00
|
|
|
* Add a listener.
|
|
|
|
|
2021-03-23 08:08:31 +01:00
|
|
|
* @param listener The object that wants to receive light update notifications.
|
|
|
|
*/
|
2021-08-29 23:40:46 +02:00
|
|
|
public void addListener(ILightUpdateListener listener) {
|
2021-09-05 01:40:40 +02:00
|
|
|
if (listener instanceof IMovingListener)
|
|
|
|
movingListeners.add(((IMovingListener) listener));
|
2021-08-29 23:40:46 +02:00
|
|
|
|
2021-09-05 04:57:32 +02:00
|
|
|
ReadOnlyBox box = listener.getVolume();
|
2021-08-29 23:40:46 +02:00
|
|
|
|
|
|
|
LongSet sections = this.sections.getAndResetContainment(listener);
|
|
|
|
LongSet chunks = this.chunks.getAndResetContainment(listener);
|
|
|
|
|
2021-09-05 04:57:32 +02:00
|
|
|
int minX = SectionPos.blockToSectionCoord(box.getMinX());
|
|
|
|
int minY = SectionPos.blockToSectionCoord(box.getMinY());
|
|
|
|
int minZ = SectionPos.blockToSectionCoord(box.getMinZ());
|
|
|
|
int maxX = SectionPos.blockToSectionCoord(box.getMaxX());
|
|
|
|
int maxY = SectionPos.blockToSectionCoord(box.getMaxY());
|
|
|
|
int maxZ = SectionPos.blockToSectionCoord(box.getMaxZ());
|
2021-09-05 01:40:40 +02:00
|
|
|
|
|
|
|
for (int x = minX; x <= maxX; x++) {
|
|
|
|
for (int z = minZ; z <= maxZ; z++) {
|
|
|
|
for (int y = minY; y <= maxY; y++) {
|
|
|
|
long sectionPos = SectionPos.asLong(x, y, z);
|
|
|
|
this.sections.put(sectionPos, listener);
|
|
|
|
sections.add(sectionPos);
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|
2021-09-05 01:40:40 +02:00
|
|
|
long chunkPos = SectionPos.asLong(x, 0, z);
|
|
|
|
this.chunks.put(chunkPos, listener);
|
|
|
|
chunks.add(chunkPos);
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-31 02:05:41 +02:00
|
|
|
* Dispatch light updates to all registered {@link ILightUpdateListener}s.
|
2021-04-30 02:19:08 +02:00
|
|
|
* @param type The type of light that changed.
|
2021-03-23 08:08:31 +01:00
|
|
|
* @param sectionPos A long representing the section position where light changed.
|
|
|
|
*/
|
2021-09-05 01:40:40 +02:00
|
|
|
public void onLightUpdate(LightType type, long sectionPos) {
|
2021-08-29 23:40:46 +02:00
|
|
|
Set<ILightUpdateListener> set = sections.get(sectionPos);
|
2021-03-23 08:08:31 +01:00
|
|
|
|
|
|
|
if (set == null || set.isEmpty()) return;
|
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
set.removeIf(l -> l.status().shouldRemove());
|
|
|
|
|
2021-09-05 04:57:32 +02:00
|
|
|
ReadOnlyBox chunkBox = GridAlignedBB.from(SectionPos.of(sectionPos));
|
2021-03-23 08:08:31 +01:00
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
for (ILightUpdateListener listener : set) {
|
2021-09-05 04:57:32 +02:00
|
|
|
listener.onLightUpdate(provider, type, chunkBox);
|
2021-08-29 23:40:46 +02:00
|
|
|
}
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-31 02:05:41 +02:00
|
|
|
* Dispatch light updates to all registered {@link ILightUpdateListener}s
|
2021-03-23 08:08:31 +01:00
|
|
|
* when the server sends lighting data for an entire chunk.
|
|
|
|
*
|
|
|
|
*/
|
2021-09-05 01:40:40 +02:00
|
|
|
public void onLightPacket(int chunkX, int chunkZ) {
|
2021-03-23 08:08:31 +01:00
|
|
|
long chunkPos = SectionPos.asLong(chunkX, 0, chunkZ);
|
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
Set<ILightUpdateListener> set = chunks.get(chunkPos);
|
2021-03-23 08:08:31 +01:00
|
|
|
|
|
|
|
if (set == null || set.isEmpty()) return;
|
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
set.removeIf(l -> l.status().shouldRemove());
|
2021-03-23 08:08:31 +01:00
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
for (ILightUpdateListener listener : set) {
|
2021-09-05 01:40:40 +02:00
|
|
|
listener.onLightPacket(provider, chunkX, chunkZ);
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
public static long blockToSection(BlockPos pos) {
|
2021-03-23 08:08:31 +01:00
|
|
|
return SectionPos.asLong(pos.getX(), pos.getY(), pos.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long sectionToChunk(long sectionPos) {
|
|
|
|
return sectionPos & 0xFFFFFFFFFFF_00000L;
|
|
|
|
}
|
2021-09-05 01:40:40 +02:00
|
|
|
|
2021-09-05 04:57:32 +02:00
|
|
|
public Stream<ReadOnlyBox> getAllBoxes() {
|
2021-09-05 01:40:40 +02:00
|
|
|
return chunks.stream().map(ILightUpdateListener::getVolume);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return chunks.isEmpty();
|
|
|
|
}
|
2021-03-23 08:08:31 +01:00
|
|
|
}
|