Skip to content

Commit

Permalink
implement granular un-registration for bukkit
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Sep 21, 2024
1 parent 138c34c commit d272003
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package revxrsal.commands.bukkit.hooks;

import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand All @@ -33,12 +34,14 @@
import revxrsal.commands.command.ExecutableCommand;
import revxrsal.commands.hook.CancelHandle;
import revxrsal.commands.hook.CommandRegisteredHook;
import revxrsal.commands.hook.CommandUnregisteredHook;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public final class BukkitCommandHooks<A extends BukkitCommandActor> implements CommandRegisteredHook<A> {
public final class BukkitCommandHooks<A extends BukkitCommandActor> implements CommandRegisteredHook<A>,
CommandUnregisteredHook<A> {

private final Set<String> registeredRootNames = new HashSet<>();

Expand Down Expand Up @@ -70,4 +73,25 @@ public void onRegistered(@NotNull ExecutableCommand<A> command, @NotNull CancelH
cmd.setUsage(command.usage());
}
}

@Override public void onUnregistered(@NotNull ExecutableCommand<A> command, @NotNull CancelHandle cancelHandle) {
String label = command.firstNode().name();
String fallbackPrefix = fallbackPrefix(command);
PluginCommand cmd = Bukkit.getServer().getPluginCommand(fallbackPrefix + ':' + label);
// check there's no other '/fallback_prefix:label' command. if so, unregister.
if (!command.lamp().registry().any(c -> c != command && c.firstNode().name().equals(label) && fallbackPrefix(c).equals(fallbackPrefix)))
if (cmd != null)
PluginCommands.unregister(cmd, plugin);

// check there's no other '/label' command. if so, unregister.
if (!command.lamp().registry().any(c -> c != command && c.firstNode().name().equals(label))) {
cmd = plugin.getCommand(label);
if (cmd != null)
PluginCommands.unregister(cmd, plugin);
}
}

private @NotNull String fallbackPrefix(@NotNull ExecutableCommand<A> command) {
return command.annotations().mapOr(FallbackPrefix.class, FallbackPrefix::value, defaultFallbackPrefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public <T extends Annotation> boolean contains(@NotNull Class<T> type) {
}
if (element instanceof Method method) {
distributeAnnotations(annotations, method, replacers);
System.out.println(annotations);
}
return new AnnotationListFromMap(annotations);
}
Expand Down
22 changes: 21 additions & 1 deletion common/src/main/java/revxrsal/commands/node/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package revxrsal.commands.node;

import org.jetbrains.annotations.CheckReturnValue;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;
Expand Down Expand Up @@ -103,7 +104,26 @@ default void execute(@NotNull A actor, @NotNull String input) {
*
* @param matches Criteria to test for
*/
void unregisterIf(@NotNull Predicate<ExecutableCommand<A>> matches);
void unregisterIf(@NotNull Predicate<@NotNull ExecutableCommand<A>> matches);

/**
* Tests whether any of the registered commands matches the
* given predicate
*
* @param matches Criteria to test for
* @return if any command matches the predicate
*/
boolean any(@NotNull Predicate<@NotNull ExecutableCommand<A>> matches);

/**
* Returns a new list of all commands that match the
* given predicate
*
* @param filterPredicate Criteria to test for
* @return a list of all commands that match the predicate
*/
@NotNull @CheckReturnValue @Contract("_ -> new")
List<ExecutableCommand<A>> filter(@NotNull Predicate<@NotNull ExecutableCommand<A>> filterPredicate);

/**
* Returns an immutable iterator of all entries in this registry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ public void execute(@NotNull A actor, @NotNull StringStream input) {
children.remove(execution);
}

@Override public boolean any(@NotNull Predicate<@NotNull ExecutableCommand<A>> matches) {
return revxrsal.commands.util.Collections.any(children, matches);
}

@Override
public @NotNull List<ExecutableCommand<A>> filter(@NotNull Predicate<@NotNull ExecutableCommand<A>> filterPredicate) {
return revxrsal.commands.util.Collections.filter(children, filterPredicate);
}

@Override public void unregisterIf(@NotNull Predicate<ExecutableCommand<A>> matches) {
children.removeIf(matches);
}
Expand Down

0 comments on commit d272003

Please sign in to comment.