-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backport playersSleepingPercentage gamerule and implement #162
Open
Cleptomania
wants to merge
13
commits into
master
Choose a base branch
from
player-sleep-percent
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9c75e08
Backport playersSleepingPercentage gamerule and implement
Cleptomania 21e6ff1
Disable by default, ignore AFK players
Cleptomania 6d1c740
cache notification timer
Cleptomania 9836492
Less funny mixin
Cleptomania 07a65ad
the greater was supposed to be lesser
Cleptomania 819b4b1
Convert config to int, and use mod detected default for EFR
Cleptomania 54e87b3
Use correct EFR coremod
Cleptomania 741731e
Handle sleeper name correctly
Cleptomania 9f18b94
Merge branch 'master' into player-sleep-percent
Dream-Master 670148e
Merge branch 'master' into player-sleep-percent
Dream-Master 1836f07
Merge branch 'master' into player-sleep-percent
Dream-Master c713d95
Merge branch 'master' into player-sleep-percent
Dream-Master 9a43217
Merge branch 'master' into player-sleep-percent
serenibyss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/mixins/java/serverutils/mixins/early/minecraft/MixinWorldServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package serverutils.mixins.early.minecraft; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.entity.player.EntityPlayerMP; | ||
import net.minecraft.profiler.Profiler; | ||
import net.minecraft.util.ChatComponentTranslation; | ||
import net.minecraft.world.World; | ||
import net.minecraft.world.WorldProvider; | ||
import net.minecraft.world.WorldServer; | ||
import net.minecraft.world.WorldSettings; | ||
import net.minecraft.world.storage.ISaveHandler; | ||
|
||
import org.objectweb.asm.Opcodes; | ||
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.Redirect; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import serverutils.ServerUtilitiesConfig; | ||
import serverutils.data.ServerUtilitiesPlayerData; | ||
import serverutils.lib.data.Universe; | ||
|
||
@Mixin(WorldServer.class) | ||
public abstract class MixinWorldServer extends World { | ||
|
||
@Shadow | ||
private boolean allPlayersSleeping; | ||
|
||
@Unique | ||
private int percent; | ||
|
||
@Unique | ||
private List<EntityPlayer> sleepingPlayers; | ||
|
||
/** | ||
* We need to access this.playerEntities from the superclass, so we're extending World, and need this fake | ||
* constructor to make Java happy | ||
**/ | ||
public MixinWorldServer(ISaveHandler p_i45368_1_, String p_i45368_2_, WorldProvider p_i45368_3_, | ||
WorldSettings p_i45368_4_, Profiler p_i45368_5_) { | ||
super(p_i45368_1_, p_i45368_2_, p_i45368_3_, p_i45368_4_, p_i45368_5_); | ||
throw new RuntimeException( | ||
"Server Utilities player sleeping percentage broke in a huge way. This error should never happen"); | ||
} | ||
|
||
@Inject(method = "<init>", at = @At("RETURN")) | ||
public void serverutilities$playersSleepingConstructor(CallbackInfo ci) { | ||
sleepingPlayers = new ArrayList<>(); | ||
} | ||
|
||
@Inject(method = "updateAllPlayersSleepingFlag", at = @At("HEAD"), cancellable = true) | ||
public void serverutilities$handlePlayersSleepingPercentage(CallbackInfo ci) { | ||
percent = Integer.parseInt(this.getGameRules().getGameRuleStringValue("playersSleepingPercentage")); | ||
if (percent > 100) { | ||
this.allPlayersSleeping = false; | ||
ci.cancel(/* /r/nosleep, vanilla behaviour */); | ||
} else { | ||
EntityPlayer theSleeper = null; | ||
sleepingPlayers.clear(); | ||
int cap = (int) Math.ceil(getListWithoutAFK(this.playerEntities).size() * percent * 0.01f); | ||
for (EntityPlayer player : this.playerEntities) { | ||
if (player.isPlayerSleeping()) { | ||
sleepingPlayers.add(player); | ||
theSleeper = player; | ||
if (sleepingPlayers.size() >= cap) { | ||
this.allPlayersSleeping = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!sleepingPlayers.isEmpty() && cap > 0 && theSleeper != null) { | ||
for (EntityPlayer player : this.playerEntities) { | ||
String percentString = String.format("%d", (sleepingPlayers.size() * 100) / cap); | ||
player.addChatMessage( | ||
new ChatComponentTranslation( | ||
"serverutilities.world.players_sleeping", | ||
theSleeper.getDisplayName(), | ||
sleepingPlayers.size(), | ||
cap, | ||
percentString)); | ||
} | ||
} | ||
ci.cancel(); | ||
} | ||
} | ||
|
||
@Redirect( | ||
method = "areAllPlayersAsleep", | ||
at = @At( | ||
value = "FIELD", | ||
target = "Lnet/minecraft/world/WorldServer;playerEntities:Ljava/util/List;", | ||
opcode = Opcodes.GETFIELD)) | ||
public List<EntityPlayer> serverutilities$speedup1(WorldServer instance) { | ||
return sleepingPlayers.isEmpty() ? this.playerEntities : sleepingPlayers; | ||
} | ||
|
||
@Inject( | ||
method = "areAllPlayersAsleep", | ||
at = @At(value = "INVOKE", target = "Ljava/util/List;iterator()Ljava/util/Iterator;"), | ||
cancellable = true) | ||
public void serverutilities$speedup2(CallbackInfoReturnable<Boolean> ctx) { | ||
if (percent < 1) ctx.setReturnValue(true); | ||
} | ||
|
||
@Inject( | ||
method = "wakeAllPlayers", | ||
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;wakeUpPlayer(ZZZ)V"), | ||
locals = LocalCapture.CAPTURE_FAILHARD) | ||
public void serverutilities$broadcast(CallbackInfo ctx, Iterator iterator, EntityPlayer player) { | ||
if (percent > 0 && percent < 100) { | ||
player.addChatMessage(new ChatComponentTranslation("serverutiltiies.world.skip_night")); | ||
} | ||
} | ||
|
||
public List<EntityPlayer> getListWithoutAFK(List<EntityPlayer> list) { | ||
long notificationTimer = ServerUtilitiesConfig.afk.getNotificationTimer(); | ||
return list.stream() | ||
.filter( | ||
(EntityPlayer entity) -> ServerUtilitiesPlayerData | ||
.get(Universe.get().getPlayer((EntityPlayerMP) entity)).afkTime <= notificationTimer) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a scenario where this feature is useful in SP since the mixin is also applied client-side?
I'm not a big fan of it printing two additional chat messages whenever you sleep in SP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe Open to lan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need to apply on both in order to work with open to lan? I don't particularly care about supporting that feature, but that's the only use case I can think of. Otherwise it could be changed to server only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turning off the chat notifications when there's only one player present could also work to prevent unnecessary messages in SP, but I don't really care much for lan either so changing it to server only is also fine by me.