Skip to content

Commit

Permalink
refactor: Add mod ID prefix to all Mixin fields and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveplays28 committed Aug 23, 2024
1 parent b8ee9e6 commit 9bb7df6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -21,20 +22,19 @@ public class CauldronBlockMixin {
@Shadow
@Final
private static float FILL_WITH_RAIN_CHANCE;

@Shadow
@Final
private static float FILL_WITH_SNOW_CHANCE;

@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);
private boolean stevesrealisticsleep$modifyFillWithPrecipitationRequirements(boolean original, @Local(argsOnly = true) @NotNull World world, @Local(argsOnly = true) @NotNull BlockPos blockPosition, @Local(argsOnly = true) @NotNull Biome.Precipitation precipitation) {
return stevesrealisticsleep$canBeFilledByPrecipitation(world, blockPosition, precipitation);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ public ServerPlayerEntityMixin(EntityType<?> type, World world) {
public abstract void sendMessage(Text message, boolean overlay);

@Inject(method = "wakeUp(ZZ)V", at = @At(value = "HEAD"))
public void wakeUpInject(boolean skipSleepTimer, boolean updateSleepingPlayers, CallbackInfo ci) {
public void stevesrealisticsleep$sendWakeUpMessage(boolean skipSleepTimer, boolean updateSleepingPlayers, CallbackInfo ci) {
var timeOfDay = getWorld().getTimeOfDay() % DAY_LENGTH;
var ticksUntilDawn = Math.abs(timeOfDay - DAWN_WAKE_UP_TIME);
var ticksUntilDusk = Math.abs(timeOfDay - DUSK_WAKE_UP_TIME);

if (ticksUntilDawn > WAKE_UP_GRACE_PERIOD_TICKS && ticksUntilDusk > WAKE_UP_GRACE_PERIOD_TICKS) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.level.ServerWorldProperties;
import net.minecraft.world.tick.WorldTickScheduler;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -46,25 +47,24 @@

@Mixin(ServerWorld.class)
public abstract class ServerWorldMixin extends World implements ServerWorldExtension {
@Unique
public double timeStepPerTick = 2;
@Unique
public int timeStepPerTickRounded = 1;
@Unique
public long tickDelay;
@Unique
public MutableText sleepMessage;
@Unique
public Boolean shouldSkipWeather = false;
@Unique
public int consecutiveSleepTicks = 0;
protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey<World> registryRef, DynamicRegistryManager registryManager, RegistryEntry<DimensionType> dimensionEntry, Supplier<Profiler> profiler, boolean isClient, boolean debugWorld, long biomeAccess, int maxChainedNeighborUpdates) {
super(properties, registryRef, registryManager, dimensionEntry, profiler, isClient, debugWorld, biomeAccess,
maxChainedNeighborUpdates
);
}

@Shadow
@Final
protected RaidManager raidManager;

@Shadow
@Final
List<ServerPlayerEntity> players;

@Shadow
@Final
private static int MAX_TICKS;

@Shadow
@Final
private ServerWorldProperties worldProperties;
Expand All @@ -79,13 +79,13 @@ public abstract class ServerWorldMixin extends World implements ServerWorldExten
private ServerChunkManager chunkManager;
@Shadow
@Final
private WorldTickScheduler<Fluid> fluidTickScheduler;
@Shadow
@Final
private boolean shouldTickTime;

protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey<World> registryRef, DynamicRegistryManager registryManager, RegistryEntry<DimensionType> dimensionEntry, Supplier<Profiler> profiler, boolean isClient, boolean debugWorld, long biomeAccess, int maxChainedNeighborUpdates) {
super(properties, registryRef, registryManager, dimensionEntry, profiler, isClient, debugWorld, biomeAccess,
maxChainedNeighborUpdates
);
}
@Shadow
public abstract ServerWorld toServerWorld();

@Shadow
public abstract List<ServerPlayerEntity> getPlayers();
Expand All @@ -96,41 +96,43 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey<World>
@Shadow
protected abstract BlockPos getLightningPos(BlockPos pos);

@Shadow
public abstract ServerWorld toServerWorld();

@Shadow
protected abstract void tickFluid(BlockPos pos, Fluid fluid);

@Shadow
@Final
private WorldTickScheduler<Fluid> fluidTickScheduler;

@Shadow
@Final
private static int MAX_TICKS;
@Unique
private double stevesrealisticsleep$timeStepPerTick = 2;
@Unique
private long stevesrealisticsleep$tickDelay;
@Unique
private MutableText stevesrealisticsleep$sleepMessage;
@Unique
private boolean stevesrealisticsleep$shouldSkipWeather = false;
@Unique
private int stevesrealisticsleep$consecutiveSleepTicks = 0;

@Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameRules;getInt(Lnet/minecraft/world/GameRules$Key;)I"))
public void tickInject(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {
public void stevesrealisticsleep$tick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {
// Calculate seconds until awake
int sleepingPlayerCount = sleepManager.getSleeping();
int playerCount = getPlayers().size();
double sleepingRatio = (double) sleepingPlayerCount / playerCount;
timeStepPerTick = SleepMathUtil.calculateTimeStepPerTick(sleepingRatio, config.sleepSpeedMultiplier, timeStepPerTick);
stevesrealisticsleep$timeStepPerTick = SleepMathUtil.calculateTimeStepPerTick(sleepingRatio, config.sleepSpeedMultiplier,
stevesrealisticsleep$timeStepPerTick
);
int timeOfDay = StevesRealisticSleepApi.getTimeOfDay(this);
// TODO: Don't assume the TPS is 20
int secondsUntilAwake = Math.abs(SleepMathUtil.calculateSecondsUntilAwake(timeOfDay, timeStepPerTick, 20));
int secondsUntilAwake = Math.abs(SleepMathUtil.calculateSecondsUntilAwake(timeOfDay, stevesrealisticsleep$timeStepPerTick, 20));

// Check if the night has (almost) ended and the weather should be skipped
if (secondsUntilAwake <= 2 && shouldSkipWeather) {
clearWeather();
shouldSkipWeather = false;
if (secondsUntilAwake <= 2 && stevesrealisticsleep$shouldSkipWeather) {
stevesrealisticsleep$clearWeather();
stevesrealisticsleep$shouldSkipWeather = false;
}

// Check if anyone is sleeping
if (sleepingPlayerCount <= 0) {
// Reset consecutive sleep ticks
consecutiveSleepTicks = 0;
stevesrealisticsleep$consecutiveSleepTicks = 0;

return;
}
Expand Down Expand Up @@ -160,7 +162,7 @@ public void tickInject(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {
}

// Fetch config values and do calculations
timeStepPerTickRounded = (int) Math.round(timeStepPerTick);
int timeStepPerTickRounded = (int) Math.round(stevesrealisticsleep$timeStepPerTick);
int ticksUntilAwake = SleepMathUtil.calculateTicksUntilAwake(timeOfDay);
boolean doDayLightCycle = server.getGameRules().getBoolean(GameRules.DO_DAYLIGHT_CYCLE);

Expand Down Expand Up @@ -200,41 +202,44 @@ public void tickInject(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {

// Check if players are still supposed to be sleeping, and send a HUD message if so
if (ticksUntilAwake > WAKE_UP_GRACE_PERIOD_TICKS) {
if (consecutiveSleepTicks >= MINIMUM_SLEEP_TICKS_TO_CLEAR_WEATHER) {
shouldSkipWeather = true;
if (stevesrealisticsleep$consecutiveSleepTicks >= MINIMUM_SLEEP_TICKS_TO_CLEAR_WEATHER) {
stevesrealisticsleep$shouldSkipWeather = true;
}

if (config.sendSleepingMessage) {
sleepMessage = Text.translatable(String.format("%s.text.sleep_message", MOD_NAMESPACE), sleepingPlayerCount, playerCount).append(
stevesrealisticsleep$sleepMessage = Text.translatable(
String.format("%s.text.sleep_message", MOD_NAMESPACE), sleepingPlayerCount, playerCount).append(
nightDayOrThunderstormText);

if (isNight) {
if (config.showTimeUntilDawn) {
sleepMessage.append(Text.translatable(String.format("%s.text.time_until_dawn", MOD_NAMESPACE), secondsUntilAwake));
stevesrealisticsleep$sleepMessage.append(
Text.translatable(String.format("%s.text.time_until_dawn", MOD_NAMESPACE), secondsUntilAwake));
}
} else if (config.showTimeUntilDusk) {
sleepMessage.append(Text.translatable(String.format("%s.text.time_until_dusk", MOD_NAMESPACE), secondsUntilAwake));
stevesrealisticsleep$sleepMessage.append(
Text.translatable(String.format("%s.text.time_until_dusk", MOD_NAMESPACE), secondsUntilAwake));
}
}

for (ServerPlayerEntity player : players) {
player.sendMessage(sleepMessage, true);
player.sendMessage(stevesrealisticsleep$sleepMessage, true);
}

consecutiveSleepTicks += timeStepPerTickRounded;
stevesrealisticsleep$consecutiveSleepTicks += timeStepPerTickRounded;
}

if (ticksUntilAwake <= WAKE_UP_GRACE_PERIOD_TICKS) {
// Wake up sleeping players
this.wakeSleepingPlayers();

// Reset time step per tick, to reset the exponential sleep speed curve calculation
timeStepPerTick = 2;
stevesrealisticsleep$timeStepPerTick = 2;
}
}

@Inject(method = "tickTime", at = @At(value = "HEAD"), cancellable = true)
public void tickTimeInject(CallbackInfo ci) {
public void stevesrealisticsleep$tickTimeWithTimeTickSpeedMultiplier(@NotNull CallbackInfo ci) {
this.worldProperties.getScheduledEvents().processEvents(this.server, this.properties.getTime());

if (!this.shouldTickTime) {
Expand All @@ -247,8 +252,8 @@ public void tickTimeInject(CallbackInfo ci) {
this.worldProperties.setTime(l);
}

if (tickDelay > 0L) {
tickDelay -= 1L;
if (stevesrealisticsleep$tickDelay > 0L) {
stevesrealisticsleep$tickDelay -= 1L;
server.getPlayerManager().sendToDimension(
new WorldTimeUpdateS2CPacket(worldProperties.getTime(), worldProperties.getTimeOfDay(),
this.properties.getGameRules().getBoolean(GameRules.DO_DAYLIGHT_CYCLE)
Expand All @@ -266,7 +271,7 @@ public void tickTimeInject(CallbackInfo ci) {
this.worldProperties.setTimeOfDay(this.properties.getTimeOfDay() + 1L);
}

tickDelay = config.tickDelay;
stevesrealisticsleep$tickDelay = config.tickDelay;

ci.cancel();
}
Expand All @@ -279,12 +284,12 @@ public void tickTimeInject(CallbackInfo ci) {
*/
@SuppressWarnings("JavadocReference")
@Inject(method = "sendSleepingStatus", at = @At(value = "HEAD"), cancellable = true)
private void sendSleepingStatusInject(CallbackInfo ci) {
private void stevesrealisticsleep$preventSendingSleepingStatus(@NotNull CallbackInfo ci) {
ci.cancel();
}

@Inject(method = "tickChunk", at = @At(value = "HEAD"))
private void tickChunkInject(WorldChunk chunk, int randomTickSpeed, CallbackInfo ci) {
private void stevesrealisticsleep$tickChunksWithChunkTickSpeedMultiplier(@NotNull WorldChunk chunk, int randomTickSpeed, CallbackInfo ci) {
if (!StevesRealisticSleepApi.isSleeping(this)) {
return;
}
Expand Down Expand Up @@ -419,7 +424,7 @@ private void tickChunkInject(WorldChunk chunk, int randomTickSpeed, CallbackInfo
}

@Unique
private void clearWeather() {
private void stevesrealisticsleep$clearWeather() {
boolean doWeatherCycle = server.getGameRules().getBoolean(GameRules.DO_WEATHER_CYCLE);

if (doWeatherCycle && (worldProperties.isRaining() || worldProperties.isThundering())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.steveplays28.stevesrealisticsleep.mixin;

import net.minecraft.server.world.SleepManager;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -9,13 +10,13 @@
@Mixin(SleepManager.class)
public class SleepManagerMixin {
/**
* Cancels {@link SleepManager#canSkipNight canSkipNight} and sets the return value to <code>false</code>.
* Cancels {@link SleepManager#canSkipNight canSkipNight} and sets the return value to {@code false}.
*
* @author Steveplays28
* @reason Method conflicts with Realistic Sleep's functionality.
*/
@Inject(method = "canSkipNight", at = @At(value = "HEAD"), cancellable = true)
public void canSkipNightInject(int percentage, CallbackInfoReturnable<Boolean> cir) {
public void stevesrealisticsleep$preventNightSkip(int percentage, @NotNull CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import net.minecraft.block.PointedDripstoneBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;

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

0 comments on commit 9bb7df6

Please sign in to comment.