Implemented Changes To Remaining Classes

This commit is contained in:
Rabbitminers 2023-04-30 20:05:50 +01:00
parent 8aedc00f96
commit 0f7a8b7b24
7 changed files with 403 additions and 173 deletions

View file

@ -1,27 +1,98 @@
package com.simibubi.create; 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;
import com.simibubi.create.content.logistics.trains.BogeyRenderer.CommonRenderer;
import com.simibubi.create.content.logistics.trains.BogeySizes; import com.simibubi.create.content.logistics.trains.BogeySizes;
import com.simibubi.create.content.logistics.trains.StandardBogeyRenderer; import com.simibubi.create.content.logistics.trains.StandardBogeyRenderer.*;
import com.simibubi.create.content.logistics.trains.TestBogeyRenderer;
import com.simibubi.create.content.logistics.trains.entity.BogeyStyle; 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.LOGGER;
import static com.simibubi.create.Create.REGISTRATE;
@SuppressWarnings("unused")
public class AllBogeyStyles { public class AllBogeyStyles {
public static final RegistryEntry<BogeyStyle> STANDARD = REGISTRATE public static final Map<ResourceLocation, BogeyStyle> BOGEY_STYLES = new HashMap<>();
.bogeyStyle("standard", new BogeyStyle())
.block(BogeySizes.SMALL, AllBlocks.SMALL_BOGEY) public static BogeyStyle STANDARD = create("standard")
.block(BogeySizes.LARGE, AllBlocks.LARGE_BOGEY) .commonRenderer(CommonStandardBogeyRenderer::new)
.renderer(new StandardBogeyRenderer()) .size(BogeySizes.SMALL, SmallStandardBogeyRenderer::new, AllBlocks.SMALL_BOGEY)
.register(); .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() { public static void register() {
LOGGER.info("Registered bogey styles from " + Create.ID); LOGGER.info("Registered bogey styles from " + Create.ID);
AllRegistries.DEFERRED_BOGEY_REGISTRY.register(FMLJavaModLoadingContext.get().getModEventBus()); }
public static class BogeyStyleBuilder {
protected final Map<BogeySizes.BogeySize, BogeyStyle.SizeData> 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> 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<? extends BogeyRenderer> renderer,
BlockEntry<? extends AbstractBogeyBlock> blockEntry) {
this.size(size, renderer, blockEntry.getId());
return this;
}
public BogeyStyleBuilder size(BogeySizes.BogeySize size, Supplier<? extends BogeyRenderer> renderer,
ResourceLocation location) {
this.sizes.put(size, new BogeyStyle.SizeData(location, renderer.get()));
return this;
}
public BogeyStyleBuilder commonRenderer(Supplier<? extends CommonRenderer> 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;
}
} }
} }

View file

@ -2,9 +2,9 @@ package com.simibubi.create.content.logistics.trains;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -15,7 +15,6 @@ import com.mojang.math.Vector3f;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllBogeyStyles; import com.simibubi.create.AllBogeyStyles;
import com.simibubi.create.AllItems; import com.simibubi.create.AllItems;
import com.simibubi.create.AllRegistries;
import com.simibubi.create.content.contraptions.wrench.IWrenchable; import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.content.logistics.trains.entity.BogeyStyle; import com.simibubi.create.content.logistics.trains.entity.BogeyStyle;
import com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity; import com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity;
@ -110,7 +109,10 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void render(@Nullable BlockState state, float wheelAngle, PoseStack ms, float partialTicks, public void render(@Nullable BlockState state, float wheelAngle, PoseStack ms, float partialTicks,
MultiBufferSource buffers, int light, int overlay, StandardBogeyTileEntity sbte) { MultiBufferSource buffers, int light, int overlay, StandardBogeyTileEntity sbte) {
final BogeyRenderer renderer = sbte.getStyle().renderer; BogeyStyle style = sbte.getStyle();
final Optional<BogeyRenderer.CommonRenderer> commonRenderer
= style.getNewCommonRenderInstance();
final BogeyRenderer renderer = style.getInWorldRenderInstance(this.getSize());
if (state != null) { if (state != null) {
ms.translate(.5f, .5f, .5f); ms.translate(.5f, .5f, .5f);
if (state.getValue(AXIS) == Direction.Axis.X) if (state.getValue(AXIS) == Direction.Axis.X)
@ -118,7 +120,9 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
} }
ms.translate(0, -1.5 - 1 / 128f, 0); ms.translate(0, -1.5 - 1 / 128f, 0);
VertexConsumer vb = buffers.getBuffer(RenderType.cutoutMipped()); VertexConsumer vb = buffers.getBuffer(RenderType.cutoutMipped());
renderer.render(sbte.getBogeyData(), wheelAngle, ms, light, vb, getSize()); renderer.render(sbte.getBogeyData(), wheelAngle, ms, light, vb);
commonRenderer.ifPresent(common ->
common.render(sbte.getBogeyData(), wheelAngle, ms, light, vb));
} }
public BogeySizes.BogeySize getSize() { public BogeySizes.BogeySize getSize() {
@ -148,8 +152,8 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
ItemStack stack = player.getItemInHand(hand); ItemStack stack = player.getItemInHand(hand);
if (!player.isShiftKeyDown() && stack.is(AllItems.WRENCH.get()) && !player.getCooldowns().isOnCooldown(stack.getItem()) if (!player.isShiftKeyDown() && stack.is(AllItems.WRENCH.get()) && !player.getCooldowns().isOnCooldown(stack.getItem())
&& AllRegistries.BOGEY_REGISTRY.get().getValues().size() > 1) { && AllBogeyStyles.BOGEY_STYLES.size() > 1) {
Collection<BogeyStyle> styles = AllRegistries.BOGEY_REGISTRY.get().getValues(); Collection<BogeyStyle> styles = AllBogeyStyles.BOGEY_STYLES.values();
if (styles.size() <= 1) if (styles.size() <= 1)
return InteractionResult.PASS; return InteractionResult.PASS;
@ -245,15 +249,14 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
BlockEntity te = level.getBlockEntity(pos); BlockEntity te = level.getBlockEntity(pos);
if (te instanceof StandardBogeyTileEntity sbte) if (te instanceof StandardBogeyTileEntity sbte)
return this.getNextStyle(sbte.getStyle()); return this.getNextStyle(sbte.getStyle());
return AllBogeyStyles.STANDARD.get(); return AllBogeyStyles.STANDARD;
} }
public BogeyStyle getNextStyle(BogeyStyle style) { public BogeyStyle getNextStyle(BogeyStyle style) {
Collection<BogeyStyle> allStyles = AllRegistries.BOGEY_REGISTRY.get().getValues(); Collection<BogeyStyle> allStyles = AllBogeyStyles.BOGEY_STYLES.values();
if (allStyles.size() <= 1) if (allStyles.size() <= 1)
return style; return style;
List<BogeyStyle> list = new ArrayList<>(allStyles); List<BogeyStyle> list = new ArrayList<>(allStyles);
list.sort(Comparator.comparing(BogeyStyle::getRegistryName));
return Iterate.cycleValue(list, style); return Iterate.cycleValue(list, style);
} }

View file

@ -25,68 +25,153 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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 { public abstract class BogeyRenderer {
Map<BogeySizes.BogeySize, Renderer> renderers = new HashMap<>();
Map<String, ModelData[]> contraptionModelData = new HashMap<>(); Map<String, ModelData[]> 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) { public Transform<?>[] getTransformsFromPartial(PartialModel model, PoseStack ms, boolean inContraption, int size) {
return (inContraption) ? transformContraptionModelData(keyFromModel(model), ms) : createModelData(model, 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) { public Transform<?>[] getTransformsFromBlockState(BlockState state, PoseStack ms, boolean inContraption, int size) {
return (inContraption) ? transformContraptionModelData(keyFromModel(state), ms) : createModelData(state, 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) { private Transform<?>[] transformContraptionModelData(String key, PoseStack ms) {
ModelData[] modelData = contraptionModelData.get(key); ModelData[] modelData = contraptionModelData.get(key);
Arrays.stream(modelData).forEach(modelDataElement -> modelDataElement.setTransform(ms)); Arrays.stream(modelData).forEach(modelDataElement -> modelDataElement.setTransform(ms));
return modelData; 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) { private Transform<?>[] createModelData(PartialModel model, int size) {
BlockState air = Blocks.AIR.defaultBlockState(); BlockState air = Blocks.AIR.defaultBlockState();
SuperByteBuffer[] data = { CachedBufferer.partial(model, air) }; SuperByteBuffer[] data = { CachedBufferer.partial(model, air) };
return expandArrayToLength(data, size); 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) { private Transform<?>[] createModelData(BlockState state, int size) {
SuperByteBuffer[] data = { CachedBufferer.block(state) }; SuperByteBuffer[] data = { CachedBufferer.block(state) };
return expandArrayToLength(data, size); 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) { private Transform<?>[] expandArrayToLength(SuperByteBuffer[] data, int size) {
return Arrays.stream(Collections.nCopies(size, data).toArray()) return Arrays.stream(Collections.nCopies(size, data).toArray())
.flatMap(inner -> Arrays.stream((SuperByteBuffer[]) inner)) .flatMap(inner -> Arrays.stream((SuperByteBuffer[]) inner))
.toArray(SuperByteBuffer[]::new); .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) { public Transform<?> getTransformFromPartial(PartialModel model, PoseStack ms, boolean inContraption) {
BlockState air = Blocks.AIR.defaultBlockState(); BlockState air = Blocks.AIR.defaultBlockState();
return inContraption ? contraptionModelData.get(keyFromModel(model))[0].setTransform(ms) return inContraption ? contraptionModelData.get(keyFromModel(model))[0].setTransform(ms)
: CachedBufferer.partial(model, air); : 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) { public void createModelInstances(MaterialManager materialManager, PartialModel model, int count) {
ModelData[] modelData = new ModelData[count]; ModelData[] modelData = new ModelData[count];
materialManager.defaultSolid().material(Materials.TRANSFORMED) materialManager.defaultSolid().material(Materials.TRANSFORMED)
@ -94,6 +179,13 @@ public abstract class BogeyRenderer {
contraptionModelData.put(keyFromModel(model), modelData); 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) { public void createModelInstances(MaterialManager materialManager, BlockState state, int count) {
ModelData[] modelData = new ModelData[count]; ModelData[] modelData = new ModelData[count];
materialManager.defaultSolid().material(Materials.TRANSFORMED) materialManager.defaultSolid().material(Materials.TRANSFORMED)
@ -101,26 +193,26 @@ public abstract class BogeyRenderer {
contraptionModelData.put(keyFromModel(state), modelData); 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) { public void createModelInstances(MaterialManager materialManager, PartialModel... models) {
for (PartialModel model : models) for (PartialModel model : models)
createModelInstances(materialManager, model, 1); createModelInstances(materialManager, model, 1);
} }
@OnlyIn(Dist.CLIENT) /**
public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, @Nullable VertexConsumer vb, * Handles scale for all model data and renders non contraption model data
BogeySizes.BogeySize size) { *
renderCommon(bogeyData, wheelAngle, ms, light, vb); * @param b The model data itself
renderers.get(size).render(bogeyData, wheelAngle, ms, light, vb); * @param ms Pose stack to render to
} * @param light light level of the scene
* @param vb Vertex Consumber to render to
@OnlyIn(Dist.CLIENT) * @param <B> Generic alias for both contraption and in-world model data
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);
public static <B extends Transform<?>> void finalize(B b, PoseStack ms, int light, @Nullable VertexConsumer vb) { public static <B extends Transform<?>> void finalize(B b, PoseStack ms, int light, @Nullable VertexConsumer vb) {
b.scale(1 - 1/512f); b.scale(1 - 1/512f);
@ -128,13 +220,10 @@ public abstract class BogeyRenderer {
byteBuf.light(light).renderInto(ms, vb); byteBuf.light(light).renderInto(ms, vb);
} }
public boolean styleImplementsSize(BogeySizes.BogeySize size) { /**
return this.renderers.containsKey(size); * Automatic handling for setting empty transforms for all model data
} *
*/
public Set<BogeySizes.BogeySize> implementedSizes() {
return renderers.keySet();
}
public void emptyTransforms() { public void emptyTransforms() {
for (ModelData[] data : contraptionModelData.values()) for (ModelData[] data : contraptionModelData.values())
@ -142,12 +231,24 @@ public abstract class BogeyRenderer {
model.setEmptyTransform(); 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) { public void updateLight(int blockLight, int skyLight) {
for (ModelData[] data : contraptionModelData.values()) for (ModelData[] data : contraptionModelData.values())
for (ModelData model : data) for (ModelData model : data)
model.setBlockLight(blockLight).setSkyLight(skyLight); model.setBlockLight(blockLight).setSkyLight(skyLight);
} }
/**
* Automatic handling for clearing all model data of a contraption
*
*/
public void remove() { public void remove() {
for (ModelData[] data : contraptionModelData.values()) for (ModelData[] data : contraptionModelData.values())
for (ModelData model : data) for (ModelData model : data)
@ -155,18 +256,43 @@ public abstract class BogeyRenderer {
contraptionModelData.clear(); 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) { private String keyFromModel(PartialModel partialModel) {
return partialModel.getLocation().toString(); 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) { private String keyFromModel(BlockState state) {
return state.toString(); 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 { public static abstract class CommonRenderer extends BogeyRenderer {
void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb); @Override
public BogeySizes.BogeySize getSize() {
return null;
}
@Override
public abstract CommonRenderer createNewInstance();
} }
} }

View file

@ -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.SMALL_BOGEY_WHEELS;
import static com.simibubi.create.AllBlockPartials.BOGEY_FRAME; 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() { @Override
renderers.put(BogeySizes.SMALL, this::renderSmall); public CommonStandardBogeyRenderer createNewInstance() {
renderers.put(BogeySizes.LARGE, this::renderLarge); return new CommonStandardBogeyRenderer();
} }
@Override @Override
public void initialiseContraptionModelData(MaterialManager materialManager, BogeySizes.BogeySize size) { public void render(CompoundTag bogeyData, float wheelAngle, PoseStack ms, int light, VertexConsumer vb) {
// Large boolean inContraption = vb == null;
createModelInstances(materialManager, LARGE_BOGEY_WHEELS, BOGEY_DRIVE, BOGEY_PISTON, BOGEY_PIN); Transform<?>[] shafts = getTransformsFromBlockState(AllBlocks.SHAFT.getDefaultState()
createModelInstances(materialManager, AllBlocks.SHAFT.getDefaultState() .setValue(ShaftBlock.AXIS, Direction.Axis.Z), ms, inContraption, 2);
.setValue(ShaftBlock.AXIS, Direction.Axis.X), 2); for (int i : Iterate.zeroAndOne) {
// Small shafts[i].translate(-.5f, .25f, i * -1)
createModelInstances(materialManager, SMALL_BOGEY_WHEELS, 2); .centre()
createModelInstances(materialManager, BOGEY_FRAME); .rotateZ(wheelAngle)
// Common .unCentre();
createModelInstances(materialManager, AllBlocks.SHAFT.getDefaultState() finalize(shafts[i], ms, light, vb);
.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);
} }
} }
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); public static class SmallStandardBogeyRenderer extends BogeyRenderer {
for (int side : Iterate.positiveAndNegative) { @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) if (!inContraption)
ms.pushPose(); 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); .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) if (!inContraption)
ms.popPose(); 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();
}
} }

View file

@ -8,24 +8,31 @@ import com.simibubi.create.content.logistics.trains.BogeyRenderer;
import com.simibubi.create.content.logistics.trains.BogeySizes; import com.simibubi.create.content.logistics.trains.BogeySizes;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.BlockAndTintGetter; import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.LightLayer; import net.minecraft.world.level.LightLayer;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import java.util.Optional;
public final class BogeyInstance { public final class BogeyInstance {
private final BogeySizes.BogeySize size;
private final BogeyStyle style;
public final CarriageBogey bogey; public final CarriageBogey bogey;
public final BogeyRenderer renderer; public final BogeyRenderer renderer;
private final BogeySizes.BogeySize size; public final Optional<BogeyRenderer.CommonRenderer> commonRenderer;
public BogeyInstance(CarriageBogey bogey, BogeyRenderer renderer, BogeySizes.BogeySize size, public BogeyInstance(CarriageBogey bogey, BogeyStyle style, BogeySizes.BogeySize size, MaterialManager materialManager) {
MaterialManager materialManager) {
this.bogey = bogey; this.bogey = bogey;
this.renderer = renderer;
this.size = size; 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() { void hiddenFrame() {
@ -38,11 +45,16 @@ public final class BogeyInstance {
return; 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) { public void updateLight(BlockAndTintGetter world, CarriageContraptionEntity entity) {
var lightPos = new BlockPos(getLightPos(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), renderer.updateLight(world.getBrightness(LightLayer.BLOCK, lightPos),
world.getBrightness(LightLayer.SKY, lightPos)); world.getBrightness(LightLayer.SKY, lightPos));
} }

View file

@ -6,7 +6,6 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.simibubi.create.AllRegistries;
import com.simibubi.create.CreateClient; import com.simibubi.create.CreateClient;
import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock; import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock;
import com.simibubi.create.foundation.networking.SimplePacketBase; 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.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraftforge.network.NetworkEvent.Context; import net.minecraftforge.network.NetworkEvent.Context;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import javax.annotation.Nullable;
public class TrainPacket extends SimplePacketBase { public class TrainPacket extends SimplePacketBase {
UUID trainId; UUID trainId;

View file

@ -1,13 +1,10 @@
package com.simibubi.create.content.logistics.trains.track; package com.simibubi.create.content.logistics.trains.track;
import com.simibubi.create.AllBogeyStyles; 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.AbstractBogeyBlock;
import com.simibubi.create.content.logistics.trains.entity.BogeyStyle; import com.simibubi.create.content.logistics.trains.entity.BogeyStyle;
import com.simibubi.create.foundation.tileEntity.CachedRenderBBTileEntity; import com.simibubi.create.foundation.tileEntity.CachedRenderBBTileEntity;
import com.simibubi.create.foundation.utility.NBTHelper; import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.RegisteredObjects;
import com.simibubi.create.foundation.utility.animation.LerpedFloat; import com.simibubi.create.foundation.utility.animation.LerpedFloat;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -38,14 +35,14 @@ public class StandardBogeyTileEntity extends CachedRenderBBTileEntity {
public void setBogeyData(@NotNull CompoundTag newData) { public void setBogeyData(@NotNull CompoundTag newData) {
if (!newData.contains(BOGEY_STYLE_KEY)) { if (!newData.contains(BOGEY_STYLE_KEY)) {
ResourceLocation style = AllBogeyStyles.STANDARD.getId(); ResourceLocation style = AllBogeyStyles.STANDARD.name;
NBTHelper.writeResourceLocation(newData, BOGEY_STYLE_KEY, style); NBTHelper.writeResourceLocation(newData, BOGEY_STYLE_KEY, style);
} }
this.bogeyData = newData; this.bogeyData = newData;
} }
public void setBogeyStyle(@NotNull BogeyStyle style) { public void setBogeyStyle(@NotNull BogeyStyle style) {
ResourceLocation location = RegisteredObjects.getKeyOrThrow(AllRegistries.BOGEY_REGISTRY.get(), style); ResourceLocation location = style.name;
CompoundTag data = this.getBogeyData(); CompoundTag data = this.getBogeyData();
NBTHelper.writeResourceLocation(data, BOGEY_STYLE_KEY, location); NBTHelper.writeResourceLocation(data, BOGEY_STYLE_KEY, location);
markUpdated(); markUpdated();
@ -55,9 +52,9 @@ public class StandardBogeyTileEntity extends CachedRenderBBTileEntity {
public BogeyStyle getStyle() { public BogeyStyle getStyle() {
CompoundTag data = this.getBogeyData(); CompoundTag data = this.getBogeyData();
ResourceLocation currentStyle = NBTHelper.readResourceLocation(data, BOGEY_STYLE_KEY); 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) { if (style == null) {
setBogeyStyle(AllBogeyStyles.STANDARD.get()); setBogeyStyle(AllBogeyStyles.STANDARD);
return getStyle(); return getStyle();
} }
return style; return style;
@ -81,7 +78,7 @@ public class StandardBogeyTileEntity extends CachedRenderBBTileEntity {
private CompoundTag createBogeyData() { private CompoundTag createBogeyData() {
CompoundTag nbt = new CompoundTag(); CompoundTag nbt = new CompoundTag();
NBTHelper.writeResourceLocation(nbt, BOGEY_STYLE_KEY, AllBogeyStyles.STANDARD.getId()); NBTHelper.writeResourceLocation(nbt, BOGEY_STYLE_KEY, AllBogeyStyles.STANDARD.name);
return nbt; return nbt;
} }