Skip to content

Commit

Permalink
fix: Fix cauldron precipitation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveplays28 committed Dec 1, 2023
1 parent fbc2ca8 commit 9e2fbd5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ dependencies {
exclude(group: "net.fabricmc.fabric-api")
}

// Server Translation API
modImplementation include("fr.catcore:server-translations-api:${project.server_translation_api_version}")

// Mixin Extras
include(implementation(annotationProcessor("io.github.llamalad7:mixinextras-fabric:0.2.1")))
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.github.steveplays28.realisticsleep.mixin;

import com.github.steveplays28.realisticsleep.api.RealisticSleepApi;
import com.github.steveplays28.realisticsleep.util.CauldronUtil;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.block.CauldronBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static com.github.steveplays28.realisticsleep.RealisticSleep.config;

Expand All @@ -23,16 +26,28 @@ public class CauldronBlockMixin {
@Final
private static float FILL_WITH_SNOW_CHANCE;

@Inject(method = "canFillWithPrecipitation", at = @At(value = "HEAD"), cancellable = true)
private static void canFillWithPrecipitationInject(World world, Biome.Precipitation precipitation, CallbackInfoReturnable<Boolean> cir) {
if (!RealisticSleepApi.isSleeping(world)) {
return;
@SuppressWarnings("unused")
@ModifyExpressionValue(method = "precipitationTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/CauldronBlock;canFillWithPrecipitation(Lnet/minecraft/world/World;Lnet/minecraft/world/biome/Biome$Precipitation;)Z"))
private boolean modifyFillWithPrecipitationRequirements(boolean original, @Local World world, @Local BlockPos pos, @Local Biome.Precipitation precipitation) {
return canBeFilledByPrecipitation(world, pos, precipitation);
}

@Unique
private boolean canBeFilledByPrecipitation(World world, BlockPos pos, Biome.Precipitation precipitation) {
if (CauldronUtil.canBeFilledByDripstone(world, pos)) {
return false;
}

final var isSleeping = RealisticSleepApi.isSleeping(world);
final var fillWithRainChance = isSleeping ? FILL_WITH_RAIN_CHANCE * config.precipitationTickSpeedMultiplier : FILL_WITH_RAIN_CHANCE;
final var fillWithSnowChance = isSleeping ? FILL_WITH_SNOW_CHANCE * config.precipitationTickSpeedMultiplier : FILL_WITH_SNOW_CHANCE;

if (precipitation == Biome.Precipitation.RAIN) {
cir.setReturnValue(world.getRandom().nextFloat() < FILL_WITH_RAIN_CHANCE * config.precipitationTickSpeedMultiplier);
return world.getRandom().nextFloat() < fillWithRainChance;
} else if (precipitation == Biome.Precipitation.SNOW) {
cir.setReturnValue(world.getRandom().nextFloat() < FILL_WITH_SNOW_CHANCE * config.precipitationTickSpeedMultiplier);
return world.getRandom().nextFloat() < fillWithSnowChance;
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,21 +368,21 @@ private void tickChunkInject(WorldChunk chunk, int randomTickSpeed, CallbackInfo
for (int i = 0; i < precipitationTickSpeedMultiplier; i++) {
int chunkSectionYOffset = chunkSection.getYOffset();
var randomPosInChunk = this.getRandomPosInChunk(chunkStartPosX, chunkSectionYOffset, chunkStartPosZ, 15);
var randomBlockStateInChunk = chunkSection.getBlockState(randomPosInChunk.getX() - chunkStartPosX,
randomPosInChunk.getY() - chunkSectionYOffset, randomPosInChunk.getZ() - chunkStartPosZ
);
var randomBlockInChunk = randomBlockStateInChunk.getBlock();
var biome = this.getBiome(randomPosInChunk).value();
var precipitation = biome.getPrecipitation();

if (precipitation == Biome.Precipitation.RAIN && biome.isCold(randomPosInChunk)) {
precipitation = Biome.Precipitation.SNOW;
if (precipitation == Biome.Precipitation.NONE) {
continue;
}

if (randomBlockInChunk instanceof CauldronBlock cauldronBlock) {
var randomBlockStateInChunk = chunkSection.getBlockState(randomPosInChunk.getX() - chunkStartPosX,
randomPosInChunk.getY() - chunkSectionYOffset, randomPosInChunk.getZ() - chunkStartPosZ
);
var randomBlockInChunk = randomBlockStateInChunk.getBlock();

if (randomBlockInChunk instanceof AbstractCauldronBlock cauldronBlock) {
cauldronBlock.precipitationTick(randomBlockStateInChunk, this, randomPosInChunk, precipitation);
} else if (randomBlockInChunk instanceof LeveledCauldronBlock leveledCauldronBlock) {
leveledCauldronBlock.precipitationTick(randomBlockStateInChunk, this, randomPosInChunk, precipitation);
cauldronBlock.scheduledTick(randomBlockStateInChunk, this.toServerWorld(), randomPosInChunk, random);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.steveplays28.realisticsleep.util;

import net.minecraft.block.PointedDripstoneBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class CauldronUtil {
public static boolean canBeFilledByDripstone(World world, BlockPos pos) {
BlockPos blockPos = PointedDripstoneBlock.getDripPos(world, pos);
return blockPos != null;
}
}

0 comments on commit 9e2fbd5

Please sign in to comment.