No more 7 circles of callbacks.

- Instead of using consumers to edit instance data, just give the user a pointer to the data.
 - Easier to use, more predictable access.
This commit is contained in:
JozsefA 2021-03-08 02:02:47 -08:00
parent d0e5be24d7
commit 8ce0f47f01
13 changed files with 164 additions and 200 deletions

View file

@ -1,7 +1,5 @@
package com.simibubi.create.content.contraptions.base;
import java.util.function.Consumer;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.content.contraptions.relays.elementary.CogWheelBlock;
import com.simibubi.create.content.contraptions.relays.elementary.ShaftBlock;
@ -19,27 +17,28 @@ public abstract class KineticTileInstance<T extends KineticTileEntity> extends T
}
protected final void updateRotation(InstanceKey<RotatingData> key, Direction.Axis axis) {
key.modifyInstance(data -> {
data.setColor(tile.network)
key.getInstance()
.setColor(tile.network)
.setRotationalSpeed(tile.getSpeed())
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(axis);
});
}
protected final Consumer<RotatingData> setupFunc(float speed, Direction.Axis axis) {
return data -> {
data.setBlockLight(world.getLightLevel(LightType.BLOCK, pos))
protected final InstanceKey<RotatingData> setup(InstanceKey<RotatingData> key, float speed, Direction.Axis axis) {
key.getInstance()
.setBlockLight(world.getLightLevel(LightType.BLOCK, pos))
.setSkyLight(world.getLightLevel(LightType.SKY, pos))
.setTileEntity(tile)
.setRotationalSpeed(speed)
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(axis);
};
return key;
}
protected final void relight(KineticData<?> data) {
data.setBlockLight(world.getLightLevel(LightType.BLOCK, pos))
protected final void relight(InstanceKey<? extends KineticData<?>> key) {
key.getInstance()
.setBlockLight(world.getLightLevel(LightType.BLOCK, pos))
.setSkyLight(world.getLightLevel(LightType.SKY, pos));
}

View file

@ -28,7 +28,7 @@ public class SingleRotatingInstance extends KineticTileInstance<KineticTileEntit
@Override
protected void init() {
Direction.Axis axis = ((IRotate) lastState.getBlock()).getRotationAxis(lastState);
rotatingModelKey = getModel().createInstance(setupFunc(tile.getSpeed(), axis));
rotatingModelKey = setup(getModel().createInstance(), tile.getSpeed(), axis);
}
@Override
@ -39,7 +39,7 @@ public class SingleRotatingInstance extends KineticTileInstance<KineticTileEntit
@Override
public void updateLight() {
rotatingModelKey.modifyInstance(this::relight);
relight(rotatingModelKey);
}
@Override

View file

@ -6,10 +6,7 @@ import com.simibubi.create.content.contraptions.base.RotatingData;
import com.simibubi.create.content.contraptions.base.SingleRotatingInstance;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext;
import com.simibubi.create.content.contraptions.components.structureMovement.render.RenderedContraption;
import com.simibubi.create.foundation.render.backend.instancing.InstancedModel;
import com.simibubi.create.foundation.render.backend.instancing.InstancedTileRenderRegistry;
import com.simibubi.create.foundation.render.backend.instancing.InstancedTileRenderer;
import com.simibubi.create.foundation.render.backend.instancing.RenderMaterial;
import com.simibubi.create.foundation.render.backend.instancing.*;
import com.simibubi.create.foundation.utility.AngleHelper;
import net.minecraft.block.BlockState;
@ -34,16 +31,15 @@ public class DrillInstance extends SingleRotatingInstance {
BlockState state = context.state;
InstancedModel<ContraptionActorData> model = renderMaterial.getModel(AllBlockPartials.DRILL_HEAD, state);
model.createInstance(data -> {
Direction facing = state.get(DrillBlock.FACING);
float eulerX = AngleHelper.verticalAngle(facing) + ((facing.getAxis() == Direction.Axis.Y) ? 180 : 0);
float eulerY = facing.getHorizontalAngle();
data.setPosition(context.localPos)
model.getInstance(model.createInstance())
.setPosition(context.localPos)
.setBlockLight(contraption.renderWorld.getLightLevel(LightType.BLOCK, context.localPos))
.setRotationOffset(0)
.setRotationAxis(0, 0, 1)
.setLocalRotation(eulerX, eulerY, 0);
});
}
@Override

View file

@ -46,17 +46,16 @@ public class HarvesterRenderer extends SafeTileEntityRenderer<HarvesterTileEntit
BlockState state = context.state;
InstancedModel<ContraptionActorData> model = renderMaterial.getModel(AllBlockPartials.HARVESTER_BLADE, state);
model.createInstance(data -> {
Direction facing = state.get(HORIZONTAL_FACING);
float originOffset = 1 / 16f;
Vector3f rotOffset = new Vector3f(0.5f, -2 * originOffset + 0.5f, originOffset + 0.5f);
data.setPosition(context.localPos)
model.getInstance(model.createInstance())
.setPosition(context.localPos)
.setBlockLight(contraption.renderWorld.getLightLevel(LightType.BLOCK, context.localPos))
.setRotationOffset(0)
.setRotationCenter(rotOffset)
.setRotationAxis(-1, 0, 0)
.setLocalRotation(0, facing.getHorizontalAngle(), 0);
});
}
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,

View file

@ -28,7 +28,7 @@ public class FanInstance extends KineticTileInstance<EncasedFanTileEntity> {
protected InstanceKey<RotatingData> shaft;
protected InstanceKey<RotatingData> fan;
public FanInstance(InstancedTileRenderer modelManager, EncasedFanTileEntity tile) {
public FanInstance(InstancedTileRenderer<?> modelManager, EncasedFanTileEntity tile) {
super(modelManager, tile);
}
@ -42,30 +42,22 @@ public class FanInstance extends KineticTileInstance<EncasedFanTileEntity> {
InstancedModel<RotatingData> fanInner =
AllBlockPartials.ENCASED_FAN_INNER.renderOnDirectionalSouthRotating(modelManager, lastState, direction.getOpposite());
shaft = shaftHalf.createInstance(data -> {
BlockPos behind = pos.offset(direction.getOpposite());
int blockLight = world.getLightLevel(LightType.BLOCK, behind);
int skyLight = world.getLightLevel(LightType.SKY, behind);
data.setRotationalSpeed(tile.getSpeed())
shaft = shaftHalf.createInstance();
shaft.getInstance()
.setRotationalSpeed(tile.getSpeed())
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector())
.setTileEntity(tile)
.setBlockLight(blockLight)
.setSkyLight(skyLight);
});
fan = fanInner.createInstance(data -> {
BlockPos inFront = pos.offset(direction);
int blockLight = world.getLightLevel(LightType.BLOCK, inFront);
int skyLight = world.getLightLevel(LightType.SKY, inFront);
.setTileEntity(tile);
data.setRotationalSpeed(getFanSpeed())
fan = fanInner.createInstance();
fan.getInstance()
.setRotationalSpeed(getFanSpeed())
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector())
.setTileEntity(tile)
.setBlockLight(blockLight)
.setSkyLight(skyLight);
});
.setTileEntity(tile);
updateLight();
}
private float getFanSpeed() {
@ -82,32 +74,31 @@ public class FanInstance extends KineticTileInstance<EncasedFanTileEntity> {
Direction.Axis axis = lastState.get(FACING).getAxis();
updateRotation(shaft, axis);
fan.modifyInstance(data -> {
data.setColor(tile.network)
fan.getInstance()
.setColor(tile.network)
.setRotationalSpeed(getFanSpeed())
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector());
});
}
@Override
public void updateLight() {
final Direction direction = lastState.get(FACING);
shaft.modifyInstance(data -> {
BlockPos behind = pos.offset(direction.getOpposite());
int blockLight = world.getLightLevel(LightType.BLOCK, behind);
int skyLight = world.getLightLevel(LightType.SKY, behind);
data.setBlockLight(blockLight)
.setSkyLight(skyLight);
});
fan.modifyInstance(data -> {
putLight(shaft, behind);
BlockPos inFront = pos.offset(direction);
int blockLight = world.getLightLevel(LightType.BLOCK, inFront);
int skyLight = world.getLightLevel(LightType.SKY, inFront);
data.setBlockLight(blockLight)
putLight(fan, inFront);
}
private void putLight(InstanceKey<RotatingData> key, BlockPos pos) {
int blockLight = world.getLightLevel(LightType.BLOCK, pos);
int skyLight = world.getLightLevel(LightType.SKY, pos);
key.getInstance()
.setBlockLight(blockLight)
.setSkyLight(skyLight);
});
}
@Override

View file

@ -39,8 +39,7 @@ public class FlyWheelInstance extends KineticTileInstance<FlywheelTileEntity> {
facing = lastState.get(BlockStateProperties.HORIZONTAL_FACING);
Direction.Axis axis = ((IRotate) lastState.getBlock()).getRotationAxis(lastState);
Consumer<RotatingData> setup = setupFunc(tile.getSpeed(), axis);
shaft = shaftModel().createInstance(setup);
shaft = setup(shaftModel().createInstance(), tile.getSpeed(), axis);
// wheel = wheelModel().setupInstance(setup);
}
@ -53,14 +52,13 @@ public class FlyWheelInstance extends KineticTileInstance<FlywheelTileEntity> {
@Override
public void updateLight() {
shaft.modifyInstance(this::relight);
relight(shaft);
// wheel.modifyInstance(this::relight);
}
@Override
public void remove() {
shaft.delete();
shaft = null;
// wheel.delete();
// wheel = null;
}

View file

@ -72,9 +72,8 @@ public class BeltInstance extends KineticTileInstance<BeltTileEntity> {
SpriteShiftEntry spriteShift = BeltRenderer.getSpriteShiftEntry(color, diagonal, bottom);
InstancedModel<BeltData> beltModel = beltPartial.renderOnBelt(modelManager, lastState);
Consumer<BeltData> setupFunc = setupFunc(bottom, spriteShift);
keys.add(beltModel.createInstance(setupFunc));
keys.add(setup(beltModel.createInstance(), bottom, spriteShift));
if (diagonal) break;
}
@ -82,7 +81,7 @@ public class BeltInstance extends KineticTileInstance<BeltTileEntity> {
if (tile.hasPulley()) {
InstancedModel<RotatingData> pulleyModel = getPulleyModel();
pulleyKey = pulleyModel.createInstance(setupFunc(tile.getSpeed(), getRotationAxis()));
pulleyKey = setup(pulleyModel.createInstance(), tile.getSpeed(), getRotationAxis());
}
}
@ -94,9 +93,10 @@ public class BeltInstance extends KineticTileInstance<BeltTileEntity> {
for (InstanceKey<BeltData> key : keys) {
SpriteShiftEntry spriteShiftEntry = BeltRenderer.getSpriteShiftEntry(color, diagonal, bottom);
key.modifyInstance(data -> data.setScrollTexture(spriteShiftEntry)
key.getInstance()
.setScrollTexture(spriteShiftEntry)
.setColor(tile.network)
.setRotationalSpeed(getScrollSpeed()));
.setRotationalSpeed(getScrollSpeed());
bottom = false;
}
@ -107,11 +107,9 @@ public class BeltInstance extends KineticTileInstance<BeltTileEntity> {
@Override
public void updateLight() {
for (InstanceKey<BeltData> key : keys) {
key.modifyInstance(this::relight);
}
keys.forEach(this::relight);
if (pulleyKey != null) pulleyKey.modifyInstance(this::relight);
if (pulleyKey != null) relight(pulleyKey);
}
@Override
@ -165,14 +163,13 @@ public class BeltInstance extends KineticTileInstance<BeltTileEntity> {
return dir;
}
private Consumer<BeltData> setupFunc(boolean bottom, SpriteShiftEntry spriteShift) {
return data -> {
private InstanceKey<BeltData> setup(InstanceKey<BeltData> key, boolean bottom, SpriteShiftEntry spriteShift) {
float rotX = (!diagonal && beltSlope != BeltSlope.HORIZONTAL ? 90 : 0) + (beltSlope == BeltSlope.DOWNWARD ? 180 : 0);
float rotY = facing.getHorizontalAngle() + (upward ? 180 : 0) + (sideways ? 90 : 0);
float rotZ = sideways ? 90 : ((vertical && facing.getAxisDirection() == Direction.AxisDirection.NEGATIVE) ? 180 : 0);
BlockPos pos = tile.getPos();
data.setTileEntity(tile)
key.getInstance()
.setTileEntity(tile)
.setBlockLight(world.getLightLevel(LightType.BLOCK, pos))
.setSkyLight(world.getLightLevel(LightType.SKY, pos))
.setRotation(rotX, rotY, rotZ)
@ -180,7 +177,8 @@ public class BeltInstance extends KineticTileInstance<BeltTileEntity> {
.setRotationOffset(bottom ? 0.5f : 0f)
.setScrollTexture(spriteShift)
.setScrollMult(diagonal ? 3f / 8f : 0.5f);
};
return key;
}
}

View file

@ -45,7 +45,7 @@ public class SplitShaftInstance extends KineticTileInstance<SplitShaftTileEntity
float splitSpeed = speed * tile.getRotationSpeedModifier(dir);
keys.add(half.createInstance(setupFunc(splitSpeed, boxAxis)));
keys.add(setup(half.createInstance(), splitSpeed, boxAxis));
}
}
@ -63,9 +63,7 @@ public class SplitShaftInstance extends KineticTileInstance<SplitShaftTileEntity
@Override
public void updateLight() {
for (InstanceKey<RotatingData> key : keys) {
key.modifyInstance(this::relight);
}
keys.forEach(this::relight);
}
@Override
@ -75,13 +73,12 @@ public class SplitShaftInstance extends KineticTileInstance<SplitShaftTileEntity
}
protected void updateRotation(InstanceKey<RotatingData> key, Direction dir) {
key.modifyInstance(data -> {
Direction.Axis axis = dir.getAxis();
data.setColor(tile.network)
key.getInstance()
.setColor(tile.network)
.setRotationalSpeed(tile.getSpeed() * tile.getRotationSpeedModifier(dir))
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector());
});
}
}

View file

@ -50,14 +50,16 @@ public class GearboxInstance extends KineticTileInstance<GearboxTileEntity> {
InstancedModel<RotatingData> shaft = AllBlockPartials.SHAFT_HALF.renderOnDirectionalSouthRotating(modelManager, lastState, direction);
InstanceKey<RotatingData> key = shaft.createInstance(data -> {
data.setBlockLight(blockLight)
InstanceKey<RotatingData> key = shaft.createInstance();
key.getInstance()
.setBlockLight(blockLight)
.setSkyLight(skyLight)
.setRotationalSpeed(getSpeed(direction))
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector())
.setTileEntity(tile);
});
keys.put(direction, key);
}
}
@ -87,15 +89,15 @@ public class GearboxInstance extends KineticTileInstance<GearboxTileEntity> {
public void onUpdate() {
updateSourceFacing();
for (Map.Entry<Direction, InstanceKey<RotatingData>> key : keys.entrySet()) {
key.getValue().modifyInstance(data -> {
Direction direction = key.getKey();
Direction.Axis axis = direction.getAxis();
data.setColor(tile.network)
key.getValue()
.getInstance()
.setColor(tile.network)
.setRotationalSpeed(getSpeed(direction))
.setRotationOffset(getRotationOffset(axis))
.setRotationAxis(Direction.getFacingFromAxis(Direction.AxisDirection.POSITIVE, axis).getUnitVector());
});
}
}
@ -105,7 +107,9 @@ public class GearboxInstance extends KineticTileInstance<GearboxTileEntity> {
int skyLight = tile.getWorld().getLightLevel(LightType.SKY, pos);
for (InstanceKey<RotatingData> key : keys.values()) {
key.modifyInstance(data -> data.setBlockLight(blockLight).setSkyLight(skyLight));
key.getInstance()
.setBlockLight(blockLight)
.setSkyLight(skyLight);
}
}

View file

@ -53,8 +53,10 @@ public class BeltTunnelInstance extends TileEntityInstance<BeltTunnelTileEntity>
float intensity = segment == 3 ? 1.5f : segment + 1;
float segmentOffset = -3 / 16f * segment;
flaps.add(model.createInstance(flapData -> {
flapData.setPosition(pos)
InstanceKey<FlapData> key = model.createInstance();
key.getInstance()
.setPosition(pos)
.setSegmentOffset(segmentOffset, 0, 0)
.setBlockLight(blockLight)
.setSkyLight(skyLight)
@ -63,7 +65,8 @@ public class BeltTunnelInstance extends TileEntityInstance<BeltTunnelTileEntity>
.setFlapScale(flapScale)
.setPivotVoxelSpace(0, 10, 1)
.setIntensity(intensity);
}));
flaps.add(key);
}
tunnelFlaps.put(direction, flaps);
@ -80,7 +83,7 @@ public class BeltTunnelInstance extends TileEntityInstance<BeltTunnelTileEntity>
float flapness = flapValue.get(AnimationTickHolder.getPartialTicks());
for (InstanceKey<FlapData> key : keys) {
key.modifyInstance(data -> data.setFlapness(flapness));
key.getInstance().setFlapness(flapness);
}
});
}
@ -95,8 +98,8 @@ public class BeltTunnelInstance extends TileEntityInstance<BeltTunnelTileEntity>
for (ArrayList<InstanceKey<FlapData>> instanceKeys : tunnelFlaps.values()) {
for (InstanceKey<FlapData> it : instanceKeys) {
it.modifyInstance(data -> data.setBlockLight(blockLight)
.setSkyLight(skyLight));
it.getInstance().setBlockLight(blockLight)
.setSkyLight(skyLight);
}
}
}

View file

@ -48,7 +48,10 @@ public class FunnelInstance extends TileEntityInstance<FunnelTileEntity> impleme
float intensity = segment == 3 ? 1.5f : segment + 1;
float segmentOffset = -3 / 16f * segment;
flaps.add(model.createInstance(flapData -> flapData.setPosition(pos)
InstanceKey<FlapData> key = model.createInstance();
key.getInstance()
.setPosition(pos)
.setSegmentOffset(segmentOffset, 0, -tile.getFlapOffset())
.setBlockLight(blockLight)
.setSkyLight(skyLight)
@ -56,7 +59,9 @@ public class FunnelInstance extends TileEntityInstance<FunnelTileEntity> impleme
.setFlapness(flapness)
.setFlapScale(-1)
.setPivotVoxelSpace(0, 10, 9.5f)
.setIntensity(intensity)));
.setIntensity(intensity);
flaps.add(key);
}
}
@ -67,7 +72,7 @@ public class FunnelInstance extends TileEntityInstance<FunnelTileEntity> impleme
float flapness = tile.flap.get(AnimationTickHolder.getPartialTicks());
for (InstanceKey<FlapData> key : flaps) {
key.modifyInstance(data -> data.setFlapness(flapness));
key.getInstance().setFlapness(flapness);
}
}
@ -79,8 +84,9 @@ public class FunnelInstance extends TileEntityInstance<FunnelTileEntity> impleme
int skyLight = world.getLightLevel(LightType.SKY, pos);
for (InstanceKey<FlapData> it : flaps) {
it.modifyInstance(data -> data.setBlockLight(blockLight)
.setSkyLight(skyLight));
it.getInstance()
.setBlockLight(blockLight)
.setSkyLight(skyLight);
}
}

View file

@ -21,10 +21,6 @@ public class InstanceKey<D extends InstanceData> {
return index != INVALID;
}
public void modifyInstance(Consumer<D> edit) {
model.modifyInstance(this, edit);
}
public D getInstance() {
return model.getInstance(this);
}

View file

@ -85,16 +85,6 @@ public abstract class InstancedModel<D extends InstanceData> extends BufferedMod
return this.data.get(key.index);
}
public synchronized void modifyInstance(InstanceKey<D> key, Consumer<D> edit) {
verifyKey(key);
D data = this.data.get(key.index);
edit.accept(data);
markIndexChanged(key.index);
}
public synchronized InstanceKey<D> createInstance() {
D instanceData = newInstance();
@ -107,19 +97,6 @@ public abstract class InstancedModel<D extends InstanceData> extends BufferedMod
return key;
}
public synchronized InstanceKey<D> createInstance(Consumer<D> setup) {
D instanceData = newInstance();
setup.accept(instanceData);
InstanceKey<D> key = new InstanceKey<>(this, data.size());
data.add(instanceData);
keys.add(key);
markIndexChanged(key.index);
return key;
}
protected void doRender() {
vao.with(vao -> {
renderSetup();