diff --git a/src/main/java/ru/aiefu/timeandwindct/ConfigurationManager.java b/src/main/java/ru/aiefu/timeandwindct/ConfigurationManager.java index d077e68..58d2424 100644 --- a/src/main/java/ru/aiefu/timeandwindct/ConfigurationManager.java +++ b/src/main/java/ru/aiefu/timeandwindct/ConfigurationManager.java @@ -91,7 +91,7 @@ public static HashMap readSysTimeCfg(){ public static void generateModConfig(){ try(FileWriter writer = getFileWriter("./config/time-and-wind/config.json")){ gson_pretty.toJson(new ModConfig(false, false, false, - false, 30, true, 50), writer); + false, 30), writer); } catch (IOException e){ e.printStackTrace(); } @@ -112,13 +112,13 @@ public static ModConfig readModConfig(){ } catch (IOException e){ e.printStackTrace(); config = new ModConfig(true, false, false, - true, 30, true, 50); + true, 30); } return config; } public static ModConfig patchModConfigV1(ModConfig config){ - return new ModConfig(config.patchSkyAngle, config.syncWithSystemTime,false, true, 30, true, 50); + return new ModConfig(config.patchSkyAngle, config.syncWithSystemTime,false, true, 30); } public static void updateTimeData(String id, int dayD, int nightD){ diff --git a/src/main/java/ru/aiefu/timeandwindct/config/ModConfig.java b/src/main/java/ru/aiefu/timeandwindct/config/ModConfig.java index 9caf375..5f813fa 100644 --- a/src/main/java/ru/aiefu/timeandwindct/config/ModConfig.java +++ b/src/main/java/ru/aiefu/timeandwindct/config/ModConfig.java @@ -6,23 +6,19 @@ public class ModConfig { public boolean systemTimePerDimensions; public boolean enableNightSkipAcceleration; public int accelerationSpeed; - public boolean enableThreshold; - public int thresholdPercentage; public int config_ver = 3; public ModConfig(boolean patchSkyAngle, boolean syncWithSystemTime, boolean systemTimePerDimensions, boolean enableNightSkipAcceleration, - int accelerationSpeed, boolean enableThreshold, int thresholdPercentage){ + int accelerationSpeed){ this.patchSkyAngle = patchSkyAngle; this.syncWithSystemTime = syncWithSystemTime; this.systemTimePerDimensions = systemTimePerDimensions; this.enableNightSkipAcceleration = enableNightSkipAcceleration; this.accelerationSpeed = accelerationSpeed; - this.enableThreshold = enableThreshold; - this.thresholdPercentage = thresholdPercentage; } public ModConfig copy(){ - return new ModConfig(patchSkyAngle, syncWithSystemTime, systemTimePerDimensions, enableNightSkipAcceleration, accelerationSpeed, enableThreshold, thresholdPercentage); + return new ModConfig(patchSkyAngle, syncWithSystemTime, systemTimePerDimensions, enableNightSkipAcceleration, accelerationSpeed); } } diff --git a/src/main/java/ru/aiefu/timeandwindct/mixin/ClientWorldMixins.java b/src/main/java/ru/aiefu/timeandwindct/mixin/ClientWorldMixins.java index 73711ce..f39f601 100644 --- a/src/main/java/ru/aiefu/timeandwindct/mixin/ClientWorldMixins.java +++ b/src/main/java/ru/aiefu/timeandwindct/mixin/ClientWorldMixins.java @@ -62,7 +62,10 @@ else if (TimeAndWindCT.timeDataMap != null && TimeAndWindCT.timeDataMap.contains @Redirect(method = "tickTime", at = @At(value = "INVOKE", target = "net/minecraft/client/multiplayer/ClientLevel.setDayTime(J)V")) private void customTickerTAW(ClientLevel clientWorld, long timeOfDay) { - this.timeTicker.tick(this, skipState, speed); + this.timeTicker.tick(this); + if(skipState){ + this.timeTicker.accelerate((ClientLevel)(Object) this, speed); + } } @Override diff --git a/src/main/java/ru/aiefu/timeandwindct/mixin/ServerWorldMixins.java b/src/main/java/ru/aiefu/timeandwindct/mixin/ServerWorldMixins.java index 9913dab..e0697d6 100644 --- a/src/main/java/ru/aiefu/timeandwindct/mixin/ServerWorldMixins.java +++ b/src/main/java/ru/aiefu/timeandwindct/mixin/ServerWorldMixins.java @@ -10,6 +10,7 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.progress.ChunkProgressListener; +import net.minecraft.server.players.SleepStatus; import net.minecraft.util.profiling.ProfilerFiller; import net.minecraft.world.RandomSequences; import net.minecraft.world.level.CustomSpawner; @@ -27,6 +28,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.Slice; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import ru.aiefu.timeandwindct.ITimeOperations; import ru.aiefu.timeandwindct.NetworkPacketsID; @@ -44,31 +46,28 @@ @Mixin(ServerLevel.class) public abstract class ServerWorldMixins extends Level implements ITimeOperations { - @Shadow @Final - List players; - protected ServerWorldMixins(WritableLevelData writableLevelData, ResourceKey resourceKey, RegistryAccess registryAccess, Holder holder, Supplier supplier, boolean bl, boolean bl2, long l, int i) { super(writableLevelData, resourceKey, registryAccess, holder, supplier, bl, bl2, l, i); } - @Shadow public abstract void setDayTime(long l); - @Shadow protected abstract void resetWeatherCycle(); - @Shadow protected abstract void wakeUpAllPlayers(); - @Shadow @Final private ServerLevelData serverLevelData; + @Shadow @Final private SleepStatus sleepStatus; + @Shadow @Final + List players; + + @Shadow protected abstract void resetWeatherCycle(); + @Unique protected Ticker timeTicker; @Unique - protected boolean enableNightSkipAcceleration = false; - @Unique - protected int accelerationSpeed = 0; + private boolean shouldUpdate = true; @Unique - private boolean shouldUpdateNSkip = true; + private boolean skipState = false; @Inject(method = "", at = @At("TAIL")) private void attachTimeDataTAW(MinecraftServer minecraftServer, Executor executor, LevelStorageSource.LevelStorageAccess levelStorageAccess, ServerLevelData serverLevelData, ResourceKey resourceKey, LevelStem levelStem, ChunkProgressListener chunkProgressListener, boolean bl, long l, List list, boolean bl2, RandomSequences randomSequences, CallbackInfo ci){ @@ -86,77 +85,75 @@ else if (TimeAndWindCT.timeDataMap != null && TimeAndWindCT.timeDataMap.contains } else this.timeTicker = new DefaultTicker(); } - @Inject(method = "updateSleepingPlayerList", at =@At("HEAD"), cancellable = true) - private void patchNightSkip(CallbackInfo ci){ - if(TimeAndWindCT.CONFIG.syncWithSystemTime){ - ci.cancel(); - } else if (TimeAndWindCT.CONFIG.enableNightSkipAcceleration){ - List totalPlayers = this.players.stream().filter(player -> !player.isSpectator() || !player.isCreative()).toList(); - if(totalPlayers.size() > 0) { - int sleepingPlayers = (int) totalPlayers.stream().filter(ServerPlayer::isSleeping).count(); - int threshold = TimeAndWindCT.CONFIG.enableThreshold ? totalPlayers.size() / 100 * TimeAndWindCT.CONFIG.thresholdPercentage : 0; - if (sleepingPlayers > threshold) { - enableNightSkipAcceleration = true; - this.accelerationSpeed = TimeAndWindCT.CONFIG.accelerationSpeed; - } else enableNightSkipAcceleration = false; - } else enableNightSkipAcceleration = false; - if(this.shouldUpdateNSkip) { - FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); - buf.writeBoolean(enableNightSkipAcceleration); - buf.writeInt(accelerationSpeed); - this.players.forEach(player -> ServerPlayNetworking.send(player, NetworkPacketsID.NIGHT_SKIP_INFO, buf)); + @Redirect(method = "tick", at =@At(value = "INVOKE", target = "net/minecraft/server/players/SleepStatus.areEnoughDeepSleeping (ILjava/util/List;)Z", ordinal = 0), + slice = @Slice(from = @At(value = "INVOKE", target = "net/minecraft/server/level/ServerLevel.advanceWeatherCycle ()V", ordinal = 0), + to = @At(value = "INVOKE", target = "net/minecraft/server/level/ServerLevel.wakeUpAllPlayers ()V", ordinal = 0))) + private boolean blockSleepCheckTAW(SleepStatus instance, int i, List list){ + return !(TimeAndWindCT.CONFIG.syncWithSystemTime || TimeAndWindCT.CONFIG.enableNightSkipAcceleration); + } + + @Inject(method = "updateSleepingPlayerList", at = @At("TAIL")) + private void syncSkipStateTAW(CallbackInfo ci){ + int i = this.getGameRules().getInt(GameRules.RULE_PLAYERS_SLEEPING_PERCENTAGE); + if(this.canAccelerate()){ + if(this.sleepStatus.areEnoughSleeping(i)){ + if(!skipState){ + skipState = true; + if(shouldUpdate) this.broadcastSkipState(true); + } + } else{ + if(skipState){ + skipState = false; + if(shouldUpdate) this.broadcastSkipState(false); + } } - ci.cancel(); } } @Inject(method = "addPlayer", at =@At("HEAD")) private void onPlayerJoin(ServerPlayer player, CallbackInfo ci){ - FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); - buf.writeBoolean(enableNightSkipAcceleration); - buf.writeInt(accelerationSpeed); - ServerPlayNetworking.send(player, NetworkPacketsID.NIGHT_SKIP_INFO, buf); - } - - @Inject(method = "wakeUpAllPlayers", at =@At("HEAD")) - private void preventPacketsSpam(CallbackInfo ci){ - this.enableNightSkipAcceleration = false; - this.shouldUpdateNSkip = false; - } - - @Inject(method = "wakeUpAllPlayers", at =@At("TAIL")) - private void preventPacketsSpamEnd(CallbackInfo ci){ - FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); - buf.writeBoolean(enableNightSkipAcceleration); - buf.writeInt(accelerationSpeed); - this.players.forEach(player -> ServerPlayNetworking.send(player, NetworkPacketsID.NIGHT_SKIP_INFO, buf)); - this.shouldUpdateNSkip = true; - - if (this.getGameRules().getBoolean(GameRules.RULE_WEATHER_CYCLE) && this.isRaining()) { - this.resetWeatherCycle(); + if(this.canAccelerate()){ + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeBoolean(skipState); + buf.writeInt(TimeAndWindCT.CONFIG.accelerationSpeed); + ServerPlayNetworking.send(player, NetworkPacketsID.NIGHT_SKIP_INFO, buf); } } @Redirect(method = "tickTime", at = @At(value = "INVOKE", target = "net/minecraft/server/level/ServerLevel.setDayTime(J)V")) private void customTickerTAW(ServerLevel world, long timeOfDay) { - this.timeTicker.tick(this, enableNightSkipAcceleration, accelerationSpeed); - if(enableNightSkipAcceleration && isThundering()){ - int thunderTime = this.serverLevelData.getThunderTime(); - if(thunderTime > 6000){ - this.serverLevelData.setThunderTime(6000); - thunderTime = 6000; - } - thunderTime -= this.accelerationSpeed; - this.serverLevelData.setThunderTime(thunderTime); - if(thunderTime < 1){ + this.timeTicker.tick(this); + if(this.skipState){ + this.timeTicker.accelerate((ServerLevel) (Object) this, TimeAndWindCT.CONFIG.accelerationSpeed); + if(this.isDay()){ + this.skipState = false; + this.shouldUpdate = false; + this.broadcastSkipState(false); + this.wakeUpAllPlayers(); this.resetWeatherCycle(); + this.shouldUpdate = true; } } } + @Unique + private void broadcastSkipState(boolean bl){ + this.players.forEach(p -> { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeBoolean(bl); + buf.writeInt(TimeAndWindCT.CONFIG.accelerationSpeed); + ServerPlayNetworking.send(p, NetworkPacketsID.NIGHT_SKIP_INFO, buf); + }); + } + + @Unique + private boolean canAccelerate(){ + return TimeAndWindCT.CONFIG.enableNightSkipAcceleration && !TimeAndWindCT.CONFIG.syncWithSystemTime; + } + @Override public void time_and_wind_custom_ticker$wakeUpAllPlayersTAW() { - wakeUpAllPlayers(); + this.wakeUpAllPlayers(); } @Override @@ -191,11 +188,11 @@ private void customTickerTAW(ServerLevel world, long timeOfDay) { @Override public void time_and_wind_custom_ticker$setSkipState(boolean bl) { - this.enableNightSkipAcceleration = bl; + } @Override public void time_and_wind_custom_ticker$setSpeed(int speed) { - this.accelerationSpeed = speed; + } } diff --git a/src/main/java/ru/aiefu/timeandwindct/mixin/SleepManagerMixins.java b/src/main/java/ru/aiefu/timeandwindct/mixin/SleepManagerMixins.java deleted file mode 100644 index 9b4a04f..0000000 --- a/src/main/java/ru/aiefu/timeandwindct/mixin/SleepManagerMixins.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.aiefu.timeandwindct.mixin; - -import net.minecraft.server.players.SleepStatus; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import ru.aiefu.timeandwindct.TimeAndWindCT; - -@Mixin(SleepStatus.class) -public class SleepManagerMixins { - - @Inject(method = "areEnoughSleeping", at =@At("HEAD"), cancellable = true) - private void disableNightSkip(int percentage, CallbackInfoReturnable cir){ - if(TimeAndWindCT.CONFIG.syncWithSystemTime || TimeAndWindCT.CONFIG.enableNightSkipAcceleration){ - cir.setReturnValue(false); - } - } -} diff --git a/src/main/java/ru/aiefu/timeandwindct/tickers/DefaultTicker.java b/src/main/java/ru/aiefu/timeandwindct/tickers/DefaultTicker.java index 502021f..793363a 100644 --- a/src/main/java/ru/aiefu/timeandwindct/tickers/DefaultTicker.java +++ b/src/main/java/ru/aiefu/timeandwindct/tickers/DefaultTicker.java @@ -1,12 +1,17 @@ package ru.aiefu.timeandwindct.tickers; +import net.minecraft.world.level.Level; import ru.aiefu.timeandwindct.ITimeOperations; public class DefaultTicker implements Ticker{ @Override - public void tick(ITimeOperations world, boolean nskip, int acceleration) { - if(nskip) world.time_and_wind_custom_ticker$setTimeOfDayTAW(world.time_and_wind_custom_ticker$getTimeOfDayTAW() + acceleration); - else world.time_and_wind_custom_ticker$setTimeOfDayTAW(world.time_and_wind_custom_ticker$getTimeOfDayTAW() + 1L); + public void tick(ITimeOperations world) { + world.time_and_wind_custom_ticker$setTimeOfDayTAW(world.time_and_wind_custom_ticker$getTimeOfDayTAW() + 1L); + } + + @Override + public void accelerate(Level level, int speed) { + ((ITimeOperations)level).time_and_wind_custom_ticker$setTimeOfDayTAW(((ITimeOperations) level).time_and_wind_custom_ticker$getTimeOfDayTAW() + speed); } } diff --git a/src/main/java/ru/aiefu/timeandwindct/tickers/SystemTimeTicker.java b/src/main/java/ru/aiefu/timeandwindct/tickers/SystemTimeTicker.java index 11125be..8b7ee16 100644 --- a/src/main/java/ru/aiefu/timeandwindct/tickers/SystemTimeTicker.java +++ b/src/main/java/ru/aiefu/timeandwindct/tickers/SystemTimeTicker.java @@ -1,5 +1,6 @@ package ru.aiefu.timeandwindct.tickers; +import net.minecraft.world.level.Level; import ru.aiefu.timeandwindct.ITimeOperations; import ru.aiefu.timeandwindct.TimeAndWindCT; import ru.aiefu.timeandwindct.config.SystemTimeConfig; @@ -44,7 +45,7 @@ public SystemTimeTicker(ITimeOperations world, SystemTimeConfig config){ world.time_and_wind_custom_ticker$setTimeOfDayTAW(unwrapTime(startingTick, world.time_and_wind_custom_ticker$getTimeOfDayTAW())); } - public void tick(ITimeOperations world, boolean nskip, int acceleration){ + public void tick(ITimeOperations world){ ++ticks; long time = world.time_and_wind_custom_ticker$getTimeOfDayTAW(); tickMod = time % 24000L < 12000 ? dayMod : nightMod; @@ -62,6 +63,11 @@ public void tick(ITimeOperations world, boolean nskip, int acceleration){ } } + @Override + public void accelerate(Level level, int speed) { + + } + public void updateTime(ITimeOperations world){ world.time_and_wind_custom_ticker$setTimeOfDayTAW(unwrapTime(calculateCurrentTick(), world.time_and_wind_custom_ticker$getTimeOfDayTAW())); } diff --git a/src/main/java/ru/aiefu/timeandwindct/tickers/Ticker.java b/src/main/java/ru/aiefu/timeandwindct/tickers/Ticker.java index 0ff2ce3..895adee 100644 --- a/src/main/java/ru/aiefu/timeandwindct/tickers/Ticker.java +++ b/src/main/java/ru/aiefu/timeandwindct/tickers/Ticker.java @@ -1,7 +1,9 @@ package ru.aiefu.timeandwindct.tickers; +import net.minecraft.world.level.Level; import ru.aiefu.timeandwindct.ITimeOperations; public interface Ticker { - void tick(ITimeOperations world, boolean nskip, int acceleration); + void tick(ITimeOperations world); + void accelerate(Level level, int speed); } diff --git a/src/main/java/ru/aiefu/timeandwindct/tickers/TimeTicker.java b/src/main/java/ru/aiefu/timeandwindct/tickers/TimeTicker.java index 9fca71d..04701ad 100644 --- a/src/main/java/ru/aiefu/timeandwindct/tickers/TimeTicker.java +++ b/src/main/java/ru/aiefu/timeandwindct/tickers/TimeTicker.java @@ -17,13 +17,10 @@ public class TimeTicker implements Ticker { private int nightTicksToAdd; private boolean nightAction; - private final Level level; private final ITimeOperations timeLevel; private float fraction; public TimeTicker(int dayD, int nightD, Level level){ - - this.level = level; this.timeLevel = (ITimeOperations) level; this.dayD = dayD; @@ -45,14 +42,8 @@ public TimeTicker(int dayD, int nightD, Level level){ } - public void tick(ITimeOperations world, boolean nskip, int acceleration) { - if(nskip){ - this.accelerate(acceleration); - if(this.level.isDay()){ - this.timeLevel.time_and_wind_custom_ticker$wakeUpAllPlayersTAW(); - } - } - else if(this.isDay()){ + public void tick(ITimeOperations world) { + if(this.isDay()){ if(this.timeLevel.time_and_wind_custom_ticker$getTimeTAW() % this.dayMod == 0){ this.update(this.dayTicksToAdd, this.dayFraction, this.dayAction); } @@ -61,6 +52,11 @@ else if(this.isDay()){ } } + @Override + public void accelerate(Level level, int speed) { + this.timeLevel.time_and_wind_custom_ticker$setTimeOfDayTAW(this.timeLevel.time_and_wind_custom_ticker$getTimeOfDayTAW() + speed); + } + public boolean isDay(){ return this.timeLevel.time_and_wind_custom_ticker$getTimeOfDayTAW() % 24000 < 12001; } @@ -75,10 +71,6 @@ public void update(int ticksToAdd, float fraction, boolean skip){ this.timeLevel.time_and_wind_custom_ticker$setTimeOfDayTAW(this.timeLevel.time_and_wind_custom_ticker$getTimeOfDayTAW() + ticksToAdd); } - public void accelerate(int val){ - this.timeLevel.time_and_wind_custom_ticker$setTimeOfDayTAW(this.timeLevel.time_and_wind_custom_ticker$getTimeOfDayTAW() + val); - } - private float getFactor(int duration, boolean bl){ return bl ? 12000.0F / duration : duration / 12000.0F; } diff --git a/src/main/resources/tawct.mixins.json b/src/main/resources/tawct.mixins.json index 1a590e7..4dd0e93 100644 --- a/src/main/resources/tawct.mixins.json +++ b/src/main/resources/tawct.mixins.json @@ -9,7 +9,6 @@ "PlayerMixins", "ServerPlayerMixins", "ServerWorldMixins", - "SleepManagerMixins", "TimeCommandMixins" ], "client": [