From 8e785c04cb34dc6b28095268c5dd6222c920d8f1 Mon Sep 17 00:00:00 2001 From: Richard Date: Sat, 23 Jan 2016 00:18:13 -0200 Subject: [PATCH] New timespan conversion expression, goddamn targetInvocationExceptions. Might have to debug a little bit to figure out how to let just the warning appear when there's a parse error. --- plugin.yml | 2 +- src/me/TheBukor/SkStuff/SkStuff.java | 4 +- .../SkStuff/expressions/ExprFileNBT.java | 3 + .../SkStuff/expressions/ExprItemNBT.java | 1 + .../SkStuff/expressions/ExprNBTOf.java | 3 + .../expressions/ExprTimespanToNumber.java | 77 +++++++++++++++++++ 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/me/TheBukor/SkStuff/expressions/ExprTimespanToNumber.java diff --git a/plugin.yml b/plugin.yml index 9b0c5f9..32b39e4 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: SkStuff author: TheBukor description: A Skript addon which adds extra functionalities such as NBT and extended WorldEdit support. -version: 1.4.1.2 +version: 1.4.1.3 main: me.TheBukor.SkStuff.SkStuff softdepend: [Skript, WorldEdit] \ No newline at end of file diff --git a/src/me/TheBukor/SkStuff/SkStuff.java b/src/me/TheBukor/SkStuff/SkStuff.java index ea74e4f..b9c9436 100644 --- a/src/me/TheBukor/SkStuff/SkStuff.java +++ b/src/me/TheBukor/SkStuff/SkStuff.java @@ -53,6 +53,7 @@ import me.TheBukor.SkStuff.expressions.ExprSelectionArea; import me.TheBukor.SkStuff.expressions.ExprSelectionOfPlayer; import me.TheBukor.SkStuff.expressions.ExprSelectionPos; import me.TheBukor.SkStuff.expressions.ExprTagOf; +import me.TheBukor.SkStuff.expressions.ExprTimespanToNumber; import me.TheBukor.SkStuff.expressions.ExprToLowerCase; import me.TheBukor.SkStuff.expressions.ExprToUpperCase; import me.TheBukor.SkStuff.expressions.ExprVanishState; @@ -79,7 +80,8 @@ public class SkStuff extends JavaPlugin { Skript.registerExpression(ExprToUpperCase.class, String.class, ExpressionType.SIMPLE, "%string% [converted] to [all] (cap[ital]s|upper[ ]case)", "convert %string% to [all] (cap[ital]s|upper[ ]case)", "capitalize [all] [char[acter]s (of|in)] %string%"); Skript.registerExpression(ExprToLowerCase.class, String.class, ExpressionType.SIMPLE, "%string% [converted] to [all] lower[ ]case", "convert %string% to [all] lower[ ]case", "un[( |-)]capitalize [all] [char[acter]s (of|in)] %string%"); Skript.registerExpression(ExprWordsToUpperCase.class, String.class, ExpressionType.SIMPLE, "(first|1st) (letter|char[acter]) (of|in) (each word|[all] words) (of|in) %string% [converted] to (cap[ital]s|upper[ ]case) (0¦|1¦ignoring [other] upper[ ]case [(char[acter]s|letters)])", "convert (first|1st) (letter|char[acter]) (of|in) (each word|[all] words) (of|in) %string% to (cap[ital]s|upper[ ]case) (0¦|1¦ignoring [other] upper[ ]case [(char[acter]s|letters)])", "capitalize (first|1st) (letter|char[acter]) (of|in) (each word|[all] words) (of|in) %string% (0¦|1¦ignoring [other] upper[ ]case [(char[acter]s|letters)])"); - exprAmount += 3; + Skript.registerExpression(ExprTimespanToNumber.class, Number.class, ExpressionType.SIMPLE, "%timespan% [converted] [in]to (0¦ticks|1¦sec[ond]s|2¦min[ute]s|3¦hours|4¦days)"); + exprAmount += 4; getLogger().info("Trying to register version specific stuff..."); Skript.registerExpression(ExprNBTOf.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]"); diff --git a/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java b/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java index 0f435a1..4570dd3 100644 --- a/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java +++ b/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java @@ -77,6 +77,7 @@ public class ExprFileNBT extends SimpleExpression { if (ex instanceof InvocationTargetException) { if (ex.getCause().getClass().getName().equals("MojangsonParseException") ) { Skript.error("Error when parsing NBT - " + ex.getCause().getMessage()); + return null; } ex.printStackTrace(); } @@ -121,6 +122,7 @@ public class ExprFileNBT extends SimpleExpression { if (ex instanceof InvocationTargetException) { if (ex.getCause().getClass().getName().equals("MojangsonParseException") ) { Skript.error("Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } @@ -148,6 +150,7 @@ public class ExprFileNBT extends SimpleExpression { if (ex instanceof InvocationTargetException) { if (ex.getCause().getClass().getName().equals("MojangsonParseException") ) { Skript.error("Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } else if (ex instanceof EOFException) { diff --git a/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java b/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java index bae0859..feb8600 100644 --- a/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java +++ b/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java @@ -71,6 +71,7 @@ public class ExprItemNBT extends SimpleExpression { } catch (Exception ex) { if (ex.getCause().getClass().getName().equals("MojangsonParseException") ) { Skript.warning(ChatColor.RED + "Error when parsing NBT - " + ex.getMessage()); + return null; } } Object newItem = null; diff --git a/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java b/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java index 3659516..cc21f50 100644 --- a/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java +++ b/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java @@ -149,6 +149,7 @@ public class ExprNBTOf extends SimpleExpression { if (ex instanceof InvocationTargetException) { if (ex.getCause().getClass().getName().equals("MojangsonParseException") ) { Skript.error("Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } @@ -207,6 +208,7 @@ public class ExprNBTOf extends SimpleExpression { if (ex instanceof InvocationTargetException) { if (ex.getCause().getClass().getName().equals("MojangsonParseException") ) { Skript.error("Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } @@ -273,6 +275,7 @@ public class ExprNBTOf extends SimpleExpression { if (ex instanceof InvocationTargetException) { if (ex.getCause().getClass().getName().equals("MojangsonParseException") ) { Skript.error("Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } diff --git a/src/me/TheBukor/SkStuff/expressions/ExprTimespanToNumber.java b/src/me/TheBukor/SkStuff/expressions/ExprTimespanToNumber.java new file mode 100644 index 0000000..fc6ac57 --- /dev/null +++ b/src/me/TheBukor/SkStuff/expressions/ExprTimespanToNumber.java @@ -0,0 +1,77 @@ +package me.TheBukor.SkStuff.expressions; + +import javax.annotation.Nullable; + +import org.bukkit.event.Event; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.util.Timespan; +import ch.njol.util.Kleenean; + +public class ExprTimespanToNumber extends SimpleExpression { + private Expression time; + private String toStringMark; + private int mark; + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + public boolean isSingle() { + return true; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int matchedPattern, Kleenean arg2, ParseResult result) { + time = (Expression) expr[0]; + mark = result.mark; + if (result.mark == 0) { + toStringMark = "ticks"; + } else if (result.mark == 1) { + toStringMark = "seconds"; + } else if (result.mark == 2) { + toStringMark = "minutes"; + } else if (result.mark == 3) { + toStringMark = "hours"; + } else if (result.mark == 4) { + toStringMark = "days"; + } + return true; + } + + @Override + public String toString(@Nullable Event e, boolean arg1) { + return time.toString(e, false) + "converted to " + toStringMark; + } + + @SuppressWarnings("deprecation") + @Override + @Nullable + protected Number[] get(Event e) { + Timespan t = time.getSingle(e); + Number ticks = null; + if (Skript.methodExists(Timespan.class, "getTicks_i")) { //Compatibility with Mirreducki's Skript patch 24+ days timespans. + ticks = t.getTicks_i(); + } else { //Standard Skript timespans, limited to roughly 24 days. + ticks = t.getTicks(); + } + if (mark == 0) { + return new Number[] { ticks }; + } else if (mark == 1) { + return new Number[] { ticks.longValue() / 20 }; + } else if (mark == 2) { + return new Number[] { ticks.longValue() / 20 / 60 }; + } else if (mark == 3) { + return new Number[] { ticks.longValue() / 20 / 60 / 60 }; + } else if (mark == 4) { + return new Number[] { ticks.longValue() / 20 / 60 / 60 / 24 }; + } + return null; + } +} \ No newline at end of file