Updates to the Bogey API (#4875)

- Provided bogey data during model initialisation and patched other oversights in BogeyRenderer
- Refactored method names in BogeyRenderer
- Implemented interface for interactions with custom bogey blocks
- Implemented wrapper record for bogey model data
This commit is contained in:
Rabbitminers 2023-07-03 17:08:54 +01:00 committed by GitHub
parent d16b3ebe34
commit 4d7b64db49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 202 additions and 103 deletions

View File

@ -187,7 +187,7 @@ public abstract class AbstractBogeyBlock<T extends AbstractBogeyBlockEntity> ext
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand,
public final InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand,
BlockHitResult hit) {
if (level.isClientSide)
return InteractionResult.PASS;
@ -239,6 +239,12 @@ public abstract class AbstractBogeyBlock<T extends AbstractBogeyBlockEntity> ext
return InteractionResult.CONSUME;
}
return onInteractWithBogey(state, level, pos, player, hand, hit);
}
// Allows for custom interactions with bogey block to be added simply
protected InteractionResult onInteractWithBogey(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand,
BlockHitResult hit) {
return InteractionResult.PASS;
}
@ -249,7 +255,6 @@ public abstract class AbstractBogeyBlock<T extends AbstractBogeyBlockEntity> ext
return BOGEYS;
}
@Override
public BlockState getRotatedBlockState(BlockState state, Direction targetedFace) {
Block block = state.getBlock();

View File

@ -4,6 +4,8 @@ import com.jozufozu.flywheel.api.MaterialManager;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.simibubi.create.content.trains.entity.CarriageBogey;
import net.minecraft.nbt.CompoundTag;
public class BackupBogeyRenderer extends BogeyRenderer.CommonRenderer {
@ -15,7 +17,7 @@ public class BackupBogeyRenderer extends BogeyRenderer.CommonRenderer {
}
@Override
public void initialiseContraptionModelData(MaterialManager materialManager) {
public void initialiseContraptionModelData(MaterialManager materialManager, CarriageBogey carriageBogey) {
}
}

View File

@ -30,8 +30,8 @@ public final class BogeyInstance {
this.renderer = this.style.createRendererInstance(this.size);
this.commonRenderer = this.style.getNewCommonRenderInstance();
commonRenderer.ifPresent(bogeyRenderer -> bogeyRenderer.initialiseContraptionModelData(materialManager));
renderer.initialiseContraptionModelData(materialManager);
commonRenderer.ifPresent(bogeyRenderer -> bogeyRenderer.initialiseContraptionModelData(materialManager, bogey));
renderer.initialiseContraptionModelData(materialManager, bogey);
}
public void beginFrame(float wheelAngle, PoseStack ms) {

View File

@ -4,6 +4,12 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;
import com.mojang.math.Matrix3f;
import com.mojang.math.Matrix4f;
import com.mojang.math.Quaternion;
import com.simibubi.create.content.trains.entity.CarriageBogey;
import org.jetbrains.annotations.Nullable;
@ -23,8 +29,15 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
/**
* This is a port of the bogey api from Extended Bogeys, If you are looking to implement your own bogeys you can find some helpful resources below:
* <p>
* - <a href="https://github.com/Rabbitminers/Extended-Bogeys/tree/1.18/multiloader/dev/common/src/main/java/com/rabbitminers/extendedbogeys/bogeys/styles">Extended Bogeys (Examples)</a>
* - <a href="https://github.com/Rabbitminers/Extended-Bogeys/blob/1.18/multiloader/dev/API_DOCS.md">Extended Bogeys (API documentation)</a>
* - <a href="https://github.com/Layers-of-Railways/Railway/tree/93e318d1e922b1e992b89b0aceef85a2d545f370/common/src/main/java/com/railwayteam/railways/content/custom_bogeys">Steam n' Rails (Examples)</a>
*/
public abstract class BogeyRenderer {
Map<String, ModelData[]> contraptionModelData = new HashMap<>();
Map<String, BogeyModelData[]> contraptionModelData = new HashMap<>();
/**
* A common interface for getting transform data for both in-world and in-contraption model data safely from a
@ -36,7 +49,7 @@ public abstract class BogeyRenderer {
* @param size The amount of models needed
* @return A generic transform which can be used for both in-world and in-contraption models
*/
public Transform<?>[] getTransformsFromPartial(PartialModel model, PoseStack ms, boolean inInstancedContraption, int size) {
public BogeyModelData[] getTransform(PartialModel model, PoseStack ms, boolean inInstancedContraption, int size) {
return (inInstancedContraption) ? transformContraptionModelData(keyFromModel(model), ms) : createModelData(model, size);
}
@ -50,10 +63,37 @@ public abstract class BogeyRenderer {
* @param size The amount of models needed
* @return A generic transform which can be used for both in-world and in-contraption models
*/
public Transform<?>[] getTransformsFromBlockState(BlockState state, PoseStack ms, boolean inContraption, int size) {
public BogeyModelData[] getTransform(BlockState state, PoseStack ms, boolean inContraption, int size) {
return inContraption ? transformContraptionModelData(keyFromModel(state), ms) : createModelData(state, size);
}
/**
* Helper function to collect or create a single model from a partial model used for both in-world and
* in-contraption rendering
*
* @param model The key of the model to be collected or instantiated
* @param ms Posestack to bind the model to if it is within a contraption
* @param inInstancedContraption Type of rendering required
* @return A generic transform which can be used for both in-world and in-contraption models
*/
public BogeyModelData getTransform(PartialModel model, PoseStack ms, boolean inInstancedContraption) {
return inInstancedContraption ? contraptionModelData.get(keyFromModel(model))[0].setTransform(ms)
: BogeyModelData.from(model);
}
/**
* A common interface for getting transform data for blockstates, for a single model
*
* @param state The state of the model to be collected or instantiated
* @param ms Posestack to bind the model to if it is within a contraption
* @param inContraption Type of model required
* @return A generic transform which can be used for both in-world and in-contraption models
*/
public BogeyModelData getTransform(BlockState state, PoseStack ms, boolean inContraption) {
return (inContraption) ? contraptionModelData.get(keyFromModel(state))[0].setTransform(ms)
: BogeyModelData.from(state);
}
/**
* Used for calling both in-world and in-contraption rendering
*
@ -64,7 +104,8 @@ public abstract class BogeyRenderer {
* @param vb (Optional) Vertex Consumer used for in-world rendering
*/
@OnlyIn(Dist.CLIENT)
public abstract void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb, boolean inContraption);
public abstract void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light,
VertexConsumer vb, boolean inContraption);
/**
* Used for calling in-contraption rendering ensuring that falsey data is handled correctly
@ -88,8 +129,8 @@ public abstract class BogeyRenderer {
* @param ms Posestack of the contraption to bind the model data to
* @return A generic transform which can be used for both in-world and in-contraption models
*/
private Transform<?>[] transformContraptionModelData(String key, PoseStack ms) {
ModelData[] modelData = contraptionModelData.get(key);
private BogeyModelData[] transformContraptionModelData(String key, PoseStack ms) {
BogeyModelData[] modelData = contraptionModelData.get(key);
Arrays.stream(modelData).forEach(modelDataElement -> modelDataElement.setTransform(ms));
return modelData;
}
@ -103,9 +144,8 @@ public abstract class BogeyRenderer {
* @param size The Amount of models needed
* @return A generic transform which can be used for both in-world and in-contraption models
*/
private Transform<?>[] createModelData(PartialModel model, int size) {
BlockState air = Blocks.AIR.defaultBlockState();
SuperByteBuffer[] data = { CachedBufferer.partial(model, air) };
private BogeyModelData[] createModelData(PartialModel model, int size) {
BogeyModelData[] data = { BogeyModelData.from(model) };
return expandArrayToLength(data, size);
}
@ -117,8 +157,8 @@ public abstract class BogeyRenderer {
* @param size Amount of models needed
* @return A generic transform which can be used for both in-world and in-contraption models
*/
private Transform<?>[] createModelData(BlockState state, int size) {
SuperByteBuffer[] data = { CachedBufferer.block(state) };
private BogeyModelData[] createModelData(BlockState state, int size) {
BogeyModelData[] data = { BogeyModelData.from(state) };
return expandArrayToLength(data, size);
}
@ -130,47 +170,20 @@ public abstract class BogeyRenderer {
* @param size Amount of models needed
* @return A generic transform which can be used for both in-world and in-contraption models
*/
private Transform<?>[] expandArrayToLength(SuperByteBuffer[] data, int size) {
private BogeyModelData[] expandArrayToLength(BogeyModelData[] data, int size) {
return Arrays.stream(Collections.nCopies(size, data).toArray())
.flatMap(inner -> Arrays.stream((SuperByteBuffer[]) inner))
.toArray(SuperByteBuffer[]::new);
}
/**
* Helper function to collect or create a single model from a partial model used for both in-world and
* in-contraption rendering
*
* @param model The key of the model to be collected or instantiated
* @param ms Posestack to bind the model to if it is within a contraption
* @param inInstancedContraption Type of rendering required
* @return A generic transform which can be used for both in-world and in-contraption models
*/
public Transform<?> getTransformFromPartial(PartialModel model, PoseStack ms, boolean inInstancedContraption) {
BlockState air = Blocks.AIR.defaultBlockState();
return inInstancedContraption ? contraptionModelData.get(keyFromModel(model))[0].setTransform(ms)
: CachedBufferer.partial(model, air);
}
/**
* A common interface for getting transform data for blockstates, for a single model
*
* @param state The state of the model to be collected or instantiated
* @param ms Posestack to bind the model to if it is within a contraption
* @param inContraption Type of model required
* @return A generic transform which can be used for both in-world and in-contraption models
*/
public Transform<?> getTransformFromBlockState(BlockState state, PoseStack ms, boolean inContraption) {
return (inContraption) ? contraptionModelData.get(keyFromModel(state))[0].setTransform(ms)
: CachedBufferer.block(state);
.flatMap(inner -> Arrays.stream((BogeyModelData[]) inner))
.toArray(BogeyModelData[]::new);
}
/**
* Provides render implementations a point in setup to instantiate all model data to be needed
*
* @param materialManager The material manager
* @param carriageBogey The bogey to create data for
*/
@OnlyIn(Dist.CLIENT)
public abstract void initialiseContraptionModelData(MaterialManager materialManager);
public abstract void initialiseContraptionModelData(MaterialManager materialManager, CarriageBogey carriageBogey);
/**
* Creates instances of models for in-world rendering to a set length from a provided partial model
@ -179,10 +192,13 @@ public abstract class BogeyRenderer {
* @param model Partial model to be instanced
* @param count Amount of models neeeded
*/
public void createModelInstances(MaterialManager materialManager, PartialModel model, int count) {
ModelData[] modelData = new ModelData[count];
materialManager.defaultSolid().material(Materials.TRANSFORMED)
.getModel(model).createInstances(modelData);
public void createModelInstance(MaterialManager materialManager, PartialModel model, int count) {
BogeyModelData[] modelData = IntStream.range(0, count)
.mapToObj(i -> materialManager.defaultSolid()
.material(Materials.TRANSFORMED)
.getModel(model).createInstance())
.map(BogeyModelData::new)
.toArray(BogeyModelData[]::new);
contraptionModelData.put(keyFromModel(model), modelData);
}
@ -193,10 +209,13 @@ public abstract class BogeyRenderer {
* @param state Blockstate of the model to be created
* @param count Amount of models needed
*/
public void createModelInstances(MaterialManager materialManager, BlockState state, int count) {
ModelData[] modelData = new ModelData[count];
materialManager.defaultSolid().material(Materials.TRANSFORMED)
.getModel(state).createInstances(modelData);
public void createModelInstance(MaterialManager materialManager, BlockState state, int count) {
BogeyModelData[] modelData = IntStream.range(0, count)
.mapToObj(i -> materialManager.defaultSolid()
.material(Materials.TRANSFORMED)
.getModel(state).createInstance())
.map(BogeyModelData::new)
.toArray(BogeyModelData[]::new);
contraptionModelData.put(keyFromModel(state), modelData);
}
@ -204,10 +223,11 @@ public abstract class BogeyRenderer {
* Creates a single instance of models for in-contraption rendering from a provided blockstate
*
* @param materialManager The material manager
* @param state Blockstate of the model to be created
* @param states Blockstates of the models to be created
*/
public void createModelInstance(MaterialManager materialManager, BlockState state) {
this.createModelInstances(materialManager, state, 1);
public void createModelInstance(MaterialManager materialManager, BlockState... states) {
for (BlockState state : states)
this.createModelInstance(materialManager, state, 1);
}
/**
@ -216,13 +236,14 @@ public abstract class BogeyRenderer {
* @param materialManager The material manager
* @param models The type of model to create instances of
*/
public void createModelInstances(MaterialManager materialManager, PartialModel... models) {
public void createModelInstance(MaterialManager materialManager, PartialModel... models) {
for (PartialModel model : models)
createModelInstances(materialManager, model, 1);
createModelInstance(materialManager, model, 1);
}
/**
* Handles scale for all model data and renders non contraption model data
* This method is deprecated, use BogeyModelData#render instead, left in
* to avoid existing usages from crashing
*
* @param b The model data itself
* @param ms Pose stack to render to
@ -231,6 +252,7 @@ public abstract class BogeyRenderer {
* @param <B> Generic alias for both contraption and in-world model data
*/
@Deprecated
public static <B extends Transform<?>> void finalize(B b, PoseStack ms, int light, @Nullable VertexConsumer vb) {
b.scale(1 - 1/512f);
if (b instanceof SuperByteBuffer byteBuf && vb != null)
@ -243,8 +265,8 @@ public abstract class BogeyRenderer {
*/
public void emptyTransforms() {
for (ModelData[] data : contraptionModelData.values())
for (ModelData model : data)
for (BogeyModelData[] data : contraptionModelData.values())
for (BogeyModelData model : data)
model.setEmptyTransform();
}
@ -256,9 +278,9 @@ public abstract class BogeyRenderer {
*/
public void updateLight(int blockLight, int skyLight) {
for (ModelData[] data : contraptionModelData.values())
for (ModelData model : data)
model.setBlockLight(blockLight).setSkyLight(skyLight);
for (BogeyModelData[] data : contraptionModelData.values())
for (BogeyModelData model : data)
model.updateLight(blockLight, skyLight);
}
/**
@ -267,8 +289,8 @@ public abstract class BogeyRenderer {
*/
public void remove() {
for (ModelData[] data : contraptionModelData.values())
for (ModelData model : data)
for (BogeyModelData[] data : contraptionModelData.values())
for (BogeyModelData model : data)
model.delete();
contraptionModelData.clear();
}
@ -301,4 +323,73 @@ public abstract class BogeyRenderer {
return null;
}
}
public record BogeyModelData(Transform<?> transform) implements Transform<BogeyModelData> {
public static BogeyModelData from(PartialModel model) {
BlockState air = Blocks.AIR.defaultBlockState();
return new BogeyModelData(CachedBufferer.partial(model, air));
}
public static BogeyModelData from(BlockState model) {
return new BogeyModelData(CachedBufferer.block(model));
}
public void render(PoseStack ms, int light, @Nullable VertexConsumer vb) {
transform.scale(1 - 1/512f);
if (transform instanceof SuperByteBuffer byteBuf && vb != null)
byteBuf.light(light).renderInto(ms, vb);
}
public BogeyModelData setTransform(PoseStack ms) {
if (this.transform instanceof ModelData model)
model.setTransform(ms);
return this;
}
public BogeyModelData setEmptyTransform() {
if (this.transform instanceof ModelData model)
model.setEmptyTransform();
return this;
}
public BogeyModelData delete() {
if (this.transform instanceof ModelData model)
model.delete();
return this;
}
public BogeyModelData updateLight(int blockLight, int skyLight) {
if (this.transform instanceof ModelData model)
model.setBlockLight(blockLight).setSkyLight(skyLight);
return this;
}
@Override
public BogeyModelData mulPose(Matrix4f pose) {
this.transform.mulPose(pose);
return this;
}
@Override
public BogeyModelData mulNormal(Matrix3f normal) {
this.transform.mulNormal(normal);
return this;
}
@Override
public BogeyModelData multiply(Quaternion quaternion) {
this.transform.multiply(quaternion);
return this;
}
@Override
public BogeyModelData scale(float factorX, float factorY, float factorZ) {
this.transform.scale(factorX, factorY, factorZ);
return this;
}
@Override
public BogeyModelData translate(double x, double y, double z) {
this.transform.translate(x, y, z);
return this;
}
}
}

View File

@ -13,6 +13,7 @@ import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.content.kinetics.simpleRelays.ShaftBlock;
import com.simibubi.create.content.trains.entity.CarriageBogey;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.Iterate;
@ -22,22 +23,22 @@ import net.minecraft.nbt.CompoundTag;
public class StandardBogeyRenderer {
public static class CommonStandardBogeyRenderer extends BogeyRenderer.CommonRenderer {
@Override
public void initialiseContraptionModelData(MaterialManager materialManager) {
createModelInstances(materialManager, AllBlocks.SHAFT.getDefaultState()
public void initialiseContraptionModelData(MaterialManager materialManager, CarriageBogey carriageBogey) {
createModelInstance(materialManager, AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.Z), 2);
}
@Override
public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb, boolean inContraption) {
boolean inInstancedContraption = vb == null;
Transform<?>[] shafts = getTransformsFromBlockState(AllBlocks.SHAFT.getDefaultState()
BogeyModelData[] shafts = getTransform(AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.Z), ms, inInstancedContraption, 2);
for (int i : Iterate.zeroAndOne) {
shafts[i].translate(-.5f, .25f, i * -1)
.centre()
.rotateZ(wheelAngle)
.unCentre();
finalize(shafts[i], ms, light, vb);
.unCentre()
.render(ms, light, vb);
}
}
}
@ -45,9 +46,9 @@ public class StandardBogeyRenderer {
public static class SmallStandardBogeyRenderer extends BogeyRenderer {
@Override
public void initialiseContraptionModelData(MaterialManager materialManager) {
createModelInstances(materialManager, SMALL_BOGEY_WHEELS, 2);
createModelInstances(materialManager, BOGEY_FRAME);
public void initialiseContraptionModelData(MaterialManager materialManager, CarriageBogey carriageBogey) {
createModelInstance(materialManager, SMALL_BOGEY_WHEELS, 2);
createModelInstance(materialManager, BOGEY_FRAME);
}
@ -59,17 +60,17 @@ public class StandardBogeyRenderer {
@Override
public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb, boolean inContraption) {
boolean inInstancedContraption = vb == null;
Transform<?> transform = getTransformFromPartial(BOGEY_FRAME, ms, inInstancedContraption);
finalize(transform, ms, light, vb);
getTransform(BOGEY_FRAME, ms, inInstancedContraption)
.render(ms, light, vb);
Transform<?>[] wheels = getTransformsFromPartial(SMALL_BOGEY_WHEELS, ms, inInstancedContraption, 2);
BogeyModelData[] wheels = getTransform(SMALL_BOGEY_WHEELS, ms, inInstancedContraption, 2);
for (int side : Iterate.positiveAndNegative) {
if (!inInstancedContraption)
ms.pushPose();
Transform<?> wheel = wheels[(side + 1)/2];
wheel.translate(0, 12 / 16f, side)
.rotateX(wheelAngle);
finalize(wheel, ms, light, vb);
wheels[(side + 1)/2]
.translate(0, 12 / 16f, side)
.rotateX(wheelAngle)
.render(ms, light, vb);
if (!inInstancedContraption)
ms.popPose();
}
@ -78,9 +79,9 @@ public class StandardBogeyRenderer {
public static class LargeStandardBogeyRenderer extends BogeyRenderer {
@Override
public void initialiseContraptionModelData(MaterialManager materialManager) {
createModelInstances(materialManager, LARGE_BOGEY_WHEELS, BOGEY_DRIVE, BOGEY_PISTON, BOGEY_PIN);
createModelInstances(materialManager, AllBlocks.SHAFT.getDefaultState()
public void initialiseContraptionModelData(MaterialManager materialManager, CarriageBogey carriageBogey) {
createModelInstance(materialManager, LARGE_BOGEY_WHEELS, BOGEY_DRIVE, BOGEY_PISTON, BOGEY_PIN);
createModelInstance(materialManager, AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.X), 2);
}
@ -93,39 +94,39 @@ public class StandardBogeyRenderer {
public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb, boolean inContraption) {
boolean inInstancedContraption = vb == null;
Transform<?>[] secondaryShafts = getTransformsFromBlockState(AllBlocks.SHAFT.getDefaultState()
BogeyModelData[] secondaryShafts = getTransform(AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.X), ms, inInstancedContraption, 2);
for (int i : Iterate.zeroAndOne) {
Transform<?> secondShaft = secondaryShafts[i];
secondShaft.translate(-.5f, .25f, .5f + i * -2)
secondaryShafts[i]
.translate(-.5f, .25f, .5f + i * -2)
.centre()
.rotateX(wheelAngle)
.unCentre();
finalize(secondShaft, ms, light, vb);
.unCentre()
.render(ms, light, vb);
}
Transform<?> bogeyDrive = getTransformFromPartial(BOGEY_DRIVE, ms, inInstancedContraption);
finalize(bogeyDrive, ms, light, vb);
getTransform(BOGEY_DRIVE, ms, inInstancedContraption)
.render(ms, light, vb);
Transform<?> bogeyPiston = getTransformFromPartial(BOGEY_PISTON, ms, inInstancedContraption)
.translate(0, 0, 1 / 4f * Math.sin(AngleHelper.rad(wheelAngle)));
finalize(bogeyPiston, ms, light, vb);
getTransform(BOGEY_PISTON, ms, inInstancedContraption)
.translate(0, 0, 1 / 4f * Math.sin(AngleHelper.rad(wheelAngle)))
.render(ms, light, vb);
if (!inInstancedContraption)
ms.pushPose();
Transform<?> bogeyWheels = getTransformFromPartial(LARGE_BOGEY_WHEELS, ms, inInstancedContraption)
getTransform(LARGE_BOGEY_WHEELS, ms, inInstancedContraption)
.translate(0, 1, 0)
.rotateX(wheelAngle);
finalize(bogeyWheels, ms, light, vb);
.rotateX(wheelAngle)
.render(ms, light, vb);
Transform<?> bogeyPin = getTransformFromPartial(BOGEY_PIN, ms, inInstancedContraption)
getTransform(BOGEY_PIN, ms, inInstancedContraption)
.translate(0, 1, 0)
.rotateX(wheelAngle)
.translate(0, 1 / 4f, 0)
.rotateX(-wheelAngle);
finalize(bogeyPin, ms, light, vb);
.rotateX(-wheelAngle)
.render(ms, light, vb);
if (!inInstancedContraption)
ms.popPose();