From 5c935fba7696f7864eb8b28be9821b19628519f4 Mon Sep 17 00:00:00 2001 From: Melledy <121644117+Melledy@users.noreply.github.com> Date: Mon, 1 Jan 2024 16:56:16 -0800 Subject: [PATCH] Fix some time related bugs when spoofing server time --- src/main/java/emu/lunarcore/LunarCore.java | 9 ++++++++- src/main/java/emu/lunarcore/game/chat/ChatMessage.java | 5 +++-- src/main/java/emu/lunarcore/game/scene/SceneBuff.java | 3 ++- .../lunarcore/game/scene/entity/EntitySummonUnit.java | 3 ++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/emu/lunarcore/LunarCore.java b/src/main/java/emu/lunarcore/LunarCore.java index 427f1c035..bcac72bb2 100644 --- a/src/main/java/emu/lunarcore/LunarCore.java +++ b/src/main/java/emu/lunarcore/LunarCore.java @@ -254,7 +254,14 @@ private static String getGitHash() { * Returns the current server's time in milliseconds to send to the client. Can be used to spoof server time. */ public static long currentServerTime() { - return System.currentTimeMillis() + timeOffset; + return convertToServerTime(System.currentTimeMillis()); + } + + /** + * Converts a timestamp (in milliseconds) to the server time + */ + public static long convertToServerTime(long time) { + return time + timeOffset; } private static void updateServerTimeOffset() { diff --git a/src/main/java/emu/lunarcore/game/chat/ChatMessage.java b/src/main/java/emu/lunarcore/game/chat/ChatMessage.java index c8b136bab..f695cfb13 100644 --- a/src/main/java/emu/lunarcore/game/chat/ChatMessage.java +++ b/src/main/java/emu/lunarcore/game/chat/ChatMessage.java @@ -1,5 +1,6 @@ package emu.lunarcore.game.chat; +import emu.lunarcore.LunarCore; import emu.lunarcore.proto.ChatOuterClass.Chat; import emu.lunarcore.proto.MsgTypeOuterClass.MsgType; import lombok.Getter; @@ -15,7 +16,7 @@ public class ChatMessage { public ChatMessage(int fromUid, int toUid) { this.fromUid = fromUid; this.toUid = toUid; - this.time = System.currentTimeMillis() / 1000; + this.time = System.currentTimeMillis(); } public ChatMessage(int fromUid, int toUid, String text) { @@ -35,7 +36,7 @@ public MsgType getType() { public Chat toProto() { var proto = Chat.newInstance() .setSenderUid(this.getFromUid()) - .setSentTime(this.getTime()) + .setSentTime(LunarCore.convertToServerTime(this.getTime()) / 1000) .setMsgType(this.getType()) .setEmote(this.getEmote()); diff --git a/src/main/java/emu/lunarcore/game/scene/SceneBuff.java b/src/main/java/emu/lunarcore/game/scene/SceneBuff.java index da4497609..5cf302934 100644 --- a/src/main/java/emu/lunarcore/game/scene/SceneBuff.java +++ b/src/main/java/emu/lunarcore/game/scene/SceneBuff.java @@ -1,5 +1,6 @@ package emu.lunarcore.game.scene; +import emu.lunarcore.LunarCore; import emu.lunarcore.proto.BuffInfoOuterClass.BuffInfo; import lombok.Getter; import lombok.Setter; @@ -52,7 +53,7 @@ public BuffInfo toProto() { .setBuffId(this.getBuffId()) .setLevel(this.getBuffLevel()) .setBaseAvatarId(this.getCasterAvatarId()) - .setAddTimeMs(this.getCreateTime()) + .setAddTimeMs(LunarCore.convertToServerTime(this.getCreateTime())) .setLifeTime(this.getDuration()) .setCount(this.getCount()); diff --git a/src/main/java/emu/lunarcore/game/scene/entity/EntitySummonUnit.java b/src/main/java/emu/lunarcore/game/scene/entity/EntitySummonUnit.java index e45481b47..0d2a212ce 100644 --- a/src/main/java/emu/lunarcore/game/scene/entity/EntitySummonUnit.java +++ b/src/main/java/emu/lunarcore/game/scene/entity/EntitySummonUnit.java @@ -1,5 +1,6 @@ package emu.lunarcore.game.scene.entity; +import emu.lunarcore.LunarCore; import emu.lunarcore.data.excel.SummonUnitExcel; import emu.lunarcore.game.avatar.GameAvatar; import emu.lunarcore.game.scene.Scene; @@ -53,7 +54,7 @@ public boolean isExpired() { public SceneEntityInfo toSceneEntityProto() { var summon = SceneSummonUnitInfo.newInstance() .setLifeTimeMs(this.getDuration()) - .setCreateTimeMs(this.getCreateTime()) + .setCreateTimeMs(LunarCore.convertToServerTime(this.getCreateTime())) .setCasterEntityId(this.getCaster().getEntityId()) .setAttachEntityId(this.getAttachedEntityId()) .setSummonUnitId(this.getExcel().getId());