diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileInstance.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileInstance.java index 9c94b9062..6beb6bdac 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileInstance.java @@ -17,13 +17,24 @@ public abstract class KineticTileInstance extends T } protected final void updateRotation(InstanceKey key, Direction.Axis axis) { - key.getInstance() - .setColor(tile.network) - .setRotationalSpeed(tile.getSpeed()) + updateRotation(key, axis, tile.getSpeed()); + } + + protected final void updateRotation(InstanceKey key, Direction.Axis axis, float speed) { + updateRotation(key.getInstance(), axis, speed); + } + + protected final void updateRotation(RotatingData key, Direction.Axis axis, float speed) { + key.setColor(tile.network) + .setRotationalSpeed(speed) .setRotationOffset(getRotationOffset(axis)) .setRotationAxis(axis); } + protected final void updateRotation(RotatingData key, Direction.Axis axis) { + updateRotation(key, axis, tile.getSpeed()); + } + protected final InstanceKey setup(InstanceKey key, float speed, Direction.Axis axis) { key.getInstance() .setBlockLight(world.getLightLevel(LightType.BLOCK, pos)) diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/SingleRotatingInstance.java b/src/main/java/com/simibubi/create/content/contraptions/base/SingleRotatingInstance.java index ed80fdc9c..ab81c0462 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/SingleRotatingInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/SingleRotatingInstance.java @@ -11,20 +11,17 @@ import net.minecraft.util.Direction; public class SingleRotatingInstance extends KineticTileInstance { - protected InstanceKey rotatingModelKey; + protected final InstanceKey rotatingModelKey; public SingleRotatingInstance(InstancedTileRenderer modelManager, KineticTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { Direction.Axis axis = ((IRotate) blockState.getBlock()).getRotationAxis(blockState); rotatingModelKey = setup(getModel().createInstance(), tile.getSpeed(), axis); } @Override - public void onUpdate() { + public void update() { Direction.Axis axis = ((IRotate) blockState.getBlock()).getRotationAxis(blockState); updateRotation(rotatingModelKey, axis); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/crank/HandCrankInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/crank/HandCrankInstance.java index 309be16a0..86c353eae 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/crank/HandCrankInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/crank/HandCrankInstance.java @@ -19,11 +19,6 @@ public class HandCrankInstance extends SingleRotatingInstance implements IDynami public HandCrankInstance(InstancedTileRenderer modelManager, KineticTileEntity tile) { super(modelManager, tile); - } - - @Override - protected void init() { - super.init(); Block block = blockState.getBlock(); AllBlockPartials renderedHandle = null; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerInstance.java index 4cd49184a..a6b7de237 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerInstance.java @@ -18,28 +18,21 @@ import static com.simibubi.create.content.contraptions.base.DirectionalKineticBl public class DeployerInstance extends ShaftInstance implements IDynamicInstance { - DeployerTileEntity tile; + final DeployerTileEntity tile; + final Direction facing; + final float yRot; + final float zRot; + final float zRotPole; - Direction facing; + protected final InstanceKey pole; - InstanceKey pole; + protected InstanceKey hand; AllBlockPartials currentHand; - InstanceKey hand; - - float yRot; - float zRot; - float zRotPole; - float progress = Float.NaN; public DeployerInstance(InstancedTileRenderer dispatcher, KineticTileEntity tile) { super(dispatcher, tile); - } - - @Override - protected void init() { - super.init(); this.tile = (DeployerTileEntity) super.tile; facing = blockState.get(FACING); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/fan/FanInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/fan/FanInstance.java index b2ba34186..ff7cfcfad 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/fan/FanInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/fan/FanInstance.java @@ -10,43 +10,29 @@ import com.simibubi.create.foundation.render.backend.instancing.InstanceKey; import com.simibubi.create.foundation.render.backend.instancing.InstancedModel; import com.simibubi.create.foundation.render.backend.instancing.InstancedTileRenderer; +import net.minecraft.client.renderer.Vector3f; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; public class FanInstance extends KineticTileInstance { - protected InstanceKey shaft; - protected InstanceKey fan; + protected final InstanceKey shaft; + protected final InstanceKey fan; + final Direction.Axis axis; + final Direction direction; public FanInstance(InstancedTileRenderer modelManager, EncasedFanTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { - final Direction direction = blockState.get(FACING); - final Direction.Axis axis = ((IRotate) blockState.getBlock()).getRotationAxis(blockState); + direction = blockState.get(FACING); + axis = ((IRotate) blockState.getBlock()).getRotationAxis(blockState); - InstancedModel shaftHalf = - AllBlockPartials.SHAFT_HALF.renderOnDirectionalSouthRotating(modelManager, blockState, direction.getOpposite()); - InstancedModel fanInner = - AllBlockPartials.ENCASED_FAN_INNER.renderOnDirectionalSouthRotating(modelManager, blockState, direction.getOpposite()); + shaft = AllBlockPartials.SHAFT_HALF.renderOnDirectionalSouthRotating(modelManager, blockState, direction.getOpposite()).createInstance(); + fan = AllBlockPartials.ENCASED_FAN_INNER.renderOnDirectionalSouthRotating(modelManager, blockState, direction.getOpposite()).createInstance(); - shaft = shaftHalf.createInstance(); - shaft.getInstance() - .setRotationalSpeed(tile.getSpeed()) - .setRotationOffset(getRotationOffset(axis)) - .setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector()) - .setTileEntity(tile); - - - fan = fanInner.createInstance(); - fan.getInstance() - .setRotationalSpeed(getFanSpeed()) - .setRotationOffset(getRotationOffset(axis)) - .setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector()) - .setTileEntity(tile); + updateRotation(shaft.getInstance().setTileEntity(tile), axis); + updateRotation(fan.getInstance().setTileEntity(tile), axis, getFanSpeed()); updateLight(); } @@ -61,21 +47,13 @@ public class FanInstance extends KineticTileInstance { } @Override - protected void onUpdate() { - Direction.Axis axis = blockState.get(FACING).getAxis(); + protected void update() { updateRotation(shaft, axis); - - fan.getInstance() - .setColor(tile.network) - .setRotationalSpeed(getFanSpeed()) - .setRotationOffset(getRotationOffset(axis)) - .setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector()); + updateRotation(fan, axis, getFanSpeed()); } @Override public void updateLight() { - final Direction direction = blockState.get(FACING); - BlockPos behind = pos.offset(direction.getOpposite()); relight(behind, shaft.getInstance()); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlyWheelInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlyWheelInstance.java index 132e4713d..5393afc6b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlyWheelInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlyWheelInstance.java @@ -23,21 +23,22 @@ import net.minecraft.util.math.MathHelper; public class FlyWheelInstance extends KineticTileInstance implements IDynamicInstance { - protected Direction facing; + protected final Direction facing; + protected final Direction connection; + protected boolean connectedLeft; protected float connectorAngleMult; - protected Direction connection; + protected final InstanceKey shaft; - protected InstanceKey shaft; + protected final InstanceKey wheel; - protected InstanceKey wheel; + protected List> connectors; protected InstanceKey upperRotating; protected InstanceKey lowerRotating; protected InstanceKey upperSliding; protected InstanceKey lowerSliding; - protected List> connectors; protected float lastAngle = Float.NaN; @@ -45,10 +46,7 @@ public class FlyWheelInstance extends KineticTileInstance im public FlyWheelInstance(InstancedTileRenderer modelManager, FlywheelTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { facing = blockState.get(BlockStateProperties.HORIZONTAL_FACING); Direction.Axis axis = ((IRotate) blockState.getBlock()).getRotationAxis(blockState); @@ -77,7 +75,6 @@ public class FlyWheelInstance extends KineticTileInstance im } updateLight(); - firstFrame = true; } @Override @@ -135,7 +132,7 @@ public class FlyWheelInstance extends KineticTileInstance im } @Override - protected void onUpdate() { + protected void update() { Direction.Axis axis = ((IRotate) blockState.getBlock()).getRotationAxis(blockState); updateRotation(shaft, axis); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java index ebee39e59..ce01d7646 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java @@ -19,12 +19,9 @@ public class EngineInstance extends TileEntityInstance { public EngineInstance(InstancedTileRenderer modelManager, EngineTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { Block block = blockState - .getBlock(); + .getBlock(); if (!(block instanceof EngineBlock)) return; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MixerInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MixerInstance.java index bd9ce61da..5623900f4 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MixerInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MixerInstance.java @@ -14,16 +14,11 @@ import net.minecraft.util.Direction; public class MixerInstance extends ShaftlessCogInstance implements IDynamicInstance { - private InstanceKey mixerHead; - private InstanceKey mixerPole; + private final InstanceKey mixerHead; + private final InstanceKey mixerPole; public MixerInstance(InstancedTileRenderer dispatcher, KineticTileEntity tile) { super(dispatcher, tile); - } - - @Override - protected void init() { - super.init(); mixerHead = rotatingMaterial().getModel(AllBlockPartials.MECHANICAL_MIXER_HEAD, blockState) .createInstance(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/press/PressInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/press/PressInstance.java index 831539c69..2927d7ea5 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/press/PressInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/press/PressInstance.java @@ -14,15 +14,10 @@ import com.simibubi.create.foundation.utility.MatrixStacker; public class PressInstance extends ShaftInstance implements IDynamicInstance { - private InstanceKey pressHead; + private final InstanceKey pressHead; public PressInstance(InstancedTileRenderer dispatcher, KineticTileEntity tile) { super(dispatcher, tile); - } - - @Override - protected void init() { - super.init(); pressHead = modelManager.getMaterial(RenderMaterials.MODELS) .getModel(AllBlockPartials.MECHANICAL_PRESS_HEAD, blockState) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java index 7499176f5..a56bbc689 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java @@ -10,37 +10,39 @@ import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.MatrixStacker; import net.minecraft.client.Minecraft; import net.minecraft.util.Direction; +import net.minecraft.util.math.MathHelper; public class StickerInstance extends TileEntityInstance implements IDynamicInstance { float lastOffset = Float.NaN; + final Direction facing; + final boolean fakeWorld; + final int offset; - private InstanceKey head; + private final InstanceKey head; public StickerInstance(InstancedTileRenderer modelManager, StickerTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { head = modelManager.getMaterial(RenderMaterials.MODELS).getModel(AllBlockPartials.STICKER_HEAD, blockState).createInstance(); + fakeWorld = tile.getWorld() != Minecraft.getInstance().world; + facing = blockState.get(StickerBlock.FACING); + offset = blockState.get(StickerBlock.EXTENDED) ? 1 : 0; + updateLight(); } @Override public void beginFrame() { - blockState = world.getBlockState(pos); - float offset = tile.piston.getValue(AnimationTickHolder.getPartialTicks()); - if (tile.getWorld() != Minecraft.getInstance().world) - offset = blockState.get(StickerBlock.EXTENDED) ? 1 : 0; + if (fakeWorld) + offset = this.offset; - if (Math.abs(offset - lastOffset) < 1e-4) + if (MathHelper.epsilonEquals(offset, lastOffset)) return; - Direction facing = blockState.get(StickerBlock.FACING); MatrixStack stack = new MatrixStack(); MatrixStacker.of(stack) .translate(getFloatingPos()) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageInstance.java index 085819e38..d2d5a8404 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageInstance.java @@ -19,32 +19,33 @@ import net.minecraft.util.math.BlockPos; public class GantryCarriageInstance extends ShaftInstance implements IDynamicInstance { - private InstanceKey gantryCogs; + private final InstanceKey gantryCogs; + + final Direction facing; + final Boolean alongFirst; + final Direction.Axis rotationAxis; + final BlockPos visualPos; public GantryCarriageInstance(InstancedTileRenderer dispatcher, KineticTileEntity tile) { super(dispatcher, tile); - } - - @Override - protected void init() { - super.init(); gantryCogs = modelManager.getMaterial(RenderMaterials.MODELS) .getModel(AllBlockPartials.GANTRY_COGS, blockState) .createInstance(); + facing = blockState.get(GantryCarriageBlock.FACING); + alongFirst = blockState.get(GantryCarriageBlock.AXIS_ALONG_FIRST_COORDINATE); + rotationAxis = KineticTileEntityRenderer.getRotationAxisOf(tile); + + visualPos = facing.getAxisDirection() == Direction.AxisDirection.POSITIVE ? tile.getPos() + : tile.getPos() + .offset(facing.getOpposite()); + updateLight(); } @Override public void beginFrame() { - blockState = tile.getBlockState(); - Direction facing = blockState.get(GantryCarriageBlock.FACING); - Boolean alongFirst = blockState.get(GantryCarriageBlock.AXIS_ALONG_FIRST_COORDINATE); - Direction.Axis rotationAxis = KineticTileEntityRenderer.getRotationAxisOf(tile); - BlockPos visualPos = facing.getAxisDirection() == Direction.AxisDirection.POSITIVE ? tile.getPos() - : tile.getPos() - .offset(facing.getOpposite()); float angleForTe = GantryCarriageRenderer.getAngleForTe(tile, visualPos, rotationAxis); Direction.Axis gantryAxis = Direction.Axis.X; diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveInstance.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveInstance.java index 765156582..8566a1a22 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveInstance.java @@ -19,17 +19,12 @@ public class FluidValveInstance extends ShaftInstance implements IDynamicInstanc protected InstanceKey pointer; - protected double xRot; - protected double yRot; - protected int pointerRotationOffset; + protected final double xRot; + protected final double yRot; + protected final int pointerRotationOffset; public FluidValveInstance(InstancedTileRenderer dispatcher, KineticTileEntity tile) { super(dispatcher, tile); - } - - @Override - protected void init() { - super.init(); Direction facing = blockState.get(FluidValveBlock.FACING); @@ -39,9 +34,8 @@ public class FluidValveInstance extends ShaftInstance implements IDynamicInstanc Direction.Axis pipeAxis = FluidValveBlock.getPipeAxis(blockState); Direction.Axis shaftAxis = KineticTileEntityRenderer.getRotationAxisOf(tile); - pointerRotationOffset = 0; - if (pipeAxis.isHorizontal() && shaftAxis == Direction.Axis.Z || pipeAxis.isVertical()) - pointerRotationOffset = 90; + boolean twist = pipeAxis.isHorizontal() && shaftAxis == Direction.Axis.Z || pipeAxis.isVertical(); + pointerRotationOffset = twist ? 90 : 0; pointer = modelManager.getBasicMaterial().getModel(AllBlockPartials.FLUID_VALVE_POINTER, blockState).createInstance(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltInstance.java b/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltInstance.java index 8b2845933..273ff564a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/belt/BeltInstance.java @@ -22,23 +22,20 @@ import net.minecraft.world.LightType; public class BeltInstance extends KineticTileInstance { - private boolean upward; - private boolean diagonal; - private boolean sideways; - private boolean vertical; - private boolean alongX; - private boolean alongZ; - private BeltSlope beltSlope; - private Direction facing; + boolean upward; + boolean diagonal; + boolean sideways; + boolean vertical; + boolean alongX; + boolean alongZ; + BeltSlope beltSlope; + Direction facing; protected ArrayList> keys; protected InstanceKey pulleyKey; public BeltInstance(InstancedTileRenderer modelManager, BeltTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { if (!AllBlocks.BELT.has(blockState)) return; @@ -77,7 +74,7 @@ public class BeltInstance extends KineticTileInstance { } @Override - public void onUpdate() { + public void update() { DyeColor color = tile.color.orElse(null); boolean bottom = true; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftInstance.java b/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftInstance.java index 73a3d6a28..5ef3575ad 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftInstance.java @@ -17,14 +17,11 @@ import net.minecraft.util.Direction; public class SplitShaftInstance extends KineticTileInstance { - protected ArrayList> keys; + protected final ArrayList> keys; public SplitShaftInstance(InstancedTileRenderer modelManager, SplitShaftTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { keys = new ArrayList<>(2); Block block = blockState.getBlock(); @@ -43,7 +40,7 @@ public class SplitShaftInstance extends KineticTileInstance faces; + protected final ArrayList faces; protected MatrixStack ms; protected GaugeInstance(InstancedTileRenderer dispatcher, KineticTileEntity tile) { super(dispatcher, tile); - } - - @Override - protected void init() { - super.init(); faces = new ArrayList<>(2); diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxInstance.java b/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxInstance.java index 77d89ad6d..8f2e4fd99 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxInstance.java @@ -18,15 +18,12 @@ import net.minecraft.world.LightType; public class GearboxInstance extends KineticTileInstance { - protected EnumMap> keys; + protected final EnumMap> keys; protected Direction sourceFacing; public GearboxInstance(InstancedTileRenderer modelManager, GearboxTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { keys = new EnumMap<>(Direction.class); final Direction.Axis boxAxis = blockState.get(BlockStateProperties.AXIS); @@ -78,7 +75,7 @@ public class GearboxInstance extends KineticTileInstance { } @Override - public void onUpdate() { + public void update() { updateSourceFacing(); for (Map.Entry> key : keys.entrySet()) { Direction direction = key.getKey(); diff --git a/src/main/java/com/simibubi/create/content/logistics/block/FlapData.java b/src/main/java/com/simibubi/create/content/logistics/block/FlapData.java index a42d8af3b..841fd523b 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/FlapData.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/FlapData.java @@ -3,12 +3,13 @@ package com.simibubi.create.content.logistics.block; import com.simibubi.create.foundation.render.backend.gl.attrib.VertexFormat; import com.simibubi.create.foundation.render.backend.instancing.InstanceData; import com.simibubi.create.foundation.render.backend.instancing.InstancedModel; +import com.simibubi.create.foundation.render.backend.instancing.impl.IFlatLight; import net.minecraft.client.renderer.Vector3f; import net.minecraft.util.math.BlockPos; import java.nio.ByteBuffer; -public class FlapData extends InstanceData { +public class FlapData extends InstanceData implements IFlatLight { public static VertexFormat FORMAT = VertexFormat.builder() .addAttributes(FlapVertexAttributes.class) @@ -61,11 +62,13 @@ public class FlapData extends InstanceData { return this; } + @Override public FlapData setBlockLight(int blockLight) { this.blockLight = (byte) ((blockLight & 0xF) << 4); return this; } + @Override public FlapData setSkyLight(int skyLight) { this.skyLight = (byte) ((skyLight & 0xF) << 4); return this; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java index 47aaf49bb..48f643efa 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java @@ -16,15 +16,11 @@ import java.util.Map; public class BeltTunnelInstance extends TileEntityInstance implements IDynamicInstance { - - private Map>> tunnelFlaps; + private final Map>> tunnelFlaps; public BeltTunnelInstance(InstancedTileRenderer modelManager, BeltTunnelTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { tunnelFlaps = new EnumMap<>(Direction.class); InstancedModel model = modelManager.getMaterial(KineticRenderMaterials.FLAPS) @@ -82,9 +78,6 @@ public class BeltTunnelInstance extends TileEntityInstance }); } - @Override - protected void onUpdate() { } - @Override public void updateLight() { int blockLight = world.getLightLevel(LightType.BLOCK, pos); diff --git a/src/main/java/com/simibubi/create/content/logistics/block/diodes/AdjustableRepeaterInstance.java b/src/main/java/com/simibubi/create/content/logistics/block/diodes/AdjustableRepeaterInstance.java index 3d4ef9fdc..b7c26136d 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/diodes/AdjustableRepeaterInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/diodes/AdjustableRepeaterInstance.java @@ -9,16 +9,13 @@ import com.simibubi.create.foundation.utility.MatrixStacker; public class AdjustableRepeaterInstance extends TileEntityInstance implements ITickableInstance { - protected InstanceKey indicator; + protected final InstanceKey indicator; protected int previousState; public AdjustableRepeaterInstance(InstancedTileRenderer modelManager, AdjustableRepeaterTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { indicator = modelManager.getBasicMaterial().getModel(AllBlockPartials.FLEXPEATER_INDICATOR, blockState).createInstance(); MatrixStack ms = new MatrixStack(); diff --git a/src/main/java/com/simibubi/create/content/logistics/block/funnel/FunnelInstance.java b/src/main/java/com/simibubi/create/content/logistics/block/funnel/FunnelInstance.java index d3db0c74d..9d2e4fb25 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/funnel/FunnelInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/funnel/FunnelInstance.java @@ -12,14 +12,11 @@ import java.util.ArrayList; public class FunnelInstance extends TileEntityInstance implements IDynamicInstance { - private ArrayList> flaps; + private final ArrayList> flaps; public FunnelInstance(InstancedTileRenderer modelManager, FunnelTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { flaps = new ArrayList<>(4); if (!tile.hasFlap()) return; @@ -71,16 +68,8 @@ public class FunnelInstance extends TileEntityInstance impleme @Override public void updateLight() { - if (flaps == null) return; - - int blockLight = world.getLightLevel(LightType.BLOCK, pos); - int skyLight = world.getLightLevel(LightType.SKY, pos); - - for (InstanceKey it : flaps) { - it.getInstance() - .setBlockLight(blockLight) - .setSkyLight(skyLight); - } + if (flaps != null) + relight(pos, flaps.stream().map(InstanceKey::getInstance)); } @Override diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmInstance.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmInstance.java index b63c070d3..12fed5bfd 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmInstance.java @@ -24,24 +24,19 @@ import java.util.ArrayList; public class ArmInstance extends SingleRotatingInstance implements IDynamicInstance { - private InstanceKey base; - private InstanceKey lowerBody; - private InstanceKey upperBody; - private InstanceKey head; - private InstanceKey claw; - private ArrayList> clawGrips; + final InstanceKey base; + final InstanceKey lowerBody; + final InstanceKey upperBody; + final InstanceKey head; + final InstanceKey claw; + private final ArrayList> clawGrips; - private ArrayList> models; + private final ArrayList> models; private boolean firstTick = true; public ArmInstance(InstancedTileRenderer modelManager, KineticTileEntity tile) { super(modelManager, tile); - } - - @Override - protected void init() { - super.init(); RenderMaterial> mat = modelManager.getMaterial(RenderMaterials.MODELS); @@ -58,8 +53,6 @@ public class ArmInstance extends SingleRotatingInstance implements IDynamicInsta clawGrips = Lists.newArrayList(clawGrip1, clawGrip2); models = Lists.newArrayList(base, lowerBody, upperBody, head, claw, clawGrip1, clawGrip2); - firstTick = true; - beginFrame(); updateLight(); } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java b/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java index def1976ce..4227837e8 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java @@ -14,18 +14,15 @@ import net.minecraft.util.Direction; public class AnalogLeverInstance extends TileEntityInstance implements IDynamicInstance { - protected InstanceKey handle; - protected InstanceKey indicator; + protected final InstanceKey handle; + protected final InstanceKey indicator; - private float rX; - private float rY; + final float rX; + final float rY; public AnalogLeverInstance(InstancedTileRenderer modelManager, AnalogLeverTileEntity tile) { super(modelManager, tile); - } - @Override - protected void init() { RenderMaterial> mat = modelManager.getMaterial(RenderMaterials.MODELS); handle = mat.getModel(AllBlockPartials.ANALOG_LEVER_HANDLE, blockState).createInstance(); diff --git a/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonInstance.java b/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonInstance.java index fd9ddce84..10aa4011b 100644 --- a/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonInstance.java +++ b/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonInstance.java @@ -11,15 +11,11 @@ import net.minecraft.util.Direction; public class SchematicannonInstance extends TileEntityInstance implements IDynamicInstance { - private InstanceKey connector; - private InstanceKey pipe; + private final InstanceKey connector; + private final InstanceKey pipe; public SchematicannonInstance(InstancedTileRenderer modelManager, SchematicannonTileEntity tile) { super(modelManager, tile); - } - - @Override - protected void init() { RenderMaterial> mat = modelManager.getMaterial(RenderMaterials.MODELS); diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedTileRenderer.java b/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedTileRenderer.java index 54142c6e4..5d31998f5 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedTileRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedTileRenderer.java @@ -79,11 +79,6 @@ public abstract class InstancedTileRenderer

{ return getMaterial(RenderMaterials.MODELS); } - @Nullable - public TileEntityInstance getInstance(T tile) { - return getInstance(tile, true); - } - @SuppressWarnings("unchecked") @Nullable public TileEntityInstance getInstance(T tile, boolean create) { @@ -127,7 +122,7 @@ public abstract class InstancedTileRenderer

{ if (!Backend.canUseInstancing()) return; if (tile instanceof IInstanceRendered) { - getInstance(tile); + getInstance(tile, true); } } @@ -143,8 +138,16 @@ public abstract class InstancedTileRenderer

{ if (tile instanceof IInstanceRendered) { TileEntityInstance instance = getInstance(tile, false); - if (instance != null) - instance.update(); + if (instance != null) { + + if (instance.shouldReset()) { + removeInternal(tile, instance); + + getInstance(tile, true); + } else { + instance.update(); + } + } } } @@ -152,17 +155,25 @@ public abstract class InstancedTileRenderer

{ if (!Backend.canUseInstancing()) return; if (tile instanceof IInstanceRendered) { - TileEntityInstance instance = getInstance(tile, false); - - if (instance != null) { - instance.remove(); - instances.remove(tile); - dynamicInstances.remove(tile); - tickableInstances.remove(tile); - } + removeInternal(tile); } } + private void removeInternal(T tile) { + TileEntityInstance instance = getInstance(tile, false); + + if (instance != null) { + removeInternal(tile, instance); + } + } + + private void removeInternal(T tile, TileEntityInstance instance) { + instance.remove(); + instances.remove(tile); + dynamicInstances.remove(tile); + tickableInstances.remove(tile); + } + public void clean() { instances.keySet().removeIf(TileEntity::isRemoved); } diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/instancing/TileEntityInstance.java b/src/main/java/com/simibubi/create/foundation/render/backend/instancing/TileEntityInstance.java index fd31e3569..4c66529da 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/instancing/TileEntityInstance.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/instancing/TileEntityInstance.java @@ -16,7 +16,7 @@ public abstract class TileEntityInstance { protected final T tile; protected final World world; protected final BlockPos pos; - protected BlockState blockState; + protected final BlockState blockState; public TileEntityInstance(InstancedTileRenderer modelManager, T tile) { this.modelManager = modelManager; @@ -24,30 +24,15 @@ public abstract class TileEntityInstance { this.world = tile.getWorld(); this.pos = tile.getPos(); this.blockState = tile.getBlockState(); - init(); - } - - public final void update() { - BlockState currentState = tile.getBlockState(); - if (blockState == currentState) { - onUpdate(); - } else { - remove(); - blockState = currentState; - init(); - } } /** - * Acquire all {@link InstanceKey}s and initialize any data you may need to calculate the instance properties. + * Update instance data here. Good for when data doesn't change very often and when animations are GPU based. + * Don't query lighting data here, that's handled separately in {@link #updateLight()}. + * + * If your animations are complex and more CPU driven, use {@link IDynamicInstance} or {@link ITickableInstance}. */ - protected abstract void init(); - - /** - * Update changed instance data using the {@link InstanceKey}s you got in {@link #init()}. - * You don't have to update light data. That should be done in {@link #updateLight()} - */ - protected void onUpdate() { } + protected void update() { } /** * Called when a light update occurs in the world. If your model needs it, update light here. @@ -59,23 +44,27 @@ public abstract class TileEntityInstance { */ public abstract void remove(); + public boolean shouldReset() { + return tile.getBlockState() != blockState; + } + public BlockPos getFloatingPos() { return pos.subtract(modelManager.getOriginCoordinate()); } - protected > void relight(BlockPos pos, IFlatLight... models) { + protected void relight(BlockPos pos, IFlatLight... models) { relight(world.getLightLevel(LightType.BLOCK, pos), world.getLightLevel(LightType.SKY, pos), models); } - protected > void relight(BlockPos pos, Stream> models) { + protected void relight(BlockPos pos, Stream> models) { relight(world.getLightLevel(LightType.BLOCK, pos), world.getLightLevel(LightType.SKY, pos), models); } - protected > void relight(int block, int sky, IFlatLight... models) { + protected void relight(int block, int sky, IFlatLight... models) { relight(block, sky, Arrays.stream(models)); } - protected > void relight(int block, int sky, Stream> models) { + protected void relight(int block, int sky, Stream> models) { models.forEach(model -> model.setBlockLight(block).setSkyLight(sky)); } }