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.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.StandardBogeyInstance;
import com.tterrag.registrate.util.entry.RegistryEntry;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@ -13,12 +13,26 @@ import static com.simibubi.create.Create.REGISTRATE;
@SuppressWarnings("unused")
public class AllBogeyStyles {
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.LARGE, AllBlocks.LARGE_BOGEY)
.renderer(new StandardBogeyRenderer())
.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() {
LOGGER.info("Registered bogey styles from " + Create.ID);
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);
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();
@ -161,6 +161,7 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
.map(s -> getNextStyle(currentStyle))
.filter(s -> s.validSizes().contains(getSize()))
.findFirst();
if (style.isPresent()) {
player.displayClientMessage(Lang.translateDirect("create.bogey.style.updated_style"), true);
sbte.setBogeyStyle(style.get());
@ -181,7 +182,6 @@ public abstract class AbstractBogeyBlock extends Block implements ITE<StandardBo
if (indexOf == -1)
return state;
int index = (indexOf + 1) % BOGEYS.size();
System.out.println("New Index " + index);
Direction bogeyUpDirection = getBogeyUpDirection();
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.AbstractBogeyBlock;
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.Couple;
import com.simibubi.create.foundation.utility.Iterate;
@ -30,12 +31,13 @@ import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
import static com.simibubi.create.content.logistics.trains.track.StandardBogeyTileEntity.BOGEY_STYLE_KEY;
public class CarriageBogey {
public Carriage carriage;
boolean isLeading;
public BogeyStyle style = AllBogeyStyles.STANDARD.get();
public CompoundTag bogeyData;
AbstractBogeyBlock type;
@ -49,14 +51,10 @@ public class CarriageBogey {
int derailAngle;
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;
public CarriageBogey(AbstractBogeyBlock type, CompoundTag bogeyData, TravellingPoint point, TravellingPoint point2) {
if (bogeyData == null || bogeyData.isEmpty())
bogeyData = this.createBogeyData(); // Prevent Crash When Updating
this.bogeyData = bogeyData;
this.type = type;
points = Couple.create(point, point2);
wheelAngle = LerpedFloat.angular();
@ -164,8 +162,7 @@ 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());
tag.put(BOGEY_STYLE_KEY, bogeyData);
return tag;
}
@ -174,12 +171,23 @@ 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));
ResourceLocation styleLocation = NBTHelper.readResourceLocation(tag,"bogeyStyle");
return CarriageBogey.fromLocation(type, styleLocation, points.getFirst(), points.getSecond());
CompoundTag data = tag.getCompound(StandardBogeyTileEntity.BOGEY_DATA_KEY);
return new CarriageBogey(type, data, points.getFirst(), points.getSecond());
}
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() {

View file

@ -32,7 +32,7 @@ public class CarriageContraptionInstance extends EntityInstance<CarriageContrapt
return;
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();
}

View file

@ -735,7 +735,7 @@ public class Train {
BlockEntity be = level.getBlockEntity(new BlockPos(bogeyPosition));
if (!(be instanceof StandardBogeyTileEntity sbte))
continue;
sbte.setBogeyStyle(bogey.style);
sbte.setBogeyData(bogey.bogeyData);
}
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.RegisteredObjects;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
@ -52,8 +53,8 @@ public class TrainPacket extends SimplePacketBase {
if (!isFirst && !buffer.readBoolean())
continue;
AbstractBogeyBlock type = (AbstractBogeyBlock) ForgeRegistries.BLOCKS.getValue(buffer.readResourceLocation());
BogeyStyle style = AllRegistries.BOGEY_REGISTRY.get().getValue(buffer.readResourceLocation());
bogies.set(isFirst, new CarriageBogey(type, style, new TravellingPoint(), new TravellingPoint()));
CompoundTag data = buffer.readNbt();
bogies.set(isFirst, new CarriageBogey(type, data, new TravellingPoint(), new TravellingPoint()));
}
int spacing = buffer.readVarInt();
carriages.add(new Carriage(bogies.getFirst(), bogies.getSecond(), spacing));
@ -91,7 +92,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.writeNbt(bogey.bogeyData);
}
buffer.writeVarInt(carriage.bogeySpacing);
}

View file

@ -598,7 +598,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
BlockPos firstBogeyPos = contraption.anchor;
StandardBogeyTileEntity firstBogeyTileEntity = (StandardBogeyTileEntity) level.getBlockEntity(firstBogeyPos);
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;
BlockPos secondBogeyPos = contraption.getSecondBogeyPos();
int bogeySpacing = 0;
@ -613,7 +613,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable
StandardBogeyTileEntity secondBogeyTileEntity =
(StandardBogeyTileEntity) level.getBlockEntity(secondBogeyPos);
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));
bogeyIndex++;

View file

@ -20,9 +20,9 @@ import net.minecraft.world.phys.Vec3;
public class StandardBogeyBlock extends AbstractBogeyBlock implements ITE<StandardBogeyTileEntity>, ProperWaterloggedBlock, ISpecialBlockItemRequirement {
private final BogeyRenderer.BogeySize size;
public StandardBogeyBlock(Properties props, BogeyRenderer.BogeySize large) {
public StandardBogeyBlock(Properties props, BogeyRenderer.BogeySize size) {
super(props);
this.size = large;
this.size = size;
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.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;
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;
;
import org.jetbrains.annotations.NotNull;
public class StandardBogeyTileEntity extends CachedRenderBBTileEntity {
private BogeyStyle style;
public CompoundTag bogeyData;
public static String BOGEY_STYLE_KEY = "BogeyStyle";
public static String BOGEY_DATA_KEY = "BogeyData";
private CompoundTag bogeyData;
public StandardBogeyTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState 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) {
this.style = style;
ResourceLocation location = RegisteredObjects.getKeyOrThrow(AllRegistries.BOGEY_REGISTRY.get(), style);
CompoundTag data = this.getBogeyData();
NBTHelper.writeResourceLocation(data, BOGEY_STYLE_KEY, location);
markUpdated();
}
@NotNull
public BogeyStyle getStyle() {
if (this.style == null)
setBogeyStyle(AllBogeyStyles.STANDARD.get());
return this.style;
CompoundTag data = this.getBogeyData();
ResourceLocation currentStyle = NBTHelper.readResourceLocation(data, BOGEY_STYLE_KEY);
BogeyStyle style = AllRegistries.BOGEY_REGISTRY.get().getValue(currentStyle);
if (style == null) setBogeyStyle(AllBogeyStyles.STANDARD.get());
return 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);
protected void saveAdditional(@NotNull CompoundTag pTag) {
CompoundTag data = this.getBogeyData();
if (data != null) pTag.put(BOGEY_DATA_KEY, data); // Now contains style
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");
if (pTag.contains(BOGEY_DATA_KEY))
this.bogeyData = pTag.getCompound(BOGEY_DATA_KEY);
else
this.bogeyData = this.createBogeyData();
super.load(pTag);
}
private CompoundTag createBogeyData() {
CompoundTag nbt = new CompoundTag();
NBTHelper.writeResourceLocation(nbt, BOGEY_STYLE_KEY, AllBogeyStyles.STANDARD.getId());
return nbt;
}
@Override
protected AABB createRenderBoundingBox() {
return super.createRenderBoundingBox().inflate(2);