From d433fdea623ff407dcd7ec1f0be0936c1f4d93f3 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sun, 28 Apr 2024 17:35:10 -0500 Subject: [PATCH] Fix white grass/water if the biome is null --- .../common/wrappers/block/BiomeWrapper.java | 2 + .../block/TintWithoutLevelOverrider.java | 72 +++++++++++++++---- .../block/cache/ClientBlockStateCache.java | 14 ++-- .../wrappers/world/ClientLevelWrapper.java | 21 ++++++ coreSubProjects | 2 +- 5 files changed, 93 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/BiomeWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/BiomeWrapper.java index b403e834c..9b8f889f6 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/BiomeWrapper.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/BiomeWrapper.java @@ -67,6 +67,8 @@ public class BiomeWrapper implements IBiomeWrapper public static final String EMPTY_BIOME_STRING = "EMPTY"; public static final BiomeWrapper EMPTY_WRAPPER = new BiomeWrapper(null, null); + public static final String PLAINS_RESOURCE_LOCATION_STRING = "minecraft:plains"; + /** keep track of broken biomes so we don't log every time */ private static final HashSet brokenResourceLocationStrings = new HashSet<>(); diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/TintWithoutLevelOverrider.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/TintWithoutLevelOverrider.java index 6f425019e..4c27070fc 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/TintWithoutLevelOverrider.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/TintWithoutLevelOverrider.java @@ -19,14 +19,20 @@ package com.seibel.distanthorizons.common.wrappers.block; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; +import com.seibel.distanthorizons.core.util.ColorUtil; +import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.level.*; import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.biome.Biomes; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.lighting.LevelLightEngine; import net.minecraft.world.level.material.FluidState; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; #if MC_VER >= MC_1_18_2 @@ -35,18 +41,54 @@ import net.minecraft.core.Holder; public class TintWithoutLevelOverrider implements BlockAndTintGetter { - final BiomeWrapper biome; + /** + * This will only ever be null if there was an issue with {@link IClientLevelWrapper#getPlainsBiomeWrapper()} + * but {@link Nullable} is there just in case. + */ + @Nullable + private final Biome biome; - public TintWithoutLevelOverrider(BiomeWrapper biome) + + + public TintWithoutLevelOverrider(BiomeWrapper biomeWrapper, IClientLevelWrapper clientLevelWrapper) { - this.biome = biome; + // try to get the wrapped biome + Biome unwrappedBiome = null; + if (biomeWrapper.biome != null) + { + unwrappedBiome = unwrap(biomeWrapper.biome); + } + + if(unwrappedBiome == null) + { + // we are looking at the empty biome wrapper, try using plains as a backup + BiomeWrapper plainsBiomeWrapper = ((BiomeWrapper) clientLevelWrapper.getPlainsBiomeWrapper()); + if (plainsBiomeWrapper != null) + { + unwrappedBiome = unwrap(plainsBiomeWrapper.biome); + } + } + + this.biome = unwrappedBiome; } + + + @Override - public int getBlockTint(BlockPos blockPos, ColorResolver colorResolver) - { - return colorResolver.getColor(_unwrap(biome.biome), blockPos.getX(), blockPos.getZ()); + public int getBlockTint(@NotNull BlockPos blockPos, @NotNull ColorResolver colorResolver) + { + if (this.biome != null) + { + return colorResolver.getColor(this.biome, blockPos.getX(), blockPos.getZ()); + } + else + { + // hopefully unneeded debug color + return ColorUtil.CYAN; + } } - private Biome _unwrap(#if MC_VER >= MC_1_18_2 Holder #else Biome #endif biome) + + private static Biome unwrap(#if MC_VER >= MC_1_18_2 Holder #else Biome #endif biome) { #if MC_VER >= MC_1_18_2 return biome.value(); @@ -55,30 +97,36 @@ public class TintWithoutLevelOverrider implements BlockAndTintGetter #endif } + + + //================// + // unused methods // + //================// + @Override - public float getShade(Direction direction, boolean shade) + public float getShade(@NotNull Direction direction, boolean shade) { throw new UnsupportedOperationException("ERROR: getShade() called on TintWithoutLevelOverrider. Object is for tinting only."); } @Override - public LevelLightEngine getLightEngine() + public @NotNull LevelLightEngine getLightEngine() { throw new UnsupportedOperationException("ERROR: getLightEngine() called on TintWithoutLevelOverrider. Object is for tinting only."); } @Nullable @Override - public BlockEntity getBlockEntity(BlockPos pos) + public BlockEntity getBlockEntity(@NotNull BlockPos pos) { throw new UnsupportedOperationException("ERROR: getBlockEntity() called on TintWithoutLevelOverrider. Object is for tinting only."); } @Override - public BlockState getBlockState(BlockPos pos) + public @NotNull BlockState getBlockState(@NotNull BlockPos pos) { throw new UnsupportedOperationException("ERROR: getBlockState() called on TintWithoutLevelOverrider. Object is for tinting only."); } @Override - public FluidState getFluidState(BlockPos pos) + public @NotNull FluidState getFluidState(@NotNull BlockPos pos) { throw new UnsupportedOperationException("ERROR: getFluidState() called on TintWithoutLevelOverrider. Object is for tinting only."); } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/cache/ClientBlockStateCache.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/cache/ClientBlockStateCache.java index 01b89f04f..52b81458c 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/cache/ClientBlockStateCache.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/block/cache/ClientBlockStateCache.java @@ -54,7 +54,6 @@ import java.util.List; */ public class ClientBlockStateCache { - private static final Logger LOGGER = DhLoggerBuilder.getLogger(); private static final HashSet BLOCK_STATES_THAT_NEED_LEVEL = new HashSet<>(); @@ -66,15 +65,20 @@ public class ClientBlockStateCache public static final RandomSource random = RandomSource.create(); #endif + public final IClientLevelWrapper levelWrapper; public final BlockState blockState; public final LevelReader level; public final BlockPos pos; + + + public ClientBlockStateCache(BlockState blockState, IClientLevelWrapper samplingLevel, DhBlockPos samplingPos) { this.blockState = blockState; - level = (LevelReader) samplingLevel.getWrappedMcObject(); - pos = McObjectConverter.Convert(samplingPos); - resolveColors(); + this.levelWrapper = samplingLevel; + this.level = (LevelReader) samplingLevel.getWrappedMcObject(); + this.pos = McObjectConverter.Convert(samplingPos); + this.resolveColors(); //LOGGER.info("ClientBlocKCache created for {}", blockState); } @@ -363,7 +367,7 @@ public class ClientBlockStateCache try { tintColor = Minecraft.getInstance().getBlockColors() - .getColor(this.blockState, new TintWithoutLevelOverrider(biome), McObjectConverter.Convert(pos), this.tintIndex); + .getColor(this.blockState, new TintWithoutLevelOverrider(biome, this.levelWrapper), McObjectConverter.Convert(pos), this.tintIndex); } catch (UnsupportedOperationException e) { diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/world/ClientLevelWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/world/ClientLevelWrapper.java index b51737876..3bfe90a38 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/world/ClientLevelWrapper.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/world/ClientLevelWrapper.java @@ -43,6 +43,7 @@ public class ClientLevelWrapper implements IClientLevelWrapper private final ClientBlockDetailMap blockMap = new ClientBlockDetailMap(this); private BlockStateWrapper dirtBlockWrapper; + private BiomeWrapper plainsBiomeWrapper; @@ -139,6 +140,26 @@ public class ClientLevelWrapper implements IClientLevelWrapper return this.blockMap.getColor(this.dirtBlockWrapper.blockState, BiomeWrapper.EMPTY_WRAPPER, DhBlockPos.ZERO); } + @Override + public IBiomeWrapper getPlainsBiomeWrapper() + { + if (this.plainsBiomeWrapper == null) + { + try + { + this.plainsBiomeWrapper = (BiomeWrapper) BiomeWrapper.deserialize(BiomeWrapper.PLAINS_RESOURCE_LOCATION_STRING, this); + } + catch (IOException e) + { + // shouldn't happen, but just in case + LOGGER.warn("Unable to get planes biome with resource location ["+BiomeWrapper.PLAINS_RESOURCE_LOCATION_STRING+"] with level ["+this+"].", e); + return null; + } + } + + return this.plainsBiomeWrapper; + } + @Override public IDimensionTypeWrapper getDimensionType() { return DimensionTypeWrapper.getDimensionTypeWrapper(this.level.dimensionType()); } diff --git a/coreSubProjects b/coreSubProjects index 3b600ce80..c83140a2d 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 3b600ce800de5eb2a26cad98bb8ba5c9cd62871b +Subproject commit c83140a2d002022f938e5521a495764c0e435b56