Skip to content

Commit

Permalink
1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
erenkarakal committed Sep 18, 2024
1 parent d84a954 commit 94d93b7
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.eren</groupId>
<artifactId>SkCheese</artifactId>
<version>1.4.1</version>
<version>1.5</version>
<packaging>jar</packaging>

<name>SkCheese</name>
Expand Down Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>com.github.SkriptLang</groupId>
<artifactId>Skript</artifactId>
<version>2.7.0</version>
<version>2.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/me/eren/skcheese/elements/EffEval.java
Original file line number Diff line number Diff line change
@@ -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<String> linesExpression;
private Expression<CommandSender> senderExpression;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
linesExpression = (Expression<String>) exprs[0];
senderExpression = (Expression<CommandSender>) 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;
}

}
59 changes: 59 additions & 0 deletions src/main/java/me/eren/skcheese/elements/ExprNumber.java
Original file line number Diff line number Diff line change
@@ -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<Number> {

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<? extends Number> getReturnType() {
return Number.class;
}

@Override
public @NotNull String toString(Event event, boolean b) {
return (pattern == 0 ? "hexadecimal " : "binary ") + regex;
}


}
14 changes: 7 additions & 7 deletions src/main/java/me/eren/skcheese/elements/ExprParsedAs.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ public class ExprParsedAs extends SimpleExpression<Object> {
);
}

private Expression<ClassInfo<?>> classInfoExpr;
private Expression<String> toParseExpr;
private Expression<ClassInfo<?>> classInfoExpression;
private Expression<String> toParseExpression;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
classInfoExpr = (Expression<ClassInfo<?>>) exprs[1];
toParseExpr = (Expression<String>) exprs[0];
classInfoExpression = (Expression<ClassInfo<?>>) exprs[1];
toParseExpression = (Expression<String>) 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

Expand All @@ -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;
}
}
98 changes: 98 additions & 0 deletions src/main/java/me/eren/skcheese/elements/bits/ExprBit.java
Original file line number Diff line number Diff line change
@@ -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<Number> {

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<Integer> positionExpr;
private Expression<Number> numberExpr;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
positionExpr = (Expression<Integer>) exprs[0];
numberExpr = (Expression<Number>) 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<? extends Number> 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);
}

}
77 changes: 77 additions & 0 deletions src/main/java/me/eren/skcheese/elements/bits/ExprBitwise.java
Original file line number Diff line number Diff line change
@@ -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<Number> {

private Expression<Number> firstExpression;
private Expression<Number> 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<? extends Number> 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;
}

}
Loading

0 comments on commit 94d93b7

Please sign in to comment.