Skip to content

Commit

Permalink
✨ Updated two expressions to be async changeable
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Jan 7, 2025
1 parent 8c3b62d commit ff5568e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.njol.skript.classes.Changer;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import info.itsthesky.disky.api.skript.EasyElement;
import info.itsthesky.disky.elements.changers.IAsyncChangeableExpression;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.attribute.IPositionableChannel;
import net.dv8tion.jda.api.requests.RestAction;
Expand All @@ -11,7 +12,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class RoleChannelPosition extends SimplePropertyExpression<Object, Number> {
public class RoleChannelPosition extends SimplePropertyExpression<Object, Number>
implements IAsyncChangeableExpression {

static {
register(
Expand Down Expand Up @@ -40,8 +42,17 @@ public class RoleChannelPosition extends SimplePropertyExpression<Object, Number
return new Class[0];
}

@Override
public void changeAsync(Event e, Object[] delta, Changer.ChangeMode mode) {
change(e, delta, mode, true);
}

@Override
public void change(@NotNull Event event, Object @NotNull [] delta, Changer.@NotNull ChangeMode mode) {
change(event, delta, mode, false);
}

public void change(@NotNull Event event, Object @NotNull [] delta, Changer.ChangeMode mode, boolean async) {
if (!EasyElement.isValid(delta))
return;

Expand Down Expand Up @@ -80,8 +91,10 @@ public void change(@NotNull Event event, Object @NotNull [] delta, Changer.@NotN
break;
}

if (action != null)
action.queue();
if (action != null) {
if (async) action.complete();
else action.queue();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@

package info.itsthesky.disky.elements.properties.tags;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import info.itsthesky.disky.DiSky;
import info.itsthesky.disky.api.skript.EasyElement;
import info.itsthesky.disky.api.skript.MultiplyPropertyExpression;
import info.itsthesky.disky.elements.changers.IAsyncChangeableExpression;
import info.itsthesky.disky.elements.sections.handler.DiSkyRuntimeHandler;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
Expand All @@ -17,6 +24,7 @@
import net.dv8tion.jda.api.requests.RestAction;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -32,7 +40,8 @@
"You must add the tag to the forum itself before adding it to the post."})
@Examples({"set {_tags::*} to tags of event-forumchannel",
"add new tag named \"resolved\" with reaction \"x\" to tags of forum with id \"000\""})
public class ThreadTags extends MultiplyPropertyExpression<Object, BaseForumTag> {
public class ThreadTags extends MultiplyPropertyExpression<Object, BaseForumTag>
implements IAsyncChangeableExpression {

static {
register(
Expand All @@ -43,6 +52,14 @@ public class ThreadTags extends MultiplyPropertyExpression<Object, BaseForumTag>
);
}

private Node node;

@Override
public boolean init(Expression<?> @NotNull [] expr, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {
node = getParser().getNode();
return super.init(expr, matchedPattern, isDelayed, parseResult);
}

@Override
public Class<?> @NotNull [] acceptChange(@NotNull Changer.ChangeMode mode) {
if (mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.REMOVE || mode == Changer.ChangeMode.ADD || mode == Changer.ChangeMode.RESET) {
Expand All @@ -55,7 +72,16 @@ public class ThreadTags extends MultiplyPropertyExpression<Object, BaseForumTag>
}

@Override
public void change(@NotNull Event e, @NotNull Object[] delta, @NotNull Changer.ChangeMode mode) {
public void changeAsync(Event e, Object[] delta, Changer.ChangeMode mode) {
change(e, delta, mode, true);
}

@Override
public void change(Event event, Object @Nullable [] delta, Changer.ChangeMode mode) {
change(event, delta, mode, false);
}

public void change(@NotNull Event e, @NotNull Object[] delta, @NotNull Changer.ChangeMode mode, boolean async) {
if (!EasyElement.isValid(delta))
return;

Expand All @@ -81,14 +107,14 @@ public void change(@NotNull Event e, @NotNull Object[] delta, @NotNull Changer.C
if (numeral.matches()) {
final ForumTag tag = channel.getAvailableTagById(input);
if (tag == null) {
Skript.warning("The tag with ID " + input + " doesn't exist in the channel " + channel.getName() + "!");
DiSkyRuntimeHandler.error(new IllegalArgumentException("The tag with ID " + input + " doesn't exist in the channel " + channel.getName() + "!"), node);
continue;
}
parsedTags.add(tag);
} else {
final List<ForumTag> tag = channel.getAvailableTagsByName(input, true);
if (tag.isEmpty()) {
Skript.warning("The tag with name " + input + " doesn't exist in the channel " + channel.getName() + "!");
DiSkyRuntimeHandler.error(new IllegalArgumentException("The tag with name " + input + " doesn't exist in the channel " + channel.getName() + "!"), node);
continue;
}
parsedTags.add(tag.get(0));
Expand Down Expand Up @@ -144,7 +170,8 @@ public void change(@NotNull Event e, @NotNull Object[] delta, @NotNull Changer.C
action = forumChannel.getManager().setAvailableTags(current);
}

action.queue(null, ex -> DiSky.getErrorHandler().exception(e, ex));
if (async) action.complete();
else action.queue();
}

@Override
Expand Down

0 comments on commit ff5568e

Please sign in to comment.