From 4292485b64cec99bf65e208bc72faffd05bec67f Mon Sep 17 00:00:00 2001 From: Aeiou <3160746+AeiouEnigma@users.noreply.github.com> Date: Sat, 18 Dec 2021 17:37:42 -0500 Subject: [PATCH 1/3] Implement Starlight compatibility. --- build.gradle | 1 + .../structureMovement/ContraptionWorld.java | 10 ++++++ .../render/ContraptionRenderDispatcher.java | 2 +- .../PlacementSimulationWorld.java | 35 +++++++++++++++++-- .../worldWrappers/chunk/WrappedChunk.java | 15 ++++++-- 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 25b917f58..beb2fff1d 100644 --- a/build.gradle +++ b/build.gradle @@ -170,6 +170,7 @@ dependencies { compileOnly fg.deobf("mezz.jei:jei-${jei_minecraft_version}:${jei_version}:api") runtimeOnly fg.deobf("mezz.jei:jei-${jei_minecraft_version}:${jei_version}") + compileOnly fg.deobf("curse.maven:starlight-526854:3559934") // implementation fg.deobf("curse.maven:druidcraft-340991:3101903") // implementation fg.deobf("com.ferreusveritas.dynamictrees:DynamicTrees-1.16.5:0.10.0-Beta25") diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionWorld.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionWorld.java index 16abe0041..d08a28337 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionWorld.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionWorld.java @@ -46,4 +46,14 @@ public class ContraptionWorld extends WrappedWorld { public void playLocalSound(double x, double y, double z, SoundEvent p_184134_7_, SoundSource p_184134_8_, float p_184134_9_, float p_184134_10_, boolean p_184134_11_) { world.playLocalSound(x, y, z, p_184134_7_, p_184134_8_, p_184134_9_, p_184134_10_, p_184134_11_); } + + @Override + public int getMinBuildHeight() { + return -1 * (int)this.contraption.bounds.getYsize(); + } + + @Override + public int getHeight() { + return -2 * getMinBuildHeight(); + } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java index 59ef2e7eb..82f43ce96 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java @@ -104,7 +104,7 @@ public class ContraptionRenderDispatcher { } public static PlacementSimulationWorld setupRenderWorld(Level world, Contraption c) { - PlacementSimulationWorld renderWorld = new PlacementSimulationWorld(world); + PlacementSimulationWorld renderWorld = new PlacementSimulationWorld(world, c); renderWorld.setTileEntities(c.presentTileEntities.values()); diff --git a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java index e97ec1adc..1f0b289d3 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java +++ b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java @@ -9,6 +9,8 @@ import java.util.function.Predicate; import com.jozufozu.flywheel.api.FlywheelWorld; +import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; + import net.minecraft.core.BlockPos; import net.minecraft.core.SectionPos; import net.minecraft.world.level.Level; @@ -16,8 +18,12 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.ChunkAccess; +import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.lighting.LevelLightEngine; +import org.jetbrains.annotations.NotNull; + public class PlacementSimulationWorld extends WrappedWorld implements FlywheelWorld { public Map blocksAdded; public Map tesAdded; @@ -27,12 +33,15 @@ public class PlacementSimulationWorld extends WrappedWorld implements FlywheelWo public WrappedChunkProvider chunkProvider; private final BlockPos.MutableBlockPos scratch = new BlockPos.MutableBlockPos(); - public PlacementSimulationWorld(Level wrapped) { - this(wrapped, new WrappedChunkProvider()); + private final Contraption contraption; + + public PlacementSimulationWorld(Level wrapped, Contraption c) { + this(wrapped, c, new WrappedChunkProvider()); } - public PlacementSimulationWorld(Level wrapped, WrappedChunkProvider chunkProvider) { + public PlacementSimulationWorld(Level wrapped, @NotNull Contraption c, WrappedChunkProvider chunkProvider) { super(wrapped, chunkProvider); + contraption = c; this.chunkProvider = chunkProvider.setPlacementWorld(this); spannedSections = new HashSet<>(); lighter = new LevelLightEngine(chunkProvider, true, false); // blockLight, skyLight @@ -117,4 +126,24 @@ public class PlacementSimulationWorld extends WrappedWorld implements FlywheelWo return state; return Blocks.AIR.defaultBlockState(); } + + @Override + public int getMinBuildHeight() { + return contraption.getContraptionWorld().getMinBuildHeight(); + } + + @Override + public int getHeight() { + return contraption.getContraptionWorld().getHeight(); + } + + // Override Starlight's ExtendedWorld interface methods: + + public LevelChunk getChunkAtImmediately(final int chunkX, final int chunkZ) { + return chunkProvider.getChunk(chunkX, chunkZ, false); + } + + public ChunkAccess getAnyChunkImmediately(final int chunkX, final int chunkZ) { + return chunkProvider.getChunk(chunkX, chunkZ); + } } diff --git a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java index fbca9d03b..60dbcd16b 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java +++ b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java @@ -7,6 +7,10 @@ import java.util.stream.Stream; import javax.annotation.Nullable; +import ca.spottedleaf.starlight.common.chunk.ExtendedChunk; + +import ca.spottedleaf.starlight.common.light.StarLightEngine; + import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import it.unimi.dsi.fastutil.longs.LongSet; @@ -30,6 +34,7 @@ import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.ticks.BlackholeTickAccess; import net.minecraft.world.ticks.TickContainerAccess; +import net.minecraftforge.fml.ModList; public class WrappedChunk extends ChunkAccess { @@ -50,11 +55,17 @@ public class WrappedChunk extends ChunkAccess { this.x = x; this.z = z; - this.sections = new LevelChunkSection[16]; + // Do not hard code the number of chunk sections + this.sections = new LevelChunkSection[world.getSectionsCount()]; - for (int i = 0; i < 16; i++) { + for (int i = 0; i < this.sections.length; i++) { sections[i] = new WrappedChunkSection(this, i << 4); } + // If Starlight is loaded, initialize its NibbleArrays for this chunk to default values + if (ModList.get().isLoaded("starlight")) { + ((ExtendedChunk)this).setBlockNibbles(StarLightEngine.getFilledEmptyLight(this)); + ((ExtendedChunk)this).setSkyNibbles(StarLightEngine.getFilledEmptyLight(this)); + } } @Override From 56fd2e0a636582f934f5fdf02079200f3f9d6029 Mon Sep 17 00:00:00 2001 From: Aeiou <3160746+AeiouEnigma@users.noreply.github.com> Date: Sun, 19 Dec 2021 17:17:17 -0500 Subject: [PATCH 2/3] Change from jetbrains @NotNull to javax @Nonnull annotation in PlacementSimulationWorld --- .../utility/worldWrappers/PlacementSimulationWorld.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java index 1f0b289d3..60410efd9 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java +++ b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/PlacementSimulationWorld.java @@ -22,7 +22,7 @@ import net.minecraft.world.level.chunk.ChunkAccess; import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.lighting.LevelLightEngine; -import org.jetbrains.annotations.NotNull; +import javax.annotation.Nonnull; public class PlacementSimulationWorld extends WrappedWorld implements FlywheelWorld { public Map blocksAdded; @@ -39,7 +39,7 @@ public class PlacementSimulationWorld extends WrappedWorld implements FlywheelWo this(wrapped, c, new WrappedChunkProvider()); } - public PlacementSimulationWorld(Level wrapped, @NotNull Contraption c, WrappedChunkProvider chunkProvider) { + public PlacementSimulationWorld(Level wrapped, @Nonnull Contraption c, WrappedChunkProvider chunkProvider) { super(wrapped, chunkProvider); contraption = c; this.chunkProvider = chunkProvider.setPlacementWorld(this); From 3415ba97ac9597ca79f68c3ccb17ba43386bbe17 Mon Sep 17 00:00:00 2001 From: Aeiou <3160746+AeiouEnigma@users.noreply.github.com> Date: Sun, 19 Dec 2021 18:43:38 -0500 Subject: [PATCH 3/3] Add Starlight to Mods enum, along with an executeIfInstalled method. Use said method in WrappedChunk in place of an if-statement to execute Starlight-dependent code --- src/main/java/com/simibubi/create/compat/Mods.java | 11 +++++++++++ .../utility/worldWrappers/chunk/WrappedChunk.java | 7 ++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/simibubi/create/compat/Mods.java b/src/main/java/com/simibubi/create/compat/Mods.java index b32960505..6f6684554 100644 --- a/src/main/java/com/simibubi/create/compat/Mods.java +++ b/src/main/java/com/simibubi/create/compat/Mods.java @@ -12,6 +12,7 @@ import net.minecraftforge.fml.ModList; */ public enum Mods { DYNAMICTREES, + STARLIGHT, TCONSTRUCT; /** @@ -38,4 +39,14 @@ public enum Mods { return Optional.of(toRun.get().get()); return Optional.empty(); } + + /** + * Simple hook to execute code if a mod is installed + * @param toExecute will be executed only if the mod is loaded + */ + public void executeIfInstalled(Supplier toExecute) { + if (isLoaded()) { + toExecute.get().run(); + } + } } diff --git a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java index 60dbcd16b..cb12d4c83 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java +++ b/src/main/java/com/simibubi/create/foundation/utility/worldWrappers/chunk/WrappedChunk.java @@ -11,6 +11,7 @@ import ca.spottedleaf.starlight.common.chunk.ExtendedChunk; import ca.spottedleaf.starlight.common.light.StarLightEngine; +import com.simibubi.create.compat.Mods; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import it.unimi.dsi.fastutil.longs.LongSet; @@ -34,7 +35,6 @@ import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.ticks.BlackholeTickAccess; import net.minecraft.world.ticks.TickContainerAccess; -import net.minecraftforge.fml.ModList; public class WrappedChunk extends ChunkAccess { @@ -61,11 +61,12 @@ public class WrappedChunk extends ChunkAccess { for (int i = 0; i < this.sections.length; i++) { sections[i] = new WrappedChunkSection(this, i << 4); } + // If Starlight is loaded, initialize its NibbleArrays for this chunk to default values - if (ModList.get().isLoaded("starlight")) { + Mods.STARLIGHT.executeIfInstalled(() -> () -> { ((ExtendedChunk)this).setBlockNibbles(StarLightEngine.getFilledEmptyLight(this)); ((ExtendedChunk)this).setSkyNibbles(StarLightEngine.getFilledEmptyLight(this)); - } + }); } @Override