diff --git a/src/main/java/com/simibubi/create/content/curiosities/tools/BlueprintOverlayRenderer.java b/src/main/java/com/simibubi/create/content/curiosities/tools/BlueprintOverlayRenderer.java index a3aec3eab..a7058a785 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/tools/BlueprintOverlayRenderer.java +++ b/src/main/java/com/simibubi/create/content/curiosities/tools/BlueprintOverlayRenderer.java @@ -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; } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/BezierConnection.java b/src/main/java/com/simibubi/create/content/logistics/trains/BezierConnection.java index a65cdb8f6..992f670ae 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/BezierConnection.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/BezierConnection.java @@ -322,7 +322,7 @@ public class BezierConnection implements Iterable { 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(); @@ -350,7 +350,7 @@ public class BezierConnection implements Iterable { 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) @@ -364,7 +364,7 @@ public class BezierConnection implements Iterable { } 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)) diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/BogeySizes.java b/src/main/java/com/simibubi/create/content/logistics/trains/BogeySizes.java index 95bf3a5c8..41fc13344 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/BogeySizes.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/BogeySizes.java @@ -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); } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/TrackGraphVisualizer.java b/src/main/java/com/simibubi/create/content/logistics/trains/TrackGraphVisualizer.java index bc2c24241..2da7a2aaa 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/TrackGraphVisualizer.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/TrackGraphVisualizer.java @@ -274,7 +274,7 @@ public class TrackGraphVisualizer { if (extended) { Vec3 materialPos = edge.getPosition(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, 1, 1, 1)) .colored(graph.color); } @@ -292,7 +292,7 @@ public class TrackGraphVisualizer { if (extended) { Vec3 materialPos = edge.getPosition(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, 1, 1, 1)) .colored(graph.color); } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterial.java b/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterial.java index 759a243ea..48a44ac07 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterial.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterial.java @@ -11,13 +11,15 @@ 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.state.BlockBehaviour; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; -import net.minecraftforge.common.util.Lazy; import net.minecraftforge.fml.DistExecutor; +import org.jetbrains.annotations.Nullable; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -44,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; @@ -55,6 +60,13 @@ public class TrackMaterial { public TrackMaterial(ResourceLocation id, String langName, NonNullSupplier> trackBlock, ResourceLocation particle, Ingredient sleeperIngredient, Ingredient railsIngredient, TrackType trackType, Supplier> modelHolder) { + this(id, langName, trackBlock, particle, sleeperIngredient, railsIngredient, trackType, modelHolder, null); + } + + public TrackMaterial(ResourceLocation id, String langName, NonNullSupplier> trackBlock, + ResourceLocation particle, Ingredient sleeperIngredient, Ingredient railsIngredient, + TrackType trackType, Supplier> modelHolder, + @Nullable TrackType.TrackBlockFactory customFactory) { this.id = id; this.langName = langName; this.trackBlock = trackBlock; @@ -62,30 +74,44 @@ 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 getTrackBlock() { + public NonNullSupplier 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 allFromMod(String modid) { + return ALL.values().stream().filter(tm -> tm.isFromMod(modid)).toList(); } - public static List> allCustomBlocks(String modid) { + public static List> allBlocksFromMod(String modid) { List> list = new ArrayList<>(); - for (TrackMaterial material : allCustom(modid)) { - list.add(material.getTrackBlock()); + for (TrackMaterial material : allFromMod(modid)) { + list.add(material.getBlockSupplier()); } return list; } @@ -93,7 +119,7 @@ public class TrackMaterial { public static List> allBlocks() { List> list = new ArrayList<>(); for (TrackMaterial material : ALL.values()) { - list.add(material.getTrackBlock()); + list.add(material.getBlockSupplier()); } return list; } @@ -108,17 +134,15 @@ public class TrackMaterial { Create.LOGGER.error("Failed to parse serialized track material: "+serializedName); return ANDESITE; } - 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; } public static class TrackType { @FunctionalInterface - protected interface TrackBlockFactory { + public interface TrackBlockFactory { TrackBlock create(BlockBehaviour.Properties properties, TrackMaterial material); } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterialFactory.java b/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterialFactory.java index 878f09ce2..8ca0d8d84 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterialFactory.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/TrackMaterialFactory.java @@ -13,6 +13,8 @@ import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.fml.DistExecutor; +import org.jetbrains.annotations.Nullable; + import java.util.function.Supplier; import java.util.stream.Stream; @@ -25,6 +27,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 +118,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 +138,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); } } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackBlock.java b/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackBlock.java index 78cc17b3b..5cec4088c 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackBlock.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackBlock.java @@ -789,7 +789,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; } } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackPlacement.java b/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackPlacement.java index adf3e9707..e8ac39927 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackPlacement.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/track/TrackPlacement.java @@ -522,7 +522,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(); @@ -545,7 +545,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))