Moved Bogey Style Inside Of Bogey Data And Implemented Bogey Data Communication

This commit is contained in:
Rabbitminers 2023-04-02 23:02:02 +01:00
parent eedd984738
commit 96566b1614
9 changed files with 91 additions and 49 deletions

View File

@ -2,8 +2,8 @@ package com.simibubi.create;
import com.simibubi.create.content.logistics.trains.BogeyRenderer; import com.simibubi.create.content.logistics.trains.BogeyRenderer;
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.simibubi.create.content.logistics.trains.entity.StandardBogeyInstance;
import com.tterrag.registrate.util.entry.RegistryEntry; import com.tterrag.registrate.util.entry.RegistryEntry;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@ -13,12 +13,26 @@ import static com.simibubi.create.Create.REGISTRATE;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class AllBogeyStyles { public class AllBogeyStyles {
public static final RegistryEntry<BogeyStyle> STANDARD = REGISTRATE public static final RegistryEntry<BogeyStyle> STANDARD = REGISTRATE
.bogeyStyle("standard", new BogeyStyle(StandardBogeyInstance.getInstanceFactory())) .bogeyStyle("standard", new BogeyStyle())
.block(BogeyRenderer.BogeySize.SMALL, AllBlocks.SMALL_BOGEY) .block(BogeyRenderer.BogeySize.SMALL, AllBlocks.SMALL_BOGEY)
.block(BogeyRenderer.BogeySize.LARGE, AllBlocks.LARGE_BOGEY) .block(BogeyRenderer.BogeySize.LARGE, AllBlocks.LARGE_BOGEY)
.renderer(new StandardBogeyRenderer()) .renderer(new StandardBogeyRenderer())
.register(); .register();
public static final RegistryEntry<BogeyStyle> TEST = REGISTRATE
.bogeyStyle("test", new BogeyStyle())
.block(BogeyRenderer.BogeySize.SMALL, AllBlocks.SMALL_BOGEY)
.block(BogeyRenderer.BogeySize.LARGE, AllBlocks.LARGE_BOGEY)
.renderer(new TestBogeyRenderer())
.register();
public static final RegistryEntry<BogeyStyle> TEST_TWO = REGISTRATE
.bogeyStyle("test_two", new BogeyStyle())
.block(BogeyRenderer.BogeySize.SMALL, AllBlocks.SMALL_BOGEY)
.block(BogeyRenderer.BogeySize.LARGE, AllBlocks.LARGE_BOGEY)
.renderer(new TestBogeyRenderer())
.register();
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()); AllRegistries.DEFERRED_BOGEY_REGISTRY.register(FMLJavaModLoadingContext.get().getModEventBus());

View File

@ -116,7 +116,7 @@ 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.bogeyData, wheelAngle, ms, light, vb, getSize()); renderer.render(sbte.getBogeyData(), wheelAngle, ms, light, vb, getSize());
} }
public abstract BogeyRenderer.BogeySize getSize(); public abstract BogeyRenderer.BogeySize getSize();
@ -161,6 +161,7 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
.map(s -> getNextStyle(currentStyle)) .map(s -> getNextStyle(currentStyle))
.filter(s -> s.validSizes().contains(getSize())) .filter(s -> s.validSizes().contains(getSize()))
.findFirst(); .findFirst();
if (style.isPresent()) { if (style.isPresent()) {
player.displayClientMessage(Lang.translateDirect("create.bogey.style.updated_style"), true); player.displayClientMessage(Lang.translateDirect("create.bogey.style.updated_style"), true);
sbte.setBogeyStyle(style.get()); sbte.setBogeyStyle(style.get());
@ -181,7 +182,6 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
if (indexOf == -1) if (indexOf == -1)
return state; return state;
int index = (indexOf + 1) % BOGEYS.size(); int index = (indexOf + 1) % BOGEYS.size();
System.out.println("New Index " + index);
Direction bogeyUpDirection = getBogeyUpDirection(); Direction bogeyUpDirection = getBogeyUpDirection();
boolean trackAxisAlongFirstCoordinate = isTrackAxisAlongFirstCoordinate(state); boolean trackAxisAlongFirstCoordinate = isTrackAxisAlongFirstCoordinate(state);

View File

@ -9,6 +9,7 @@ import com.simibubi.create.Create;
import com.simibubi.create.content.logistics.trains.DimensionPalette; import com.simibubi.create.content.logistics.trains.DimensionPalette;
import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock; import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock;
import com.simibubi.create.content.logistics.trains.TrackGraph; import com.simibubi.create.content.logistics.trains.TrackGraph;
import com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity;
import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.Couple; import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.Iterate;
@ -30,12 +31,13 @@ import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import static com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity.BOGEY_STYLE_KEY;
public class CarriageBogey { public class CarriageBogey {
public Carriage carriage; public Carriage carriage;
boolean isLeading; boolean isLeading;
public BogeyStyle style = AllBogeyStyles.STANDARD.get();
public CompoundTag bogeyData; public CompoundTag bogeyData;
AbstractBogeyBlock type; AbstractBogeyBlock type;
@ -49,14 +51,10 @@ public class CarriageBogey {
int derailAngle; int derailAngle;
public static CarriageBogey fromLocation(AbstractBogeyBlock type, ResourceLocation style, TravellingPoint point, TravellingPoint point2) { public CarriageBogey(AbstractBogeyBlock type, CompoundTag bogeyData, TravellingPoint point, TravellingPoint point2) {
BogeyStyle bogeyStyle = AllRegistries.BOGEY_REGISTRY.get().getValue(style); if (bogeyData == null || bogeyData.isEmpty())
return new CarriageBogey(type, bogeyStyle, point, point2); bogeyData = this.createBogeyData(); // Prevent Crash When Updating
} this.bogeyData = bogeyData;
public CarriageBogey(AbstractBogeyBlock type, BogeyStyle style, TravellingPoint point, TravellingPoint point2) {
if (style != null)
this.style = style;
this.type = type; this.type = type;
points = Couple.create(point, point2); points = Couple.create(point, point2);
wheelAngle = LerpedFloat.angular(); wheelAngle = LerpedFloat.angular();
@ -164,8 +162,7 @@ public class CarriageBogey {
tag.putString("Type", RegisteredObjects.getKeyOrThrow((Block) type) tag.putString("Type", RegisteredObjects.getKeyOrThrow((Block) type)
.toString()); .toString());
tag.put("Points", points.serializeEach(tp -> tp.write(dimensions))); tag.put("Points", points.serializeEach(tp -> tp.write(dimensions)));
if (style.getRegistryName() != null) tag.put(BOGEY_STYLE_KEY, bogeyData);
NBTHelper.writeResourceLocation(tag, "bogeyStyle", style.getRegistryName());
return tag; return tag;
} }
@ -174,12 +171,23 @@ public class CarriageBogey {
AbstractBogeyBlock type = (AbstractBogeyBlock) ForgeRegistries.BLOCKS.getValue(location); AbstractBogeyBlock type = (AbstractBogeyBlock) ForgeRegistries.BLOCKS.getValue(location);
Couple<TravellingPoint> points = Couple.deserializeEach(tag.getList("Points", Tag.TAG_COMPOUND), Couple<TravellingPoint> points = Couple.deserializeEach(tag.getList("Points", Tag.TAG_COMPOUND),
c -> TravellingPoint.read(c, graph, dimensions)); c -> TravellingPoint.read(c, graph, dimensions));
ResourceLocation styleLocation = NBTHelper.readResourceLocation(tag,"bogeyStyle"); CompoundTag data = tag.getCompound(StandardBogeyTileEntity.BOGEY_DATA_KEY);
return CarriageBogey.fromLocation(type, styleLocation, points.getFirst(), points.getSecond()); return new CarriageBogey(type, data, points.getFirst(), points.getSecond());
} }
public BogeyInstance createInstance(MaterialManager materialManager) { public BogeyInstance createInstance(MaterialManager materialManager) {
return style.createInstance(this, type.getSize(), materialManager); return this.getStyle().createInstance(this, type.getSize(), materialManager);
}
public BogeyStyle getStyle() {
ResourceLocation location = NBTHelper.readResourceLocation(this.bogeyData, BOGEY_STYLE_KEY);
return AllRegistries.BOGEY_REGISTRY.get().getValue(location);
}
private CompoundTag createBogeyData() {
CompoundTag nbt = new CompoundTag();
NBTHelper.writeResourceLocation(nbt, BOGEY_STYLE_KEY, AllBogeyStyles.STANDARD.getId());
return nbt;
} }
void setLeading() { void setLeading() {

View File

@ -32,7 +32,7 @@ public class CarriageContraptionInstance extends EntityInstance<CarriageContrapt
return; return;
bogeys = carriage.bogeys.mapNotNullWithParam((bogey, manager) -> bogeys = carriage.bogeys.mapNotNullWithParam((bogey, manager) ->
bogey.style.createInstance(bogey, bogey.type.getSize(), manager), materialManager); bogey.getStyle().createInstance(bogey, bogey.type.getSize(), manager), materialManager);
updateLight(); updateLight();
} }

View File

@ -735,7 +735,7 @@ public class Train {
BlockEntity be = level.getBlockEntity(new BlockPos(bogeyPosition)); BlockEntity be = level.getBlockEntity(new BlockPos(bogeyPosition));
if (!(be instanceof StandardBogeyTileEntity sbte)) if (!(be instanceof StandardBogeyTileEntity sbte))
continue; continue;
sbte.setBogeyStyle(bogey.style); sbte.setBogeyData(bogey.bogeyData);
} }
offset += carriage.bogeySpacing; offset += carriage.bogeySpacing;

View File

@ -14,6 +14,7 @@ import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.RegisteredObjects; import com.simibubi.create.foundation.utility.RegisteredObjects;
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.resources.ResourceLocation;
@ -52,8 +53,8 @@ public class TrainPacket extends SimplePacketBase {
if (!isFirst && !buffer.readBoolean()) if (!isFirst && !buffer.readBoolean())
continue; continue;
AbstractBogeyBlock type = (AbstractBogeyBlock) ForgeRegistries.BLOCKS.getValue(buffer.readResourceLocation()); AbstractBogeyBlock type = (AbstractBogeyBlock) ForgeRegistries.BLOCKS.getValue(buffer.readResourceLocation());
BogeyStyle style = AllRegistries.BOGEY_REGISTRY.get().getValue(buffer.readResourceLocation()); CompoundTag data = buffer.readNbt();
bogies.set(isFirst, new CarriageBogey(type, style, new TravellingPoint(), new TravellingPoint())); bogies.set(isFirst, new CarriageBogey(type, data, new TravellingPoint(), new TravellingPoint()));
} }
int spacing = buffer.readVarInt(); int spacing = buffer.readVarInt();
carriages.add(new Carriage(bogies.getFirst(), bogies.getSecond(), spacing)); carriages.add(new Carriage(bogies.getFirst(), bogies.getSecond(), spacing));
@ -91,7 +92,7 @@ public class TrainPacket extends SimplePacketBase {
} }
CarriageBogey bogey = carriage.bogeys.get(first); CarriageBogey bogey = carriage.bogeys.get(first);
buffer.writeResourceLocation(RegisteredObjects.getKeyOrThrow((Block) bogey.type)); buffer.writeResourceLocation(RegisteredObjects.getKeyOrThrow((Block) bogey.type));
buffer.writeResourceLocation(RegisteredObjects.getKeyOrThrow(AllRegistries.BOGEY_REGISTRY.get(), bogey.style)); buffer.writeNbt(bogey.bogeyData);
} }
buffer.writeVarInt(carriage.bogeySpacing); buffer.writeVarInt(carriage.bogeySpacing);
} }

View File

@ -598,7 +598,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
BlockPos firstBogeyPos = contraption.anchor; BlockPos firstBogeyPos = contraption.anchor;
StandardBogeyTileEntity firstBogeyTileEntity = (StandardBogeyTileEntity) level.getBlockEntity(firstBogeyPos); StandardBogeyTileEntity firstBogeyTileEntity = (StandardBogeyTileEntity) level.getBlockEntity(firstBogeyPos);
CarriageBogey firstBogey = CarriageBogey firstBogey =
new CarriageBogey(typeOfFirstBogey, firstBogeyTileEntity.getStyle(), points.get(pointIndex), points.get(pointIndex + 1)); new CarriageBogey(typeOfFirstBogey, firstBogeyTileEntity.getBogeyData(), points.get(pointIndex), points.get(pointIndex + 1));
CarriageBogey secondBogey = null; CarriageBogey secondBogey = null;
BlockPos secondBogeyPos = contraption.getSecondBogeyPos(); BlockPos secondBogeyPos = contraption.getSecondBogeyPos();
int bogeySpacing = 0; int bogeySpacing = 0;
@ -613,7 +613,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
StandardBogeyTileEntity secondBogeyTileEntity = StandardBogeyTileEntity secondBogeyTileEntity =
(StandardBogeyTileEntity) level.getBlockEntity(secondBogeyPos); (StandardBogeyTileEntity) level.getBlockEntity(secondBogeyPos);
bogeySpacing = bogeyLocations[bogeyIndex + 1] - bogeyLocations[bogeyIndex]; bogeySpacing = bogeyLocations[bogeyIndex + 1] - bogeyLocations[bogeyIndex];
secondBogey = new CarriageBogey(bogeyTypes[bogeyIndex + 1], secondBogeyTileEntity.getStyle(), secondBogey = new CarriageBogey(bogeyTypes[bogeyIndex + 1], secondBogeyTileEntity.getBogeyData(),
points.get(pointIndex + 2), points.get(pointIndex + 3)); points.get(pointIndex + 2), points.get(pointIndex + 3));
bogeyIndex++; bogeyIndex++;

View File

@ -20,9 +20,9 @@ import net.minecraft.world.phys.Vec3;
public class StandardBogeyBlock extends AbstractBogeyBlock implements ITE<StandardBogeyTileEntity>, ProperWaterloggedBlock, ISpecialBlockItemRequirement { public class StandardBogeyBlock extends AbstractBogeyBlock implements ITE<StandardBogeyTileEntity>, ProperWaterloggedBlock, ISpecialBlockItemRequirement {
private final BogeyRenderer.BogeySize size; private final BogeyRenderer.BogeySize size;
public StandardBogeyBlock(Properties props, BogeyRenderer.BogeySize large) { public StandardBogeyBlock(Properties props, BogeyRenderer.BogeySize size) {
super(props); super(props);
this.size = large; this.size = size;
registerDefaultState(defaultBlockState().setValue(WATERLOGGED, false)); registerDefaultState(defaultBlockState().setValue(WATERLOGGED, false));
} }

View File

@ -7,62 +7,81 @@ 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;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.AABB;
;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class StandardBogeyTileEntity extends CachedRenderBBTileEntity { public class StandardBogeyTileEntity extends CachedRenderBBTileEntity {
private BogeyStyle style; public static String BOGEY_STYLE_KEY = "BogeyStyle";
public CompoundTag bogeyData; public static String BOGEY_DATA_KEY = "BogeyData";
private CompoundTag bogeyData;
public StandardBogeyTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public StandardBogeyTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state); super(type, pos, state);
} }
public CompoundTag getBogeyData() {
if (this.bogeyData == null || !this.bogeyData.contains(BOGEY_STYLE_KEY))
this.bogeyData = this.createBogeyData();
return this.bogeyData;
}
public void setBogeyData(@NotNull CompoundTag newData) {
if (!newData.contains(BOGEY_STYLE_KEY)) {
ResourceLocation style = AllBogeyStyles.STANDARD.getId();
NBTHelper.writeResourceLocation(newData, BOGEY_STYLE_KEY, style);
}
this.bogeyData = newData;
}
public void setBogeyStyle(@NotNull BogeyStyle style) { public void setBogeyStyle(@NotNull BogeyStyle style) {
this.style = style; ResourceLocation location = RegisteredObjects.getKeyOrThrow(AllRegistries.BOGEY_REGISTRY.get(), style);
CompoundTag data = this.getBogeyData();
NBTHelper.writeResourceLocation(data, BOGEY_STYLE_KEY, location);
markUpdated(); markUpdated();
} }
@NotNull @NotNull
public BogeyStyle getStyle() { public BogeyStyle getStyle() {
if (this.style == null) CompoundTag data = this.getBogeyData();
setBogeyStyle(AllBogeyStyles.STANDARD.get()); ResourceLocation currentStyle = NBTHelper.readResourceLocation(data, BOGEY_STYLE_KEY);
return this.style; BogeyStyle style = AllRegistries.BOGEY_REGISTRY.get().getValue(currentStyle);
if (style == null) setBogeyStyle(AllBogeyStyles.STANDARD.get());
return style;
} }
@Override @Override
protected void saveAdditional(CompoundTag pTag) { protected void saveAdditional(@NotNull CompoundTag pTag) {
if (style != null && style.getRegistryName() != null) CompoundTag data = this.getBogeyData();
NBTHelper.writeResourceLocation(pTag, "bogeyStyle", style.getRegistryName()); if (data != null) pTag.put(BOGEY_DATA_KEY, data); // Now contains style
if (bogeyData != null)
pTag.put("bogeyData", bogeyData);
super.saveAdditional(pTag); super.saveAdditional(pTag);
} }
@Override @Override
public void load(CompoundTag pTag) { public void load(CompoundTag pTag) {
if (pTag.contains("bogeyStyle")) { if (pTag.contains(BOGEY_DATA_KEY))
ResourceLocation location = NBTHelper.readResourceLocation(pTag, "bogeyStyle"); this.bogeyData = pTag.getCompound(BOGEY_DATA_KEY);
this.style = AllRegistries.BOGEY_REGISTRY.get().getValue(location); else
} else { this.bogeyData = this.createBogeyData();
this.style = AllBogeyStyles.STANDARD.get();
}
if (pTag.contains("bogeyData"))
this.bogeyData = pTag.getCompound("bogeyData");
super.load(pTag); super.load(pTag);
} }
private CompoundTag createBogeyData() {
CompoundTag nbt = new CompoundTag();
NBTHelper.writeResourceLocation(nbt, BOGEY_STYLE_KEY, AllBogeyStyles.STANDARD.getId());
return nbt;
}
@Override @Override
protected AABB createRenderBoundingBox() { protected AABB createRenderBoundingBox() {
return super.createRenderBoundingBox().inflate(2); return super.createRenderBoundingBox().inflate(2);