Skip to content

Commit

Permalink
Sododium compattata workerini!
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Jul 4, 2024
1 parent 1593326 commit 75f145d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 80 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ dependencies {
include modImplementation("org.ladysnake.cardinal-components-api:cardinal-components-entity:${property('deps.cardinal-components-api')}")

modImplementation "com.terraformersmc:modmenu:${property('deps.modmenu')}"
modClientCompileOnly "maven.modrinth:sodium:${property('deps.sodium')}"
modClientImplementation "maven.modrinth:sodium:${property('deps.sodium')}"
}

processResources {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.enjarai.trickster.mixin.client.sodium;

import dev.enjarai.trickster.cca.ModChunkCumponents;
import dev.enjarai.trickster.cca.ShadowDisguiseMapComponent;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import me.jellysquid.mods.sodium.client.world.WorldSlice;
import net.minecraft.block.BlockState;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.world.chunk.EmptyChunk;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(WorldSlice.class)
public abstract class WorldSliceMixin {
@Shadow
@Final
private ClientWorld world;

@Unique
private final Long2ObjectOpenHashMap<ShadowDisguiseMapComponent> componentCache = new Long2ObjectOpenHashMap<>();

@Inject(
method = "getBlockState(III)Lnet/minecraft/block/BlockState;",
at = @At(
value = "FIELD",
target = "Lme/jellysquid/mods/sodium/client/world/WorldSlice;originX:I"
),
cancellable = true
)
private void disguiseBlockState(int x, int y, int z, CallbackInfoReturnable<BlockState> cir) {
var chunkX = x >> 4;
var chunkZ = z >> 4;
var chunkId = ((long) chunkX) << 32 | ((long) chunkZ);

var disguises = componentCache.get(chunkId);
if (disguises == null) {
var chunk = world.getChunk(chunkX, chunkZ);

if (chunk != null && !(chunk instanceof EmptyChunk)) {
disguises = ModChunkCumponents.SHADOW_DISGUISE_MAP.get(chunk);
}

if (disguises != null) {
componentCache.put(chunkId, disguises);
}
}

if (disguises != null) {
var funnyState = disguises.getFunnyState(x, y, z);

if (funnyState != null) {
cir.setReturnValue(funnyState);
}
}
}
}
2 changes: 1 addition & 1 deletion src/client/resources/trickster.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"HeldItemRendererMixin",
"InGameHudMixin",
"MouseMixin",
"SodiumLevelSliceMixin",
"sodium.WorldSliceMixin",
"PlayerRendererMixin",
"RenderedChunkMixin",
"SillyHairsFeatureRendererMixin",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/enjarai/trickster/ConditionalMixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ConditionalMixins implements IMixinConfigPlugin {
private static final Supplier<Boolean> TRUE = () -> true;

private static final Map<String, Supplier<Boolean>> CONDITIONS = ImmutableMap.of(
"dev.enjarai.trickster.mixin.client.SodiumLevelSliceMixin", () -> FabricLoader.getInstance().isModLoaded("sodium")
"dev.enjarai.trickster.mixin.client.sodium.WorldSliceMixin", () -> FabricLoader.getInstance().isModLoaded("sodium")
);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public BlockState getFunnyState(BlockPos pos) {
return null;
}

@Nullable
public BlockState getFunnyState(int x, int y, int z) {
var block = disguises.get(encodePos(x, y, z));
if (block != null) {
return block.getDefaultState();
}
return null;
}

public boolean setFunnyState(BlockPos pos, Block block) {
if (disguises.put(encodePos(pos), block) != block) {
chunk.setNeedsSaving(true);
Expand All @@ -83,9 +92,13 @@ public boolean clearFunnyState(BlockPos pos) {
}

public int encodePos(BlockPos pos) {
var x = pos.getX() & 15;
var z = (pos.getZ() & 15) << 4;
var y = pos.getY() << 8;
return y | z | x;
return encodePos(pos.getX(), pos.getY(), pos.getZ());
}

public int encodePos(int x, int y, int z) {
var xe = x & 15;
var ze = (z & 15) << 4;
var ye = y << 8;
return ye | ze | xe;
}
}

0 comments on commit 75f145d

Please sign in to comment.