From 94d93b732226e92b65dbff9049465730f960516c Mon Sep 17 00:00:00 2001 From: Eren KARA Date: Wed, 18 Sep 2024 17:38:49 +0300 Subject: [PATCH] 1.5 --- pom.xml | 4 +- .../me/eren/skcheese/elements/EffEval.java | 52 ++++++++++ .../me/eren/skcheese/elements/ExprNumber.java | 59 +++++++++++ .../eren/skcheese/elements/ExprParsedAs.java | 14 +-- .../eren/skcheese/elements/bits/ExprBit.java | 98 +++++++++++++++++++ .../skcheese/elements/bits/ExprBitwise.java | 77 +++++++++++++++ .../skcheese/elements/bits/ExprShift.java | 75 ++++++++++++++ .../elements/pairs/ExprPairValue.java | 1 - .../skcheese/elements/switches/SecSwitch.java | 2 +- 9 files changed, 371 insertions(+), 11 deletions(-) create mode 100644 src/main/java/me/eren/skcheese/elements/EffEval.java create mode 100644 src/main/java/me/eren/skcheese/elements/ExprNumber.java create mode 100644 src/main/java/me/eren/skcheese/elements/bits/ExprBit.java create mode 100644 src/main/java/me/eren/skcheese/elements/bits/ExprBitwise.java create mode 100644 src/main/java/me/eren/skcheese/elements/bits/ExprShift.java diff --git a/pom.xml b/pom.xml index b4dcc92..0d10a69 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.eren SkCheese - 1.4.1 + 1.5 jar SkCheese @@ -78,7 +78,7 @@ com.github.SkriptLang Skript - 2.7.0 + 2.9.0 provided diff --git a/src/main/java/me/eren/skcheese/elements/EffEval.java b/src/main/java/me/eren/skcheese/elements/EffEval.java new file mode 100644 index 0000000..3547c72 --- /dev/null +++ b/src/main/java/me/eren/skcheese/elements/EffEval.java @@ -0,0 +1,52 @@ +package me.eren.skcheese.elements; + +import ch.njol.skript.Skript; +import ch.njol.skript.command.EffectCommandEvent; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.TriggerItem; +import ch.njol.skript.lang.parser.ParserInstance; +import ch.njol.util.Kleenean; +import me.eren.skcheese.SkCheese; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.event.Event; + +public class EffEval extends Effect { + + static { + if (SkCheese.isSyntaxEnabled("eval")) + Skript.registerEffect(EffEval.class, "eval[uate] %strings% [as %-commandsender%]"); + } + + private Expression linesExpression; + private Expression senderExpression; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + linesExpression = (Expression) exprs[0]; + senderExpression = (Expression) exprs[1]; + return true; + } + + @Override + protected void execute(Event event) { + CommandSender sender = this.senderExpression != null ? this.senderExpression.getSingle(event) : Bukkit.getConsoleSender(); + for (String line : linesExpression.getArray(event)) { + ParserInstance parserInstance = ParserInstance.get(); + parserInstance.setCurrentEvent("effect command", EffectCommandEvent.class); + Effect effect = Effect.parse(line, null); + parserInstance.deleteCurrentEvent(); + if (effect != null) { + TriggerItem.walk(effect, new EffectCommandEvent(sender, line)); + } + } + } + + @Override + public String toString(Event e, boolean d) { + return "evaluate '" + linesExpression + "' as " + senderExpression; + } + +} diff --git a/src/main/java/me/eren/skcheese/elements/ExprNumber.java b/src/main/java/me/eren/skcheese/elements/ExprNumber.java new file mode 100644 index 0000000..94066e2 --- /dev/null +++ b/src/main/java/me/eren/skcheese/elements/ExprNumber.java @@ -0,0 +1,59 @@ +package me.eren.skcheese.elements; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import me.eren.skcheese.SkCheese; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; + +public class ExprNumber extends SimpleExpression { + + private String regex; + private int pattern; + + static { + if (SkCheese.isSyntaxEnabled("binary-and-hex-numbers")) + Skript.registerExpression( + ExprNumber.class, + Number.class, + ExpressionType.SIMPLE, + "0(x|X)<[A-Fa-f0-9]+>", + "0(b|B)<[0-1]+>" + ); + } + + @Override + public boolean init(Expression @NotNull [] expressions, int i, @NotNull Kleenean kleenean, SkriptParser.@NotNull ParseResult parseResult) { + pattern = i; + regex = parseResult.regexes.get(0).group(); + return true; + } + + @Override + protected Number @NotNull [] get(@NotNull Event event) { + if (regex == null) return new Number[0]; + Number result = Integer.parseInt(regex, pattern == 0 ? 16 : 2); + return new Number[]{ result }; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public @NotNull Class getReturnType() { + return Number.class; + } + + @Override + public @NotNull String toString(Event event, boolean b) { + return (pattern == 0 ? "hexadecimal " : "binary ") + regex; + } + + +} \ No newline at end of file diff --git a/src/main/java/me/eren/skcheese/elements/ExprParsedAs.java b/src/main/java/me/eren/skcheese/elements/ExprParsedAs.java index f0b9791..a941fa3 100644 --- a/src/main/java/me/eren/skcheese/elements/ExprParsedAs.java +++ b/src/main/java/me/eren/skcheese/elements/ExprParsedAs.java @@ -38,20 +38,20 @@ public class ExprParsedAs extends SimpleExpression { ); } - private Expression> classInfoExpr; - private Expression toParseExpr; + private Expression> classInfoExpression; + private Expression toParseExpression; @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - classInfoExpr = (Expression>) exprs[1]; - toParseExpr = (Expression) exprs[0]; + classInfoExpression = (Expression>) exprs[1]; + toParseExpression = (Expression) exprs[0]; return true; } @Override protected Object[] get(Event e) { - ClassInfo classInfo = classInfoExpr.getSingle(e); - String toParse = toParseExpr.getSingle(e); + ClassInfo classInfo = classInfoExpression.getSingle(e); + String toParse = toParseExpression.getSingle(e); if (classInfo == null || toParse == null) return null; if (classInfo.getC() == String.class) return new Object[]{ toParse }; // because parsing as string returns null @@ -71,6 +71,6 @@ public Class getReturnType() { @Override public String toString(Event e, boolean debug) { - return toParseExpr + " parsed as type " + classInfoExpr; + return toParseExpression + " parsed as type " + classInfoExpression; } } diff --git a/src/main/java/me/eren/skcheese/elements/bits/ExprBit.java b/src/main/java/me/eren/skcheese/elements/bits/ExprBit.java new file mode 100644 index 0000000..666e6c8 --- /dev/null +++ b/src/main/java/me/eren/skcheese/elements/bits/ExprBit.java @@ -0,0 +1,98 @@ +package me.eren.skcheese.elements.bits; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; +import me.eren.skcheese.SkCheese; +import org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; + +public class ExprBit extends SimpleExpression { + + static { + if (SkCheese.isSyntaxEnabled("bit-operations")) + Skript.registerExpression(ExprBit.class, Number.class, ExpressionType.COMBINED, + "[the] %integer%(st|nd|rd|th) bit (in|of) %numbers%", + "bit %integer% (in|of) %numbers%"); + } + + private Expression positionExpr; + private Expression numberExpr; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + positionExpr = (Expression) exprs[0]; + numberExpr = (Expression) exprs[1]; + return true; + } + + @Override + protected @Nullable Number[] get(Event e) { + Integer integer = positionExpr.getSingle(e); + if (integer == null) return null; + int pos = integer; + Number[] numbers = numberExpr.getArray(e); + Number[] bits = new Number[numbers.length]; + for (int i = 0; i < numbers.length; i++) { + long num = numbers[i].longValue(); + num >>= (pos - 1); + num &= 1; + bits[i] = num; + } + return bits; + } + + @Override + public boolean isSingle() { + return numberExpr.isSingle(); + } + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + return "bit " + positionExpr.toString(e, debug) + " of " + numberExpr.toString(e, debug); + } + + @Override + public @Nullable Class[] acceptChange(Changer.ChangeMode mode) { + if (mode == Changer.ChangeMode.SET) + return CollectionUtils.array(Boolean.class, Number.class); + return null; + } + + @Override + public void change(Event e, @Nullable Object[] delta, Changer.ChangeMode mode) { + if (delta[0] == null) return; + Integer integer = positionExpr.getSingle(e); + if (integer == null) return; + int pos = integer; + Number[] numbers = numberExpr.getArray(e); + int x; + if (delta[0] instanceof Boolean bool) + x = bool ? 1 : 0; + else x = ((Number) delta[0]).intValue(); + if (x != 1 && x != 0) return; + for (int i = 0; i < numbers.length; i++) { + long num = 1L << (pos - 1); + if (x == 0) { + num = ~num; + num &= numbers[i].longValue(); + } + else { + num |= numbers[i].longValue(); + } + numbers[i] = num; + } + numberExpr.change(e, numbers, Changer.ChangeMode.SET); + } + +} \ No newline at end of file diff --git a/src/main/java/me/eren/skcheese/elements/bits/ExprBitwise.java b/src/main/java/me/eren/skcheese/elements/bits/ExprBitwise.java new file mode 100644 index 0000000..89d0013 --- /dev/null +++ b/src/main/java/me/eren/skcheese/elements/bits/ExprBitwise.java @@ -0,0 +1,77 @@ +package me.eren.skcheese.elements.bits; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.util.LiteralUtils; +import ch.njol.util.Kleenean; +import me.eren.skcheese.SkCheese; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; + +public class ExprBitwise extends SimpleExpression { + + private Expression firstExpression; + private Expression secondExpression; + + private int pattern; + + static { + if (SkCheese.isSyntaxEnabled("bit-operations")) + Skript.registerExpression( + ExprBitwise.class, Number.class, ExpressionType.COMBINED, + "%number% \\& %number%", + "%number% \\| %number%", + "%number% \\^\\^ %number%", + "\\~%number%" + ); + } + + @Override + protected Number @NotNull [] get(@NotNull Event event) { + Number first = this.firstExpression.getSingle(event); + Number second = this.secondExpression != null ? this.secondExpression.getSingle(event) : null; + if (first == null) first = 0; + if (second == null) second = 0; + return switch (pattern) { + case 0 -> new Number[]{ first.intValue() & second.intValue() }; + case 1 -> new Number[]{ first.intValue() | second.intValue() }; + case 2 -> new Number[]{ first.intValue() ^ second.intValue() }; + case 3 -> new Number[]{ ~first.intValue() }; + default -> new Number[0]; + }; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public @NotNull Class getReturnType() { + return Number.class; + } + + @Override + public @NotNull String toString(Event event, boolean b) { + return switch (pattern) { + case 0 -> "bitwise AND"; + case 1 -> "bitwise inclusive OR"; + case 2 -> "bitwise exclusive OR"; + case 3 -> "Unary bitwise complement"; + default -> "bitwise operator"; + }; + } + + @Override + public boolean init(Expression @NotNull [] expressions, int i, @NotNull Kleenean kleenean, SkriptParser.@NotNull ParseResult parseResult) { + pattern = i; + firstExpression = LiteralUtils.defendExpression(expressions[0]); + if(pattern != 3) + secondExpression = LiteralUtils.defendExpression(expressions[1]); + return true; + } + +} \ No newline at end of file diff --git a/src/main/java/me/eren/skcheese/elements/bits/ExprShift.java b/src/main/java/me/eren/skcheese/elements/bits/ExprShift.java new file mode 100644 index 0000000..9b7ca09 --- /dev/null +++ b/src/main/java/me/eren/skcheese/elements/bits/ExprShift.java @@ -0,0 +1,75 @@ +package me.eren.skcheese.elements.bits; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.util.LiteralUtils; +import ch.njol.util.Kleenean; +import me.eren.skcheese.SkCheese; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; + +public class ExprShift extends SimpleExpression { + + private Expression firstExpression; + private Expression secondExpression; + + private int pattern; + + static { + if (SkCheese.isSyntaxEnabled("bit-operations")) + Skript.registerExpression( + ExprShift.class, + Number.class, + ExpressionType.COMBINED, + "%number% \\<\\< %number%", + "%number% \\>\\> %number%", + "%number% \\>\\>\\> %number%" + ); + } + + @Override + public boolean init(Expression @NotNull [] expressions, int i, @NotNull Kleenean kleenean, SkriptParser.@NotNull ParseResult parseResult) { + pattern = i; + firstExpression = LiteralUtils.defendExpression(expressions[0]); + secondExpression = LiteralUtils.defendExpression(expressions[1]); + return true; + } + + @Override + protected Number @NotNull [] get(@NotNull Event event) { + Number first = this.firstExpression.getSingle(event); + Number second = this.secondExpression != null ? this.secondExpression.getSingle(event) : null; + if (first == null) first = 0; + if (second == null) second = 0; + return switch (pattern) { + case 0 -> new Number[]{ first.intValue() << second.intValue() }; + case 1 -> new Number[]{ first.intValue() >> second.intValue() }; + case 2 -> new Number[]{ first.intValue() >>> second.intValue() }; + default -> new Number[0]; + }; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public @NotNull Class getReturnType() { + return Number.class; + } + + @Override + public @NotNull String toString(Event event, boolean b) { + return switch (pattern) { + case 0 -> "Signed left shift"; + case 1 -> "Signed right shift"; + case 2 -> "Unsigned right shift"; + default -> "shift operator"; + }; + } + +} diff --git a/src/main/java/me/eren/skcheese/elements/pairs/ExprPairValue.java b/src/main/java/me/eren/skcheese/elements/pairs/ExprPairValue.java index a83fafb..8df865a 100644 --- a/src/main/java/me/eren/skcheese/elements/pairs/ExprPairValue.java +++ b/src/main/java/me/eren/skcheese/elements/pairs/ExprPairValue.java @@ -56,7 +56,6 @@ public Class getReturnType() { return Pair.class; } - @Override public String toString(Event e, boolean debug) { return (isFirst ? "first" : "second") + "value of " + pairExpr; diff --git a/src/main/java/me/eren/skcheese/elements/switches/SecSwitch.java b/src/main/java/me/eren/skcheese/elements/switches/SecSwitch.java index 392980e..c2b4f7b 100644 --- a/src/main/java/me/eren/skcheese/elements/switches/SecSwitch.java +++ b/src/main/java/me/eren/skcheese/elements/switches/SecSwitch.java @@ -106,7 +106,7 @@ public boolean init(Expression[] expressions, int matchedPattern, @NotNull Kl if (found == null) found = defaultCase; - found.trigger().execute(event); + TriggerItem.walk(found.trigger(), event); return getNext(); }