From 5299571540b6a1252381a5ecdad0add3ad7dd386 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 14 Sep 2024 22:45:16 -0700 Subject: [PATCH] A little encapsulation goes a long way - Encapsulate instancerProvider and renderOrigin in AbstractVisual - Saves 8 bytes per visual --- .../lib/visual/AbstractBlockEntityVisual.java | 2 +- .../flywheel/lib/visual/AbstractEntityVisual.java | 2 ++ .../flywheel/lib/visual/AbstractVisual.java | 12 ++++++++---- .../dev/engine_room/flywheel/vanilla/BellVisual.java | 2 +- .../engine_room/flywheel/vanilla/ChestVisual.java | 2 +- .../engine_room/flywheel/vanilla/MinecartVisual.java | 5 +++-- .../flywheel/vanilla/ShulkerBoxVisual.java | 4 ++-- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractBlockEntityVisual.java b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractBlockEntityVisual.java index a82e08459..e31332743 100644 --- a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractBlockEntityVisual.java +++ b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractBlockEntityVisual.java @@ -52,7 +52,7 @@ public AbstractBlockEntityVisual(VisualizationContext ctx, T blockEntity, float this.blockEntity = blockEntity; this.pos = blockEntity.getBlockPos(); this.blockState = blockEntity.getBlockState(); - this.visualPos = pos.subtract(renderOrigin); + this.visualPos = pos.subtract(ctx.renderOrigin()); } @Override diff --git a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractEntityVisual.java b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractEntityVisual.java index f9aecce1e..65f0643ba 100644 --- a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractEntityVisual.java +++ b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractEntityVisual.java @@ -67,6 +67,7 @@ public double distanceSquared(double x, double y, double z) { */ public Vector3f getVisualPosition() { Vec3 pos = entity.position(); + var renderOrigin = renderOrigin(); return new Vector3f((float) (pos.x - renderOrigin.getX()), (float) (pos.y - renderOrigin.getY()), (float) (pos.z - renderOrigin.getZ())); @@ -81,6 +82,7 @@ public Vector3f getVisualPosition() { */ public Vector3f getVisualPosition(float partialTick) { Vec3 pos = entity.position(); + var renderOrigin = renderOrigin(); return new Vector3f((float) (Mth.lerp(partialTick, entity.xOld, pos.x) - renderOrigin.getX()), (float) (Mth.lerp(partialTick, entity.yOld, pos.y) - renderOrigin.getY()), (float) (Mth.lerp(partialTick, entity.zOld, pos.z) - renderOrigin.getZ())); diff --git a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractVisual.java b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractVisual.java index c21866435..e6ed8c227 100644 --- a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractVisual.java +++ b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/AbstractVisual.java @@ -13,16 +13,12 @@ public abstract class AbstractVisual implements Visual { * Useful for passing to child visuals. */ protected final VisualizationContext visualizationContext; - protected final InstancerProvider instancerProvider; - protected final Vec3i renderOrigin; protected final Level level; protected boolean deleted = false; public AbstractVisual(VisualizationContext ctx, Level level, float partialTick) { this.visualizationContext = ctx; - this.instancerProvider = ctx.instancerProvider(); - this.renderOrigin = ctx.renderOrigin(); this.level = level; } @@ -32,6 +28,14 @@ public void update(float partialTick) { protected abstract void _delete(); + protected InstancerProvider instancerProvider() { + return visualizationContext.instancerProvider(); + } + + protected Vec3i renderOrigin() { + return visualizationContext.renderOrigin(); + } + @Override public final void delete() { if (deleted) { diff --git a/common/src/main/java/dev/engine_room/flywheel/vanilla/BellVisual.java b/common/src/main/java/dev/engine_room/flywheel/vanilla/BellVisual.java index ad7648c7b..063a9d59b 100644 --- a/common/src/main/java/dev/engine_room/flywheel/vanilla/BellVisual.java +++ b/common/src/main/java/dev/engine_room/flywheel/vanilla/BellVisual.java @@ -46,7 +46,7 @@ public BellVisual(VisualizationContext ctx, BellBlockEntity blockEntity, float p } private OrientedInstance createBellInstance() { - return instancerProvider.instancer(InstanceTypes.ORIENTED, BELL_MODEL.get()) + return instancerProvider().instancer(InstanceTypes.ORIENTED, BELL_MODEL.get()) .createInstance(); } diff --git a/common/src/main/java/dev/engine_room/flywheel/vanilla/ChestVisual.java b/common/src/main/java/dev/engine_room/flywheel/vanilla/ChestVisual.java index 7953d549e..51f7ef8df 100644 --- a/common/src/main/java/dev/engine_room/flywheel/vanilla/ChestVisual.java +++ b/common/src/main/java/dev/engine_room/flywheel/vanilla/ChestVisual.java @@ -78,7 +78,7 @@ public ChestVisual(VisualizationContext ctx, T blockEntity, float partialTick) { ChestType chestType = blockState.hasProperty(ChestBlock.TYPE) ? blockState.getValue(ChestBlock.TYPE) : ChestType.SINGLE; TextureAtlasSprite sprite = Sheets.chooseMaterial(blockEntity, chestType, isChristmas()).sprite(); - instances = InstanceTree.create(instancerProvider, LAYER_LOCATIONS.get(chestType), (path, mesh) -> { + instances = InstanceTree.create(instancerProvider(), LAYER_LOCATIONS.get(chestType), (path, mesh) -> { return new Model.ConfiguredMesh(MATERIAL, new RetexturedMesh(mesh, sprite)); }); lid = instances.childOrThrow("lid"); diff --git a/common/src/main/java/dev/engine_room/flywheel/vanilla/MinecartVisual.java b/common/src/main/java/dev/engine_room/flywheel/vanilla/MinecartVisual.java index 50f8f0341..b7fc711df 100644 --- a/common/src/main/java/dev/engine_room/flywheel/vanilla/MinecartVisual.java +++ b/common/src/main/java/dev/engine_room/flywheel/vanilla/MinecartVisual.java @@ -82,7 +82,7 @@ private static ModelHolder createBodyModelHolder(ModelLayerLocation layer) { } private TransformedInstance createBodyInstance() { - return instancerProvider.instancer(InstanceTypes.TRANSFORMED, bodyModel.get()) + return instancerProvider().instancer(InstanceTypes.TRANSFORMED, bodyModel.get()) .createInstance(); } @@ -101,7 +101,7 @@ private TransformedInstance createContentsInstance() { return null; } - return instancerProvider.instancer(InstanceTypes.TRANSFORMED, Models.block(blockState)) + return instancerProvider().instancer(InstanceTypes.TRANSFORMED, Models.block(blockState)) .createInstance(); } @@ -141,6 +141,7 @@ private void updateInstances(float partialTick) { double posY = Mth.lerp(partialTick, entity.yOld, entity.getY()); double posZ = Mth.lerp(partialTick, entity.zOld, entity.getZ()); + var renderOrigin = renderOrigin(); stack.translate(posX - renderOrigin.getX(), posY - renderOrigin.getY(), posZ - renderOrigin.getZ()); float yaw = Mth.lerp(partialTick, entity.yRotO, entity.getYRot()); diff --git a/common/src/main/java/dev/engine_room/flywheel/vanilla/ShulkerBoxVisual.java b/common/src/main/java/dev/engine_room/flywheel/vanilla/ShulkerBoxVisual.java index 7e1a2f538..be5d85ba7 100644 --- a/common/src/main/java/dev/engine_room/flywheel/vanilla/ShulkerBoxVisual.java +++ b/common/src/main/java/dev/engine_room/flywheel/vanilla/ShulkerBoxVisual.java @@ -78,12 +78,12 @@ public ShulkerBoxVisual(VisualizationContext ctx, ShulkerBoxBlockEntity blockEnt } private TransformedInstance createBaseInstance(Material texture) { - return instancerProvider.instancer(InstanceTypes.TRANSFORMED, BASE_MODELS.get(texture)) + return instancerProvider().instancer(InstanceTypes.TRANSFORMED, BASE_MODELS.get(texture)) .createInstance(); } private TransformedInstance createLidInstance(Material texture) { - return instancerProvider.instancer(InstanceTypes.TRANSFORMED, LID_MODELS.get(texture)) + return instancerProvider().instancer(InstanceTypes.TRANSFORMED, LID_MODELS.get(texture)) .createInstance(); }