Skip to content

Commit

Permalink
Add Paper tag base and tick_times tag (#2192)
Browse files Browse the repository at this point in the history
* Add Paper tag base and tick_times tag

* Return durations for paper tick_times

* Add missing paper key to docs
  • Loading branch information
mergu committed Apr 18, 2020
1 parent 54c4ff9 commit b631601
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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();
}
}
@@ -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 <paper.tick_times>
// @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;
}
}
}

0 comments on commit b631601

Please sign in to comment.