Added Resource Location To NBT helper methods

This commit is contained in:
Rabbitminers 2023-03-30 18:47:13 +01:00
parent da593fccb1
commit 50ff081704
9 changed files with 113 additions and 37 deletions

View File

@ -1,15 +1,10 @@
package com.simibubi.create; 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.entity.BogeyStyle; import com.simibubi.create.content.logistics.trains.entity.BogeyStyle;
import com.simibubi.create.content.logistics.trains.entity.StandardBogeyInstance; 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;
import net.minecraftforge.registries.DeferredRegister;
import static com.simibubi.create.Create.LOGGER; import static com.simibubi.create.Create.LOGGER;
import static com.simibubi.create.Create.REGISTRATE; import static com.simibubi.create.Create.REGISTRATE;
@ -17,7 +12,7 @@ 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.class)) .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)
.register(); .register();

View File

@ -30,7 +30,7 @@ import com.simibubi.create.content.contraptions.fluids.tank.FluidTankBlock;
import com.simibubi.create.content.curiosities.deco.SlidingDoorBlock; import com.simibubi.create.content.curiosities.deco.SlidingDoorBlock;
import com.simibubi.create.content.logistics.block.redstone.RedstoneLinkBlock; import com.simibubi.create.content.logistics.block.redstone.RedstoneLinkBlock;
import com.simibubi.create.content.logistics.block.vault.ItemVaultBlock; import com.simibubi.create.content.logistics.block.vault.ItemVaultBlock;
import com.simibubi.create.content.logistics.trains.IBogeyBlock; import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock;
import com.simibubi.create.content.logistics.trains.ITrackBlock; import com.simibubi.create.content.logistics.trains.ITrackBlock;
import com.simibubi.create.content.logistics.trains.management.edgePoint.station.StationBlock; import com.simibubi.create.content.logistics.trains.management.edgePoint.station.StationBlock;
import com.simibubi.create.foundation.config.ContraptionMovementSetting; import com.simibubi.create.foundation.config.ContraptionMovementSetting;
@ -337,7 +337,7 @@ public class BlockMovementChecks {
return direction == state.getValue(StickerBlock.FACING) return direction == state.getValue(StickerBlock.FACING)
&& !isNotSupportive(world.getBlockState(pos.relative(direction)), direction.getOpposite()); && !isNotSupportive(world.getBlockState(pos.relative(direction)), direction.getOpposite());
} }
if (block instanceof IBogeyBlock bogey) if (block instanceof AbstractBogeyBlock bogey)
return bogey.getStickySurfaces(world, pos, state) return bogey.getStickySurfaces(world, pos, state)
.contains(direction); .contains(direction);
if (block instanceof WhistleBlock) if (block instanceof WhistleBlock)

View File

@ -20,8 +20,8 @@ public class BogeyTileEntityRenderer<T extends BlockEntity> extends SafeTileEnti
float angle = 0; float angle = 0;
if (te instanceof StandardBogeyTileEntity sbte) if (te instanceof StandardBogeyTileEntity sbte)
angle = sbte.getVirtualAngle(partialTicks); angle = sbte.getVirtualAngle(partialTicks);
if (blockState.getBlock()instanceof IBogeyBlock bogey) if (blockState.getBlock()instanceof AbstractBogeyBlock bogey)
bogey.render(blockState, angle, ms, partialTicks, buffer, light, overlay); bogey.render(blockState, angle, ms, partialTicks, buffer, light, overlay, te.getTileData());
} }
} }

View File

@ -30,7 +30,8 @@ public abstract class BogeyInstance {
public final BogeyRenderer renderer; public final BogeyRenderer renderer;
private final BogeyRenderer.BogeySize size; private final BogeyRenderer.BogeySize size;
public BogeyInstance(CarriageBogey bogey, BogeyRenderer renderer, BogeyRenderer.BogeySize size, MaterialManager materialManager) { public BogeyInstance(CarriageBogey bogey, BogeyRenderer renderer, BogeyRenderer.BogeySize size,
MaterialManager materialManager) {
this.bogey = bogey; this.bogey = bogey;
this.renderer = renderer; this.renderer = renderer;
this.size = size; this.size = size;
@ -38,6 +39,8 @@ public abstract class BogeyInstance {
renderer.initialiseContraptionModelData(materialManager, size); renderer.initialiseContraptionModelData(materialManager, size);
} }
public abstract BogeyInstanceFactory getInstanceFactory();
protected void hiddenFrame() { protected void hiddenFrame() {
beginFrame(0, null); beginFrame(0, null);
} }
@ -61,4 +64,10 @@ public abstract class BogeyInstance {
return bogey.getAnchorPosition() != null ? bogey.getAnchorPosition() return bogey.getAnchorPosition() != null ? bogey.getAnchorPosition()
: entity.getLightProbePosition(AnimationTickHolder.getPartialTicks()); : entity.getLightProbePosition(AnimationTickHolder.getPartialTicks());
} }
@FunctionalInterface
interface BogeyInstanceFactory {
BogeyInstance create(CarriageBogey bogey, BogeyRenderer.BogeySize size,
MaterialManager materialManager);
}
} }

View File

@ -1,23 +1,60 @@
package com.simibubi.create.content.logistics.trains.entity; package com.simibubi.create.content.logistics.trains.entity;
import com.jozufozu.flywheel.api.MaterialManager;
import com.simibubi.create.content.logistics.trains.BogeyRenderer;
import com.simibubi.create.content.logistics.trains.BogeyRenderer.BogeySize; import com.simibubi.create.content.logistics.trains.BogeyRenderer.BogeySize;
import com.simibubi.create.content.logistics.trains.IBogeyBlock; import com.simibubi.create.content.logistics.trains.AbstractBogeyBlock;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistryEntry; import net.minecraftforge.registries.ForgeRegistryEntry;
import net.minecraftforge.registries.IForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistryEntry;
import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
public class BogeyStyle extends ForgeRegistryEntry<BogeyStyle> implements IForgeRegistryEntry<BogeyStyle> { public final class BogeyStyle extends ForgeRegistryEntry<BogeyStyle> implements IForgeRegistryEntry<BogeyStyle> {
public Map<BogeySize, IBogeyBlock> blocks; public Map<BogeySize, ResourceLocation> blocks = new EnumMap<>(BogeySize.class);
final Class<? extends BogeyInstance> instance; public BogeyInstance.BogeyInstanceFactory instance;
public Component displayName;
public SoundType soundType;
public CompoundTag defaultData;
public BogeyRenderer renderer;
public BogeyStyle(Class<? extends BogeyInstance> instance) { public <T extends AbstractBogeyBlock> void addBlockForSize(BogeySize size, T block) {
this.instance = instance; this.addBlockForSize(size, block.getRegistryName());
} }
public <C extends BogeyInstance> BogeyInstance getInstance() throws IllegalAccessException, InstantiationException { public void addBlockForSize(BogeySize size, ResourceLocation location) {
return instance.newInstance(); blocks.put(size, location);
}
public Block getNextBlock(BogeySize currentSize) {
return Stream.iterate(getNextSize(currentSize), this::getNextSize)
.filter(size -> blocks.containsKey(size))
.findFirst()
.map(size -> ForgeRegistries.BLOCKS.getValue(blocks.get(size)))
.orElse(ForgeRegistries.BLOCKS.getValue(blocks.get(currentSize)));
}
public Set<BogeySize> validSizes() {
return blocks.keySet();
}
private BogeySize getNextSize(BogeySize size) {
BogeySize[] sizes = BogeySize.values();
int nextOrdinal = (size.ordinal() + 1) % sizes.length;
return sizes[nextOrdinal];
}
public BogeyInstance createInstance(CarriageBogey bogey, BogeySize size, MaterialManager materialManager) {
return instance.create(bogey, size, materialManager);
} }
} }

View File

@ -13,6 +13,7 @@ import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.renderer.entity.EntityRendererProvider; import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LightLayer; import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer<CarriageContraptionEntity> { public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer<CarriageContraptionEntity> {
@ -65,8 +66,10 @@ public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer
translateBogey(ms, bogey, bogeySpacing, viewYRot, viewXRot, partialTicks); translateBogey(ms, bogey, bogeySpacing, viewYRot, viewXRot, partialTicks);
int light = getBogeyLightCoords(entity, bogey, partialTicks); int light = getBogeyLightCoords(entity, bogey, partialTicks);
BlockEntity be = entity.getContraption().presentTileEntities.get(bogeyPos);
bogey.type.render(null, bogey.wheelAngle.getValue(partialTicks), ms, partialTicks, buffers, light, bogey.type.render(null, bogey.wheelAngle.getValue(partialTicks), ms, partialTicks, buffers, light,
overlay); overlay, be.getTileData());
ms.popPose(); ms.popPose();
} }

View File

@ -17,4 +17,9 @@ public class StandardBogeyInstance extends BogeyInstance {
public static StandardBogeyInstance frame(CarriageBogey bogey, MaterialManager materialManager) { public static StandardBogeyInstance frame(CarriageBogey bogey, MaterialManager materialManager) {
return new StandardBogeyInstance(bogey, BogeyRenderer.BogeySize.SMALL, materialManager); return new StandardBogeyInstance(bogey, BogeyRenderer.BogeySize.SMALL, materialManager);
} }
@Override
public BogeyInstanceFactory getInstanceFactory() {
return StandardBogeyInstance::new;
}
} }

View File

@ -1,10 +1,10 @@
package com.simibubi.create.foundation.data; package com.simibubi.create.foundation.data;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllParticleTypes;
import com.simibubi.create.AllRegistries; 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.BogeyRenderer;
import com.simibubi.create.content.logistics.trains.IBogeyBlock; 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.tterrag.registrate.AbstractRegistrate; import com.tterrag.registrate.AbstractRegistrate;
@ -14,28 +14,27 @@ import com.tterrag.registrate.builders.AbstractBuilder;
import com.tterrag.registrate.builders.BuilderCallback; import com.tterrag.registrate.builders.BuilderCallback;
import com.tterrag.registrate.util.entry.BlockEntry; import com.tterrag.registrate.util.entry.BlockEntry;
import com.tterrag.registrate.util.entry.RegistryEntry;
import com.tterrag.registrate.util.nullness.NonNullSupplier; import com.tterrag.registrate.util.nullness.NonNullSupplier;
import net.minecraft.core.particles.ParticleType; import net.minecraft.core.particles.ParticleType;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.SoundType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Supplier; import java.util.function.Supplier;
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public class BogeyStyleBuilder<T extends BogeyStyle, P> extends AbstractBuilder<BogeyStyle, T, P, BogeyStyleBuilder<T, P>> { public class BogeyStyleBuilder<T extends BogeyStyle, P> extends AbstractBuilder<BogeyStyle, T, P, BogeyStyleBuilder<T, P>> {
private final T style; private final T style;
private NonNullSupplier<BogeyRenderer> renderer;
private NonNullSupplier<Map<BogeyRenderer.BogeySize, BlockEntry<? extends IBogeyBlock>>> bogeyBlocks private Supplier<SoundType> soundType;
= () -> new EnumMap<>(BogeyRenderer.BogeySize.class);
private Supplier<AllSoundEvents.SoundEntry> sounds;
private Supplier<CompoundTag> data; private Supplier<CompoundTag> data;
private Supplier<ParticleType<?>> particles; private Supplier<ParticleType<?>> particles;
@ -46,9 +45,10 @@ public class BogeyStyleBuilder<T extends BogeyStyle, P> extends AbstractBuilder<
protected BogeyStyleBuilder(AbstractRegistrate<?> owner, P parent, String name, BuilderCallback callback, T style) { protected BogeyStyleBuilder(AbstractRegistrate<?> owner, P parent, String name, BuilderCallback callback, T style) {
super(owner, parent, name, callback, AllRegistries.Keys.BOGEYS); super(owner, parent, name, callback, AllRegistries.Keys.BOGEYS);
this.style = style; this.style = style;
this.particles = AllParticleTypes.AIR_FLOW::get;
// bogeyBlocks.get().put(BogeyRenderer.BogeySize.SMALL, AllBlocks.SMALL_BOGEY.get()); this.data = CompoundTag::new;
// bogeyBlocks.get().put(BogeyRenderer.BogeySize.LARGE, AllBlocks.LARGE_BOGEY.get()); this.block(BogeyRenderer.BogeySize.SMALL, AllBlocks.SMALL_BOGEY);
this.block(BogeyRenderer.BogeySize.LARGE, AllBlocks.LARGE_BOGEY);
} }
public BogeyStyleBuilder<T, P> defaultData(CompoundTag data) { public BogeyStyleBuilder<T, P> defaultData(CompoundTag data) {
@ -61,18 +61,30 @@ public class BogeyStyleBuilder<T extends BogeyStyle, P> extends AbstractBuilder<
return this; return this;
} }
public BogeyStyleBuilder<T, P> soundType(AllSoundEvents.SoundEntry soundEntry) { public BogeyStyleBuilder<T, P> soundType(SoundType soundEntry) {
this.sounds = () -> soundEntry; this.soundType = () -> soundEntry;
return this; return this;
} }
public BogeyStyleBuilder<T, P> block(BogeyRenderer.BogeySize size, BlockEntry<? extends IBogeyBlock> block) { public BogeyStyleBuilder<T, P> block(BogeyRenderer.BogeySize size, BlockEntry<? extends AbstractBogeyBlock> block) {
this.bogeyBlocks.get().put(size, block); return this.block(size, block.getId());
}
public BogeyStyleBuilder<T, P> block(BogeyRenderer.BogeySize size, ResourceLocation location) {
this.style.addBlockForSize(size, location);
return this;
}
public BogeyStyleBuilder<T, P> renderer(BogeyRenderer renderer) {
this.renderer = () -> renderer;
return this; return this;
} }
@Override @Override
protected @NotNull T createEntry() { protected @NotNull T createEntry() {
style.soundType = soundType.get();
style.defaultData = data.get();
style.renderer = renderer.get();
return style; return style;
} }
} }

View File

@ -13,6 +13,7 @@ import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.IntTag; import net.minecraft.nbt.IntTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.AABB;
@ -108,4 +109,18 @@ public class NBTHelper {
return new CompoundTag(); return new CompoundTag();
} }
public static void writeResourceLocation(CompoundTag nbt, String key, ResourceLocation location) {
// Ensure correct format
nbt.putString(key, location.toString());
}
public static ResourceLocation readResourceLocation(CompoundTag nbt, String key) {
if (!nbt.contains(key))
return null;
String[] data = nbt.getString(key).split(":");
if (data.length != 2)
return null;
return new ResourceLocation(data[0], data[1]);
}
} }