Code cleanup

This commit is contained in:
szumielxd 2024-07-24 16:01:44 +02:00
parent 41881dbb7d
commit 0670f88a4e
Signed by untrusted user: szumielxd
GPG Key ID: 5D0CCFC4CB110524
5 changed files with 37 additions and 27 deletions

View File

@ -1,13 +1,17 @@
package com.btk5h.skriptdb;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.VariableString;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Optional;
import org.bukkit.event.Event;
import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.lang.parser.ParserInstance;
public class SkriptUtil {
private static final Field STRING;
@ -56,6 +60,17 @@ public class SkriptUtil {
}
}
@SuppressWarnings("deprecation")
public static boolean isCurrentEvent(Class<? extends Event> event) {
try {
Class.forName("ch.njol.skript.lang.parser.ParserInstance");
return ParserInstance.get().isCurrentEvent(event);
} catch (ClassNotFoundException e) {
return ScriptLoader.isCurrentEvent(event);
}
}
private static Optional<Field> tryGetOldStringField() {
try {
Field f = VariableString.class.getDeclaredField("string");

View File

@ -4,32 +4,26 @@ import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class SQLQueryCompleteEvent extends Event {
private final static HandlerList HANDLERS = new HandlerList();
private static final HandlerList HANDLERS = new HandlerList();
private final String argument;
public SQLQueryCompleteEvent(String argument) {
super(true);
this.argument = argument;
// this.variables = variables;
}
public static HandlerList getHandlerList() {
return HANDLERS;
}
@Override
public String getEventName() {
return super.getEventName();
}
@Override
public HandlerList getHandlers() {
return HANDLERS;
return getHandlerList();
}
public String getQuery() {
return argument;
}
// public String getVariables() {return;}
public static HandlerList getHandlerList() {
return HANDLERS;
}
}

View File

@ -32,8 +32,9 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
static {
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
"[with [a] [max[imum]] [connection] life[ ]time of %-timespan%] " + "[[(using|with)] [a] driver %-string%]");
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% "
+ "[with [a] [max[imum]] [connection] life[ ]time of %-timespan%] "
+ "[[(using|with)] [a] driver %-string%]");
}
private Expression<String> url;
@ -100,8 +101,7 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
SkriptParser.ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
url = (Expression<String>) exprs[0];
maxLifetime = (Expression<Timespan>) exprs[1];
driver = (Expression<String>) exprs[2];

View File

@ -1,6 +1,10 @@
package com.btk5h.skriptdb.skript;
import ch.njol.skript.ScriptLoader;
import org.bukkit.event.Event;
import com.btk5h.skriptdb.SkriptUtil;
import com.btk5h.skriptdb.events.SQLQueryCompleteEvent;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
@ -8,8 +12,6 @@ import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.util.Kleenean;
import com.btk5h.skriptdb.events.SQLQueryCompleteEvent;
import org.bukkit.event.Event;
/**
* Stores the error from the last executed statement, if there was one.
@ -51,7 +53,7 @@ public class ExprSQLQuery extends SimpleExpression<String> {
@Override
public boolean init(final Expression<?>[] expressions, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) {
if (!ScriptLoader.isCurrentEvent(SQLQueryCompleteEvent.class)) {
if (!SkriptUtil.isCurrentEvent(SQLQueryCompleteEvent.class)) {
Skript.error("Cannot use 'sql query' outside of a complete of sql query event", ErrorQuality.SEMANTIC_ERROR);
return false;
}

View File

@ -53,8 +53,7 @@ public class ExprUnsafe extends SimpleExpression<String> {
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
SkriptParser.ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
stringExpression = (Expression<String>) exprs[0];
rawExpression = parseResult.expr.substring("unsafe".length()).trim();
return true;