Skip to content

Commit

Permalink
Fix some time related bugs when spoofing server time
Browse files Browse the repository at this point in the history
  • Loading branch information
Melledy committed Jan 2, 2024
1 parent 9170768 commit 5c935fb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/main/java/emu/lunarcore/LunarCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/emu/lunarcore/game/chat/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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());

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/emu/lunarcore/game/scene/SceneBuff.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 5c935fb

Please sign in to comment.