From 5bb5f372b6a87bc138d04482649cf72ebc2bd249 Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 28 Apr 2020 18:38:56 -0300 Subject: [PATCH 1/2] scoreboard_remove(obj, key) will remove the key from the objective. Fixes #255 --- docs/scarpet/Full.md | 19 ++++--- docs/scarpet/api/Scoreboard.md | 7 ++- .../java/carpet/script/CarpetExpression.java | 54 +++++++++++-------- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/docs/scarpet/Full.md b/docs/scarpet/Full.md index 68206456..427ecb67 100644 --- a/docs/scarpet/Full.md +++ b/docs/scarpet/Full.md @@ -2217,11 +2217,15 @@ It returns a `map` with a report indicating how many chunks were affected, and h These functions help scan larger areas of blocks without using generic loop functions, like nested `loop`. -### `scan(cx, cy, cz, dx, dy, dz, px?, py?, pz?, expr)` +### `scan(cx, cy, cz, dx, dy, dz, px?, py?, pz?, expr)`, `scan(center, range, lower_range?, expr)` -Evaluates expression over area of blocks defined by its center (`cx, cy, cz`), expanded in all directions -by `dx, dy, dz` blocks, or optionally in negative with `d` coords, and `p` coords in positive values. `expr` -receives `_x, _y, _z` as coords of current analyzed block and `_`, which represents the block itself. +Evaluates expression over area of blocks defined by its center `center = (cx, cy, cz)`, expanded in all directions +by `range = (dx, dy, dz)` blocks, or optionally in negative with `range` coords, and `upper_range` coords in +positive values. +`center` can be defined either as a three coordinates, list of three coords, or block value. +`range` and `lower_range` can have the same representations, just if its a block, it computes the distance to the center +as range instead of taking the values as is. +`expr` receives `_x, _y, _z` as coords of current analyzed block and `_`, which represents the block itself. Returns number of successful evaluations of `expr` (with `true` boolean result) unless called in void context, which would cause the expression not be evaluated for their boolean value. @@ -2951,9 +2955,12 @@ scoreboard_add('counter') scoreboard_add('lvl','level') -### `scoreboard_remove(objective)` +### `scoreboard_remove(objective)` `scoreboard_remove(objective, key)` -Removes an objective. Returns `true` if objective has existed and has been removed. +Removes an entire objective, or an entry in the scoreboard associated with the key. +Returns `true` if objective has existed and has been removed, or previous +value of the scoreboard if players score is removed. Returns `null` if objective didn't exist, or a key was missing +for the objective. ### `scoreboard_display(place, objective)` diff --git a/docs/scarpet/api/Scoreboard.md b/docs/scarpet/api/Scoreboard.md index a509bb15..b638fb93 100644 --- a/docs/scarpet/api/Scoreboard.md +++ b/docs/scarpet/api/Scoreboard.md @@ -17,9 +17,12 @@ scoreboard_add('counter') scoreboard_add('lvl','level') -### `scoreboard_remove(objective)` +### `scoreboard_remove(objective)` `scoreboard_remove(objective, key)` -Removes an objective. Returns `true` if objective has existed and has been removed. +Removes an entire objective, or an entry in the scoreboard associated with the key. +Returns `true` if objective has existed and has been removed, or previous +value of the scoreboard if players score is removed. Returns `null` if objective didn't exist, or a key was missing +for the objective. ### `scoreboard_display(place, objective)` diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 30c3f3e4..3bf675f1 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -1296,6 +1296,26 @@ public class CarpetExpression } + private static String getScoreboardKeyFromValue(Value keyValue) + { + if (keyValue instanceof EntityValue) + { + Entity e = ((EntityValue) keyValue).getEntity(); + if (e instanceof PlayerEntity) + { + return e.getName().getString(); + } + else + { + return e.getUuidAsString(); + } + } + else + { + return keyValue.getString(); + } + } + private void API_Scoreboard() { // scoreboard(player,'objective') @@ -1317,24 +1337,7 @@ public class CarpetExpression Value ret = ListValue.wrap(scoreboard.getAllPlayerScores(objective).stream().map(s -> new StringValue(s.getPlayerName())).collect(Collectors.toList())); return (_c, _t) -> ret; } - String key; - Value keyValue = lv.get(1).evalValue(c); - if (keyValue instanceof EntityValue) - { - Entity e = ((EntityValue) keyValue).getEntity(); - if (e instanceof PlayerEntity) - { - key = e.getDisplayName().getString(); - } - else - { - key = e.getUuidAsString(); - } - } - else - { - key = keyValue.getString(); - } + String key = getScoreboardKeyFromValue(lv.get(1).evalValue(c)); if (!scoreboard.playerHasObjective(key, objective) && lv.size()==2) return LazyValue.NULL; ScoreboardPlayerScore scoreboardPlayerScore = scoreboard.getPlayerScore(key, objective); @@ -1346,15 +1349,24 @@ public class CarpetExpression return (_c, _t) -> retval; }); - this.expr.addLazyFunction("scoreboard_remove", 1, (c, t, lv)-> { + this.expr.addLazyFunction("scoreboard_remove", -1, (c, t, lv)-> { CarpetContext cc = (CarpetContext)c; Scoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard(); String objectiveName = lv.get(0).evalValue(c).getString(); ScoreboardObjective objective = scoreboard.getObjective(objectiveName); if (objective == null) return LazyValue.FALSE; - scoreboard.removeObjective(objective); - return LazyValue.TRUE; + if (lv.size() == 0) + { + scoreboard.removeObjective(objective); + return LazyValue.TRUE; + } + String key = getScoreboardKeyFromValue(lv.get(1).evalValue(c)); + if (!scoreboard.playerHasObjective(key, objective)) return LazyValue.NULL; + ScoreboardPlayerScore scoreboardPlayerScore = scoreboard.getPlayerScore(key, objective); + Value previous = new NumericValue(scoreboardPlayerScore.getScore()); + scoreboard.resetPlayerScore(key, objective); + return (c_, t_) -> previous; }); // objective_add('lvl','level') From 0b72f85474bb3dc49f9fc800233eea4fe93b03bb Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 28 Apr 2020 19:39:13 -0300 Subject: [PATCH 2/2] messenger can parse style --- src/main/java/carpet/utils/Messenger.java | 65 ++++++++++++----------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/main/java/carpet/utils/Messenger.java b/src/main/java/carpet/utils/Messenger.java index 3b422262..7604d13e 100644 --- a/src/main/java/carpet/utils/Messenger.java +++ b/src/main/java/carpet/utils/Messenger.java @@ -3,14 +3,15 @@ package carpet.utils; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.entity.EntityCategory; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; -import net.minecraft.text.LiteralText; -import net.minecraft.util.Formatting; import net.minecraft.text.BaseText; -import net.minecraft.server.MinecraftServer; import net.minecraft.text.ClickEvent; import net.minecraft.text.HoverEvent; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Style; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; +import net.minecraft.util.Formatting; +import net.minecraft.server.MinecraftServer; import net.minecraft.world.dimension.DimensionType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -52,32 +53,33 @@ public class Messenger / = action added to the previous component */ - private static BaseText _applyStyleToTextComponent(BaseText comp, String style) + public static Style parseStyle(String style) { //could be rewritten to be more efficient - comp.getStyle().setItalic(style.indexOf('i')>=0); - comp.getStyle().setStrikethrough(style.indexOf('s')>=0); - comp.getStyle().setUnderline(style.indexOf('u')>=0); - comp.getStyle().setBold(style.indexOf('b')>=0); - comp.getStyle().setObfuscated(style.indexOf('o')>=0); - comp.getStyle().setColor(Formatting.WHITE); - if (style.indexOf('w')>=0) comp.getStyle().setColor(Formatting.WHITE); // not needed - if (style.indexOf('y')>=0) comp.getStyle().setColor(Formatting.YELLOW); - if (style.indexOf('m')>=0) comp.getStyle().setColor(Formatting.LIGHT_PURPLE); - if (style.indexOf('r')>=0) comp.getStyle().setColor(Formatting.RED); - if (style.indexOf('c')>=0) comp.getStyle().setColor(Formatting.AQUA); - if (style.indexOf('l')>=0) comp.getStyle().setColor(Formatting.GREEN); - if (style.indexOf('t')>=0) comp.getStyle().setColor(Formatting.BLUE); - if (style.indexOf('f')>=0) comp.getStyle().setColor(Formatting.DARK_GRAY); - if (style.indexOf('g')>=0) comp.getStyle().setColor(Formatting.GRAY); - if (style.indexOf('d')>=0) comp.getStyle().setColor(Formatting.GOLD); - if (style.indexOf('p')>=0) comp.getStyle().setColor(Formatting.DARK_PURPLE); - if (style.indexOf('n')>=0) comp.getStyle().setColor(Formatting.DARK_RED); - if (style.indexOf('q')>=0) comp.getStyle().setColor(Formatting.DARK_AQUA); - if (style.indexOf('e')>=0) comp.getStyle().setColor(Formatting.DARK_GREEN); - if (style.indexOf('v')>=0) comp.getStyle().setColor(Formatting.DARK_BLUE); - if (style.indexOf('k')>=0) comp.getStyle().setColor(Formatting.BLACK); - return comp; + Style styleInstance = new Style(); + styleInstance.setItalic(style.indexOf('i')>=0); + styleInstance.setStrikethrough(style.indexOf('s')>=0); + styleInstance.setUnderline(style.indexOf('u')>=0); + styleInstance.setBold(style.indexOf('b')>=0); + styleInstance.setObfuscated(style.indexOf('o')>=0); + styleInstance.setColor(Formatting.WHITE); + if (style.indexOf('w')>=0) styleInstance.setColor(Formatting.WHITE); // not needed + if (style.indexOf('y')>=0) styleInstance.setColor(Formatting.YELLOW); + if (style.indexOf('m')>=0) styleInstance.setColor(Formatting.LIGHT_PURPLE); + if (style.indexOf('r')>=0) styleInstance.setColor(Formatting.RED); + if (style.indexOf('c')>=0) styleInstance.setColor(Formatting.AQUA); + if (style.indexOf('l')>=0) styleInstance.setColor(Formatting.GREEN); + if (style.indexOf('t')>=0) styleInstance.setColor(Formatting.BLUE); + if (style.indexOf('f')>=0) styleInstance.setColor(Formatting.DARK_GRAY); + if (style.indexOf('g')>=0) styleInstance.setColor(Formatting.GRAY); + if (style.indexOf('d')>=0) styleInstance.setColor(Formatting.GOLD); + if (style.indexOf('p')>=0) styleInstance.setColor(Formatting.DARK_PURPLE); + if (style.indexOf('n')>=0) styleInstance.setColor(Formatting.DARK_RED); + if (style.indexOf('q')>=0) styleInstance.setColor(Formatting.DARK_AQUA); + if (style.indexOf('e')>=0) styleInstance.setColor(Formatting.DARK_GREEN); + if (style.indexOf('v')>=0) styleInstance.setColor(Formatting.DARK_BLUE); + if (style.indexOf('k')>=0) styleInstance.setColor(Formatting.BLACK); + return styleInstance; } public static String heatmap_color(double actual, double reference) { @@ -143,7 +145,8 @@ public class Messenger return previous_message; } BaseText txt = new LiteralText(str); - return _applyStyleToTextComponent(txt, desc); + txt.setStyle(parseStyle(desc)); + return txt; } public static BaseText tp(String desc, Vec3d pos) { return tp(desc, pos.x, pos.y, pos.z); } public static BaseText tp(String desc, BlockPos pos) { return tp(desc, pos.getX(), pos.getY(), pos.getZ()); } @@ -263,7 +266,7 @@ public class Messenger public static BaseText s(String text, String style) { BaseText message = new LiteralText(text); - _applyStyleToTextComponent(message, style); + message.setStyle(parseStyle(style)); return message; }