Code cleanup

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

View File

@ -1,13 +1,17 @@
package com.btk5h.skriptdb; 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.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional; 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 { public class SkriptUtil {
private static final Field STRING; 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() { private static Optional<Field> tryGetOldStringField() {
try { try {
Field f = VariableString.class.getDeclaredField("string"); Field f = VariableString.class.getDeclaredField("string");

View File

@ -4,32 +4,26 @@ import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class SQLQueryCompleteEvent extends Event { public class SQLQueryCompleteEvent extends Event {
private final static HandlerList HANDLERS = new HandlerList();
private static final HandlerList HANDLERS = new HandlerList();
private final String argument; private final String argument;
public SQLQueryCompleteEvent(String argument) { public SQLQueryCompleteEvent(String argument) {
super(true); super(true);
this.argument = argument; this.argument = argument;
// this.variables = variables;
}
public static HandlerList getHandlerList() {
return HANDLERS;
}
@Override
public String getEventName() {
return super.getEventName();
} }
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return HANDLERS; return getHandlerList();
} }
public String getQuery() { public String getQuery() {
return argument; 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 { static {
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class, Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " + ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% "
"[with [a] [max[imum]] [connection] life[ ]time of %-timespan%] " + "[[(using|with)] [a] driver %-string%]"); + "[with [a] [max[imum]] [connection] life[ ]time of %-timespan%] "
+ "[[(using|with)] [a] driver %-string%]");
} }
private Expression<String> url; private Expression<String> url;
@ -100,8 +101,7 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
SkriptParser.ParseResult parseResult) {
url = (Expression<String>) exprs[0]; url = (Expression<String>) exprs[0];
maxLifetime = (Expression<Timespan>) exprs[1]; maxLifetime = (Expression<Timespan>) exprs[1];
driver = (Expression<String>) exprs[2]; driver = (Expression<String>) exprs[2];

View File

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

View File

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