From 6932e5b6820bd9c4ca17b64a65aa41469b743be4 Mon Sep 17 00:00:00 2001 From: gnembon Date: Wed, 15 Apr 2020 11:01:26 -0300 Subject: [PATCH] added 'last_tick_times' --- docs/scarpet/api/Auxiliary.md | 9 +++++++++ docs/scarpet/resources/editors/idea/2.txt | 1 + docs/scarpet/resources/editors/npp/scarpet.xml | 2 +- .../java/carpet/script/CarpetExpression.java | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/scarpet/api/Auxiliary.md b/docs/scarpet/api/Auxiliary.md index 52941068..db5cba2e 100644 --- a/docs/scarpet/api/Auxiliary.md +++ b/docs/scarpet/api/Auxiliary.md @@ -126,6 +126,15 @@ Returns dimension-specific tick counter. Returns current daytime clock value. If `new_time` is specified, sets a new clock to that value. Daytime clocks are shared between all dimensions. +### `last_tick_times()` + +Returns a 100-long array of recent tick times, in milliseconds. First item on the list is the most recent tick +If called outside of the main tick (either throgh scheduled tasks, or async execution), then the first item on the +list may refer to the previous tick performance. In this case the last entry (tick 100) would refer to the most current +tick. For all intent and purpose, `last_tick_times():0` should be used as last tick execution time, but +individual tick times may vary greatly, and these need to be taken with the little grain of +averaging. + ### `game_tick(mstime?)` Causes game to run for one tick. By default runs it and returns control to the program, but can optionally diff --git a/docs/scarpet/resources/editors/idea/2.txt b/docs/scarpet/resources/editors/idea/2.txt index 31aa5e6f..edcf56fb 100644 --- a/docs/scarpet/resources/editors/idea/2.txt +++ b/docs/scarpet/resources/editors/idea/2.txt @@ -38,6 +38,7 @@ inventory_remove inventory_set inventory_size item_category +last_tick_times light liquid load_app_data diff --git a/docs/scarpet/resources/editors/npp/scarpet.xml b/docs/scarpet/resources/editors/npp/scarpet.xml index 6f5366e0..8ad6f2ea 100644 --- a/docs/scarpet/resources/editors/npp/scarpet.xml +++ b/docs/scarpet/resources/editors/npp/scarpet.xml @@ -67,7 +67,7 @@ logger sound particle particle_line particle_rect - run save tick_time world_time day_time game_tick seed current_dimension in_dimension + run save tick_time world_time day_time last_tick_times game_tick seed current_dimension in_dimension plop harvest destroy schedule nbt create_marker remove_all_markers load_app_data store_app_data escape_nbt statistic diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index d11f126e..3bb7ffd5 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -2687,6 +2687,22 @@ public class CarpetExpression return (cc, tt) -> time; }); + this.expr.addLazyFunction("last_tick_times", -1, (c, t, lv) -> + { + //assuming we are in the tick world section + // might be off one tick when run in the off tasks or asynchronously. + int currentReportedTick = ((CarpetContext) c).s.getMinecraftServer().getTicks()-1; + List ticks = new ArrayList<>(100); + final long[] tickArray = ((CarpetContext) c).s.getMinecraftServer().lastTickLengths; + for (int i=currentReportedTick+100; i > currentReportedTick; i--) + { + ticks.add(new NumericValue(((double)tickArray[i % 100])/1000000.0)); + } + Value ret = ListValue.wrap(ticks); + return (cc, tt) -> ret; + }); + + this.expr.addLazyFunction("game_tick", -1, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; if (!s.getMinecraftServer().isOnThread()) throw new InternalExpressionException("Unable to run ticks from threads");