Skip to content

Commit

Permalink
private <E> Iterator<E> dontCountSpectatorsAsAwake(List<E> instance, …
Browse files Browse the repository at this point in the history
…Operation<Iterator<E>> original)
  • Loading branch information
Yoghurt4C committed Oct 29, 2024
1 parent a1e7add commit d1e93e5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EntityPlayer> sleepyPlayers = new ArrayList<>();

public static void registerGamerule(World world) {
if (!world.isRemote && !world.getGameRules().hasRule(GAMERULE_NAME)) {
world.getGameRules().addGameRule(GAMERULE_NAME, DEFAULT_VALUE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ public List<String> getMixins(Set<String> loadedCoreMods) {
}
}

if (ConfigMixins.enablePlayersSleepingPecentageGamerule) {
mixins.add("playerssleepingpercentage.MixinWorldServer");
}

return mixins;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<EntityPlayer> 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")));
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/assets/etfuturum/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ commands.etfuturum.fill.usage=/fill <from> <to> <block>
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
Expand Down

0 comments on commit d1e93e5

Please sign in to comment.