TheMisterFish / Fabric_OfflinePlayersReworked

Allows a user to logout, leaving a fake player that loads the chunks
Creative Commons Zero v1.0 Universal
1 stars 0 forks source link

[Bug] sun gets stuck at sunrise when a player is in offline mode #44

Open podium868909 opened 17 hours ago

podium868909 commented 17 hours ago

Describe the bug the sun will get stuck at sunrise

conditions of bug to occur: a player is in offline mode gamerule sleepingpercentage is set so that only 1 player needs to sleep for the players online on the server currently ignoreSleepingPercentage=true in the config

the modifySleepersNeeded in SleepStatus_sleepersNeededMixin.java line 24: result -= to_ignore; result is set to 1 and to_ignore is also 1 so result will become 0, causing there to be 0 players required to sleep and set it to constantly be sunrise

To Reproduce Steps to reproduce the behavior:

  1. launch server
  2. let 2 players join
  3. set gamerule playersSleepingPercentage to 50
  4. 1 player to go offline
  5. the player still on the server will see the sun stuck at sunrise

Expected behavior the sun to not be stuck at sunrise

Screenshots If applicable, add screenshots to help explain your problem.

Versions (please complete the following information):

Additional context i did a bit of coding and i think i managed to fix it and tested it a bit but not fully


@Mixin(SleepStatus.class)
public class SleepStatus_sleepersNeededMixin {
    @Shadow private int activePlayers;

    @Inject(method = "update", at = @At("RETURN"), cancellable = true)
    private void modifySleepersNeeded(List<ServerPlayer> list, CallbackInfoReturnable<Boolean> cir) {
        if (ModConfigs.IGNORE_SLEEPING_PERCENTAGE) {
            var storage = OfflinePlayersReworked.getStorage();
            int to_ignore = storage.findAll().stream()
                    .filter(offlinePlayerModel -> !offlinePlayerModel.isDied() && !offlinePlayerModel.isKicked())
                    .toList()
                    .size();
            this.activePlayers -= to_ignore;
        }
    }
}```
TheMisterFish commented 17 hours ago

I have to take a look at it, good find though. Wouldn't it be easier to check if the value is 0 or lower and if it is set it to 1 as a failsafe?

podium868909 commented 16 hours ago

removing the players in sleepersneeded i think removes the player count after the gamerule playerssleepingpercentage division so like if there were 2 players and 1 was offline, it would be ceil[ 2/2 - 1 ] = 0 (error!!) which is wrong because it should be ceil[ (2-1)/2 ] = 1 (OK)

sleepersneeded is also i think called many times a second so using update i think which is called less would be more performant i didn't manage to test it much though sorry

TheMisterFish commented 16 hours ago

Clear. Thanks for the explenation :)

TheMisterFish commented 6 hours ago

@podium868909 I've created a PR, it adds a check to ssee if the serverPlayer is a instance of OfflinePlayer. If it is, it won't get counted. My main reason for doing it like this is because I don't know what would happen if sleepingPlayers would get upped (++) while the activePlayers would get downed (--). This change would also make sure the number can never be lower than 0 as it doesn't -- at all. What do you think?