diff --git a/src/main/java/com/simibubi/create/AllBogeyStyles.java b/src/main/java/com/simibubi/create/AllBogeyStyles.java index be789fdd7..17cccf4b8 100644 --- a/src/main/java/com/simibubi/create/AllBogeyStyles.java +++ b/src/main/java/com/simibubi/create/AllBogeyStyles.java @@ -1,27 +1,98 @@ package com.simibubi.create; +import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock; import com.simibubi.create.content.logistics.trains.BogeyRenderer; +import com.simibubi.create.content.logistics.trains.BogeyRenderer.CommonRenderer; import com.simibubi.create.content.logistics.trains.BogeySizes; -import com.simibubi.create.content.logistics.trains.StandardBogeyRenderer; -import com.simibubi.create.content.logistics.trains.TestBogeyRenderer; +import com.simibubi.create.content.logistics.trains.StandardBogeyRenderer.*; import com.simibubi.create.content.logistics.trains.entity.BogeyStyle; -import com.tterrag.registrate.util.entry.RegistryEntry; -import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; + +import com.simibubi.create.foundation.utility.Lang; + +import com.tterrag.registrate.util.entry.BlockEntry; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; import static com.simibubi.create.Create.LOGGER; -import static com.simibubi.create.Create.REGISTRATE; -@SuppressWarnings("unused") public class AllBogeyStyles { - public static final RegistryEntry STANDARD = REGISTRATE - .bogeyStyle("standard", new BogeyStyle()) - .block(BogeySizes.SMALL, AllBlocks.SMALL_BOGEY) - .block(BogeySizes.LARGE, AllBlocks.LARGE_BOGEY) - .renderer(new StandardBogeyRenderer()) - .register(); + public static final Map BOGEY_STYLES = new HashMap<>(); + + public static BogeyStyle STANDARD = create("standard") + .commonRenderer(CommonStandardBogeyRenderer::new) + .size(BogeySizes.SMALL, SmallStandardBogeyRenderer::new, AllBlocks.SMALL_BOGEY) + .size(BogeySizes.LARGE, LargeStandardBogeyRenderer::new, AllBlocks.LARGE_BOGEY) + .build(); + + public static BogeyStyleBuilder create(String name) { + return create(Create.asResource(name)); + } + + public static BogeyStyleBuilder create(ResourceLocation name) { + return new BogeyStyleBuilder(name); + } public static void register() { LOGGER.info("Registered bogey styles from " + Create.ID); - AllRegistries.DEFERRED_BOGEY_REGISTRY.register(FMLJavaModLoadingContext.get().getModEventBus()); + } + + public static class BogeyStyleBuilder { + protected final Map sizes = new HashMap<>(); + protected final ResourceLocation name; + + protected Component displayName = Lang.translateDirect("create.bogeys.invalid"); + protected ResourceLocation soundType = AllSoundEvents.TRAIN2.getId(); + protected CompoundTag defaultData = new CompoundTag(); + protected Optional commonRenderer = Optional.empty(); + + public BogeyStyleBuilder(ResourceLocation name) { + this.name = name; + } + + public BogeyStyleBuilder displayName(Component displayName) { + this.displayName = displayName; + return this; + } + + public BogeyStyleBuilder soundType(ResourceLocation soundType) { + this.soundType = soundType; + return this; + } + + public BogeyStyleBuilder defaultData(CompoundTag defaultData) { + this.defaultData = defaultData; + return this; + } + + public BogeyStyleBuilder size(BogeySizes.BogeySize size, Supplier renderer, + BlockEntry blockEntry) { + this.size(size, renderer, blockEntry.getId()); + return this; + } + + public BogeyStyleBuilder size(BogeySizes.BogeySize size, Supplier renderer, + ResourceLocation location) { + this.sizes.put(size, new BogeyStyle.SizeData(location, renderer.get())); + return this; + } + + public BogeyStyleBuilder commonRenderer(Supplier commonRenderer) { + this.commonRenderer = Optional.of(commonRenderer.get()); + return this; + } + + public BogeyStyle build() { + BogeyStyle entry = + new BogeyStyle(name, displayName, soundType, defaultData, sizes, commonRenderer); + BOGEY_STYLES.put(name, entry); + return entry; + } } } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/AbstractBogeyBlock.java b/src/main/java/com/simibubi/create/content/logistics/trains/AbstractBogeyBlock.java index 83d223d64..d3691bde0 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/AbstractBogeyBlock.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/AbstractBogeyBlock.java @@ -2,9 +2,9 @@ package com.simibubi.create.content.logistics.trains; import java.util.ArrayList; import java.util.Collection; -import java.util.Comparator; import java.util.EnumSet; import java.util.List; +import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; @@ -15,7 +15,6 @@ import com.mojang.math.Vector3f; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBogeyStyles; import com.simibubi.create.AllItems; -import com.simibubi.create.AllRegistries; import com.simibubi.create.content.contraptions.wrench.IWrenchable; import com.simibubi.create.content.logistics.trains.entity.BogeyStyle; import com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity; @@ -110,7 +109,10 @@ public abstract class AbstractBogeyBlock extends Block implements ITE commonRenderer + = style.getNewCommonRenderInstance(); + final BogeyRenderer renderer = style.getInWorldRenderInstance(this.getSize()); if (state != null) { ms.translate(.5f, .5f, .5f); if (state.getValue(AXIS) == Direction.Axis.X) @@ -118,7 +120,9 @@ public abstract class AbstractBogeyBlock extends Block implements ITE + common.render(sbte.getBogeyData(), wheelAngle, ms, light, vb)); } public BogeySizes.BogeySize getSize() { @@ -148,8 +152,8 @@ public abstract class AbstractBogeyBlock extends Block implements ITE 1) { - Collection styles = AllRegistries.BOGEY_REGISTRY.get().getValues(); + && AllBogeyStyles.BOGEY_STYLES.size() > 1) { + Collection styles = AllBogeyStyles.BOGEY_STYLES.values(); if (styles.size() <= 1) return InteractionResult.PASS; @@ -245,15 +249,14 @@ public abstract class AbstractBogeyBlock extends Block implements ITE allStyles = AllRegistries.BOGEY_REGISTRY.get().getValues(); + Collection allStyles = AllBogeyStyles.BOGEY_STYLES.values(); if (allStyles.size() <= 1) return style; List list = new ArrayList<>(allStyles); - list.sort(Comparator.comparing(BogeyStyle::getRegistryName)); return Iterate.cycleValue(list, style); } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java b/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java index ae7ce39ce..5839646ce 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java @@ -25,68 +25,153 @@ import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collections; -import java.util.EnumMap; import java.util.HashMap; import java.util.Map; -import java.util.Set; - -/* - * Proof of concept implementation of a common and generic bogey render - * - Model data is handled as a generic Transform which allows them to be modified in a common manner and then - * "finalised" seperately - * - Model instances (used for contraptions) are stored in a map keyed by the partial type, this should probably - * be replaced as it has a few drawbacks notably all model data is the same between sizes for the same type - * - Lighting and removal is automatically handled by collecting values from the map - * - Renderers are stored by type so they can be easily added or removed for specific sizes without individual - * overriden methods for each size - */ - -// Seperate From BogeyInstance So It Can Be Used Inworld public abstract class BogeyRenderer { - Map renderers = new HashMap<>(); Map contraptionModelData = new HashMap<>(); + /** + * A common interface for getting transform data for both in-world and in-contraption model data safely from a + * partial model + * + * @param model The key for the model data to instantiate or retrieve + * @param ms The posestack used for contraption model data + * @param inContraption The type of model needed + * @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 inContraption, int size) { return (inContraption) ? transformContraptionModelData(keyFromModel(model), ms) : createModelData(model, size); } + /** + * A common interface for getting transform data for both in-world and in-contraption model data safely from a + * blockstate + * + * @param state The key for the model data to instantiate or retrieve + * @param ms The posestack used for contraption model data + * @param inContraption The type of model needed + * @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) { return (inContraption) ? transformContraptionModelData(keyFromModel(state), ms) : createModelData(state, size); } + /** + * Used for calling both in-world and in-contraption rendering + * + * @param bogeyData Custom data stored on the bogey able to be used for rendering + * @param wheelAngle The angle of the wheel + * @param ms The posestack to render to + * @param light (Optional) Light used for in-world rendering + * @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); + + /** + * Used for calling in-contraption rendering ensuring that falsey data is handled correctly + * + * @param bogeyData Custom data stored on the bogey able to be used for rendering + * @param wheelAngle The angle of the wheel + * @param ms The posestack to render to + */ + @OnlyIn(Dist.CLIENT) + public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms) { + this.render(bogeyData, wheelAngle, ms, 0, null); + } + + public abstract BogeySizes.BogeySize getSize(); + + /** + * Used to collect Contraption Model Data for in-contraption rendering, should not be utilised directly when + * rendering to prevent render type mismatch + * + * @param key The key used to access the model + * @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); Arrays.stream(modelData).forEach(modelDataElement -> modelDataElement.setTransform(ms)); return modelData; } + + /** + * Used for in world rendering, creates a set count of model data to be rendered, allowing for a generic response + * when rendering multiple models both in-world and in-contraption for example, with wheels + * + * @param model The partial model of the model data ot be made + * @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) }; return expandArrayToLength(data, size); } + /** + * Used for in world rendering, creates a set count of model data to be rendered, allowing for a generic response + * when rendering multiple models both in-world and in-contraption for example, with wheels + * + * @param state The state of the model data to be made + * @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) }; return expandArrayToLength(data, size); } + /** + * Utility function to clone in-world models to a set size to allow for common handling of rendering with multiple + * instances of the same model for example with wheels + * + * @param data An in-world model to be replicated + * @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) { 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 inContraption 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 inContraption) { BlockState air = Blocks.AIR.defaultBlockState(); return inContraption ? contraptionModelData.get(keyFromModel(model))[0].setTransform(ms) : CachedBufferer.partial(model, air); } - @OnlyIn(Dist.CLIENT) - public abstract void initialiseContraptionModelData(MaterialManager materialManager, BogeySizes.BogeySize size); + /** + * Provides render implementations a point in setup to instantiate all model data to be needed + * + * @param materialManager The material manager + */ + @OnlyIn(Dist.CLIENT) + public abstract void initialiseContraptionModelData(MaterialManager materialManager); + + /** + * Creates instances of models for in-world rendering to a set length from a provided partial model + * + * @param materialManager The material manager + * @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) @@ -94,6 +179,13 @@ public abstract class BogeyRenderer { contraptionModelData.put(keyFromModel(model), modelData); } + /** + * Creates instances of models for in-world rendering to a set length from a provided blockstate + * + * @param materialManager The material manager + * @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) @@ -101,26 +193,26 @@ public abstract class BogeyRenderer { contraptionModelData.put(keyFromModel(state), modelData); } + /** + * Helper function to create a single model instance for in-contraption rendering + * + * @param materialManager The material manager + * @param models The type of model to create instances of + */ public void createModelInstances(MaterialManager materialManager, PartialModel... models) { for (PartialModel model : models) createModelInstances(materialManager, model, 1); } - @OnlyIn(Dist.CLIENT) - public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, @Nullable VertexConsumer vb, - BogeySizes.BogeySize size) { - renderCommon(bogeyData, wheelAngle, ms, light, vb); - renderers.get(size).render(bogeyData, wheelAngle, ms, light, vb); - } - - @OnlyIn(Dist.CLIENT) - public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, BogeySizes.BogeySize size) { - this.render(bogeyData, wheelAngle, ms, 0, null, size); - } - - @OnlyIn(Dist.CLIENT) - public abstract void renderCommon(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, - @Nullable VertexConsumer vb); + /** + * Handles scale for all model data and renders non contraption model data + * + * @param b The model data itself + * @param ms Pose stack to render to + * @param light light level of the scene + * @param vb Vertex Consumber to render to + * @param Generic alias for both contraption and in-world model data + */ public static > void finalize(B b, PoseStack ms, int light, @Nullable VertexConsumer vb) { b.scale(1 - 1/512f); @@ -128,13 +220,10 @@ public abstract class BogeyRenderer { byteBuf.light(light).renderInto(ms, vb); } - public boolean styleImplementsSize(BogeySizes.BogeySize size) { - return this.renderers.containsKey(size); - } - - public Set implementedSizes() { - return renderers.keySet(); - } + /** + * Automatic handling for setting empty transforms for all model data + * + */ public void emptyTransforms() { for (ModelData[] data : contraptionModelData.values()) @@ -142,12 +231,24 @@ public abstract class BogeyRenderer { model.setEmptyTransform(); } + /** + * Automatic handling for updating all model data's light + * + * @param blockLight the blocklight to be applied + * @param skyLight the skylight to be applied + */ + public void updateLight(int blockLight, int skyLight) { for (ModelData[] data : contraptionModelData.values()) for (ModelData model : data) model.setBlockLight(blockLight).setSkyLight(skyLight); } + /** + * Automatic handling for clearing all model data of a contraption + * + */ + public void remove() { for (ModelData[] data : contraptionModelData.values()) for (ModelData model : data) @@ -155,18 +256,43 @@ public abstract class BogeyRenderer { contraptionModelData.clear(); } + /** + * Create a model key from a partial model, so it can be easily accessed + * + * @param partialModel the model we want a unique key for + * @return Key of the model + */ + private String keyFromModel(PartialModel partialModel) { return partialModel.getLocation().toString(); } + /** + * Create a model key from a blockstate, so it can be easily accessed + * + * @param state Blockstate of the model + * @return Key of the model + */ + private String keyFromModel(BlockState state) { return state.toString(); } - public abstract BogeyRenderer newInstance(); + /** + * Used for rendering in contraptions allowing for individual instances of models for each component + * + * @return new Instance of renderer + */ + public abstract BogeyRenderer createNewInstance(); - @FunctionalInterface - interface Renderer { - void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb); + + public static abstract class CommonRenderer extends BogeyRenderer { + @Override + public BogeySizes.BogeySize getSize() { + return null; + } + + @Override + public abstract CommonRenderer createNewInstance(); } } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/StandardBogeyRenderer.java b/src/main/java/com/simibubi/create/content/logistics/trains/StandardBogeyRenderer.java index 1e3551332..2dddffba2 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/StandardBogeyRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/StandardBogeyRenderer.java @@ -22,105 +22,130 @@ import static com.simibubi.create.AllBlockPartials.BOGEY_PISTON; import static com.simibubi.create.AllBlockPartials.SMALL_BOGEY_WHEELS; import static com.simibubi.create.AllBlockPartials.BOGEY_FRAME; -public class StandardBogeyRenderer extends BogeyRenderer { +public class StandardBogeyRenderer { + public static class CommonStandardBogeyRenderer extends BogeyRenderer.CommonRenderer { + @Override + public void initialiseContraptionModelData(MaterialManager materialManager) { + createModelInstances(materialManager, AllBlocks.SHAFT.getDefaultState() + .setValue(ShaftBlock.AXIS, Direction.Axis.Z), 2); + } - public StandardBogeyRenderer() { - renderers.put(BogeySizes.SMALL, this::renderSmall); - renderers.put(BogeySizes.LARGE, this::renderLarge); - } + @Override + public CommonStandardBogeyRenderer createNewInstance() { + return new CommonStandardBogeyRenderer(); + } - @Override - public void initialiseContraptionModelData(MaterialManager materialManager, BogeySizes.BogeySize size) { - // Large - createModelInstances(materialManager, LARGE_BOGEY_WHEELS, BOGEY_DRIVE, BOGEY_PISTON, BOGEY_PIN); - createModelInstances(materialManager, AllBlocks.SHAFT.getDefaultState() - .setValue(ShaftBlock.AXIS, Direction.Axis.X), 2); - // Small - createModelInstances(materialManager, SMALL_BOGEY_WHEELS, 2); - createModelInstances(materialManager, BOGEY_FRAME); - // Common - createModelInstances(materialManager, AllBlocks.SHAFT.getDefaultState() - .setValue(ShaftBlock.AXIS, Direction.Axis.Z), 2); - } - - @Override - public void renderCommon(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, @Nullable VertexConsumer vb) { - boolean inContraption = vb == null; - Transform[] shafts = getTransformsFromBlockState(AllBlocks.SHAFT.getDefaultState() - .setValue(ShaftBlock.AXIS, Direction.Axis.Z), ms, inContraption, 2); - for (int i : Iterate.zeroAndOne) { - shafts[i].translate(-.5f, .25f, i * -1) - .centre() - .rotateZ(wheelAngle) - .unCentre(); - finalize(shafts[i], ms, light, vb); + @Override + public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb) { + boolean inContraption = vb == null; + Transform[] shafts = getTransformsFromBlockState(AllBlocks.SHAFT.getDefaultState() + .setValue(ShaftBlock.AXIS, Direction.Axis.Z), ms, inContraption, 2); + for (int i : Iterate.zeroAndOne) { + shafts[i].translate(-.5f, .25f, i * -1) + .centre() + .rotateZ(wheelAngle) + .unCentre(); + finalize(shafts[i], ms, light, vb); + } } } - public void renderSmall(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, - @Nullable VertexConsumer vb) { - boolean inContraption = vb == null; - Transform transform = getTransformFromPartial(BOGEY_FRAME, ms, inContraption); - finalize(transform, ms, light, vb); - Transform[] wheels = getTransformsFromPartial(SMALL_BOGEY_WHEELS, ms, inContraption, 2); - for (int side : Iterate.positiveAndNegative) { + public static class SmallStandardBogeyRenderer extends BogeyRenderer { + @Override + public void initialiseContraptionModelData(MaterialManager materialManager) { + createModelInstances(materialManager, SMALL_BOGEY_WHEELS, 2); + createModelInstances(materialManager, BOGEY_FRAME); + } + + @Override + public BogeyRenderer createNewInstance() { + return new SmallStandardBogeyRenderer(); + } + + @Override + public BogeySizes.BogeySize getSize() { + return BogeySizes.SMALL; + } + + @Override + public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb) { + boolean inContraption = vb == null; + Transform transform = getTransformFromPartial(BOGEY_FRAME, ms, inContraption); + finalize(transform, ms, light, vb); + + Transform[] wheels = getTransformsFromPartial(SMALL_BOGEY_WHEELS, ms, inContraption, 2); + for (int side : Iterate.positiveAndNegative) { + if (!inContraption) + ms.pushPose(); + Transform wheel = wheels[(side + 1)/2]; + wheel.translate(0, 12 / 16f, side) + .rotateX(wheelAngle); + finalize(wheel, ms, light, vb); + if (!inContraption) + ms.popPose(); + } + } + } + + 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() + .setValue(ShaftBlock.AXIS, Direction.Axis.X), 2); + } + + @Override + public BogeyRenderer createNewInstance() { + return new LargeStandardBogeyRenderer(); + } + + @Override + public BogeySizes.BogeySize getSize() { + return BogeySizes.LARGE; + } + + @Override + public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb) { + boolean inContraption = vb == null; + + Transform[] secondaryShafts = getTransformsFromBlockState(AllBlocks.SHAFT.getDefaultState() + .setValue(ShaftBlock.AXIS, Direction.Axis.X), ms, inContraption, 2); + + for (int i : Iterate.zeroAndOne) { + Transform secondShaft = secondaryShafts[i]; + secondShaft.translate(-.5f, .25f, .5f + i * -2) + .centre() + .rotateX(wheelAngle) + .unCentre(); + finalize(secondShaft, ms, light, vb); + } + + Transform bogeyDrive = getTransformFromPartial(BOGEY_DRIVE, ms, inContraption); + finalize(bogeyDrive, ms, light, vb); + + Transform bogeyPiston = getTransformFromPartial(BOGEY_PISTON, ms, inContraption) + .translate(0, 0, 1 / 4f * Math.sin(AngleHelper.rad(wheelAngle))); + finalize(bogeyPiston, ms, light, vb); + if (!inContraption) ms.pushPose(); - Transform wheel = wheels[(side + 1)/2]; - wheel.translate(0, 12 / 16f, side) + + Transform bogeyWheels = getTransformFromPartial(LARGE_BOGEY_WHEELS, ms, inContraption) + .translate(0, 1, 0) .rotateX(wheelAngle); - finalize(wheel, ms, light, vb); + finalize(bogeyWheels, ms, light, vb); + + Transform bogeyPin = getTransformFromPartial(BOGEY_PIN, ms, inContraption) + .translate(0, 1, 0) + .rotateX(wheelAngle) + .translate(0, 1 / 4f, 0) + .rotateX(-wheelAngle); + finalize(bogeyPin, ms, light, vb); + if (!inContraption) ms.popPose(); } } - - public void renderLarge(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, - @Nullable VertexConsumer vb) { - - boolean inContraption = vb == null; - - Transform[] secondaryShafts = getTransformsFromBlockState(AllBlocks.SHAFT.getDefaultState() - .setValue(ShaftBlock.AXIS, Direction.Axis.X), ms, inContraption, 2); - - for (int i : Iterate.zeroAndOne) { - Transform secondShaft = secondaryShafts[i]; - secondShaft.translate(-.5f, .25f, .5f + i * -2) - .centre() - .rotateX(wheelAngle) - .unCentre(); - finalize(secondShaft, ms, light, vb); - } - - Transform bogeyDrive = getTransformFromPartial(BOGEY_DRIVE, ms, inContraption); - finalize(bogeyDrive, ms, light, vb); - - Transform bogeyPiston = getTransformFromPartial(BOGEY_PISTON, ms, inContraption) - .translate(0, 0, 1 / 4f * Math.sin(AngleHelper.rad(wheelAngle))); - finalize(bogeyPiston, ms, light, vb); - - if (!inContraption) - ms.pushPose(); - - Transform bogeyWheels = getTransformFromPartial(LARGE_BOGEY_WHEELS, ms, inContraption) - .translate(0, 1, 0) - .rotateX(wheelAngle); - finalize(bogeyWheels, ms, light, vb); - - Transform bogeyPin = getTransformFromPartial(BOGEY_PIN, ms, inContraption) - .translate(0, 1, 0) - .rotateX(wheelAngle) - .translate(0, 1 / 4f, 0) - .rotateX(-wheelAngle); - finalize(bogeyPin, ms, light, vb); - - if (!inContraption) - ms.popPose(); - } - - @Override - public BogeyRenderer newInstance() { - return new StandardBogeyRenderer(); - } } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java index e868a15b6..a9b0ff8af 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java @@ -8,24 +8,31 @@ import com.simibubi.create.content.logistics.trains.BogeyRenderer; import com.simibubi.create.content.logistics.trains.BogeySizes; import net.minecraft.core.BlockPos; -import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.BlockAndTintGetter; import net.minecraft.world.level.LightLayer; import net.minecraft.world.phys.Vec3; +import java.util.Optional; + public final class BogeyInstance { + private final BogeySizes.BogeySize size; + private final BogeyStyle style; public final CarriageBogey bogey; public final BogeyRenderer renderer; - private final BogeySizes.BogeySize size; + public final Optional commonRenderer; - public BogeyInstance(CarriageBogey bogey, BogeyRenderer renderer, BogeySizes.BogeySize size, - MaterialManager materialManager) { + public BogeyInstance(CarriageBogey bogey, BogeyStyle style, BogeySizes.BogeySize size, MaterialManager materialManager) { this.bogey = bogey; - this.renderer = renderer; this.size = size; + this.style = style; - renderer.initialiseContraptionModelData(materialManager, size); + this.renderer = this.style.createRendererInstance(this.size); + this.commonRenderer = this.style.getNewCommonRenderInstance(); + + commonRenderer.ifPresent(bogeyRenderer -> + bogeyRenderer.initialiseContraptionModelData(materialManager)); + renderer.initialiseContraptionModelData(materialManager); } void hiddenFrame() { @@ -38,11 +45,16 @@ public final class BogeyInstance { return; } - renderer.render(new CompoundTag(), wheelAngle, ms, this.size); + commonRenderer.ifPresent(bogeyRenderer -> + bogeyRenderer.render(bogey.bogeyData, wheelAngle, ms)); + renderer.render(bogey.bogeyData, wheelAngle, ms); } public void updateLight(BlockAndTintGetter world, CarriageContraptionEntity entity) { var lightPos = new BlockPos(getLightPos(entity)); + commonRenderer.ifPresent(bogeyRenderer + -> bogeyRenderer.updateLight(world.getBrightness(LightLayer.BLOCK, lightPos), + world.getBrightness(LightLayer.SKY, lightPos))); renderer.updateLight(world.getBrightness(LightLayer.BLOCK, lightPos), world.getBrightness(LightLayer.SKY, lightPos)); } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/TrainPacket.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/TrainPacket.java index 73ed8b505..3d27c86cd 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/TrainPacket.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/TrainPacket.java @@ -6,7 +6,6 @@ import java.util.Map; import java.util.UUID; import java.util.function.Supplier; -import com.simibubi.create.AllRegistries; import com.simibubi.create.CreateClient; import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock; import com.simibubi.create.foundation.networking.SimplePacketBase; @@ -17,13 +16,10 @@ import com.simibubi.create.foundation.utility.RegisteredObjects; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.Block; import net.minecraftforge.network.NetworkEvent.Context; import net.minecraftforge.registries.ForgeRegistries; -import javax.annotation.Nullable; - public class TrainPacket extends SimplePacketBase { UUID trainId; diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/track/StandardBogeyTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/trains/track/StandardBogeyTileEntity.java index a6c9185bb..f54183da0 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/track/StandardBogeyTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/track/StandardBogeyTileEntity.java @@ -1,13 +1,10 @@ package com.simibubi.create.content.logistics.trains.track; import com.simibubi.create.AllBogeyStyles; -import com.simibubi.create.AllRegistries; -import com.simibubi.create.content.contraptions.processing.BasinTileEntity; import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock; import com.simibubi.create.content.logistics.trains.entity.BogeyStyle; import com.simibubi.create.foundation.tileEntity.CachedRenderBBTileEntity; import com.simibubi.create.foundation.utility.NBTHelper; -import com.simibubi.create.foundation.utility.RegisteredObjects; import com.simibubi.create.foundation.utility.animation.LerpedFloat; import net.minecraft.core.BlockPos; @@ -38,14 +35,14 @@ public class StandardBogeyTileEntity extends CachedRenderBBTileEntity { public void setBogeyData(@NotNull CompoundTag newData) { if (!newData.contains(BOGEY_STYLE_KEY)) { - ResourceLocation style = AllBogeyStyles.STANDARD.getId(); + ResourceLocation style = AllBogeyStyles.STANDARD.name; NBTHelper.writeResourceLocation(newData, BOGEY_STYLE_KEY, style); } this.bogeyData = newData; } public void setBogeyStyle(@NotNull BogeyStyle style) { - ResourceLocation location = RegisteredObjects.getKeyOrThrow(AllRegistries.BOGEY_REGISTRY.get(), style); + ResourceLocation location = style.name; CompoundTag data = this.getBogeyData(); NBTHelper.writeResourceLocation(data, BOGEY_STYLE_KEY, location); markUpdated(); @@ -55,9 +52,9 @@ public class StandardBogeyTileEntity extends CachedRenderBBTileEntity { public BogeyStyle getStyle() { CompoundTag data = this.getBogeyData(); ResourceLocation currentStyle = NBTHelper.readResourceLocation(data, BOGEY_STYLE_KEY); - BogeyStyle style = AllRegistries.BOGEY_REGISTRY.get().getValue(currentStyle); + BogeyStyle style = AllBogeyStyles.BOGEY_STYLES.get(currentStyle); if (style == null) { - setBogeyStyle(AllBogeyStyles.STANDARD.get()); + setBogeyStyle(AllBogeyStyles.STANDARD); return getStyle(); } return style; @@ -81,7 +78,7 @@ public class StandardBogeyTileEntity extends CachedRenderBBTileEntity { private CompoundTag createBogeyData() { CompoundTag nbt = new CompoundTag(); - NBTHelper.writeResourceLocation(nbt, BOGEY_STYLE_KEY, AllBogeyStyles.STANDARD.getId()); + NBTHelper.writeResourceLocation(nbt, BOGEY_STYLE_KEY, AllBogeyStyles.STANDARD.name); return nbt; }