Skip to content

Commit

Permalink
A little encapsulation goes a long way
Browse files Browse the repository at this point in the history
- Encapsulate instancerProvider and renderOrigin in AbstractVisual
- Saves 8 bytes per visual
  • Loading branch information
Jozufozu committed Sep 15, 2024
1 parent b24e872 commit 5299571
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 5299571

Please sign in to comment.