Skip to content

Commit

Permalink
use monotonic time for all local timers, also player flags shouldn't …
Browse files Browse the repository at this point in the history
…preload before first tick
  • Loading branch information
mcmonkey4eva committed Apr 21, 2022
1 parent c28aaf7 commit e7e20d4
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 34 deletions.
3 changes: 3 additions & 0 deletions plugin/src/main/java/com/denizenscript/denizen/Denizen.java
Expand Up @@ -79,6 +79,8 @@ public static Denizen getInstance() {
return instance;
}

public static boolean hasTickedOnce = false;

public static String versionTag = null;
private boolean startedSuccessful = false;

Expand Down Expand Up @@ -386,6 +388,7 @@ else if (javaVersion.startsWith("17")) {
final boolean hadCitizensBork = citizensBork;
// Run everything else on the first server tick
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
hasTickedOnce = true;
try {
if (hadCitizensBork) {
Depends.setupCitizens();
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.persistence.Persist;
Expand Down Expand Up @@ -151,7 +152,7 @@ public void NPCPush(NPCPushEvent event) {
if (event.getNPC() == npc && pushable) {
event.setCancelled(false);
// On Push action / Push Trigger
if (System.currentTimeMillis() > pushedTimer) {
if (CoreUtilities.monotonicMillis() > pushedTimer) {
// Get pusher
Player pusher = null;
for (Entity le : event.getNPC().getEntity().getNearbyEntities(1, 1, 1)) {
Expand All @@ -161,7 +162,7 @@ public void NPCPush(NPCPushEvent event) {
}
if (pusher != null) {
new NPCTag(npc).action("push", PlayerTag.mirrorBukkitPlayer(pusher));
pushedTimer = System.currentTimeMillis() + (delay * 1000);
pushedTimer = CoreUtilities.monotonicMillis() + (delay * 1000);
}
} // End push action
if (!pushed && returnable) {
Expand Down
Expand Up @@ -2407,8 +2407,7 @@ else if (foodLevel / maxHunger < 1) {
registerOnlineOnlyTag(TimeTag.class, "last_action_time", (attribute, object) -> {
// The internal time values use monotonic time - this converts to real time.
long playerMilliTime = NMSHandler.playerHelper.getLastActionTime(object.getPlayerEntity());
long relativeMillis = System.nanoTime() / 1000000L - playerMilliTime;
return new TimeTag(System.currentTimeMillis() - relativeMillis);
return new TimeTag(CoreUtilities.monotonicMillisToReal(playerMilliTime));
});

// <--[tag]
Expand Down
Expand Up @@ -127,7 +127,7 @@ public static String getID(NPC npc, PlayerTag player) {
public static boolean getEngaged(NPC npc, PlayerTag player) {
String id = getID(npc, player);
if (currentlyEngaged.containsKey(id)) {
if (currentlyEngaged.get(id) > System.currentTimeMillis()) {
if (currentlyEngaged.get(id) > CoreUtilities.monotonicMillis()) {
return true;
}
}
Expand Down Expand Up @@ -163,6 +163,6 @@ public static void setEngaged(NPC npc, PlayerTag player, boolean engaged) {
* @param duration the number of seconds to engage the NPCTag
*/
public static void setEngaged(NPC npc, PlayerTag player, int duration) {
currentlyEngaged.put(getID(npc, player), System.currentTimeMillis() + duration * 1000);
currentlyEngaged.put(getID(npc, player), CoreUtilities.monotonicMillis() + duration * 1000);
}
}
Expand Up @@ -15,6 +15,7 @@
import com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ScriptUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -106,7 +107,7 @@ public static void runClickable(UUID id, Player player) {
if (clickable == null) {
return;
}
if (clickable.until != 0 && System.currentTimeMillis() > clickable.until) {
if (clickable.until != 0 && CoreUtilities.monotonicMillis() > clickable.until) {
clickables.remove(id);
return;
}
Expand Down Expand Up @@ -206,7 +207,7 @@ public void execute(ScriptEntry scriptEntry) {
newClickable.path = path == null ? null : path.asString();
newClickable.definitions = definitions;
newClickable.remainingUsages = usages == null ? -1 : usages.asInt();
newClickable.until = until == null ? 0 : (System.currentTimeMillis() + until.getMillis());
newClickable.until = until == null ? 0 : (CoreUtilities.monotonicMillis() + until.getMillis());
newClickable.context = scriptEntry.context;
newClickable.npc = Utilities.getEntryNPC(scriptEntry);
newClickable.defMap = defMap;
Expand Down
Expand Up @@ -13,6 +13,7 @@
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
Expand Down Expand Up @@ -140,7 +141,7 @@ else if (LocationTag.matches(chunkText)) {
switch (Action.valueOf(action.asString())) {
case ADD:
if (length.getSeconds() != 0) {
chunkDelays.put(coord, System.currentTimeMillis() + length.getMillis());
chunkDelays.put(coord, CoreUtilities.monotonicMillis() + length.getMillis());
}
else {
chunkDelays.put(coord, (long) 0);
Expand All @@ -152,7 +153,7 @@ else if (LocationTag.matches(chunkText)) {
chunk.addPluginChunkTicket(Denizen.getInstance());
if (length.getSeconds() > 0) {
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
if (chunkDelays.containsKey(coord) && chunkDelays.get(coord) <= System.currentTimeMillis()) {
if (chunkDelays.containsKey(coord) && chunkDelays.get(coord) <= CoreUtilities.monotonicMillis()) {
chunk.removePluginChunkTicket(Denizen.getInstance());
chunkDelays.remove(coord);
}
Expand Down Expand Up @@ -195,7 +196,7 @@ public void stopDespawn(NPCDespawnEvent e) {
if (chunkDelays.get(coord) == 0) {
e.setCancelled(true);
}
else if (System.currentTimeMillis() < chunkDelays.get(coord)) {
else if (CoreUtilities.monotonicMillis() < chunkDelays.get(coord)) {
e.setCancelled(true);
}
else {
Expand Down
Expand Up @@ -296,7 +296,7 @@ public void execute(final ScriptEntry scriptEntry) {
@Override
public void run() {
try {
long start = System.currentTimeMillis();
long start = CoreUtilities.monotonicMillis();
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
Expand Down Expand Up @@ -325,7 +325,7 @@ public void run() {
}
handleLocation(nLoc, index, materialList, doPhysics, natural, radius, height, depth, percs, sourcePlayer, scriptEntry);
index++;
if (System.currentTimeMillis() - start > maxDelay) {
if (CoreUtilities.monotonicMillis() - start > maxDelay) {
break;
}
}
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.denizenscript.denizen.scripts.triggers.core.DamageTrigger;
import com.denizenscript.denizen.scripts.triggers.core.ProximityTrigger;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.triggers.core.ClickTrigger;
Expand Down Expand Up @@ -81,7 +82,7 @@ public boolean checkCooldown(NPC npc, PlayerTag player, AbstractTrigger triggerC
else if (!playerCooldown.get(player.getName() + "/" + npc.getId()).containsKey(triggerClass.name)) {
return true;
}
else if (System.currentTimeMillis() > playerCooldown.get(player.getName() + "/" + npc.getId()).get(triggerClass.name)) {
else if (CoreUtilities.monotonicMillis() > playerCooldown.get(player.getName() + "/" + npc.getId()).get(triggerClass.name)) {
return true;
}
return false;
Expand All @@ -97,7 +98,7 @@ public void setCooldown(NPC npc, PlayerTag player, AbstractTrigger triggerClass,
triggerMap.remove(player.getName() + "/" + npc.getId());
}
else {
triggerMap.put(triggerClass.name, System.currentTimeMillis() + (long) (seconds * 1000));
triggerMap.put(triggerClass.name, CoreUtilities.monotonicMillis() + (long) (seconds * 1000));
}
playerCooldown.put(player.getName() + "/" + npc.getId(), triggerMap);
}
Expand Down
Expand Up @@ -87,7 +87,7 @@ public void run(ReplaceableTagEvent event) {
});
}

public static final long serverStartTimeMillis = System.currentTimeMillis();
public static final long serverStartTimeMillis = CoreUtilities.monotonicMillis();

public void serverTag(ReplaceableTagEvent event) {
if (!event.matches("server") || event.replaced()) {
Expand Down Expand Up @@ -1131,12 +1131,12 @@ else if (recipe instanceof CookingRecipe<?>) {
// Returns the time the server started.
// -->
if (attribute.startsWith("started_time")) {
event.setReplacedObject(new TimeTag(DenizenCore.startTime)
event.setReplacedObject(new TimeTag(CoreUtilities.monotonicMillisToReal(DenizenCore.startTime))
.getObjectAttribute(attribute.fulfill(1)));
}
if (attribute.startsWith("start_time")) {
Deprecations.timeTagRewrite.warn(attribute.context);
event.setReplacedObject(new DurationTag(DenizenCore.startTime / 50)
event.setReplacedObject(new DurationTag(CoreUtilities.monotonicMillisToReal(DenizenCore.startTime) / 50)
.getObjectAttribute(attribute.fulfill(1)));
}

Expand Down Expand Up @@ -1277,7 +1277,7 @@ else if (recipe instanceof CookingRecipe<?>) {
// In many cases <@link tag server.delta_time_since_start> is preferable.
// -->
if (attribute.startsWith("real_time_since_start")) {
event.setReplacedObject(new DurationTag((System.currentTimeMillis() - serverStartTimeMillis) / 1000.0)
event.setReplacedObject(new DurationTag((CoreUtilities.monotonicMillis() - serverStartTimeMillis) / 1000.0)
.getObjectAttribute(attribute.fulfill(1)));
}

Expand All @@ -1287,6 +1287,7 @@ else if (recipe instanceof CookingRecipe<?>) {
// @description
// Returns the number of milliseconds since Jan 1, 1970.
// Note that this can change every time the tag is read!
// Use <@link tag util.time_now> if you need stable time.
// -->
if (attribute.startsWith("current_time_millis")) {
event.setReplacedObject(new ElementTag(System.currentTimeMillis())
Expand Down Expand Up @@ -2211,7 +2212,7 @@ else if (attribute.startsWith("debug_enabled")) {
// Returns the time that Denizen scripts were last reloaded.
// -->
else if (attribute.startsWith("last_reload")) {
event.setReplacedObject(new TimeTag(DenizenCore.lastReloadTime).getObjectAttribute(attribute.fulfill(1)));
event.setReplacedObject(new TimeTag(CoreUtilities.monotonicMillisToReal(DenizenCore.lastReloadTime)).getObjectAttribute(attribute.fulfill(1)));
}

// <--[tag]
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void buildDelayed(AreaContainmentObject area, Location center, Runnable r
int index = 0;
@Override
public void run() {
long start = System.currentTimeMillis();
long start = CoreUtilities.monotonicMillis();
while (index < goal) {
long z = index % ((long) z_height);
long y = ((index - z) % ((long)y_length * z_height)) / ((long) z_height);
Expand All @@ -100,7 +100,7 @@ public void run() {
refLoc.setZ(lowZ + z);
blocks[index] = (constraint == null || constraint.doesContainLocation(refLoc)) ? new FullBlockData(refLoc.getBlock(), copyFlags) : STRUCTURE_VOID;
index++;
if (System.currentTimeMillis() - start > maxDelayMs) {
if (CoreUtilities.monotonicMillis() - start > maxDelayMs) {
SchematicCommand.noPhys = false;
return;
}
Expand Down Expand Up @@ -277,14 +277,14 @@ public void setBlocksDelayed(final Runnable runme, final InputParams input, long
@Override
public void run() {
SchematicCommand.noPhys = true;
long start = System.currentTimeMillis();
long start = CoreUtilities.monotonicMillis();
while (index < goal) {
int z = index % (z_height);
int y = ((index - z) % (y_length * z_height)) / z_height;
int x = (index - y - z) / (y_length * z_height);
setBlockSingle(blocks[index], x, y, z, input);
index++;
if (System.currentTimeMillis() - start > maxDelayMs) {
if (CoreUtilities.monotonicMillis() - start > maxDelayMs) {
SchematicCommand.noPhys = false;
return;
}
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -104,15 +105,15 @@ else if (Denizen.supportsPaper) {
prefix = "pastetype=log"
+ "&response=micro&v=200&pastetitle=Denizen+Debug+Logs+From+" + URLEncoder.encode(ChatColor.stripColor(Bukkit.getServer().getMotd()))
+ "&pastecontents=" + URLEncoder.encode("Java Version: " + System.getProperty("java.version")
+ "\nUp-time: " + new DurationTag((System.currentTimeMillis() - DenizenCore.startTime) / 50).formatted(false)
+ "\nUp-time: " + new DurationTag((CoreUtilities.monotonicMillis() - DenizenCore.startTime) / 1000.0).formatted(false)
+ "\nServer Version: " + Bukkit.getServer().getName() + " version " + Bukkit.getServer().getVersion()
+ "\nDenizen Version: Core: " + DenizenCore.VERSION + ", CraftBukkit: " + Denizen.getInstance().coreImplementation.getImplementationVersion()
+ "\nActive Plugins (" + pluginCount + "): " + pluginlist.substring(0, pluginlist.length() - 2)
+ "\nLoaded Worlds (" + worldCount + "): " + worldlist.substring(0, worldlist.length() - 2)
+ "\nOnline Players (" + playerCount + "): " + playerlist.substring(0, playerlist.length() - 2)
+ "\nTotal Players Ever: " + PlayerTag.getAllPlayers().size() + " (" + validPl + " valid, " + invalidPl + " invalid)"
+ "\nMode: " + onlineMode
+ "\nLast reload: " + new DurationTag((System.currentTimeMillis() - DenizenCore.lastReloadTime) / 1000.0).formatted(false) + " ago"
+ "\nLast reload: " + new DurationTag((CoreUtilities.monotonicMillis() - DenizenCore.lastReloadTime) / 1000.0).formatted(false) + " ago"
+ "\n\n", "UTF-8");
}
catch (Throwable ex) {
Expand Down

0 comments on commit e7e20d4

Please sign in to comment.