Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TimeSinceRest #1670

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
protected double lastRightClickTime = 0.0;
protected Vector3 lastRightClickPos = null;

private int timeSinceRest;

public int getStartActionTick() {
return startAction;
}
Expand Down Expand Up @@ -1129,6 +1131,8 @@ public boolean sleepOn(Vector3 pos) {

this.level.sleepTicks = 60;

this.timeSinceRest = 0;

return true;
}

Expand Down Expand Up @@ -1731,6 +1735,10 @@ public boolean onUpdate(int currentTick) {
if (this.getFoodData() != null) this.getFoodData().update(tickDiff);
}
}

if (!this.isSleeping()) {
this.timeSinceRest++;
}
}

this.checkTeleportPosition();
Expand Down Expand Up @@ -1983,6 +1991,11 @@ protected void processLogin() {

this.forceMovement = this.teleportPosition = this.getPosition();

if (!this.namedTag.contains("TimeSinceRest")) {
this.namedTag.putInt("TimeSinceRest", 0);
}
this.timeSinceRest = this.namedTag.getInt("TimeSinceRest");

ResourcePacksInfoPacket infoPacket = new ResourcePacksInfoPacket();
infoPacket.resourcePackEntries = this.server.getResourcePackManager().getResourceStack();
infoPacket.mustAccept = this.server.getForceResources();
Expand Down Expand Up @@ -3769,6 +3782,8 @@ public void save(boolean async) {
this.namedTag.putInt("foodLevel", this.getFoodData().getLevel());
this.namedTag.putFloat("foodSaturationLevel", this.getFoodData().getFoodSaturationLevel());

this.namedTag.putInt("TimeSinceRest", this.timeSinceRest);

if (!this.username.isEmpty() && this.namedTag != null) {
this.server.saveOfflinePlayerData(this.uuid, this.namedTag, async);
}
Expand Down Expand Up @@ -3972,6 +3987,8 @@ public void kill() {
this.setExperience(0, 0);
}

this.timeSinceRest = 0;

if (showMessages && !ev.getDeathMessage().toString().isEmpty()) {
this.server.broadcast(ev.getDeathMessage(), Server.BROADCAST_CHANNEL_USERS);
}
Expand Down Expand Up @@ -5103,4 +5120,12 @@ public String toString() {
"', location=" + super.toString() +
')';
}

public int getTimeSinceRest() {
return timeSinceRest;
}

public void setTimeSinceRest(int timeSinceRest) {
this.timeSinceRest = timeSinceRest;
}
}