Skip to content

Commit

Permalink
A bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Oct 25, 2023
1 parent ee6274c commit 149b23d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/neoforged/camelot/BotMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public static void main(String[] args) {
for (final long toUnban : users) {
// We do not use allOf because we do not want a deleted user to cause all unbans to fail
guild.unban(UserSnowflake.fromId(toUnban)).reason("rec: Ban expired")
.queue(suc -> {} /* don't remove the entry here, the ModerationActionRecorder should, and if it doesn't, the unban failed so it should be reattempted next minute */, new ErrorHandler()
.handle(ErrorResponse.UNKNOWN_USER, e -> db.delete(toUnban, guild.getIdLong()))); // User doesn't exist, so don't care about the unban anymore
.queue(_ -> {} /* don't remove the entry here, the ModerationActionRecorder should, and if it doesn't, the unban failed so it should be reattempted next minute */, new ErrorHandler()
.handle(ErrorResponse.UNKNOWN_USER, _ -> db.delete(toUnban, guild.getIdLong()))); // User doesn't exist, so don't care about the unban anymore
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,27 @@ protected String getComponentId(Object... arguments) {

@Override
public void onEvent(@NotNull GenericEvent gevent) {
if (gevent instanceof GenericComponentInteractionCreateEvent event) {
if (event.getComponentId().startsWith(baseComponentId)) {
final String[] arguments = computeArgs(event.getComponentId(), baseComponentId);
switch (event.getComponentType()) {
case BUTTON -> onButton((ButtonInteractionEvent) event, arguments);
case STRING_SELECT -> onStringSelect((StringSelectInteractionEvent) event, arguments);
case USER_SELECT, CHANNEL_SELECT, ROLE_SELECT, MENTIONABLE_SELECT ->
onEntitySelect((EntitySelectInteractionEvent) event, arguments);
switch (gevent) {
case GenericComponentInteractionCreateEvent event -> {
if (event.getComponentId().startsWith(baseComponentId)) {
final String[] arguments = computeArgs(event.getComponentId(), baseComponentId);
switch (event.getComponentType()) {
case BUTTON -> onButton((ButtonInteractionEvent) event, arguments);
case STRING_SELECT -> onStringSelect((StringSelectInteractionEvent) event, arguments);
case USER_SELECT, CHANNEL_SELECT, ROLE_SELECT, MENTIONABLE_SELECT ->
onEntitySelect((EntitySelectInteractionEvent) event, arguments);
}
}
}
} else if (gevent instanceof ModalInteractionEvent event) {
if (event.getModalId().startsWith(baseComponentId)) {
final String[] arguments = computeArgs(event.getModalId(), baseComponentId);
onModal(event, arguments);

case ModalInteractionEvent event -> {
if (event.getModalId().startsWith(baseComponentId)) {
final String[] arguments = computeArgs(event.getModalId(), baseComponentId);
onModal(event, arguments);
}
}

default -> {}
}
}

Expand Down
32 changes: 19 additions & 13 deletions src/main/java/net/neoforged/camelot/module/TricksModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,27 @@ public void registerCommands(CommandClientBuilder builder) {
public void registerListeners(JDABuilder builder) {
builder.addEventListeners((EventListener) EvalCommand::onEvent)
.addEventListeners((EventListener) gevent -> {
if (gevent instanceof GuildReadyEvent event) {
if (slashTrickManagers.containsKey(event.getGuild().getIdLong())) return;
switch (gevent) {
case GuildReadyEvent event -> {
if (slashTrickManagers.containsKey(event.getGuild().getIdLong())) return;

final SlashTrickManager manager = new SlashTrickManager(
event.getGuild().getIdLong(), Database.main().onDemand(SlashTricksDAO.class), Database.main().onDemand(TricksDAO.class)
);
manager.updateCommands(event.getGuild());
event.getJDA().addEventListener(manager);
slashTrickManagers.put(event.getGuild().getIdLong(), manager);
} else if (gevent instanceof GuildLeaveEvent event) {
final SlashTrickManager trickManager = slashTrickManagers.get(event.getGuild().getIdLong());
if (trickManager == null) return;
final SlashTrickManager manager = new SlashTrickManager(
event.getGuild().getIdLong(), Database.main().onDemand(SlashTricksDAO.class), Database.main().onDemand(TricksDAO.class)
);
manager.updateCommands(event.getGuild());
event.getJDA().addEventListener(manager);
slashTrickManagers.put(event.getGuild().getIdLong(), manager);
}

event.getJDA().removeEventListener(trickManager);
slashTrickManagers.remove(event.getGuild().getIdLong());
case GuildLeaveEvent event -> {
final SlashTrickManager trickManager = slashTrickManagers.get(event.getGuild().getIdLong());
if (trickManager == null) return;

event.getJDA().removeEventListener(trickManager);
slashTrickManagers.remove(event.getGuild().getIdLong());
}

default -> {}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Set;

public class ScriptFileSystem extends FileSystem {
private final ScriptPath root = new ScriptPath(this, "/");
private final ScriptPath root = ScriptPath.path(this, "/");
private final FileSystemProvider provider;

public ScriptFileSystem(FileSystemProvider provider) {
Expand Down Expand Up @@ -73,9 +73,9 @@ public Path getPath(@NotNull String first, @NotNull String... more) {
final String[] args = new String[more.length + 1];
args[0] = first;
System.arraycopy(more, 0, args, 1, more.length);
return new ScriptPath(this, args);
return ScriptPath.path(this, args);
}
return new ScriptPath(this, first);
return ScriptPath.path(this, first);
}

@Override
Expand Down
115 changes: 61 additions & 54 deletions src/main/java/net/neoforged/camelot/script/fs/ScriptPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,51 @@
import java.util.Deque;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.stream.IntStream;

@ParametersAreNonnullByDefault
public final class ScriptPath implements Path {
private final ScriptFileSystem fileSystem;
private final boolean absolute;
private final String[] pathParts;
public record ScriptPath(ScriptFileSystem fileSystem, boolean absolute, String[] pathParts,
Function<ScriptPath, ScriptPath> normalized /* lazily-compute the normalized path */) implements Path {

// Store the normalized path after it has been created first
private ScriptPath normalized;
public static final Function<ScriptPath, ScriptPath> NORMALIZE_SELF = p -> p;
private static Function<ScriptPath, ScriptPath> normalizeCompute() {
return new Function<>() {
ScriptPath normalized;

ScriptPath(final ScriptFileSystem fileSystem, final String... pathParts) {
this.fileSystem = fileSystem;
@Override
public ScriptPath apply(ScriptPath paths) {
if (normalized != null) return normalized;

final Deque<String> normalizedPath = new ArrayDeque<>();
for (final String pathPart : paths.pathParts) {
switch (pathPart) {
case "." -> {
}
case ".." -> {
if (normalizedPath.isEmpty() || normalizedPath.getLast().equals("..")) {
// .. on an empty path is allowed, so keep it
normalizedPath.addLast(pathPart);
} else {
normalizedPath.removeLast();
}
}
default -> normalizedPath.addLast(pathPart);
}
}
normalized = new ScriptPath(paths.fileSystem, paths.absolute, true, normalizedPath.toArray(new String[0]));
return normalized;
}
};
}

static ScriptPath path(final ScriptFileSystem fileSystem, final String... pathParts) {
final boolean absolute;
final String[] pp;
if (pathParts.length == 0) {
this.absolute = false;
this.pathParts = new String[0];
absolute = false;
pp = new String[0];
} else {
StringBuilder joiner = new StringBuilder();
for (int i = 0; i < pathParts.length; i++) {
Expand All @@ -42,10 +70,10 @@ public final class ScriptPath implements Path {
}
}
final var longstring = joiner.toString();
this.absolute = longstring.startsWith("/");
this.pathParts = getPathParts(longstring);
absolute = longstring.startsWith("/");
pp = getPathParts(longstring);
}
this.normalized = null;
return new ScriptPath(fileSystem, absolute, pp);
}

// Private constructor only for known correct split and extra value for absolute
Expand All @@ -54,14 +82,10 @@ public final class ScriptPath implements Path {
}

private ScriptPath(final ScriptFileSystem fileSystem, boolean absolute, boolean isNormalized, final String... pathParts) {
this.fileSystem = fileSystem;
this.absolute = absolute;
this.pathParts = pathParts;
if (isNormalized) this.normalized = this;
else this.normalized = null;
this(fileSystem, absolute, pathParts, isNormalized ? NORMALIZE_SELF : normalizeCompute());
}

private String[] getPathParts(final String longstring) {
private static String[] getPathParts(final String longstring) {
final String clean = longstring.replace('\\', '/');
int startIndex = 0;
final List<String> parts = new ArrayList<>();
Expand Down Expand Up @@ -142,9 +166,9 @@ public boolean startsWith(final Path other) {
if (other.getFileSystem() != this.getFileSystem()) {
return false;
}
if (other instanceof ScriptPath bp) {
if (this.absolute != bp.absolute) return false;
return checkArraysMatch(this.pathParts, bp.pathParts, false);
if (other instanceof ScriptPath(_, boolean abs, String[] bparts, _)) {
if (this.absolute != abs) return false;
return checkArraysMatch(this.pathParts, bparts, false);
}
return false;
}
Expand All @@ -155,9 +179,9 @@ public boolean endsWith(final Path other) {
if (other.getFileSystem() != this.getFileSystem()) {
return false;
}
if (other instanceof ScriptPath bp) {
if (!this.absolute && bp.absolute) return false;
return checkArraysMatch(this.pathParts, bp.pathParts, true);
if (other instanceof ScriptPath(_, boolean abs, String[] bparts, _)) {
if (!this.absolute && abs) return false;
return checkArraysMatch(this.pathParts, bparts, true);
}
return false;
}
Expand All @@ -174,33 +198,16 @@ private static boolean checkArraysMatch(String[] array1, String[] array2, boolea

@Override
public Path normalize() {
if (normalized != null) return normalized;
final Deque<String> normalizedPath = new ArrayDeque<>();
for (final String pathPart : this.pathParts) {
switch (pathPart) {
case "." -> {}
case ".." -> {
if (normalizedPath.isEmpty() || normalizedPath.getLast().equals("..")) {
// .. on an empty path is allowed, so keep it
normalizedPath.addLast(pathPart);
} else {
normalizedPath.removeLast();
}
}
default -> normalizedPath.addLast(pathPart);
}
}
normalized = new ScriptPath(this.fileSystem, this.absolute, true, normalizedPath.toArray(new String[0]));
return normalized;
return normalized.apply(this);
}

@Override
public Path resolve(final Path other) {
if (other instanceof ScriptPath path) {
if (path.isAbsolute()) return path;
final String[] mergedParts = new String[this.pathParts.length + path.pathParts.length];
if (other instanceof ScriptPath(_, boolean isAbs, String[] parts, _)) {
if (isAbs) return other;
final String[] mergedParts = new String[this.pathParts.length + parts.length];
System.arraycopy(this.pathParts, 0, mergedParts, 0, this.pathParts.length);
System.arraycopy(path.pathParts, 0, mergedParts, this.pathParts.length, path.pathParts.length);
System.arraycopy(parts, 0, mergedParts, this.pathParts.length, parts.length);
return new ScriptPath(this.fileSystem, this.absolute, mergedParts);
}
return other;
Expand Down Expand Up @@ -263,20 +270,20 @@ public WatchKey register(final WatchService watcher, final WatchEvent.Kind<?>[]

@Override
public int compareTo(final Path other) {
if (other instanceof ScriptPath path) {
if (this.absolute && !path.absolute) return 1;
else if (!this.absolute && path.absolute) return -1;
else return Arrays.compare(this.pathParts, path.pathParts);
if (other instanceof ScriptPath(_, boolean abs, String[] parts, _)) {
if (this.absolute && !abs) return 1;
else if (!this.absolute && abs) return -1;
else return Arrays.compare(this.pathParts, parts);
} else {
return 0;
}
}

@Override
public boolean equals(final Object o) {
return o instanceof ScriptPath p &&
p.getFileSystem() == this.getFileSystem() &&
this.absolute == p.absolute && Arrays.equals(this.pathParts, p.pathParts);
return o instanceof ScriptPath(ScriptFileSystem fs, boolean abs, String[] parts, _) &&
fs == this.getFileSystem() &&
this.absolute == abs && Arrays.equals(this.pathParts, parts);
}

@Override
Expand Down

0 comments on commit 149b23d

Please sign in to comment.