From 8438e94c6e8cbfe49e0b7448ed46aa06560722d2 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 18 May 2023 02:54:47 -0400 Subject: [PATCH] Fix player pausing --- .../emu/grasscutter/game/player/Player.java | 8 +++-- .../emu/grasscutter/game/world/World.java | 29 +++++++++++++++---- .../packet/recv/HandlerPlayerSetPauseReq.java | 19 +++++++----- .../packet/send/PacketPlayerSetPauseRsp.java | 5 ++-- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 25db3ccd0..070fe54a8 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -771,7 +771,7 @@ public class Player implements PlayerHook { } public void setPaused(boolean newPauseState) { - boolean oldPauseState = this.paused; + var oldPauseState = this.paused; this.paused = newPauseState; if (newPauseState && !oldPauseState) { @@ -1498,8 +1498,10 @@ public class Player implements PlayerHook { PropChangeReason.PROP_CHANGE_REASON_PLAYER_ADD_EXP)); case PROP_PLAYER_LEVEL -> this.sendPacket(new PacketPlayerPropChangeReasonNotify(this, prop, currentValue, value, PropChangeReason.PROP_CHANGE_REASON_LEVELUP)); - case PROP_PLAYER_WORLD_LEVEL -> this.sendPacket(new PacketPlayerPropChangeReasonNotify(this, prop, currentValue, value, - PropChangeReason.PROP_CHANGE_REASON_MANUAL_ADJUST_WORLD_LEVEL)); + + // TODO: Handle world level changing. + // case PROP_PLAYER_WORLD_LEVEL -> this.sendPacket(new PacketPlayerPropChangeReasonNotify(this, prop, currentValue, value, + // PropChangeReason.PROP_CHANGE_REASON_MANUAL_ADJUST_WORLD_LEVEL)); } // Update player with packet. diff --git a/src/main/java/emu/grasscutter/game/world/World.java b/src/main/java/emu/grasscutter/game/world/World.java index 7b66bb91c..63dd9b5c2 100644 --- a/src/main/java/emu/grasscutter/game/world/World.java +++ b/src/main/java/emu/grasscutter/game/world/World.java @@ -1,7 +1,5 @@ package emu.grasscutter.game.world; -import static emu.grasscutter.server.event.player.PlayerTeleportEvent.TeleportType.SCRIPT; - import emu.grasscutter.data.GameData; import emu.grasscutter.data.excels.dungeon.DungeonData; import emu.grasscutter.game.player.Player; @@ -24,13 +22,17 @@ import emu.grasscutter.utils.Position; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; +import lombok.Getter; +import lombok.val; +import org.jetbrains.annotations.NotNull; + import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; -import lombok.Getter; -import lombok.val; + +import static emu.grasscutter.server.event.player.PlayerTeleportEvent.TeleportType.SCRIPT; public final class World implements Iterable { @Getter private final GameServer server; @@ -477,7 +479,12 @@ public final class World implements Iterable { * @param paused True if the world should be paused. */ public void setPaused(boolean paused) { - this.getWorldTime(); // Update the world time. + // Check if this world is a multiplayer world. + if (this.isMultiplayer) return; + + // Update the world time. + this.getWorldTime(); + this.updateTime(); // If the world is being un-paused, update the last update time. if (this.isPaused != paused && !paused) { @@ -526,6 +533,16 @@ public final class World implements Iterable { player -> player.getQuestManager().queueEvent(QuestContent.QUEST_CONTENT_GAME_TIME_TICK)); } + /** + * Notifies all players of the current world time. + */ + public void updateTime() { + this.getPlayers().forEach(p -> + p.sendPacket(new PacketPlayerGameTimeNotify(p))); + this.getPlayers().forEach(p -> + p.sendPacket(new PacketSceneTimeNotify(p))); + } + /** * Locks the world time. * @@ -535,10 +552,12 @@ public final class World implements Iterable { this.timeLocked = locked; // Notify players of the locking. + this.updateTime(); this.getPlayers() .forEach(player -> player.setProperty(PlayerProperty.PROP_IS_GAME_TIME_LOCKED, locked)); } + @NotNull @Override public Iterator iterator() { return this.getPlayers().iterator(); diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerPlayerSetPauseReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerPlayerSetPauseReq.java index 9db0f66a4..c88c94217 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerPlayerSetPauseReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerPlayerSetPauseReq.java @@ -4,22 +4,25 @@ import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.PlayerSetPauseReqOuterClass.PlayerSetPauseReq; +import emu.grasscutter.net.proto.RetcodeOuterClass.Retcode; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.packet.send.PacketPlayerSetPauseRsp; -import emu.grasscutter.server.packet.send.PacketPlayerTimeNotify; -import emu.grasscutter.server.packet.send.PacketSceneTimeNotify; @Opcodes(PacketOpcodes.PlayerSetPauseReq) public class HandlerPlayerSetPauseReq extends PacketHandler { @Override public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { - PlayerSetPauseReq req = PlayerSetPauseReq.parseFrom(payload); + var req = PlayerSetPauseReq.parseFrom(payload); + var player = session.getPlayer(); + var world = player.getWorld(); - session.send(new PacketPlayerSetPauseRsp()); - session.getPlayer().setPaused(req.getIsPaused()); - - session.send(new PacketPlayerTimeNotify(session.getPlayer())); - session.send(new PacketSceneTimeNotify(session.getPlayer())); + // Check if the player is in a multiplayer world. + if (player.isInMultiplayer()) { + session.send(new PacketPlayerSetPauseRsp(Retcode.RET_FAIL)); + } else { + world.setPaused(req.getIsPaused()); + session.send(new PacketPlayerSetPauseRsp(Retcode.RET_SUCC)); + } } } diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketPlayerSetPauseRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketPlayerSetPauseRsp.java index 8b32a79ae..ab3036630 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketPlayerSetPauseRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketPlayerSetPauseRsp.java @@ -7,9 +7,10 @@ import emu.grasscutter.net.proto.RetcodeOuterClass.Retcode; public class PacketPlayerSetPauseRsp extends BasePacket { - public PacketPlayerSetPauseRsp() { + public PacketPlayerSetPauseRsp(Retcode retcode) { super(PacketOpcodes.PlayerSetPauseRsp); - this.setData(PlayerSetPauseRsp.newBuilder().setRetcode(Retcode.RET_SUCC_VALUE)); + this.setData(PlayerSetPauseRsp.newBuilder() + .setRetcode(retcode.getNumber())); } }