Linked Style Registry To Bogey Blocks

This commit is contained in:
Rabbitminers 2023-04-01 18:39:58 +01:00
parent 71f839ee51
commit 0c0b5a1ed6
12 changed files with 119 additions and 37 deletions

View file

@ -1,6 +1,7 @@
package com.simibubi.create;
import com.simibubi.create.content.logistics.trains.BogeyRenderer;
import com.simibubi.create.content.logistics.trains.StandardBogeyRenderer;
import com.simibubi.create.content.logistics.trains.entity.BogeyStyle;
import com.simibubi.create.content.logistics.trains.entity.StandardBogeyInstance;
import com.tterrag.registrate.util.entry.RegistryEntry;
@ -12,9 +13,10 @@ import static com.simibubi.create.Create.REGISTRATE;
@SuppressWarnings("unused")
public class AllBogeyStyles {
public static final RegistryEntry<BogeyStyle> STANDARD = REGISTRATE
.bogeyStyle("standard", new BogeyStyle())
.bogeyStyle("standard", new BogeyStyle(StandardBogeyInstance.getInstanceFactory()))
.block(BogeyRenderer.BogeySize.SMALL, AllBlocks.SMALL_BOGEY)
.block(BogeyRenderer.BogeySize.LARGE, AllBlocks.LARGE_BOGEY)
.renderer(new StandardBogeyRenderer())
.register();
public static void register() {

View file

@ -13,6 +13,8 @@ import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Vector3f;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllBogeyStyles;
import com.simibubi.create.AllRegistries;
import com.simibubi.create.AllTileEntities;
import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.content.logistics.trains.entity.BogeyStyle;
@ -21,6 +23,7 @@ import com.simibubi.create.content.schematics.ISpecialBlockItemRequirement;
import com.simibubi.create.content.schematics.ItemRequirement;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.block.ProperWaterloggedBlock;
import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.RegisteredObjects;
import net.minecraft.client.renderer.MultiBufferSource;
@ -98,8 +101,8 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
@OnlyIn(Dist.CLIENT)
public void render(@Nullable BlockState state, float wheelAngle, PoseStack ms, float partialTicks,
MultiBufferSource buffers, int light, int overlay, CompoundTag bogeyData) {
final BogeyRenderer renderer = getStyle().renderer;
MultiBufferSource buffers, int light, int overlay, StandardBogeyTileEntity sbte) {
final BogeyRenderer renderer = getStyle(sbte).renderer;
if (state != null) {
ms.translate(.5f, .5f, .5f);
if (state.getValue(AXIS) == Direction.Axis.X)
@ -107,7 +110,7 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
}
ms.translate(0, -1.5 - 1 / 128f, 0);
VertexConsumer vb = buffers.getBuffer(RenderType.cutoutMipped());
renderer.render(bogeyData, wheelAngle, ms, light, vb, getSize());
renderer.render(sbte.bogeyData, wheelAngle, ms, light, vb, getSize());
}
public abstract BogeyRenderer.BogeySize getSize();
@ -168,11 +171,8 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
return new ItemRequirement(ItemRequirement.ItemUseType.CONSUME, AllBlocks.RAILWAY_CASING.asStack());
}
public CompoundTag getBogeyData(@NotNull Level level, BlockPos pos) {
BlockEntity te = level.getBlockEntity(pos);
if (te == null) return new CompoundTag();
return te.getTileData();
public BogeyStyle getStyle(StandardBogeyTileEntity sbte) {
sbte.setBogeyStyle(AllBogeyStyles.STANDARD.get());
return AllBogeyStyles.STANDARD.get();
}
public abstract BogeyStyle getStyle();
}

View file

@ -17,11 +17,11 @@ public class BogeyTileEntityRenderer<T extends BlockEntity> extends SafeTileEnti
protected void renderSafe(T te, float partialTicks, PoseStack ms, MultiBufferSource buffer, int light,
int overlay) {
BlockState blockState = te.getBlockState();
float angle = 0;
if (te instanceof StandardBogeyTileEntity sbte)
angle = sbte.getVirtualAngle(partialTicks);
if (blockState.getBlock()instanceof AbstractBogeyBlock bogey)
bogey.render(blockState, angle, ms, partialTicks, buffer, light, overlay, te.getTileData());
if (te instanceof StandardBogeyTileEntity sbte) {
float angle = sbte.getVirtualAngle(partialTicks);
if (blockState.getBlock() instanceof AbstractBogeyBlock bogey)
bogey.render(blockState, angle, ms, partialTicks, buffer, light, overlay, sbte);
}
}
}

View file

@ -39,8 +39,6 @@ public abstract class BogeyInstance {
renderer.initialiseContraptionModelData(materialManager, size);
}
public abstract BogeyInstanceFactory getInstanceFactory();
protected void hiddenFrame() {
beginFrame(0, null);
}

View file

@ -22,12 +22,16 @@ import java.util.stream.Stream;
public final class BogeyStyle extends ForgeRegistryEntry<BogeyStyle> implements IForgeRegistryEntry<BogeyStyle> {
public Map<BogeySize, ResourceLocation> blocks = new EnumMap<>(BogeySize.class);
public BogeyInstance.BogeyInstanceFactory instance;
public BogeyInstance.BogeyInstanceFactory instanceFactory;
public Component displayName;
public SoundType soundType;
public CompoundTag defaultData;
public BogeyRenderer renderer;
public BogeyStyle(BogeyInstance.BogeyInstanceFactory factory) {
this.instanceFactory = factory;
}
public <T extends AbstractBogeyBlock> void addBlockForSize(BogeySize size, T block) {
this.addBlockForSize(size, block.getRegistryName());
}
@ -55,6 +59,6 @@ public final class BogeyStyle extends ForgeRegistryEntry<BogeyStyle> implements
}
public BogeyInstance createInstance(CarriageBogey bogey, BogeySize size, MaterialManager materialManager) {
return instance.create(bogey, size, materialManager);
return instanceFactory.create(bogey, size, materialManager);
}
}

View file

@ -3,6 +3,8 @@ package com.simibubi.create.content.logistics.trains.entity;
import javax.annotation.Nullable;
import com.jozufozu.flywheel.api.MaterialManager;
import com.simibubi.create.AllBogeyStyles;
import com.simibubi.create.AllRegistries;
import com.simibubi.create.Create;
import com.simibubi.create.content.logistics.trains.DimensionPalette;
import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock;
@ -10,6 +12,7 @@ import com.simibubi.create.content.logistics.trains.TrackGraph;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.RegisteredObjects;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.animation.LerpedFloat;
@ -25,12 +28,16 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
public class CarriageBogey {
public Carriage carriage;
boolean isLeading;
public BogeyStyle style = AllBogeyStyles.STANDARD.get();
public CompoundTag bogeyData;
AbstractBogeyBlock type;
Couple<TravellingPoint> points;
@ -42,7 +49,14 @@ public class CarriageBogey {
int derailAngle;
public CarriageBogey(AbstractBogeyBlock type, TravellingPoint point, TravellingPoint point2) {
public static CarriageBogey fromLocation(AbstractBogeyBlock type, ResourceLocation style, TravellingPoint point, TravellingPoint point2) {
BogeyStyle bogeyStyle = AllRegistries.BOGEY_REGISTRY.get().getValue(style);
return new CarriageBogey(type, bogeyStyle, point, point2);
}
public CarriageBogey(AbstractBogeyBlock type, BogeyStyle style, TravellingPoint point, TravellingPoint point2) {
if (style != null)
this.style = style;
this.type = type;
points = Couple.create(point, point2);
wheelAngle = LerpedFloat.angular();
@ -150,6 +164,8 @@ public class CarriageBogey {
tag.putString("Type", RegisteredObjects.getKeyOrThrow((Block) type)
.toString());
tag.put("Points", points.serializeEach(tp -> tp.write(dimensions)));
if (style.getRegistryName() != null)
NBTHelper.writeResourceLocation(tag, "bogeyStyle", style.getRegistryName());
return tag;
}
@ -158,12 +174,12 @@ public class CarriageBogey {
AbstractBogeyBlock type = (AbstractBogeyBlock) ForgeRegistries.BLOCKS.getValue(location);
Couple<TravellingPoint> points = Couple.deserializeEach(tag.getList("Points", Tag.TAG_COMPOUND),
c -> TravellingPoint.read(c, graph, dimensions));
CarriageBogey carriageBogey = new CarriageBogey(type, points.getFirst(), points.getSecond());
return carriageBogey;
ResourceLocation styleLocation = NBTHelper.readResourceLocation(tag,"bogeyStyle");
return CarriageBogey.fromLocation(type, styleLocation, points.getFirst(), points.getSecond());
}
public BogeyInstance createInstance(MaterialManager materialManager) {
return type.getStyle().createInstance(this, type.getSize(), materialManager);
return style.createInstance(this, type.getSize(), materialManager);
}
void setLeading() {

View file

@ -7,6 +7,8 @@ import com.jozufozu.flywheel.util.transform.TransformStack;
import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionEntityRenderer;
import com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.culling.Frustum;
@ -69,7 +71,7 @@ public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer
BlockEntity be = entity.getContraption().presentTileEntities.get(bogeyPos);
bogey.type.render(null, bogey.wheelAngle.getValue(partialTicks), ms, partialTicks, buffers, light,
overlay, be.getTileData());
overlay, (StandardBogeyTileEntity) be);
ms.popPose();
}

View file

@ -18,8 +18,7 @@ public class StandardBogeyInstance extends BogeyInstance {
return new StandardBogeyInstance(bogey, BogeyRenderer.BogeySize.SMALL, materialManager);
}
@Override
public BogeyInstanceFactory getInstanceFactory() {
public static BogeyInstanceFactory getInstanceFactory() {
return StandardBogeyInstance::new;
}
}

View file

@ -6,6 +6,7 @@ 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;
@ -15,10 +16,13 @@ import com.simibubi.create.foundation.utility.RegisteredObjects;
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;
@ -44,11 +48,12 @@ public class TrainPacket extends SimplePacketBase {
int size = buffer.readVarInt();
for (int i = 0; i < size; i++) {
Couple<CarriageBogey> bogies = Couple.create(null, null);
for (boolean first : Iterate.trueAndFalse) {
if (!first && !buffer.readBoolean())
for (boolean isFirst : Iterate.trueAndFalse) {
if (!isFirst && !buffer.readBoolean())
continue;
AbstractBogeyBlock type = (AbstractBogeyBlock) ForgeRegistries.BLOCKS.getValue(buffer.readResourceLocation());
bogies.set(first, new CarriageBogey(type, new TravellingPoint(), new TravellingPoint()));
BogeyStyle style = AllRegistries.BOGEY_REGISTRY.get().getValue(buffer.readResourceLocation());
bogies.set(isFirst, new CarriageBogey(type, style, new TravellingPoint(), new TravellingPoint()));
}
int spacing = buffer.readVarInt();
carriages.add(new Carriage(bogies.getFirst(), bogies.getSecond(), spacing));
@ -86,6 +91,7 @@ public class TrainPacket extends SimplePacketBase {
}
CarriageBogey bogey = carriage.bogeys.get(first);
buffer.writeResourceLocation(RegisteredObjects.getKeyOrThrow((Block) bogey.type));
buffer.writeResourceLocation(RegisteredObjects.getKeyOrThrow(AllRegistries.BOGEY_REGISTRY.get(), bogey.style));
}
buffer.writeVarInt(carriage.bogeySpacing);
}

View file

@ -31,6 +31,7 @@ import com.simibubi.create.content.logistics.trains.TrackNodeLocation.Discovered
import com.simibubi.create.content.logistics.trains.entity.Carriage;
import com.simibubi.create.content.logistics.trains.entity.CarriageBogey;
import com.simibubi.create.content.logistics.trains.entity.CarriageContraption;
import com.simibubi.create.content.logistics.trains.entity.StandardBogeyInstance;
import com.simibubi.create.content.logistics.trains.entity.Train;
import com.simibubi.create.content.logistics.trains.entity.TrainPacket;
import com.simibubi.create.content.logistics.trains.entity.TravellingPoint;
@ -38,6 +39,7 @@ import com.simibubi.create.content.logistics.trains.management.edgePoint.EdgePoi
import com.simibubi.create.content.logistics.trains.management.edgePoint.TrackTargetingBehaviour;
import com.simibubi.create.content.logistics.trains.management.schedule.Schedule;
import com.simibubi.create.content.logistics.trains.management.schedule.ScheduleItem;
import com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity;
import com.simibubi.create.foundation.advancement.AllAdvancements;
import com.simibubi.create.foundation.block.ProperWaterloggedBlock;
import com.simibubi.create.foundation.config.AllConfigs;
@ -65,7 +67,9 @@ import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
@ -592,8 +596,10 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
}
AbstractBogeyBlock typeOfFirstBogey = bogeyTypes[bogeyIndex];
BlockPos firstBogeyPos = contraption.anchor;
StandardBogeyTileEntity firstBogeyTileEntity = (StandardBogeyTileEntity) level.getBlockEntity(firstBogeyPos);
CarriageBogey firstBogey =
new CarriageBogey(typeOfFirstBogey, points.get(pointIndex), points.get(pointIndex + 1));
new CarriageBogey(typeOfFirstBogey, firstBogeyTileEntity.getStyle(), points.get(pointIndex), points.get(pointIndex + 1));
CarriageBogey secondBogey = null;
BlockPos secondBogeyPos = contraption.getSecondBogeyPos();
int bogeySpacing = 0;
@ -605,10 +611,11 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
contraptions.size() + 1);
return;
}
StandardBogeyTileEntity secondBogeyTileEntity =
(StandardBogeyTileEntity) level.getBlockEntity(secondBogeyPos);
bogeySpacing = bogeyLocations[bogeyIndex + 1] - bogeyLocations[bogeyIndex];
secondBogey = new CarriageBogey(bogeyTypes[bogeyIndex + 1], points.get(pointIndex + 2),
points.get(pointIndex + 3));
secondBogey = new CarriageBogey(bogeyTypes[bogeyIndex + 1], secondBogeyTileEntity.getStyle(),
points.get(pointIndex + 2), points.get(pointIndex + 3));
bogeyIndex++;
} else if (!typeOfFirstBogey.allowsSingleBogeyCarriage()) {

View file

@ -1,20 +1,63 @@
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.animation.LerpedFloat;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
public class StandardBogeyTileEntity extends CachedRenderBBTileEntity {
private BogeyStyle style;
public CompoundTag bogeyData;
public StandardBogeyTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}
public void setBogeyStyle(BogeyStyle style) {
this.style = style;
markUpdated();
}
public BogeyStyle getStyle() {
return this.style;
}
@Override
protected void saveAdditional(CompoundTag pTag) {
if (style != null && style.getRegistryName() != null)
NBTHelper.writeResourceLocation(pTag, "bogeyStyle", style.getRegistryName());
if (bogeyData != null)
pTag.put("bogeyData", bogeyData);
super.saveAdditional(pTag);
}
@Override
public void load(CompoundTag pTag) {
if (pTag.contains("bogeyStyle")) {
ResourceLocation location = NBTHelper.readResourceLocation(pTag, "bogeyStyle");
this.style = AllRegistries.BOGEY_REGISTRY.get().getValue(location);
} else {
this.style = AllBogeyStyles.STANDARD.get();
}
if (pTag.contains("bogeyData"))
this.bogeyData = pTag.getCompound("bogeyData");
super.load(pTag);
}
@Override
protected AABB createRenderBoundingBox() {
return super.createRenderBoundingBox().inflate(2);
@ -36,4 +79,10 @@ public class StandardBogeyTileEntity extends CachedRenderBBTileEntity {
virtualAnimation.setValue(newWheelAngle);
}
private void markUpdated() {
setChanged();
Level level = getLevel();
if (level != null)
getLevel().sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
}
}

View file

@ -3,6 +3,7 @@ package com.simibubi.create.foundation.data;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllParticleTypes;
import com.simibubi.create.AllRegistries;
import com.simibubi.create.AllSoundEvents;
import com.simibubi.create.content.logistics.trains.BogeyRenderer;
import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock;
import com.simibubi.create.content.logistics.trains.entity.BogeyStyle;
@ -34,7 +35,7 @@ import java.util.function.Supplier;
public class BogeyStyleBuilder<T extends BogeyStyle, P> extends AbstractBuilder<BogeyStyle, T, P, BogeyStyleBuilder<T, P>> {
private final T style;
private NonNullSupplier<BogeyRenderer> renderer;
private Supplier<SoundType> soundType;
private Supplier<AllSoundEvents.SoundEntry> soundType;
private Supplier<CompoundTag> data;
private Supplier<ParticleType<?>> particles;
@ -62,7 +63,6 @@ public class BogeyStyleBuilder<T extends BogeyStyle, P> extends AbstractBuilder<
}
public BogeyStyleBuilder<T, P> soundType(SoundType soundEntry) {
this.soundType = () -> soundEntry;
return this;
}
@ -82,7 +82,6 @@ public class BogeyStyleBuilder<T extends BogeyStyle, P> extends AbstractBuilder<
@Override
protected @NotNull T createEntry() {
style.soundType = soundType.get();
style.defaultData = data.get();
style.renderer = renderer.get();
return style;