diff --git a/common/src/api/java/dev/engine_room/flywheel/api/visual/Effect.java b/common/src/api/java/dev/engine_room/flywheel/api/visual/Effect.java index d3ed43cf2..446038942 100644 --- a/common/src/api/java/dev/engine_room/flywheel/api/visual/Effect.java +++ b/common/src/api/java/dev/engine_room/flywheel/api/visual/Effect.java @@ -1,7 +1,5 @@ package dev.engine_room.flywheel.api.visual; -import java.util.List; - import dev.engine_room.flywheel.api.visualization.VisualizationContext; /** @@ -16,5 +14,5 @@ public interface Effect { * @param ctx The visualization context. * @return An arbitrary EffectVisual. */ - List> visualize(VisualizationContext ctx, float partialTick); + EffectVisual visualize(VisualizationContext ctx, float partialTick); } diff --git a/common/src/api/java/dev/engine_room/flywheel/api/visualization/BlockEntityVisualizer.java b/common/src/api/java/dev/engine_room/flywheel/api/visualization/BlockEntityVisualizer.java index 41b5f99e9..6d18aca1f 100644 --- a/common/src/api/java/dev/engine_room/flywheel/api/visualization/BlockEntityVisualizer.java +++ b/common/src/api/java/dev/engine_room/flywheel/api/visualization/BlockEntityVisualizer.java @@ -1,7 +1,5 @@ package dev.engine_room.flywheel.api.visualization; -import java.util.List; - import dev.engine_room.flywheel.api.visual.BlockEntityVisual; import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; import net.minecraft.world.level.block.entity.BlockEntity; @@ -18,7 +16,7 @@ public interface BlockEntityVisualizer { * @param blockEntity The block entity to construct a visual for. * @return The visual. */ - List> createVisual(VisualizationContext ctx, T blockEntity, float partialTick); + BlockEntityVisual createVisual(VisualizationContext ctx, T blockEntity, float partialTick); /** * Checks if the given block entity should not be rendered with the vanilla {@link BlockEntityRenderer}. diff --git a/common/src/api/java/dev/engine_room/flywheel/api/visualization/EntityVisualizer.java b/common/src/api/java/dev/engine_room/flywheel/api/visualization/EntityVisualizer.java index 9e398da27..bee36c02e 100644 --- a/common/src/api/java/dev/engine_room/flywheel/api/visualization/EntityVisualizer.java +++ b/common/src/api/java/dev/engine_room/flywheel/api/visualization/EntityVisualizer.java @@ -1,7 +1,5 @@ package dev.engine_room.flywheel.api.visualization; -import java.util.List; - import dev.engine_room.flywheel.api.visual.EntityVisual; import net.minecraft.client.renderer.entity.EntityRenderer; import net.minecraft.world.entity.Entity; @@ -18,7 +16,7 @@ public interface EntityVisualizer { * @param entity The entity to construct a visual for. * @return The visual. */ - List> createVisual(VisualizationContext ctx, T entity, float partialTick); + EntityVisual createVisual(VisualizationContext ctx, T entity, float partialTick); /** * Checks if the given entity should not render with the vanilla {@link EntityRenderer}. diff --git a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleBlockEntityVisualizer.java b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleBlockEntityVisualizer.java index d3c302d45..c80d2fbb9 100644 --- a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleBlockEntityVisualizer.java +++ b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleBlockEntityVisualizer.java @@ -1,6 +1,5 @@ package dev.engine_room.flywheel.lib.visual; -import java.util.List; import java.util.Objects; import java.util.function.Predicate; @@ -24,8 +23,8 @@ public SimpleBlockEntityVisualizer(Factory visualFactory, Predicate skipVa } @Override - public List> createVisual(VisualizationContext ctx, T blockEntity, float partialTick) { - return List.of(visualFactory.create(ctx, blockEntity, partialTick)); + public BlockEntityVisual createVisual(VisualizationContext ctx, T blockEntity, float partialTick) { + return visualFactory.create(ctx, blockEntity, partialTick); } @Override diff --git a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleEntityVisualizer.java b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleEntityVisualizer.java index 4757dc6ff..a5bd479ca 100644 --- a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleEntityVisualizer.java +++ b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/SimpleEntityVisualizer.java @@ -1,6 +1,5 @@ package dev.engine_room.flywheel.lib.visual; -import java.util.List; import java.util.Objects; import java.util.function.Predicate; @@ -24,8 +23,8 @@ public SimpleEntityVisualizer(Factory visualFactory, Predicate skipVanilla } @Override - public List> createVisual(VisualizationContext ctx, T entity, float partialTick) { - return List.of(visualFactory.create(ctx, entity, partialTick)); + public EntityVisual createVisual(VisualizationContext ctx, T entity, float partialTick) { + return visualFactory.create(ctx, entity, partialTick); } @Override diff --git a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/VisualizationManagerImpl.java b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/VisualizationManagerImpl.java index c9ac5d420..e0accaf2d 100644 --- a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/VisualizationManagerImpl.java +++ b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/VisualizationManagerImpl.java @@ -17,7 +17,6 @@ import dev.engine_room.flywheel.api.instance.Instance; import dev.engine_room.flywheel.api.task.Plan; import dev.engine_room.flywheel.api.task.TaskExecutor; -import dev.engine_room.flywheel.api.visual.BlockEntityVisual; import dev.engine_room.flywheel.api.visual.DynamicVisual; import dev.engine_room.flywheel.api.visual.Effect; import dev.engine_room.flywheel.api.visual.TickableVisual; @@ -262,23 +261,21 @@ public void renderCrumbling(RenderContext context, Long2ObjectMap instances = new ArrayList<>(); - for (BlockEntityVisual visual : visualList) { - visual.collectCrumblingInstances(instance -> { - if (instance != null) { - instances.add(instance); - } - }); - } + visual.collectCrumblingInstances(instance -> { + if (instance != null) { + instances.add(instance); + } + }); if (instances.isEmpty()) { // The visual doesn't want to render anything crumbling. diff --git a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/BlockEntityStorage.java b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/BlockEntityStorage.java index d89273dcc..db465f461 100644 --- a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/BlockEntityStorage.java +++ b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/BlockEntityStorage.java @@ -1,6 +1,5 @@ package dev.engine_room.flywheel.impl.visualization.manager; -import java.util.List; import java.util.function.Supplier; import org.jetbrains.annotations.Nullable; @@ -17,14 +16,14 @@ import net.minecraft.world.level.block.entity.BlockEntity; public class BlockEntityStorage extends Storage { - private final Long2ObjectMap>> posLookup = new Long2ObjectOpenHashMap<>(); + private final Long2ObjectMap> posLookup = new Long2ObjectOpenHashMap<>(); public BlockEntityStorage(Supplier visualizationContextSupplier) { super(visualizationContextSupplier); } @Nullable - public List> visualAtPos(long pos) { + public BlockEntityVisual visualAtPos(long pos) { return posLookup.get(pos); } @@ -53,18 +52,19 @@ public boolean willAccept(BlockEntity blockEntity) { } @Override - protected List> createRaw(BlockEntity obj, float partialTick) { + @Nullable + protected BlockEntityVisual createRaw(BlockEntity obj, float partialTick) { var visualizer = VisualizationHelper.getVisualizer(obj); if (visualizer == null) { - return List.of(); + return null; } - var visualList = visualizer.createVisual(visualizationContextSupplier.get(), obj, partialTick); + var visual = visualizer.createVisual(visualizationContextSupplier.get(), obj, partialTick); BlockPos blockPos = obj.getBlockPos(); - posLookup.put(blockPos.asLong(), visualList); + posLookup.put(blockPos.asLong(), visual); - return visualList; + return visual; } @Override diff --git a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EffectStorage.java b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EffectStorage.java index a59ef7e85..dbe3e3942 100644 --- a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EffectStorage.java +++ b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EffectStorage.java @@ -1,6 +1,5 @@ package dev.engine_room.flywheel.impl.visualization.manager; -import java.util.List; import java.util.function.Supplier; import dev.engine_room.flywheel.api.visual.Effect; @@ -14,7 +13,7 @@ public EffectStorage(Supplier visualizationContextSupplier } @Override - protected List> createRaw(Effect obj, float partialTick) { + protected EffectVisual createRaw(Effect obj, float partialTick) { return obj.visualize(visualizationContextSupplier.get(), partialTick); } diff --git a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EntityStorage.java b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EntityStorage.java index eed2e0f2d..5a4cc18e3 100644 --- a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EntityStorage.java +++ b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/EntityStorage.java @@ -1,6 +1,5 @@ package dev.engine_room.flywheel.impl.visualization.manager; -import java.util.List; import java.util.function.Supplier; import dev.engine_room.flywheel.api.visual.EntityVisual; @@ -16,10 +15,10 @@ public EntityStorage(Supplier visualizationContextSupplier } @Override - protected List> createRaw(Entity obj, float partialTick) { + protected EntityVisual createRaw(Entity obj, float partialTick) { var visualizer = VisualizationHelper.getVisualizer(obj); if (visualizer == null) { - return List.of(); + return null; } return visualizer.createVisual(visualizationContextSupplier.get(), obj, partialTick); diff --git a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/VisualManagerImpl.java b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/VisualManagerImpl.java index ea385a634..3abc3c7ac 100644 --- a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/VisualManagerImpl.java +++ b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/manager/VisualManagerImpl.java @@ -26,7 +26,8 @@ public S getStorage() { @Override public int getVisualCount() { - return getStorage().visualCount(); + return getStorage().getAllVisuals() + .size(); } @Override diff --git a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/storage/Storage.java b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/storage/Storage.java index 58c8ce42b..b599dc26e 100644 --- a/common/src/main/java/dev/engine_room/flywheel/impl/visualization/storage/Storage.java +++ b/common/src/main/java/dev/engine_room/flywheel/impl/visualization/storage/Storage.java @@ -1,10 +1,13 @@ package dev.engine_room.flywheel.impl.visualization.storage; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.function.Supplier; +import org.jetbrains.annotations.Nullable; + import dev.engine_room.flywheel.api.task.Plan; import dev.engine_room.flywheel.api.visual.DynamicVisual; import dev.engine_room.flywheel.api.visual.LitVisual; @@ -26,50 +29,59 @@ public abstract class Storage { protected final List simpleTickableVisuals = new ArrayList<>(); protected final LitVisualStorage litVisuals = new LitVisualStorage(); - private final Map> visuals = new Reference2ObjectOpenHashMap<>(); + private final Map visuals = new Reference2ObjectOpenHashMap<>(); public Storage(Supplier visualizationContextSupplier) { this.visualizationContextSupplier = visualizationContextSupplier; } - public int visualCount() { - int out = 0; - for (var visualList : visuals.values()) { - out += visualList.size(); - } - return out; + public Collection getAllVisuals() { + return visuals.values(); } public void add(T obj, float partialTick) { - var visualList = this.visuals.get(obj); + Visual visual = visuals.get(obj); - if (visualList == null) { + if (visual == null) { create(obj, partialTick); } } public void remove(T obj) { - var visualList = this.visuals.remove(obj); + Visual visual = visuals.remove(obj); - if (visualList == null || visualList.isEmpty()) { + if (visual == null) { return; } - for (Visual visual : visualList) { - remove(visual); + if (visual instanceof TickableVisual tickable) { + if (visual instanceof SimpleTickableVisual simpleTickable) { + simpleTickableVisuals.remove(simpleTickable); + } else { + tickableVisuals.remove(tickable); + } + } + if (visual instanceof DynamicVisual dynamic) { + if (visual instanceof SimpleDynamicVisual simpleDynamic) { + simpleDynamicVisuals.remove(simpleDynamic); + } else { + dynamicVisuals.remove(dynamic); + } + } + if (visual instanceof LitVisual lit) { + litVisuals.remove(lit); } + visual.delete(); } public void update(T obj, float partialTick) { - var visualList = visuals.get(obj); + Visual visual = visuals.get(obj); - if (visualList == null || visualList.isEmpty()) { + if (visual == null) { return; } - for (Visual visual : visualList) { - visual.update(partialTick); - } + visual.update(partialTick); } public void recreateAll(float partialTick) { @@ -78,17 +90,13 @@ public void recreateAll(float partialTick) { simpleTickableVisuals.clear(); simpleDynamicVisuals.clear(); litVisuals.clear(); - visuals.replaceAll((obj, visuals) -> { - visuals.forEach(Visual::delete); + visuals.replaceAll((obj, visual) -> { + visual.delete(); var out = createRaw(obj, partialTick); - if (out.isEmpty()) { - return null; - } - - for (Visual visual : out) { - setup(visual); + if (out != null) { + setup(out); } return out; @@ -99,28 +107,22 @@ public void invalidate() { tickableVisuals.clear(); dynamicVisuals.clear(); litVisuals.clear(); - for (var visualList : visuals.values()) { - for (Visual visual : visualList) { - visual.delete(); - } - } + visuals.values() + .forEach(Visual::delete); visuals.clear(); } private void create(T obj, float partialTick) { - var visuals = createRaw(obj, partialTick); + var visual = createRaw(obj, partialTick); - if (visuals.isEmpty()) { - return; - } - this.visuals.put(obj, visuals); - - for (Visual visual : visuals) { + if (visual != null) { setup(visual); + visuals.put(obj, visual); } } - protected abstract List createRaw(T obj, float partialTick); + @Nullable + protected abstract Visual createRaw(T obj, float partialTick); public Plan framePlan() { return NestedPlan.of(dynamicVisuals, litVisuals.plan(), ForEachPlan.of(() -> simpleDynamicVisuals, SimpleDynamicVisual::beginFrame)); @@ -156,27 +158,6 @@ private void setup(Visual visual) { } } - private void remove(Visual visual) { - if (visual instanceof TickableVisual tickable) { - if (visual instanceof SimpleTickableVisual simpleTickable) { - simpleTickableVisuals.remove(simpleTickable); - } else { - tickableVisuals.remove(tickable); - } - } - if (visual instanceof DynamicVisual dynamic) { - if (visual instanceof SimpleDynamicVisual simpleDynamic) { - simpleDynamicVisuals.remove(simpleDynamic); - } else { - dynamicVisuals.remove(dynamic); - } - } - if (visual instanceof LitVisual lit) { - litVisuals.remove(lit); - } - visual.delete(); - } - /** * Is the given object currently capable of being added? *