Merge remote-tracking branch 'origin/mc1.18/railways-api' into mc1.18/0.5.1

This commit is contained in:
simibubi 2023-05-10 11:24:58 +02:00
commit 3e9074cddc
11 changed files with 113 additions and 54 deletions

View file

@ -195,6 +195,7 @@ import com.simibubi.create.content.logistics.block.vault.ItemVaultBlockEntity;
import com.simibubi.create.content.logistics.item.LecternControllerBlockEntity;
import com.simibubi.create.content.logistics.item.LecternControllerRenderer;
import com.simibubi.create.content.logistics.trains.BogeyBlockEntityRenderer;
import com.simibubi.create.content.logistics.trains.TrackMaterial;
import com.simibubi.create.content.logistics.trains.management.display.FlapDisplayBlockEntity;
import com.simibubi.create.content.logistics.trains.management.display.FlapDisplayRenderer;
import com.simibubi.create.content.logistics.trains.management.edgePoint.observer.TrackObserverBlockEntity;
@ -831,8 +832,8 @@ public class AllBlockEntityTypes {
public static final BlockEntityEntry<TrackBlockEntity> TRACK = REGISTRATE
.blockEntity("track", TrackBlockEntity::new)
.instance(() -> TrackInstance::new)
.validBlocksDeferred(TrackMaterial::allBlocks)
.renderer(() -> TrackRenderer::new)
.validBlocks(AllBlocks.TRACK)
.register();
public static final BlockEntityEntry<FakeTrackBlockEntity> FAKE_TRACK = REGISTRATE

View file

@ -105,7 +105,7 @@ public class BlueprintOverlayRenderer {
int tracks = info.requiredTracks;
while (tracks > 0) {
ingredients.add(Pair.of(new ItemStack(info.trackMaterial.getTrackBlock().get(), Math.min(64, tracks)), info.hasRequiredTracks));
ingredients.add(Pair.of(new ItemStack(info.trackMaterial.getBlock(), Math.min(64, tracks)), info.hasRequiredTracks));
tracks -= 64;
}

View file

@ -358,7 +358,7 @@ public class BezierConnection implements Iterable<BezierConnection.Segment> {
Inventory inv = player.getInventory();
int tracks = getTrackItemCost();
while (tracks > 0) {
inv.placeItemBackInInventory(new ItemStack(getMaterial().getTrackBlock().get(), Math.min(64, tracks)));
inv.placeItemBackInInventory(new ItemStack(getMaterial().getBlock(), Math.min(64, tracks)));
tracks -= 64;
}
int girders = getGirderItemCost();
@ -386,7 +386,7 @@ public class BezierConnection implements Iterable<BezierConnection.Segment> {
continue;
Vec3 v = VecHelper.offsetRandomly(segment.position, level.random, .125f)
.add(origin);
ItemEntity entity = new ItemEntity(level, v.x, v.y, v.z, new ItemStack(getMaterial().getTrackBlock().get()));
ItemEntity entity = new ItemEntity(level, v.x, v.y, v.z, getMaterial().asStack());
entity.setDefaultPickUpDelay();
level.addFreshEntity(entity);
if (!hasGirder)
@ -400,7 +400,7 @@ public class BezierConnection implements Iterable<BezierConnection.Segment> {
}
public void spawnDestroyParticles(Level level) {
BlockParticleOption data = new BlockParticleOption(ParticleTypes.BLOCK, getMaterial().getTrackBlock().get().defaultBlockState());
BlockParticleOption data = new BlockParticleOption(ParticleTypes.BLOCK, getMaterial().getBlock().defaultBlockState());
BlockParticleOption girderData =
new BlockParticleOption(ParticleTypes.BLOCK, AllBlocks.METAL_GIRDER.getDefaultState());
if (!(level instanceof ServerLevel slevel))

View file

@ -48,7 +48,7 @@ public class BogeySizes {
return BOGEY_SIZES.size();
}
public record BogeySize(ResourceLocation location, Float wheelRadius) {
public record BogeySize(ResourceLocation location, float wheelRadius) {
public BogeySize(String modId, String name, float wheelRadius) {
this(new ResourceLocation(modId, name), wheelRadius);
}

View file

@ -20,7 +20,6 @@ import com.simibubi.create.foundation.utility.outliner.Outliner;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
@ -38,7 +37,6 @@ public class TrackGraphVisualizer {
Vec3 camera = cameraEntity.getEyePosition();
Outliner outliner = CreateClient.OUTLINER;
boolean ctrl = false; // AllKeys.isKeyDown(GLFW.GLFW_KEY_LEFT_CONTROL);
Map<UUID, SignalEdgeGroup> allGroups = Create.RAILWAYS.sided(null).signalEdgeGroups;
float width = 1 / 8f;
@ -271,8 +269,8 @@ public class TrackGraphVisualizer {
Vec3 materialPos = edge.getPosition(graph, 0.5)
.add(0, 1, 0);
CreateClient.OUTLINER.showItem(Pair.of(edge, edge.edgeData), materialPos,
new ItemStack(edge.getTrackMaterial().trackBlock.get()
.get()));
edge.getTrackMaterial()
.asStack());
CreateClient.OUTLINER.showAABB(edge.edgeData, AABB.ofSize(materialPos, .25, 0, .25)
.move(0, -0.5, 0))
.lineWidth(1 / 16f)
@ -292,9 +290,8 @@ public class TrackGraphVisualizer {
if (extended) {
Vec3 materialPos = edge.getPosition(graph, 0.5)
.add(0, 1, 0);
CreateClient.OUTLINER.showItem(Pair.of(edge, edge.edgeData), materialPos,
new ItemStack(edge.getTrackMaterial().trackBlock.get()
.get()));
CreateClient.OUTLINER.showItem(Pair.of(edge, edge.edgeData), materialPos, edge.getTrackMaterial()
.asStack());
CreateClient.OUTLINER.showAABB(edge.edgeData, AABB.ofSize(materialPos, .25, 0, .25)
.move(0, -0.5, 0))
.lineWidth(1 / 16f)

View file

@ -8,6 +8,8 @@ import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.core.PartialModel;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllPartialModels;
@ -18,7 +20,9 @@ import com.tterrag.registrate.util.nullness.NonNullSupplier;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@ -42,6 +46,9 @@ public class TrackMaterial {
public final ResourceLocation particle;
public final TrackType trackType;
@Nullable
private final TrackMaterial.TrackType.TrackBlockFactory customFactory;
@OnlyIn(Dist.CLIENT)
protected TrackModelHolder modelHolder;
@ -53,6 +60,13 @@ public class TrackMaterial {
public TrackMaterial(ResourceLocation id, String langName, NonNullSupplier<NonNullSupplier<? extends TrackBlock>> trackBlock,
ResourceLocation particle, Ingredient sleeperIngredient, Ingredient railsIngredient,
TrackType trackType, Supplier<Supplier<TrackModelHolder>> modelHolder) {
this(id, langName, trackBlock, particle, sleeperIngredient, railsIngredient, trackType, modelHolder, null);
}
public TrackMaterial(ResourceLocation id, String langName, NonNullSupplier<NonNullSupplier<? extends TrackBlock>> trackBlock,
ResourceLocation particle, Ingredient sleeperIngredient, Ingredient railsIngredient,
TrackType trackType, Supplier<Supplier<TrackModelHolder>> modelHolder,
@Nullable TrackType.TrackBlockFactory customFactory) {
this.id = id;
this.langName = langName;
this.trackBlock = trackBlock;
@ -60,38 +74,52 @@ public class TrackMaterial {
this.railsIngredient = railsIngredient;
this.particle = particle;
this.trackType = trackType;
this.customFactory = customFactory;
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> this.modelHolder = modelHolder.get().get());
ALL.put(this.id, this);
}
public NonNullSupplier<? extends TrackBlock> getTrackBlock() {
public NonNullSupplier<? extends TrackBlock> getBlockSupplier() {
return this.trackBlock.get();
}
public TrackBlock createBlock(BlockBehaviour.Properties properties) {
return this.trackType.factory.create(properties, this);
public TrackBlock getBlock() {
return getBlockSupplier().get();
}
public boolean isCustom(String modId) {
public ItemStack asStack() {
return asStack(1);
}
public ItemStack asStack(int count) {
return new ItemStack(getBlock(), count);
}
public TrackBlock createBlock(BlockBehaviour.Properties properties) {
return (this.customFactory != null ? this.customFactory : this.trackType.factory)
.create(properties, this);
}
public boolean isFromMod(String modId) {
return this.id.getNamespace().equals(modId);
}
public static TrackMaterial[] allCustom(String modid) {
return ALL.values().stream().filter(tm -> tm.isCustom(modid)).toArray(TrackMaterial[]::new);
public static List<TrackMaterial> allFromMod(String modid) {
return ALL.values().stream().filter(tm -> tm.isFromMod(modid)).toList();
}
public static List<NonNullSupplier<? extends TrackBlock>> allCustomBlocks(String modid) {
List<NonNullSupplier<? extends TrackBlock>> list = new ArrayList<>();
for (TrackMaterial material : allCustom(modid)) {
list.add(material.getTrackBlock());
public static List<NonNullSupplier<? extends Block>> allBlocksFromMod(String modid) {
List<NonNullSupplier<? extends Block>> list = new ArrayList<>();
for (TrackMaterial material : allFromMod(modid)) {
list.add(material.getBlockSupplier());
}
return list;
}
public static List<NonNullSupplier<? extends TrackBlock>> allBlocks() {
List<NonNullSupplier<? extends TrackBlock>> list = new ArrayList<>();
public static List<NonNullSupplier<? extends Block>> allBlocks() {
List<NonNullSupplier<? extends Block>> list = new ArrayList<>();
for (TrackMaterial material : ALL.values()) {
list.add(material.getTrackBlock());
list.add(material.getBlockSupplier());
}
return list;
}
@ -105,10 +133,8 @@ public class TrackMaterial {
return ANDESITE;
ResourceLocation id = ResourceLocation.tryParse(serializedName);
if (id != null)
for (TrackMaterial material : ALL.values())
if (material.id.equals(id))
return material;
if (ALL.containsKey(id))
return ALL.get(id);
Create.LOGGER.error("Failed to locate serialized track material: " + serializedName);
return ANDESITE;
@ -116,7 +142,7 @@ public class TrackMaterial {
public static class TrackType {
@FunctionalInterface
protected interface TrackBlockFactory {
public interface TrackBlockFactory {
TrackBlock create(BlockBehaviour.Properties properties, TrackMaterial material);
}

View file

@ -1,9 +1,13 @@
package com.simibubi.create.content.logistics.trains;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;
import com.jozufozu.flywheel.core.PartialModel;
import com.simibubi.create.AllTags;
import com.simibubi.create.content.logistics.trains.track.TrackBlock;
import com.tterrag.registrate.util.nullness.NonNullSupplier;
import net.minecraft.resources.ResourceLocation;
@ -13,9 +17,6 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class TrackMaterialFactory {
private final ResourceLocation id;
private String langName;
@ -25,6 +26,9 @@ public class TrackMaterialFactory {
private ResourceLocation particle;
private TrackMaterial.TrackType trackType = TrackMaterial.TrackType.STANDARD;
@Nullable
private TrackMaterial.TrackType.TrackBlockFactory customFactory = null;
@OnlyIn(Dist.CLIENT)
private TrackMaterial.TrackModelHolder modelHolder;
@OnlyIn(Dist.CLIENT)
@ -113,6 +117,11 @@ public class TrackMaterialFactory {
return this;
}
public TrackMaterialFactory customBlockFactory(TrackMaterial.TrackType.TrackBlockFactory factory) {
this.customFactory = factory;
return this;
}
public TrackMaterial build() {
assert trackBlock != null;
assert langName != null;
@ -128,6 +137,6 @@ public class TrackMaterialFactory {
modelHolder = new TrackMaterial.TrackModelHolder(tieModel, leftSegmentModel, rightSegmentModel);
}
});
return new TrackMaterial(id, langName, trackBlock, particle, sleeperIngredient, railsIngredient, trackType, () -> () -> modelHolder);
return new TrackMaterial(id, langName, trackBlock, particle, sleeperIngredient, railsIngredient, trackType, () -> () -> modelHolder, customFactory);
}
}

View file

@ -808,8 +808,7 @@ public class TrackBlock extends Block
for (TrackMaterial material : otherTrackAmounts.keySet()) {
int amt = otherTrackAmounts.getOrDefault(material, 0);
while (amt > 0) {
stacks.add(new ItemStack(material.getTrackBlock()
.get(), Math.min(amt, 64)));
stacks.add(material.asStack(Math.min(amt, 64)));
amt -= 64;
}
}

View file

@ -521,7 +521,7 @@ public class TrackPlacement {
BlockPos offsetPos = pos.offset(offset.x, offset.y, offset.z);
BlockState stateAtPos = level.getBlockState(offsetPos);
// copy over all shared properties from the shaped state to the correct track material block
BlockState toPlace = copyProperties(state, info.trackMaterial.getTrackBlock().get().defaultBlockState());
BlockState toPlace = copyProperties(state, info.trackMaterial.getBlock().defaultBlockState());
boolean canPlace = stateAtPos.getMaterial()
.isReplaceable();
@ -544,7 +544,7 @@ public class TrackPlacement {
return info;
if (!simulate) {
BlockState onto = info.trackMaterial.getTrackBlock().get().defaultBlockState();
BlockState onto = info.trackMaterial.getBlock().defaultBlockState();
BlockState stateAtPos = level.getBlockState(targetPos1);
level.setBlock(targetPos1, ProperWaterloggedBlock.withWater(level,
(AllTags.AllBlockTags.TRACKS.matches(stateAtPos) ? stateAtPos : copyProperties(state1, onto))

View file

@ -1,6 +1,9 @@
package com.simibubi.create.foundation.data;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import javax.annotation.Nullable;
@ -13,7 +16,9 @@ import com.tterrag.registrate.builders.BuilderCallback;
import com.tterrag.registrate.util.OneTimeEventReceiver;
import com.tterrag.registrate.util.nullness.NonNullSupplier;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.util.NonNullPredicate;
import net.minecraftforge.fml.DistExecutor;
@ -25,6 +30,9 @@ public class CreateBlockEntityBuilder<T extends BlockEntity, P> extends BlockEnt
private NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory;
private NonNullPredicate<T> renderNormally;
private Collection<NonNullSupplier<? extends Collection<NonNullSupplier<? extends Block>>>> deferredValidBlocks =
new ArrayList<>();
public static <T extends BlockEntity, P> BlockEntityBuilder<T, P> create(AbstractRegistrate<?> owner, P parent,
String name, BuilderCallback callback, BlockEntityFactory<T> factory) {
return new CreateBlockEntityBuilder<>(owner, parent, name, callback, factory);
@ -35,15 +43,35 @@ public class CreateBlockEntityBuilder<T extends BlockEntity, P> extends BlockEnt
super(owner, parent, name, callback, factory);
}
public CreateBlockEntityBuilder<T, P> instance(NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory) {
public CreateBlockEntityBuilder<T, P> validBlocksDeferred(
NonNullSupplier<? extends Collection<NonNullSupplier<? extends Block>>> blocks) {
deferredValidBlocks.add(blocks);
return this;
}
@Override
protected BlockEntityType<T> createEntry() {
deferredValidBlocks.stream()
.map(Supplier::get)
.flatMap(Collection::stream)
.forEach(this::validBlock);
return super.createEntry();
}
public CreateBlockEntityBuilder<T, P> instance(
NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory) {
return instance(instanceFactory, true);
}
public CreateBlockEntityBuilder<T, P> instance(NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory, boolean renderNormally) {
public CreateBlockEntityBuilder<T, P> instance(
NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory,
boolean renderNormally) {
return instance(instanceFactory, be -> renderNormally);
}
public CreateBlockEntityBuilder<T, P> instance(NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory, NonNullPredicate<T> renderNormally) {
public CreateBlockEntityBuilder<T, P> instance(
NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory,
NonNullPredicate<T> renderNormally) {
if (this.instanceFactory == null) {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> this::registerInstance);
}
@ -56,7 +84,8 @@ public class CreateBlockEntityBuilder<T extends BlockEntity, P> extends BlockEnt
protected void registerInstance() {
OneTimeEventReceiver.addModListener(FMLClientSetupEvent.class, $ -> {
NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory = this.instanceFactory;
NonNullSupplier<BiFunction<MaterialManager, T, BlockEntityInstance<? super T>>> instanceFactory =
this.instanceFactory;
if (instanceFactory != null) {
NonNullPredicate<T> renderNormally = this.renderNormally;
InstancedRenderRegistry.configure(getEntry())

View file

@ -21,17 +21,13 @@ import com.simibubi.create.foundation.ponder.content.trains.TrackScenes;
import com.simibubi.create.foundation.ponder.content.trains.TrainScenes;
import com.simibubi.create.foundation.ponder.content.trains.TrainSignalScenes;
import com.simibubi.create.foundation.ponder.content.trains.TrainStationScenes;
import com.tterrag.registrate.util.entry.BlockEntry;
import com.tterrag.registrate.util.entry.ItemProviderEntry;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import java.util.stream.Collectors;
public class PonderIndex {
static final PonderRegistrationHelper HELPER = new PonderRegistrationHelper(Create.ID);
@ -329,12 +325,14 @@ public class PonderIndex {
.addStoryBoard("threshold_switch", DetectorScenes::thresholdSwitch);
// Trains
HELPER.forComponents(TrackMaterial.allBlocks().stream()
.map((trackSupplier) -> new BlockEntry<TrackBlock>(
// note: these blocks probably WON'T be in the Create Registrate, but a simple code trace reveals the Entry's registrate isn't used
Create.REGISTRATE,
RegistryObject.create(trackSupplier.get().getRegistryName(), ForgeRegistries.BLOCKS)))
.toList())
HELPER.forComponents(TrackMaterial.allBlocks()
.stream()
.map((trackSupplier) -> new BlockEntry<TrackBlock>(
// note: these blocks probably WON'T be in the Create Registrate, but a simple
// code trace reveals the Entry's registrate isn't used
Create.REGISTRATE, RegistryObject.create(trackSupplier.get()
.getRegistryName(), ForgeRegistries.BLOCKS)))
.toList())
.addStoryBoard("train_track/placement", TrackScenes::placement)
.addStoryBoard("train_track/portal", TrackScenes::portal)
.addStoryBoard("train_track/chunks", TrackScenes::chunks);