From d1e93e5f20263172e44425eceac5a2231ef4b4fc Mon Sep 17 00:00:00 2001 From: Yoghurt4C Date: Wed, 30 Oct 2024 01:42:15 +0300 Subject: [PATCH] private Iterator dontCountSpectatorsAsAwake(List instance, Operation> original) --- .../configuration/configs/ConfigMixins.java | 2 + .../core/handlers/ServerEventHandler.java | 5 ++ .../gamerule/PlayersSleepingPercentage.java | 21 +++++ .../mixinplugin/EtFuturumEarlyMixins.java | 4 + .../MixinWorldServer.java | 77 +++++++++++++++++++ .../assets/etfuturum/lang/en_US.lang | 3 + 6 files changed, 112 insertions(+) create mode 100644 src/main/java/ganymedes01/etfuturum/gamerule/PlayersSleepingPercentage.java create mode 100644 src/main/java/ganymedes01/etfuturum/mixins/early/playerssleepingpercentage/MixinWorldServer.java diff --git a/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigMixins.java b/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigMixins.java index 65c86b58..cb60547f 100644 --- a/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigMixins.java +++ b/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigMixins.java @@ -44,6 +44,7 @@ public class ConfigMixins extends ConfigBase { public static boolean fireproofItems; public static boolean thinPanes; public static boolean colorGrassBlockItemSides; + public static boolean enablePlayersSleepingPecentageGamerule; static final String catBackport = "backported features"; static final String catOptimization = "optimizations"; @@ -113,5 +114,6 @@ protected void syncConfigOptions() { arrowFallingFix = getBoolean("arrowFallingFix", catFixes, true, "Prevents arrows from falling off of blocks too easily\nModified classes: net.minecraft.entity.EntityArrow"); collidedThrowableFix = getBoolean("collidedThrowableFix", catFixes, true, "Fixes EntityThrowable entities not calling onEntityCollidedWithBlock, causing them to not trigger target blocks or chime amethyst.\nModified classes: net.minecraft.entity.projectile.EntityThrowable"); hideSingleLevelEnchants = getBoolean("hideSingleLevelEnchants", catFixes, true, "Fixes enchantments with only one possible level displaying a level in their name. E.G. \"Silk Touch I\" becomes \"Silk Touch\".\nModified Classes: net.minecraft.enchantment.Enchantment"); + enablePlayersSleepingPecentageGamerule = getBoolean("enablePlayersSleepingPecentageGamerule", catBackport, true, "You nappa, you get slappa"); } } \ No newline at end of file diff --git a/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java b/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java index 4a5059eb..4c0e5ed4 100644 --- a/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java +++ b/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java @@ -32,6 +32,7 @@ import ganymedes01.etfuturum.entities.*; import ganymedes01.etfuturum.entities.ai.EntityAIOpenCustomDoor; import ganymedes01.etfuturum.gamerule.DoWeatherCycle; +import ganymedes01.etfuturum.gamerule.PlayersSleepingPercentage; import ganymedes01.etfuturum.gamerule.RandomTickSpeed; import ganymedes01.etfuturum.items.ItemArrowTipped; import ganymedes01.etfuturum.lib.Reference; @@ -1782,6 +1783,10 @@ public void loadWorldEvent(WorldEvent.Load event) { if (ConfigMixins.enableDoWeatherCycle) { DoWeatherCycle.registerGamerule(event.world); } + + if (ConfigMixins.enablePlayersSleepingPecentageGamerule) { + PlayersSleepingPercentage.registerGamerule(event.world); + } if(ConfigMixins.enableRandomTickSpeed) { RandomTickSpeed.registerGamerule(event.world); diff --git a/src/main/java/ganymedes01/etfuturum/gamerule/PlayersSleepingPercentage.java b/src/main/java/ganymedes01/etfuturum/gamerule/PlayersSleepingPercentage.java new file mode 100644 index 00000000..655bc7e1 --- /dev/null +++ b/src/main/java/ganymedes01/etfuturum/gamerule/PlayersSleepingPercentage.java @@ -0,0 +1,21 @@ +package ganymedes01.etfuturum.gamerule; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; + +import java.util.ArrayList; +import java.util.List; + +public class PlayersSleepingPercentage { + public static final PlayersSleepingPercentage INSTANCE = new PlayersSleepingPercentage(); + public static final String GAMERULE_NAME = "playersSleepingPercentage"; + public static final String DEFAULT_VALUE = "100"; + + public List sleepyPlayers = new ArrayList<>(); + + public static void registerGamerule(World world) { + if (!world.isRemote && !world.getGameRules().hasRule(GAMERULE_NAME)) { + world.getGameRules().addGameRule(GAMERULE_NAME, DEFAULT_VALUE); + } + } +} diff --git a/src/main/java/ganymedes01/etfuturum/mixinplugin/EtFuturumEarlyMixins.java b/src/main/java/ganymedes01/etfuturum/mixinplugin/EtFuturumEarlyMixins.java index 6f90ecdc..389cc354 100644 --- a/src/main/java/ganymedes01/etfuturum/mixinplugin/EtFuturumEarlyMixins.java +++ b/src/main/java/ganymedes01/etfuturum/mixinplugin/EtFuturumEarlyMixins.java @@ -267,6 +267,10 @@ public List getMixins(Set loadedCoreMods) { } } + if (ConfigMixins.enablePlayersSleepingPecentageGamerule) { + mixins.add("playerssleepingpercentage.MixinWorldServer"); + } + return mixins; } diff --git a/src/main/java/ganymedes01/etfuturum/mixins/early/playerssleepingpercentage/MixinWorldServer.java b/src/main/java/ganymedes01/etfuturum/mixins/early/playerssleepingpercentage/MixinWorldServer.java new file mode 100644 index 00000000..9794c509 --- /dev/null +++ b/src/main/java/ganymedes01/etfuturum/mixins/early/playerssleepingpercentage/MixinWorldServer.java @@ -0,0 +1,77 @@ +package ganymedes01.etfuturum.mixins.early.playerssleepingpercentage; + +import ganymedes01.etfuturum.spectator.SpectatorMode; +import net.minecraft.client.resources.I18n; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.profiler.Profiler; +import net.minecraft.util.ChatComponentText; +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.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.LocalCapture; + +import java.util.Iterator; +import java.util.List; + +import static ganymedes01.etfuturum.gamerule.PlayersSleepingPercentage.GAMERULE_NAME; +import static ganymedes01.etfuturum.gamerule.PlayersSleepingPercentage.INSTANCE; + +@Mixin(WorldServer.class) +public abstract class MixinWorldServer extends World { + @Shadow + private boolean allPlayersSleeping; + + 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 ArithmeticException("2 + 2 = 5 ???"); + } + + @Inject(method = "updateAllPlayersSleepingFlag", at = @At("HEAD"), cancellable = true) + public void hhheheheheeh(CallbackInfo ctx) { + int percentrillo = Integer.parseInt(this.getGameRules().getGameRuleStringValue(GAMERULE_NAME)); + if (percentrillo > 100) { + this.allPlayersSleeping = false; + ctx.cancel(/* /r/nosleep, vanilla behaviour */); + } else if (percentrillo < 100) { + INSTANCE.sleepyPlayers.clear(); + int cap = (int) (this.playerEntities.size() * Math.ceil(percentrillo * 0.01f)); + for (EntityPlayer player : this.playerEntities) { + if (player.isPlayerSleeping() || SpectatorMode.isSpectator(player)) { + INSTANCE.sleepyPlayers.add(player); + if (INSTANCE.sleepyPlayers.size() >= cap) { + this.allPlayersSleeping = true; + break; + } + } + } + + if (!INSTANCE.sleepyPlayers.isEmpty()) { + for (EntityPlayer paypiggy : this.playerEntities) { + paypiggy.addChatMessage(new ChatComponentText(I18n.format("sleep.players_sleeping", INSTANCE.sleepyPlayers.size(), cap))); + } + } + ctx.cancel(); + } + } + + @Redirect(method = "areAllPlayersAsleep", at = @At(value = "FIELD", target = "Lnet/minecraft/world/WorldServer;playerEntities:Ljava/util/List;", opcode = Opcodes.GETFIELD)) + public List baited(WorldServer instance) { + return INSTANCE.sleepyPlayers; + } + + @Inject(method = "wakeAllPlayers", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;wakeUpPlayer(ZZZ)V"), locals = LocalCapture.CAPTURE_FAILHARD) + public void broadcast(CallbackInfo ctx, Iterator iterator, EntityPlayer player) { + if (!this.getGameRules().getGameRuleStringValue(GAMERULE_NAME).equals("101")) { + player.addChatMessage(new ChatComponentText(I18n.format("sleep.skipping_night"))); + } + } +} diff --git a/src/main/resources/assets/etfuturum/lang/en_US.lang b/src/main/resources/assets/etfuturum/lang/en_US.lang index f3d6a96d..1ef21eb6 100755 --- a/src/main/resources/assets/etfuturum/lang/en_US.lang +++ b/src/main/resources/assets/etfuturum/lang/en_US.lang @@ -27,6 +27,9 @@ commands.etfuturum.fill.usage=/fill commands.etfuturum.fill.outOfWorld=Cannot place blocks outside of world commands.etfuturum.fill.success=Successfully filled %d block(s) +sleep.players_sleeping=%s/%s players sleeping +sleep.skipping_night=Sleeping through this night + # Containers/GUI container.enchant.lapis.one=1 Lapis Lazuli container.enchant.lapis.many=%d Lapis Lazuli