Skip to content

Commit

Permalink
✨ Added better checks in the move role effect, and added way to move …
Browse files Browse the repository at this point in the history
…a role for a given amount of time
  • Loading branch information
ItsTheSky committed Dec 29, 2024
1 parent 0935fad commit b8008e7
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions src/main/java/info/itsthesky/disky/elements/effects/MoveRole.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -27,52 +29,84 @@ 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<Role> exprTarget;

private Expression<Role> exprRole;
private boolean isAbove;
private Expression<Number> 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<Role>) expressions[0];
exprRole = (Expression<Role>) expressions[1];
isAbove = i == 0;
moveType = MoveType.values()[i];
if (moveType == MoveType.ABOVE || moveType == MoveType.UNDER)
exprRole = (Expression<Role>) expressions[1];
else
exprMod = (Expression<Number>) expressions[1];

return true;
}

@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);
}
}

0 comments on commit b8008e7

Please sign in to comment.