Skip to content

Commit

Permalink
feat: Add Sodium compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveplays28 committed Jun 10, 2024
1 parent 2f881fe commit 1f20d99
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 128 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//file:noinspection GroovyAssignabilityCheck

plugins {
id 'fabric-loom' version '1.2-SNAPSHOT'
id 'maven-publish'
id "fabric-loom" version "${loom_version}"
id "maven-publish"
}

version = project.mod_version
Expand Down Expand Up @@ -46,6 +46,9 @@ dependencies {

// Fabric Permissions API
include(modImplementation("me.lucko:fabric-permissions-api:${project.fabric_permissions_api_version}"))

// Sodium
modImplementation("maven.modrinth:sodium:${project.sodium_version}")
}

processResources {
Expand Down
14 changes: 4 additions & 10 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ org.gradle.jvmargs=-Xmx1G
# Check these on https://modmuss50.me/fabric.html
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.14.25
loader_version=0.15.6
loom_version=1.6-SNAPSHOT

# Mod properties
mod_version=1.0-SNAPSHOT
Expand All @@ -14,17 +15,10 @@ archives_base_name=simple-seasons

# Runtime mods
modmenu_version=7.2.1
sodium_version=mc1.20.1-0.5.1
reeses_sodium_options_version=mc1.20.1-1.6.4
lithium_version=mc1.20.1-0.11.2
lazydfu_version=0.1.3
ferrite_core_version=6.0.0-fabric
appleskin_version=2.5.0+mc1.20
cloth_config_version=11.1.106+fabric
modernfix_version=5.6.1+mc1.20.1
starlight_version=1.1.2+1.20

# Dependencies
mixin_extras_version=0.3.6
fabric_api_version=0.87.0+1.20.1
fabric_api_version=0.92.2+1.20.1
fabric_permissions_api_version=0.2-SNAPSHOT
sodium_version=mc1.20.1-0.5.8
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.steveplays28.simpleseasons.client.compat.sodium;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class SimpleSeasonsSodiumCompat {
private static final @NotNull String SODIUM_MOD_ID = "sodium";
private static final @NotNull String EMBEDDIUM_MOD_ID = "embeddium";
private static final @NotNull String RUBIDIUM_MOD_ID = "rubidium";

public static boolean isSodiumLoaded() {
var fabricLoader = FabricLoader.getInstance();
return fabricLoader.isModLoaded(SODIUM_MOD_ID) || fabricLoader.isModLoaded(EMBEDDIUM_MOD_ID) || fabricLoader.isModLoaded(
RUBIDIUM_MOD_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonParser;
import io.github.steveplays28.simpleseasons.SimpleSeasons;
import io.github.steveplays28.simpleseasons.api.SimpleSeasonsApi;
import io.github.steveplays28.simpleseasons.client.extension.world.ClientWorldExtension;
import io.github.steveplays28.simpleseasons.client.registry.SeasonColorRegistries;
import io.github.steveplays28.simpleseasons.client.util.season.color.SeasonColorUtil;
import io.github.steveplays28.simpleseasons.state.world.SeasonTracker;
Expand All @@ -12,15 +13,16 @@
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.resource.SimpleResourceReloadListener;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.color.world.BiomeColors;
import net.minecraft.client.color.world.FoliageColors;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.SimpleRegistry;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.profiler.Profiler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -136,24 +138,64 @@ private void loadSeasonColors(@NotNull ResourceManager resourceManager, @NotNull
}

private void registerBlockColorProvider(Identifier blockIdentifier) {
ColorProviderRegistry.BLOCK.register((blockState, world, blockPos, tintIndex) -> {
if (world == null || blockPos == null) {
ColorProviderRegistry.BLOCK.register((blockState, blockRenderView, startBlockPos, tintIndex) -> {
@NotNull var client = MinecraftClient.getInstance();
@Nullable var clientWorld = client.world;
if (clientWorld == null || startBlockPos == null) {
return FoliageColors.getDefaultColor();
}

return BiomeColors.getFoliageColor(world, blockPos);
@NotNull var blockPosColorCache = ((ClientWorldExtension) clientWorld).simple_seasons$getColorCache().blockPosColorCache;
@Nullable var cachedStartBlockSeasonColor = blockPosColorCache.get(startBlockPos);
if (cachedStartBlockSeasonColor != null) {
return cachedStartBlockSeasonColor.toInt();
}

var biomeBlendedBlockSeasonColor = clientWorld.calculateColor(startBlockPos, (biome, x, z) -> {
@NotNull var blockPos = new BlockPos((int) Math.round(x), startBlockPos.getY(), (int) Math.round(z));
// Nullable due to concurrency
@Nullable var cachedBlockSeasonColor = blockPosColorCache.get(blockPos);
if (cachedBlockSeasonColor != null) {
return cachedBlockSeasonColor.toInt();
}

@Nullable var blockSeasonColor = SeasonColorUtil.getBlockSeasonColor(
Registries.BLOCK.getId(clientWorld.getBlockState(blockPos).getBlock()),
clientWorld.getBiome(blockPos),
SimpleSeasonsApi.getSeason(clientWorld),
SimpleSeasonsApi.getSeasonProgress(clientWorld)
);
if (blockSeasonColor != null) {
return blockSeasonColor.toInt();
}

@Nullable var startBlockSeasonColor = SeasonColorUtil.getBlockSeasonColor(
Registries.BLOCK.getId(clientWorld.getBlockState(startBlockPos).getBlock()),
clientWorld.getBiome(startBlockPos),
SimpleSeasonsApi.getSeason(clientWorld),
SimpleSeasonsApi.getSeasonProgress(clientWorld)
);
if (startBlockSeasonColor == null) {
return SeasonColorUtil.FALLBACK_SEASON_COLOR_PRECALCULATED;
}

return startBlockSeasonColor.toInt();
});

blockPosColorCache.put(startBlockPos, new Color(biomeBlendedBlockSeasonColor));
return biomeBlendedBlockSeasonColor;
}, Registries.BLOCK.get(blockIdentifier));
}

private void registerItemColorProvider(Identifier itemIdentifier) {
ColorProviderRegistry.ITEM.register((stack, tintIndex) -> {
var client = MinecraftClient.getInstance();
@NotNull var client = MinecraftClient.getInstance();
if (client.world == null || client.player == null) {
throw new IllegalStateException(
"Error occurred while registering item color providers: client.world == null || client.player == null.");
}

var itemSeasonColor = SeasonColorUtil.getItemSeasonColor(Registries.ITEM.getId(stack.getItem()),
@Nullable var itemSeasonColor = SeasonColorUtil.getItemSeasonColor(Registries.ITEM.getId(stack.getItem()),
client.world.getBiome(client.player.getBlockPos()),
SimpleSeasonsApi.getSeason(client.world), SimpleSeasonsApi.getSeasonProgress(client.world)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.github.steveplays28.simpleseasons.state.world.SeasonTracker;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.MinecraftClient;
Expand All @@ -16,19 +15,17 @@

@Environment(EnvType.CLIENT)
public class ClientSeasonTracker extends SeasonTracker {
private MinecraftClient client;

public ClientSeasonTracker() {
super();

ClientLifecycleEvents.CLIENT_STARTED.register(client -> this.client = client);
ClientPlayNetworking.registerGlobalReceiver(SEASON_PACKET_CHANNEL, this::onSeasonStatePacketReceived);
}

@Override
public void setSeasonProgress(float seasonProgress) {
super.setSeasonProgress(seasonProgress);

@NotNull var client = MinecraftClient.getInstance();
if (client.world == null) {
throw new IllegalStateException("Error occurred while setting the season's progress: client.world == null.");
}
Expand All @@ -41,8 +38,11 @@ private void onSeasonStatePacketReceived(@NotNull MinecraftClient client, @NotNu
return;
}

this.setSeason(buf.readInt());
this.setSeasonProgress(buf.readFloat());
RenderingUtil.reloadChunkColors(client.world);
final var seasonId = buf.readInt();
final var seasonProgress = buf.readFloat();
client.executeSync(() -> {
this.setSeason(seasonId);
this.setSeasonProgress(seasonProgress);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
package io.github.steveplays28.simpleseasons.client.util.rendering;

import io.github.steveplays28.simpleseasons.client.compat.sodium.SimpleSeasonsSodiumCompat;
import io.github.steveplays28.simpleseasons.mixin.client.accessor.WorldRendererAccessor;
import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Environment(EnvType.CLIENT)
public class RenderingUtil {
@SuppressWarnings("ForLoopReplaceableByForEach")
public static void reloadChunkColors(@NotNull ClientWorld world) {
world.reloadColor();
public static void reloadChunkColors(@NotNull ClientWorld clientWorld) {
clientWorld.reloadColor();

var chunks = ((WorldRendererAccessor) world.worldRenderer).getChunks();
if (SimpleSeasonsSodiumCompat.isSodiumLoaded()) {
@NotNull var client = MinecraftClient.getInstance();
@Nullable var player = client.player;
if (player == null) {
return;
}

var playerChunkPos = player.getChunkPos();
var clampedViewDistance = MinecraftClient.getInstance().options.getClampedViewDistance();
SodiumWorldRenderer.instance().scheduleRebuildForChunks(
playerChunkPos.x - clampedViewDistance, clientWorld.getBottomY() >> 4, playerChunkPos.z - clampedViewDistance,
playerChunkPos.x + clampedViewDistance, clientWorld.getTopY() >> 4, playerChunkPos.z + clampedViewDistance, true
);
return;
}

@Nullable var chunks = ((WorldRendererAccessor) clientWorld.worldRenderer).getChunks();
if (chunks == null) {
return;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
@Environment(EnvType.CLIENT)
@Mixin(WorldRenderer.class)
public interface WorldRendererAccessor {
@Accessor("chunks")
@Accessor
@Nullable BuiltChunkStorage getChunks();
}

This file was deleted.

1 change: 1 addition & 0 deletions src/main/resources/simple-seasons.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ accessible method net/minecraft/client/render/WorldRenderer scheduleChunkRender
accessible class net/minecraft/world/biome/Biome$Weather
accessible field net/minecraft/world/biome/Biome weather Lnet/minecraft/world/biome/Biome$Weather;
accessible class net/minecraft/client/render/WorldRenderer$ChunkInfo
accessible class net/minecraft/client/world/ClientChunkManager$ClientChunkMap
2 changes: 0 additions & 2 deletions src/main/resources/simple-seasons.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
"world.gen.feature.FreezeTopLayerFeatureMixin"
],
"client": [
"client.accessor.ChunkRendererRegionAccessor",
"client.accessor.WorldRendererAccessor",
"client.color.world.BiomeColorsMixin",
"client.render.WorldRendererMixin",
"client.world.ClientWorldMixin"
],
Expand Down

0 comments on commit 1f20d99

Please sign in to comment.