From b6316013cadd339d9d4e0ac0ad51b1aad7706b17 Mon Sep 17 00:00:00 2001 From: Jay Hopkins Date: Sat, 18 Apr 2020 05:48:51 -0400 Subject: [PATCH] Add Paper tag base and tick_times tag (#2192) * Add Paper tag base and tick_times tag * Return durations for paper tick_times * Add missing paper key to docs --- .../denizen/paper/PaperModule.java | 6 +++ .../denizen/paper/tags/PaperTagBase.java | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 paper/src/main/java/com/denizenscript/denizen/paper/tags/PaperTagBase.java diff --git a/paper/src/main/java/com/denizenscript/denizen/paper/PaperModule.java b/paper/src/main/java/com/denizenscript/denizen/paper/PaperModule.java index 3b5f2f981b..4477e54622 100644 --- a/paper/src/main/java/com/denizenscript/denizen/paper/PaperModule.java +++ b/paper/src/main/java/com/denizenscript/denizen/paper/PaperModule.java @@ -3,6 +3,7 @@ import com.denizenscript.denizen.objects.EntityTag; import com.denizenscript.denizen.paper.events.*; import com.denizenscript.denizen.paper.properties.EntityCanTick; +import com.denizenscript.denizen.paper.tags.PaperTagBase; import com.denizenscript.denizen.utilities.debugging.Debug; import com.denizenscript.denizencore.events.ScriptEvent; import com.denizenscript.denizencore.objects.properties.PropertyParser; @@ -11,6 +12,7 @@ public class PaperModule { public static void init() { Debug.log("Loading Paper support module..."); + // Events ScriptEvent.registerScriptEvent(new EntityKnocksbackEntityScriptEvent()); ScriptEvent.registerScriptEvent(new PlayerEquipsArmorScriptEvent()); @@ -19,7 +21,11 @@ public static void init() { ScriptEvent.registerScriptEvent(new PlayerStopsSpectatingScriptEvent()); ScriptEvent.registerScriptEvent(new ProjectileCollideScriptEvent()); ScriptEvent.registerScriptEvent(new TNTPrimesScriptEvent()); + // Properties PropertyParser.registerProperty(EntityCanTick.class, EntityTag.class); + + // Paper Tags + new PaperTagBase(); } } diff --git a/paper/src/main/java/com/denizenscript/denizen/paper/tags/PaperTagBase.java b/paper/src/main/java/com/denizenscript/denizen/paper/tags/PaperTagBase.java new file mode 100644 index 0000000000..d5f4aef3f9 --- /dev/null +++ b/paper/src/main/java/com/denizenscript/denizen/paper/tags/PaperTagBase.java @@ -0,0 +1,46 @@ +package com.denizenscript.denizen.paper.tags; + +import com.denizenscript.denizencore.objects.core.DurationTag; +import com.denizenscript.denizencore.objects.core.ListTag; +import com.denizenscript.denizencore.tags.TagRunnable; +import com.denizenscript.denizencore.tags.Attribute; +import com.denizenscript.denizencore.tags.ReplaceableTagEvent; +import com.denizenscript.denizencore.tags.TagManager; +import org.bukkit.Bukkit; + +public class PaperTagBase { + + public PaperTagBase() { + TagManager.registerTagHandler(new TagRunnable.RootForm() { + @Override + public void run(ReplaceableTagEvent event) { + paperTag(event); + } + }, "paper"); + } + + public void paperTag(ReplaceableTagEvent event) { + if (!event.matches("paper") || event.replaced()) { + return; + } + + Attribute attribute = event.getAttributes().fulfill(1); + + // <--[tag] + // @attribute + // @returns ListTag(DurationTag) + // @Plugin Paper + // @description + // Returns a sample of the server's last 5s of tick times as a list of durations. + // On average, a tick should take 50ms or less for a stable 20tps. + // --> + if (attribute.startsWith("tick_times")) { + ListTag list = new ListTag(); + for (long time : Bukkit.getServer().getTickTimes()) { + list.addObject(new DurationTag(time / 1000000000D)); + } + event.setReplacedObject(list.getObjectAttribute(attribute.fulfill(1))); + return; + } + } +}