diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java index be0c09a4a..5c9a6fdd5 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java @@ -21,9 +21,10 @@ import com.jozufozu.flywheel.api.visualization.VisualizationLevel; import com.jozufozu.flywheel.api.visualization.VisualizationManager; import com.jozufozu.flywheel.extension.LevelExtension; import com.jozufozu.flywheel.impl.task.FlwTaskExecutor; -import com.jozufozu.flywheel.impl.visualization.manager.BlockEntityVisualManager; -import com.jozufozu.flywheel.impl.visualization.manager.EffectVisualManager; -import com.jozufozu.flywheel.impl.visualization.manager.EntityVisualManager; +import com.jozufozu.flywheel.impl.visualization.manager.BlockEntityStorage; +import com.jozufozu.flywheel.impl.visualization.manager.EffectStorage; +import com.jozufozu.flywheel.impl.visualization.manager.EntityStorage; +import com.jozufozu.flywheel.impl.visualization.manager.VisualManagerImpl; import com.jozufozu.flywheel.lib.task.Flag; import com.jozufozu.flywheel.lib.task.IfElsePlan; import com.jozufozu.flywheel.lib.task.MapContextPlan; @@ -49,9 +50,9 @@ public class VisualizationManagerImpl implements VisualizationManager { private final Engine engine; private final TaskExecutor taskExecutor; - private final BlockEntityVisualManager blockEntities; - private final EntityVisualManager entities; - private final EffectVisualManager effects; + private final VisualManagerImpl blockEntities; + private final VisualManagerImpl entities; + private final VisualManagerImpl effects; private final Plan tickPlan; private final Plan framePlan; @@ -65,9 +66,9 @@ public class VisualizationManagerImpl implements VisualizationManager { .createEngine(level); taskExecutor = FlwTaskExecutor.get(); - blockEntities = new BlockEntityVisualManager(engine); - entities = new EntityVisualManager(engine); - effects = new EffectVisualManager(engine); + blockEntities = new VisualManagerImpl<>(new BlockEntityStorage(engine)); + entities = new VisualManagerImpl<>(new EntityStorage(engine)); + effects = new VisualManagerImpl<>(new EffectStorage(engine)); tickPlan = blockEntities.createTickPlan() .and(entities.createTickPlan()) @@ -221,7 +222,8 @@ public class VisualizationManagerImpl implements VisualizationManager { continue; } - var visual = blockEntities.visualAtPos(entry.getLongKey()); + var visual = blockEntities.getStorage() + .visualAtPos(entry.getLongKey()); if (visual == null) { // The block doesn't have a visual, this is probably the common case. diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/BlockEntityStorage.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/BlockEntityStorage.java new file mode 100644 index 000000000..4b1350158 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/BlockEntityStorage.java @@ -0,0 +1,76 @@ +package com.jozufozu.flywheel.impl.visualization.manager; + +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.api.backend.Engine; +import com.jozufozu.flywheel.api.visual.BlockEntityVisual; +import com.jozufozu.flywheel.api.visual.Visual; +import com.jozufozu.flywheel.api.visualization.VisualizationContext; +import com.jozufozu.flywheel.impl.visualization.VisualizationHelper; +import com.jozufozu.flywheel.impl.visualization.storage.Storage; + +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.BlockGetter; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; + +public class BlockEntityStorage extends Storage { + private final Long2ObjectMap> posLookup = new Long2ObjectOpenHashMap<>(); + + public BlockEntityStorage(Engine engine) { + super(engine); + } + + public BlockEntityVisual visualAtPos(long pos) { + return posLookup.get(pos); + } + + @Override + public boolean willAccept(BlockEntity blockEntity) { + if (blockEntity.isRemoved()) { + return false; + } + + if (!VisualizationHelper.canVisualize(blockEntity)) { + return false; + } + + Level level = blockEntity.getLevel(); + if (level == null) { + return false; + } + + if (level.isEmptyBlock(blockEntity.getBlockPos())) { + return false; + } + + BlockPos pos = blockEntity.getBlockPos(); + BlockGetter existingChunk = level.getChunkForCollisions(pos.getX() >> 4, pos.getZ() >> 4); + return existingChunk != null; + } + + @Override + @Nullable + protected Visual createRaw(BlockEntity obj) { + var visualizer = VisualizationHelper.getVisualizer(obj); + if (visualizer == null) { + return null; + } + + var visual = visualizer.createVisual(new VisualizationContext(engine, engine.renderOrigin()), obj); + + BlockPos blockPos = obj.getBlockPos(); + posLookup.put(blockPos.asLong(), visual); + + return visual; + } + + @Override + public void remove(BlockEntity obj) { + super.remove(obj); + posLookup.remove(obj.getBlockPos() + .asLong()); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/BlockEntityVisualManager.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/BlockEntityVisualManager.java deleted file mode 100644 index 428046f18..000000000 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/BlockEntityVisualManager.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.jozufozu.flywheel.impl.visualization.manager; - -import org.jetbrains.annotations.Nullable; - -import com.jozufozu.flywheel.api.backend.Engine; -import com.jozufozu.flywheel.api.visual.BlockEntityVisual; -import com.jozufozu.flywheel.api.visual.Visual; -import com.jozufozu.flywheel.api.visualization.VisualizationContext; -import com.jozufozu.flywheel.impl.visualization.VisualizationHelper; -import com.jozufozu.flywheel.impl.visualization.storage.Storage; - -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; -import net.minecraft.core.BlockPos; -import net.minecraft.world.level.BlockGetter; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.entity.BlockEntity; - -public class BlockEntityVisualManager extends AbstractVisualManager { - private final BlockEntityStorage storage; - - public BlockEntityVisualManager(Engine engine) { - storage = new BlockEntityStorage(engine); - } - - @Override - protected Storage getStorage() { - return storage; - } - - public BlockEntityVisual visualAtPos(long pos) { - return storage.posLookup.get(pos); - } - - private static class BlockEntityStorage extends Storage { - private final Long2ObjectMap> posLookup = new Long2ObjectOpenHashMap<>(); - - public BlockEntityStorage(Engine engine) { - super(engine); - } - - @Override - public boolean willAccept(BlockEntity blockEntity) { - if (blockEntity.isRemoved()) { - return false; - } - - if (!VisualizationHelper.canVisualize(blockEntity)) { - return false; - } - - Level level = blockEntity.getLevel(); - if (level == null) { - return false; - } - - if (level.isEmptyBlock(blockEntity.getBlockPos())) { - return false; - } - - BlockPos pos = blockEntity.getBlockPos(); - BlockGetter existingChunk = level.getChunkForCollisions(pos.getX() >> 4, pos.getZ() >> 4); - return existingChunk != null; - } - - @Override - @Nullable - protected Visual createRaw(BlockEntity obj) { - var visualizer = VisualizationHelper.getVisualizer(obj); - if (visualizer == null) { - return null; - } - - var visual = visualizer.createVisual(new VisualizationContext(engine, engine.renderOrigin()), obj); - - BlockPos blockPos = obj.getBlockPos(); - posLookup.put(blockPos.asLong(), visual); - - return visual; - } - - @Override - public void remove(BlockEntity obj) { - super.remove(obj); - posLookup.remove(obj.getBlockPos().asLong()); - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EffectStorage.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EffectStorage.java new file mode 100644 index 000000000..50a91fbb9 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EffectStorage.java @@ -0,0 +1,23 @@ +package com.jozufozu.flywheel.impl.visualization.manager; + +import com.jozufozu.flywheel.api.backend.Engine; +import com.jozufozu.flywheel.api.visual.Effect; +import com.jozufozu.flywheel.api.visual.EffectVisual; +import com.jozufozu.flywheel.api.visualization.VisualizationContext; +import com.jozufozu.flywheel.impl.visualization.storage.Storage; + +public class EffectStorage extends Storage { + public EffectStorage(Engine engine) { + super(engine); + } + + @Override + protected EffectVisual createRaw(Effect obj) { + return obj.visualize(new VisualizationContext(engine, engine.renderOrigin())); + } + + @Override + public boolean willAccept(Effect obj) { + return true; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EffectVisualManager.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EffectVisualManager.java deleted file mode 100644 index 9814578ac..000000000 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EffectVisualManager.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.jozufozu.flywheel.impl.visualization.manager; - -import com.jozufozu.flywheel.api.backend.Engine; -import com.jozufozu.flywheel.api.visual.Effect; -import com.jozufozu.flywheel.api.visual.EffectVisual; -import com.jozufozu.flywheel.api.visualization.VisualizationContext; -import com.jozufozu.flywheel.impl.visualization.storage.Storage; - -public class EffectVisualManager extends AbstractVisualManager { - private final EffectStorage storage; - - public EffectVisualManager(Engine engine) { - storage = new EffectStorage(engine); - } - - @Override - protected Storage getStorage() { - return storage; - } - - private static class EffectStorage extends Storage { - public EffectStorage(Engine engine) { - super(engine); - } - - @Override - protected EffectVisual createRaw(Effect obj) { - return obj.visualize(new VisualizationContext(engine, engine.renderOrigin())); - } - - @Override - public boolean willAccept(Effect obj) { - return true; - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EntityStorage.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EntityStorage.java new file mode 100644 index 000000000..a79d12ed4 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EntityStorage.java @@ -0,0 +1,43 @@ +package com.jozufozu.flywheel.impl.visualization.manager; + +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.api.backend.Engine; +import com.jozufozu.flywheel.api.visual.Visual; +import com.jozufozu.flywheel.api.visualization.VisualizationContext; +import com.jozufozu.flywheel.impl.visualization.VisualizationHelper; +import com.jozufozu.flywheel.impl.visualization.storage.Storage; + +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.Level; + +public class EntityStorage extends Storage { + public EntityStorage(Engine engine) { + super(engine); + } + + @Override + @Nullable + protected Visual createRaw(Entity obj) { + var visualizer = VisualizationHelper.getVisualizer(obj); + if (visualizer == null) { + return null; + } + + return visualizer.createVisual(new VisualizationContext(engine, engine.renderOrigin()), obj); + } + + @Override + public boolean willAccept(Entity entity) { + if (!entity.isAlive()) { + return false; + } + + if (!VisualizationHelper.canVisualize(entity)) { + return false; + } + + Level level = entity.level(); + return level != null; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EntityVisualManager.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EntityVisualManager.java deleted file mode 100644 index b12c9baf8..000000000 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/EntityVisualManager.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.jozufozu.flywheel.impl.visualization.manager; - -import org.jetbrains.annotations.Nullable; - -import com.jozufozu.flywheel.api.backend.Engine; -import com.jozufozu.flywheel.api.visual.Visual; -import com.jozufozu.flywheel.api.visualization.VisualizationContext; -import com.jozufozu.flywheel.impl.visualization.VisualizationHelper; -import com.jozufozu.flywheel.impl.visualization.storage.Storage; - -import net.minecraft.world.entity.Entity; -import net.minecraft.world.level.Level; - -public class EntityVisualManager extends AbstractVisualManager { - private final EntityStorage storage; - - public EntityVisualManager(Engine engine) { - storage = new EntityStorage(engine); - } - - @Override - protected Storage getStorage() { - return storage; - } - - private static class EntityStorage extends Storage { - public EntityStorage(Engine engine) { - super(engine); - } - - @Override - @Nullable - protected Visual createRaw(Entity obj) { - var visualizer = VisualizationHelper.getVisualizer(obj); - if (visualizer == null) { - return null; - } - - return visualizer.createVisual(new VisualizationContext(engine, engine.renderOrigin()), obj); - } - - @Override - public boolean willAccept(Entity entity) { - if (!entity.isAlive()) { - return false; - } - - if (!VisualizationHelper.canVisualize(entity)) { - return false; - } - - Level level = entity.level(); - return level != null; - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/AbstractVisualManager.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/VisualManagerImpl.java similarity index 93% rename from src/main/java/com/jozufozu/flywheel/impl/visualization/manager/AbstractVisualManager.java rename to src/main/java/com/jozufozu/flywheel/impl/visualization/manager/VisualManagerImpl.java index 9093e9fae..d08b9c08c 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/AbstractVisualManager.java +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/manager/VisualManagerImpl.java @@ -18,18 +18,23 @@ import com.jozufozu.flywheel.impl.visualization.storage.Transaction; import com.jozufozu.flywheel.lib.task.MapContextPlan; import com.jozufozu.flywheel.lib.task.SimplePlan; -public abstract class AbstractVisualManager implements VisualManager { +public class VisualManagerImpl> implements VisualManager { private final Queue> queue = new ConcurrentLinkedQueue<>(); protected DistanceUpdateLimiterImpl tickLimiter; protected DistanceUpdateLimiterImpl frameLimiter; - public AbstractVisualManager() { + protected final S storage; + + public VisualManagerImpl(S storage) { tickLimiter = createUpdateLimiter(); frameLimiter = createUpdateLimiter(); + this.storage = storage; } - protected abstract Storage getStorage(); + public S getStorage() { + return storage; + } protected DistanceUpdateLimiterImpl createUpdateLimiter() { if (FlwConfig.get()