Skip to content

Commit

Permalink
night skip acceleration changes/more code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CmdrJane committed Dec 18, 2023
1 parent bb88b79 commit 1c44e87
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static HashMap<String, SystemTimeConfig> 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();
}
Expand All @@ -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){
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/ru/aiefu/timeandwindct/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
129 changes: 63 additions & 66 deletions src/main/java/ru/aiefu/timeandwindct/mixin/ServerWorldMixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,31 +46,28 @@
@Mixin(ServerLevel.class)
public abstract class ServerWorldMixins extends Level implements ITimeOperations {

@Shadow @Final
List<ServerPlayer> players;

protected ServerWorldMixins(WritableLevelData writableLevelData, ResourceKey<Level> resourceKey, RegistryAccess registryAccess, Holder<DimensionType> holder, Supplier<ProfilerFiller> 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<ServerPlayer> 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 = "<init>", at = @At("TAIL"))
private void attachTimeDataTAW(MinecraftServer minecraftServer, Executor executor, LevelStorageSource.LevelStorageAccess levelStorageAccess, ServerLevelData serverLevelData, ResourceKey<Level> resourceKey, LevelStem levelStem, ChunkProgressListener chunkProgressListener, boolean bl, long l, List<CustomSpawner> list, boolean bl2, RandomSequences randomSequences, CallbackInfo ci){
Expand All @@ -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<ServerPlayer> 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<ServerPlayer> 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
Expand Down Expand Up @@ -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;

}
}
19 changes: 0 additions & 19 deletions src/main/java/ru/aiefu/timeandwindct/mixin/SleepManagerMixins.java

This file was deleted.

11 changes: 8 additions & 3 deletions src/main/java/ru/aiefu/timeandwindct/tickers/DefaultTicker.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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()));
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ru/aiefu/timeandwindct/tickers/Ticker.java
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 7 additions & 15 deletions src/main/java/ru/aiefu/timeandwindct/tickers/TimeTicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit 1c44e87

Please sign in to comment.