From ab86fc929292521f42f1dd7751bd65da9805e9a0 Mon Sep 17 00:00:00 2001 From: gnembon Date: Wed, 20 May 2020 02:09:57 -0300 Subject: [PATCH 1/2] added 'portal_cooldown' and 'portal_timer' to entity API (query and modify) --- docs/scarpet/Full.md | 16 ++++++++++++++++ docs/scarpet/api/Entities.md | 16 ++++++++++++++++ src/main/java/carpet/fakes/EntityInterface.java | 4 ++++ .../carpet/mixins/Entity_scarpetEventsMixin.java | 13 +++++++++++++ .../java/carpet/script/value/EntityValue.java | 16 ++++++++++++++++ 5 files changed, 65 insertions(+) diff --git a/docs/scarpet/Full.md b/docs/scarpet/Full.md index 9882a423..b8c7ae9c 100644 --- a/docs/scarpet/Full.md +++ b/docs/scarpet/Full.md @@ -2526,6 +2526,14 @@ Breeding age of passive entity, in ticks. If negative, time to adulthood, if pos For living entities, the number of ticks they fall outside of immediate player presence. +### `query(e, 'portal_cooldown')` + +Number of ticks remaining until an entity can use a portal again. + +### `query(e, 'portal_timer')` + +Number of ticks an entity sits in a portal. + ### `query(e, 'item')` The item triple (name, count, nbt) if its an item entity, `null` otherwise @@ -2739,6 +2747,14 @@ Sets the breeding age for the animal. Sets a custom despawn timer value. +### `modify(e, 'portal_cooldown', number)` + +Sets a custom number of ticks remaining until an entity can use a portal again. + +### `modify(e, 'portal_timer', number)` + +Sets a custom number of ticks an entity sits in a portal. + ### `modify(e, 'dismount')` Dismounts riding entity. diff --git a/docs/scarpet/api/Entities.md b/docs/scarpet/api/Entities.md index b41bbde0..36365a78 100644 --- a/docs/scarpet/api/Entities.md +++ b/docs/scarpet/api/Entities.md @@ -204,6 +204,14 @@ Breeding age of passive entity, in ticks. If negative, time to adulthood, if pos For living entities, the number of ticks they fall outside of immediate player presence. +### `query(e, 'portal_cooldown')` + +Number of ticks remaining until an entity can use a portal again. + +### `query(e, 'portal_timer')` + +Number of ticks an entity sits in a portal. + ### `query(e, 'item')` The item triple (name, count, nbt) if its an item entity, `null` otherwise @@ -417,6 +425,14 @@ Sets the breeding age for the animal. Sets a custom despawn timer value. +### `modify(e, 'portal_cooldown', number)` + +Sets a custom number of ticks remaining until an entity can use a portal again. + +### `modify(e, 'portal_timer', number)` + +Sets a custom number of ticks an entity sits in a portal. + ### `modify(e, 'dismount')` Dismounts riding entity. diff --git a/src/main/java/carpet/fakes/EntityInterface.java b/src/main/java/carpet/fakes/EntityInterface.java index 3e4713b9..4970d5e7 100644 --- a/src/main/java/carpet/fakes/EntityInterface.java +++ b/src/main/java/carpet/fakes/EntityInterface.java @@ -11,4 +11,8 @@ public interface EntityInterface boolean isPermanentVehicle(); void setPermanentVehicle(boolean permanent); + + int getPortalTimer(); + + void setPortalTimer(int amount); } diff --git a/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java b/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java index c4193054..45a8afcf 100644 --- a/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java @@ -14,6 +14,7 @@ public abstract class Entity_scarpetEventsMixin implements EntityInterface { @Shadow public boolean removed; + @Shadow protected int netherPortalTime; private boolean permanentVehicle; private final EntityEventsGroup events = new EntityEventsGroup(); @@ -36,6 +37,18 @@ public abstract class Entity_scarpetEventsMixin implements EntityInterface permanentVehicle = permanent; } + @Override + public int getPortalTimer() + { + return netherPortalTime; + } + + @Override + public void setPortalTimer(int amount) + { + netherPortalTime = amount; + } + @Inject(method = "tick", at = @At("HEAD")) private void onTickCall(CallbackInfo ci) { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index d6948451..45be924e 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -259,6 +259,8 @@ public class EntityValue extends Value put("item", (e, a) -> (e instanceof ItemEntity)?ListValue.fromItemStack(((ItemEntity) e).getStack()):Value.NULL); put("count", (e, a) -> (e instanceof ItemEntity)?new NumericValue(((ItemEntity) e).getStack().getCount()):Value.NULL); put("pickup_delay", (e, a) -> (e instanceof ItemEntity)?new NumericValue(((ItemEntityInterface) e).getPickupDelayCM()):Value.NULL); + put("portal_cooldown", (e , a) ->new NumericValue(((EntityInterface)e).getPortalTimer())); + put("portal_timer", (e , a) ->new NumericValue(e.netherPortalCooldown)); // ItemEntity -> despawn timer via ssGetAge put("is_baby", (e, a) -> (e instanceof LivingEntity)?new NumericValue(((LivingEntity) e).isBaby()):Value.NULL); put("target", (e, a) -> { @@ -825,6 +827,20 @@ public class EntityValue extends Value } }); + put("portal_cooldown", (e , v) -> + { + if (v==null) + throw new InternalExpressionException("'portal_cooldown' requires a value to set"); + e.netherPortalCooldown = NumericValue.asNumber(v).getInt(); + }); + + put("portal_timer", (e , v) -> + { + if (v==null) + throw new InternalExpressionException("'portal_timer' requires a value to set"); + ((EntityInterface) e).setPortalTimer(NumericValue.asNumber(v).getInt()); + }); + put("ai", (e, v) -> { if (e instanceof MobEntity) From 934038828cb24b3a5d3fa2895fd6b2d20daf3a45 Mon Sep 17 00:00:00 2001 From: gnembon Date: Wed, 20 May 2020 02:13:12 -0300 Subject: [PATCH 2/2] handled errors in server docked tasks to continue stack trace of internal exceptions. --- .../java/carpet/script/CarpetExpression.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index b77e2ea4..d1329558 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -139,6 +139,7 @@ import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.CompletionException; import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -2833,12 +2834,43 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("task_dock", 1, (c, t, lv) -> { + this.expr.addLazyFunctionWithDelegation("task_dock", 1, (c, t, expr, tok, lv) -> { CarpetContext cc = (CarpetContext)c; MinecraftServer server = cc.s.getMinecraftServer(); if (server.isOnThread()) return lv.get(0); // pass through for on thread tasks Value[] result = new Value[]{Value.NULL}; - ((CarpetContext) c).s.getMinecraftServer().submitAndJoin(() -> result[0] = lv.get(0).evalValue(c, t)); + RuntimeException[] internal = new RuntimeException[]{null}; + try + { + ((CarpetContext) c).s.getMinecraftServer().submitAndJoin(() -> + { + try + { + result[0] = lv.get(0).evalValue(c, t); + } + catch (ExpressionException exc) + { + internal[0] = exc; + } + catch (InternalExpressionException exc) + { + internal[0] = new ExpressionException(c, expr, tok, exc.getMessage(), exc.stack); + } + + catch (ArithmeticException exc) + { + internal[0] = new ExpressionException(c, expr, tok, "Your math is wrong, "+exc.getMessage()); + } + }); + } + catch (CompletionException exc) + { + throw new InternalExpressionException("Error while executing docked task section, internal stack trace is gone"); + } + if (internal[0] != null) + { + throw internal[0]; + } Value ret = result[0]; // preventing from lazy evaluating of the result in case a future completes later return (_c, _t) -> ret; // pass through placeholder