Implement game time locking

implement for quests
This commit is contained in:
KingRainbow44
2023-05-08 00:11:29 -04:00
parent d32f6982be
commit a4f5d3286d
4 changed files with 61 additions and 11 deletions

View File

@@ -510,7 +510,8 @@ public final class Scene {
this.finishLoading();
this.checkPlayerRespawn();
if (this.tickCount++ % 10 == 0) broadcastPacket(new PacketSceneTimeNotify(this));
if (this.tickCount++ % 10 == 0)
this.broadcastPacket(new PacketSceneTimeNotify(this));
}
/** Validates a player's current position. Teleports the player if the player is out of bounds. */

View File

@@ -31,7 +31,7 @@ import java.util.stream.Collectors;
import lombok.Getter;
import lombok.val;
public class World implements Iterable<Player> {
public final class World implements Iterable<Player> {
@Getter private final GameServer server;
@Getter private final Player host;
@Getter private final List<Player> players;
@@ -42,12 +42,12 @@ public class World implements Iterable<Player> {
private int nextPeerId = 0;
private int worldLevel;
private boolean isMultiplayer;
@Getter private boolean isMultiplayer, timeLocked = false;
private long lastUpdateTime;
@Getter private int tickCount = 0;
@Getter private boolean isPaused = false;
@Getter private long currentWorldTime = 0;
@Getter private long currentWorldTime;
public World(Player player) {
this(player, false);
@@ -118,10 +118,6 @@ public class World implements Iterable<Player> {
return this.getPlayers().size();
}
public boolean isMultiplayer() {
return isMultiplayer;
}
/**
* Gets the next entity ID for the specified entity type.
*
@@ -427,7 +423,7 @@ public class World implements Iterable<Player> {
// store updated world time every 60 seconds. (in-game hour)
if (this.tickCount % 60 == 0) {
this.getHost().updatePlayerGameTime(currentWorldTime);
this.getHost().updatePlayerGameTime(this.currentWorldTime);
}
this.tickCount++;
@@ -438,13 +434,13 @@ public class World implements Iterable<Player> {
/** Returns the in-game world time in real milliseconds. */
public long getWorldTime() {
if (!this.isPaused) {
if (!this.isPaused && !this.timeLocked) {
var newUpdateTime = System.currentTimeMillis();
this.currentWorldTime += (newUpdateTime - lastUpdateTime);
this.lastUpdateTime = newUpdateTime;
}
return currentWorldTime;
return this.currentWorldTime;
}
/** Returns the current in game days world time in in-game minutes (0-1439) */
@@ -512,6 +508,9 @@ public class World implements Iterable<Player> {
* @param days The number of days to add.
*/
public void changeTime(int time, int days) {
// Check if the time is locked.
if (this.timeLocked) return;
// Calculate time differences.
var currentTime = this.getGameTime();
var diff = time - currentTime;
@@ -526,6 +525,18 @@ public class World implements Iterable<Player> {
player -> player.getQuestManager().queueEvent(QuestContent.QUEST_CONTENT_GAME_TIME_TICK));
}
/**
* Locks the world time.
*
* @param locked True if the world time should be locked.
*/
public void lockTime(boolean locked) {
this.timeLocked = locked;
// Broadcast the state change.
this.broadcastPacket(new PacketClientLockGameTimeNotify(this));
}
@Override
public Iterator<Player> iterator() {
return this.getPlayers().iterator();