diff --git a/src/main/java/info/itsthesky/disky/elements/effects/MoveRole.java b/src/main/java/info/itsthesky/disky/elements/effects/MoveRole.java index 73606f09..0f119231 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/MoveRole.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/MoveRole.java @@ -1,6 +1,7 @@ package info.itsthesky.disky.elements.effects; import ch.njol.skript.Skript; +import ch.njol.skript.config.Node; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; @@ -9,6 +10,7 @@ import ch.njol.skript.util.AsyncEffect; import ch.njol.util.Kleenean; import info.itsthesky.disky.DiSky; +import info.itsthesky.disky.elements.sections.handler.DiSkyRuntimeHandler; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.requests.restaction.order.RoleOrderAction; import org.bukkit.event.Event; @@ -27,21 +29,36 @@ public class MoveRole extends AsyncEffect { Skript.registerEffect( MoveRole.class, "move [the] [discord] role %role% above [the] [discord] %role%", - "move [the] [discord] role %role% under [the] [discord] %role%" + "move [the] [discord] role %role% under [the] [discord] %role%", + "move [the] [discord] role %role% up [%-number% time[s]]", + "move [the] [discord] role %role% down [%-number% time[s]]" ); } + public enum MoveType { + ABOVE, UNDER, + UP, DOWN + } + private Expression exprTarget; + private Expression exprRole; - private boolean isAbove; + private Expression exprMod; + + private MoveType moveType; + private Node node; @Override public boolean init(Expression[] expressions, int i, Kleenean kleenean, ParseResult parseResult) { getParser().setHasDelayBefore(Kleenean.TRUE); + node = getParser().getNode(); exprTarget = (Expression) expressions[0]; - exprRole = (Expression) expressions[1]; - isAbove = i == 0; + moveType = MoveType.values()[i]; + if (moveType == MoveType.ABOVE || moveType == MoveType.UNDER) + exprRole = (Expression) expressions[1]; + else + exprMod = (Expression) expressions[1]; return true; } @@ -49,30 +66,47 @@ public boolean init(Expression[] expressions, int i, Kleenean kleenean, ParseRes @Override public void execute(@NotNull Event e) { final Role target = parseSingle(exprTarget, e); - final Role role = parseSingle(exprRole, e); - if (target == null || role == null) return; + final @Nullable Role role = parseSingle(exprRole, e); + final Number mod = parseSingle(exprMod, e, 1); + if (target == null) { + DiSkyRuntimeHandler.exprNotSet(node, exprTarget); + return; + } + + if (role == null && (moveType == MoveType.ABOVE || moveType == MoveType.UNDER)) { + DiSkyRuntimeHandler.exprNotSet(node, exprRole); + return; + } if (target.getGuild().getIdLong() != role.getGuild().getIdLong()) { - Skript.error("The specified roles are not in the same guild!"); + DiSkyRuntimeHandler.error(new IllegalArgumentException("The specified roles are not in the same guild! (first is from '"+target.getGuild().getName()+"' and the second is from '"+role.getGuild().getName()+"')"), node); + return; + } + + if (mod.intValue() < 1) { + DiSkyRuntimeHandler.error(new IllegalArgumentException("The modifier must be at least 1! Got: " + mod), node); return; } RoleOrderAction action = target.getGuild().modifyRolePositions(); - if (isAbove) - action = action.moveAbove(role); - else - action = action.moveBelow(role); + action = switch (moveType) { + case ABOVE -> action.moveAbove(role); + case UNDER -> action.moveBelow(role); + case UP -> action.moveUp(mod.intValue()); + case DOWN -> action.moveDown(mod.intValue()); + }; try { action.complete(); } catch (Exception ex) { - DiSky.getErrorHandler().exception(e, ex); - return; + DiSkyRuntimeHandler.error(ex, node); } } @Override public @NotNull String toString(@Nullable Event e, boolean debug) { - return "move role " + exprTarget.toString(e, debug) + " " + (isAbove ? "above" : "under") + " role " + exprRole.toString(e, debug); + if (moveType == MoveType.ABOVE || moveType == MoveType.UNDER) + return "move role " + exprTarget.toString(e, debug) + " " + moveType.name().toLowerCase() + " " + exprRole.toString(e, debug); + return "move role " + exprTarget.toString(e, debug) + " " + moveType.name().toLowerCase() + " " + exprMod.toString(e, debug); } }