From 8ebea8c8d3499a8ca281f565362e5ad82d4283f6 Mon Sep 17 00:00:00 2001 From: NonSwag Date: Thu, 28 Sep 2023 17:17:02 +0200 Subject: [PATCH] implemented api changes --- .../thenextlvl/commander/CommanderPlugin.java | 2 +- .../commander/command/CommanderCommand.java | 71 ++++++++++-------- .../implementation/CraftCommandManager.java | 52 ++++++++++++++ .../implementation/CraftCommander.java | 29 ++------ .../CraftPermissionManager.java | 72 +++++++++++++++++++ .../command/CraftCommandRegistry.java | 12 ---- .../command/CraftPlatformCommandRegistry.java | 37 ---------- .../permission/CraftPermissionRegistry.java | 12 ---- .../CraftPlatformPermissionRegistry.java | 37 ---------- .../commander/listener/CommandListener.java | 22 ++++-- 10 files changed, 190 insertions(+), 156 deletions(-) create mode 100644 paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommandManager.java create mode 100644 paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftPermissionManager.java delete mode 100644 paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftCommandRegistry.java delete mode 100644 paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftPlatformCommandRegistry.java delete mode 100644 paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPermissionRegistry.java delete mode 100644 paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPlatformPermissionRegistry.java diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/CommanderPlugin.java b/paper-plugin/src/main/java/net/thenextlvl/commander/CommanderPlugin.java index 09ffbe2..858a34c 100644 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/CommanderPlugin.java +++ b/paper-plugin/src/main/java/net/thenextlvl/commander/CommanderPlugin.java @@ -26,7 +26,7 @@ public void onEnable() { if (registration == null) return; var commander = (CraftCommander) registration.getProvider(); - Bukkit.getScheduler().runTask(this, () -> commander.platform().permissionRegistry().overridePermissions()); + Bukkit.getScheduler().runTask(this, () -> commander.permissionManager().overridePermissions()); registerCommands(commander); registerListeners(commander); diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/command/CommanderCommand.java b/paper-plugin/src/main/java/net/thenextlvl/commander/command/CommanderCommand.java index 9714ebb..016ca65 100644 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/command/CommanderCommand.java +++ b/paper-plugin/src/main/java/net/thenextlvl/commander/command/CommanderCommand.java @@ -1,6 +1,7 @@ package net.thenextlvl.commander.command; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.api.CommandInfo; import net.thenextlvl.commander.implementation.CraftCommander; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -9,11 +10,11 @@ import org.bukkit.permissions.Permission; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.regex.Pattern; public class CommanderCommand extends Command implements PluginIdentifiableCommand { private final CraftCommander commander; @@ -49,38 +50,53 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No return true; } + private void reset(CommandSender sender, String[] args) { + if (args.length != 2) { + notifySyntax(sender, "/command reset [command]"); + return; + } + var s1 = commander.commandRegistry().resetPermissions(args[1]); + var s2 = commander.commandRegistry().registerCommand(args[1]); + var s3 = commander.commandRegistry().revealCommand(args[1]); + var message = s1 || s2 || s3 ? "command.reset" : "nothing.changed"; + commander.bundle().sendMessage(sender, message, Placeholder.parsed("command", args[1])); + } + private void permission(CommandSender sender, String[] args) { if (args.length >= 2 && args[1].equals("set")) permissionSet(sender, args); else if (args.length >= 2 && args[1].equals("reset")) permissionReset(sender, args); else if (args.length >= 2 && args[1].equals("query")) permissionQuery(sender, args); - else notifySyntax(sender, "permission set | reset | query"); + else notifySyntax(sender, "/command permission set | reset | query"); } private void permissionSet(CommandSender sender, String[] args) { - if (args.length == 4) { - var command = args[2]; - var permission = args[3].equals("null") ? null : args[3]; - commander.permissionRegistry().overridePermission(command, permission); - commander.bundle().sendMessage(sender, "permission.set", - Placeholder.parsed("permission", String.valueOf(permission)), - Placeholder.parsed("command", command)); - commander.platform().commandRegistry().updateCommands(); - } else notifySyntax(sender, "permission set [command] [permission]"); + if (args.length != 4) { + notifySyntax(sender, "/command permission set [command] [permission]"); + return; + } + var permission = args[3].equals("null") ? null : args[3]; + var success = commander.commandRegistry().overridePermissions(args[2], permission); + var message = success ? "permission.set" : "nothing.changed"; + commander.bundle().sendMessage(sender, message, + Placeholder.parsed("permission", String.valueOf(permission)), + Placeholder.parsed("command", args[2])); + if (success) commander.commandManager().updateCommands(); } private void permissionReset(CommandSender sender, String[] args) { - if (args.length == 3) { - var command = args[2]; - var success = commander.permissionRegistry().resetPermission(command); - var message = success ? "permission.reset" : "nothing.changed"; - commander.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); - if (success) commander.platform().commandRegistry().updateCommands(); - } else notifySyntax(sender, "permission reset [command]"); + if (args.length != 3) { + notifySyntax(sender, "/command permission reset [command]"); + return; + } + var success = commander.commandRegistry().resetPermissions(args[2]); + var message = success ? "permission.reset" : "nothing.changed"; + commander.bundle().sendMessage(sender, message, Placeholder.parsed("command", args[2])); + if (success) commander.commandManager().updateCommands(); } private void permissionQuery(CommandSender sender, String[] args) { if (args.length != 3) { - notifySyntax(sender, "permission query [command]"); + notifySyntax(sender, "/command permission query [command]"); return; } var command = Bukkit.getCommandMap().getCommand(args[2]); @@ -94,24 +110,23 @@ private void permissionQuery(CommandSender sender, String[] args) { private void unregister(CommandSender sender, String[] args) { if (args.length == 2) try { - if (args[1].contains("*")) Pattern.compile(args[1].replaceAll("\\*", ".+")); - var success = commander.commandRegistry().unregisterCommands(args[1]); + if (args[1].contains("*")) CommandInfo.compile(args[1]); + var success = commander.commandRegistry().registerCommandInfo(CommandInfo.remove(args[1])); var message = success ? "command.unregistered" : "nothing.changed"; commander.bundle().sendMessage(sender, message, Placeholder.parsed("command", args[1])); - if (success) commander.platform().commandRegistry().updateCommands(); + if (success) commander.commandManager().updateCommands(); } catch (Exception e) { commander.bundle().sendMessage(sender, "query.invalid", Placeholder.parsed("query", args[1])); } - else notifySyntax(sender, "unregister [command]"); + else notifySyntax(sender, "/command unregister [command]"); } private void register(CommandSender sender, String[] args) { if (args.length != 2) { - notifySyntax(sender, "register [command]"); + notifySyntax(sender, "/command register [command]"); return; } - var command = args[1]; - var success = commander.commandRegistry().registerCommand(command); + var success = commander.commandRegistry().registerCommand(args[1]); var message = success ? "command.registered" : "nothing.changed"; commander.bundle().sendMessage(sender, message, Placeholder.parsed("command", args[1])); if (success) commander.commandManager().updateCommands(); @@ -142,7 +157,7 @@ private void reveal(CommandSender sender, String[] args) { } private void notifySyntax(CommandSender sender, String message) { - commander.bundle().sendRawMessage(sender, "%prefix% /command " + message + commander.bundle().sendRawMessage(sender, " " + message .replace("[", "[") .replace("]", "]") .replace("|", "|")); @@ -150,7 +165,7 @@ private void notifySyntax(CommandSender sender, String message) { @Override public @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException { - List suggestions = new ArrayList<>(); + var suggestions = new ArrayList(); if (args.length <= 1) { suggestions.add("permission"); suggestions.add("unregister"); diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommandManager.java b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommandManager.java new file mode 100644 index 0000000..ae10220 --- /dev/null +++ b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommandManager.java @@ -0,0 +1,52 @@ +package net.thenextlvl.commander.implementation; + +import core.annotation.MethodsReturnNotNullByDefault; +import core.annotation.ParametersAreNotNullByDefault; +import core.annotation.TypesAreNotNullByDefault; +import net.thenextlvl.commander.api.CommandInfo; +import net.thenextlvl.commander.api.platform.CommandManager; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +import java.util.Collection; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +@TypesAreNotNullByDefault +@MethodsReturnNotNullByDefault +@ParametersAreNotNullByDefault +public record CraftCommandManager(CraftCommander commander) implements CommandManager { + + @Override + public Stream getCommandNames() { + return Stream.concat( + Bukkit.getCommandMap().getKnownCommands().keySet().stream(), + getCommands().stream().map(Command::getLabel) + ); + } + + @Override + public Collection getCommands() { + return Bukkit.getCommandMap().getKnownCommands().values(); + } + + @Override + public Stream getCommands(String query) { + return Bukkit.getCommandMap().getKnownCommands().keySet().stream() + .filter(command -> CommandInfo.nameMatches(command, query)) + .map(s -> getCommand(s).orElse(null)) + .filter(Objects::nonNull); + } + + @Override + public Optional getCommand(String literal) { + return Optional.ofNullable(Bukkit.getCommandMap().getCommand(literal)); + } + + @Override + public void updateCommands() { + Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + } +} diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommander.java b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommander.java index 279c816..bbd993e 100644 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommander.java +++ b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftCommander.java @@ -6,11 +6,8 @@ import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.thenextlvl.commander.api.CommandRegistry; import net.thenextlvl.commander.api.Commander; -import net.thenextlvl.commander.implementation.command.CraftCommandRegistry; -import net.thenextlvl.commander.implementation.command.CraftPlatformCommandRegistry; -import net.thenextlvl.commander.implementation.permission.CraftPermissionRegistry; -import net.thenextlvl.commander.implementation.permission.CraftPlatformPermissionRegistry; import org.bukkit.entity.Player; import java.io.File; @@ -20,9 +17,9 @@ @Accessors(fluent = true) public class CraftCommander implements Commander { private final ComponentBundle bundle; - private final CraftCommandRegistry commandRegistry; - private final CraftPermissionRegistry permissionRegistry; - private final CraftPlatformRegistry platform; + private final CommandRegistry commandRegistry; + private final CraftCommandManager commandManager; + private final CraftPermissionManager permissionManager; public CraftCommander(File dataFolder) { bundle = new ComponentBundle(new File(dataFolder, "translations"), audience -> @@ -34,20 +31,8 @@ public CraftCommander(File dataFolder) { TagResolver.standard(), Placeholder.parsed("prefix", bundle().format(Locale.US, "prefix")) )).build()); - commandRegistry = new CraftCommandRegistry(this, dataFolder); - permissionRegistry = new CraftPermissionRegistry(this, dataFolder); - platform = new CraftPlatformRegistry(); - } - - @Getter - @Accessors(fluent = true) - public class CraftPlatformRegistry implements PlatformRegistry { - private final CraftPlatformCommandRegistry commandRegistry; - private final CraftPlatformPermissionRegistry permissionRegistry; - - private CraftPlatformRegistry() { - commandRegistry = new CraftPlatformCommandRegistry(CraftCommander.this); - permissionRegistry = new CraftPlatformPermissionRegistry(CraftCommander.this); - } + commandRegistry = new CommandRegistry(this, dataFolder); + commandManager = new CraftCommandManager(this); + permissionManager = new CraftPermissionManager(this); } } diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftPermissionManager.java b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftPermissionManager.java new file mode 100644 index 0000000..7130f8b --- /dev/null +++ b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/CraftPermissionManager.java @@ -0,0 +1,72 @@ +package net.thenextlvl.commander.implementation; + +import core.annotation.MethodsReturnNotNullByDefault; +import core.annotation.ParametersAreNotNullByDefault; +import core.annotation.TypesAreNotNullByDefault; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.experimental.Accessors; +import net.thenextlvl.commander.api.platform.PermissionManager; +import org.bukkit.command.Command; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; + +@Getter +@RequiredArgsConstructor +@Accessors(fluent = true) +@TypesAreNotNullByDefault +@MethodsReturnNotNullByDefault +@ParametersAreNotNullByDefault +public class CraftPermissionManager implements PermissionManager { + private final CraftCommander commander; + private final Map originalPermissions = new HashMap<>(); + + @Override + public boolean overridePermissions(String query, @Nullable String permission) { + var success = new AtomicBoolean(); + commander().commandManager().getCommands(query).forEach(command -> { + if (overridePermission(command, permission)) success.set(true); + }); + return success.get(); + } + + @Override + public boolean resetPermissions(String query) { + var success = new AtomicBoolean(); + commander().commandManager().getCommands(query).forEach(command -> { + if (resetPermission(command)) success.set(true); + }); + return success.get(); + } + + @Override + public boolean resetPermission(Command command) { + if (!originalPermissions().containsKey(command.getLabel())) return false; + var permission = originalPermissions().remove(command.getLabel()); + if (Objects.equals(command.getPermission(), permission)) return false; + command.setPermission(permission); + return Objects.equals(command.getPermission(), permission); + } + + @Override + public boolean overridePermission(Command command, @Nullable String permission) { + originalPermissions().putIfAbsent(command.getLabel(), command.getPermission()); + if (Objects.equals(command.getPermission(), permission)) return false; + command.setPermission(permission); + return Objects.equals(command.getPermission(), permission); + } + + @Override + public @Nullable String getOriginalPermission(String literal) { + return originalPermissions().get(literal); + } + + @Override + public boolean hasOriginalPermission(String literal) { + return !originalPermissions().containsKey(literal); + } +} diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftCommandRegistry.java b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftCommandRegistry.java deleted file mode 100644 index 4c04e20..0000000 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftCommandRegistry.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.thenextlvl.commander.implementation.command; - -import net.thenextlvl.commander.api.command.CommandRegistry; -import net.thenextlvl.commander.implementation.CraftCommander; - -import java.io.File; - -public class CraftCommandRegistry extends CommandRegistry { - public CraftCommandRegistry(CraftCommander commander, File dataFolder) { - super(commander, dataFolder); - } -} diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftPlatformCommandRegistry.java b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftPlatformCommandRegistry.java deleted file mode 100644 index 183ebf1..0000000 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/command/CraftPlatformCommandRegistry.java +++ /dev/null @@ -1,37 +0,0 @@ -package net.thenextlvl.commander.implementation.command; - -import net.thenextlvl.commander.api.command.PlatformCommandRegistry; -import net.thenextlvl.commander.implementation.CraftCommander; -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.entity.Player; - -import java.util.Collection; -import java.util.Optional; -import java.util.stream.Stream; - -public record CraftPlatformCommandRegistry(CraftCommander commander) implements PlatformCommandRegistry { - - @Override - public Stream getCommandNamespaces() { - return Stream.concat( - Bukkit.getCommandMap().getKnownCommands().keySet().stream(), - getCommands().stream().map(Command::getLabel) - ); - } - - @Override - public Collection getCommands() { - return Bukkit.getCommandMap().getKnownCommands().values(); - } - - @Override - public Optional getCommand(String literal) { - return Optional.ofNullable(Bukkit.getCommandMap().getCommand(literal)); - } - - @Override - public void updateCommands() { - Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - } -} diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPermissionRegistry.java b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPermissionRegistry.java deleted file mode 100644 index 33623c6..0000000 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPermissionRegistry.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.thenextlvl.commander.implementation.permission; - -import net.thenextlvl.commander.api.permission.PermissionRegistry; -import net.thenextlvl.commander.implementation.CraftCommander; - -import java.io.File; - -public class CraftPermissionRegistry extends PermissionRegistry { - public CraftPermissionRegistry(CraftCommander commander, File dataFolder) { - super(commander, dataFolder); - } -} diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPlatformPermissionRegistry.java b/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPlatformPermissionRegistry.java deleted file mode 100644 index 962cc10..0000000 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/implementation/permission/CraftPlatformPermissionRegistry.java +++ /dev/null @@ -1,37 +0,0 @@ -package net.thenextlvl.commander.implementation.permission; - -import net.thenextlvl.commander.api.permission.PlatformPermissionRegistry; -import net.thenextlvl.commander.implementation.CraftCommander; -import org.jetbrains.annotations.Nullable; - -import java.util.Objects; - -public record CraftPlatformPermissionRegistry(CraftCommander commander) implements PlatformPermissionRegistry { - - @Override - public boolean overridePermission(String literal, @Nullable String permission) { - return commander.platform().commandRegistry().getCommand(literal).map(command -> { - commander().permissionRegistry().getOriginalPermissions() - .putIfAbsent(command.getLabel(), command.getPermission()); - if (Objects.equals(command.getPermission(), permission)) return false; - command.setPermission(permission); - return true; - }).orElse(false); - } - - @Override - public @Nullable String getOriginalPermission(String literal) { - return commander().platform().commandRegistry().getCommand(literal) - .map(command -> commander().permissionRegistry().getOriginalPermissions().get(command.getLabel())) - .orElse(null); - } - - @Override - public boolean hasOriginalPermission(String literal) { - var originalPermissions = commander().permissionRegistry().getOriginalPermissions(); - if (originalPermissions.containsKey(literal)) return true; - return commander().platform().commandRegistry().getCommand(literal) - .map(command -> originalPermissions.containsKey(command.getLabel())) - .orElse(true); - } -} diff --git a/paper-plugin/src/main/java/net/thenextlvl/commander/listener/CommandListener.java b/paper-plugin/src/main/java/net/thenextlvl/commander/listener/CommandListener.java index 7ff9eaa..f9de3c6 100644 --- a/paper-plugin/src/main/java/net/thenextlvl/commander/listener/CommandListener.java +++ b/paper-plugin/src/main/java/net/thenextlvl/commander/listener/CommandListener.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.api.CommandInfo; import net.thenextlvl.commander.implementation.CraftCommander; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -17,14 +18,15 @@ public class CommandListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onCommandSend(PlayerCommandSendEvent event) { - event.getCommands().removeIf(literal -> commander.commandRegistry().isCommandRemoved(literal)); + event.getCommands().removeIf(literal -> commander.commandRegistry().containsCommandInfo(info -> + info.status() != null && info.nameMatches(literal))); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onCommand(ServerCommandEvent event) { var literal = event.getCommand().split(" ")[0]; - if (commander.platform().commandRegistry().isCommandRegistered(literal) - && !commander.commandRegistry().isCommandRemoved(literal)) return; + if (commander.commandManager().isCommandRegistered(literal) + && !commander.commandRegistry().isRemoved(literal)) return; commander.bundle().sendMessage(event.getSender(), "command.unknown", Placeholder.parsed("command", literal)); event.setCancelled(true); @@ -33,7 +35,7 @@ public void onCommand(ServerCommandEvent event) { @EventHandler(priority = EventPriority.HIGHEST) public void onUnknownCommand(UnknownCommandEvent event) { var literal = event.getCommandLine().split(" ")[0]; - if (!commander.commandRegistry().isCommandRemoved(literal)) return; + if (!commander.commandRegistry().isRemoved(literal)) return; event.message(commander.bundle().component(event.getSender(), "command.unknown", Placeholder.parsed("command", literal))); } @@ -42,18 +44,24 @@ public void onUnknownCommand(UnknownCommandEvent event) { public void onCommand(PlayerCommandPreprocessEvent event) { var player = event.getPlayer(); var literal = event.getMessage().substring(1).split(" ")[0]; - var command = commander.platform().commandRegistry().getCommand(literal).orElse(null); - if (command == null || commander.commandRegistry().isCommandRemoved(literal)) { + var command = commander.commandManager().getCommand(literal).orElse(null); + if (command == null || commander.commandRegistry().isRemoved(literal)) { event.setCancelled(true); if (literal.isBlank()) return; commander.bundle().sendMessage(player, "command.unknown", Placeholder.parsed("command", literal)); } else if (!command.testPermissionSilent(player)) { - var permission = command.getPermission() != null ? command.getPermission() : null; + var permission = command.getPermission() != null ? command.getPermission() : getPermission(literal); if (permission != null) commander.bundle().sendMessage(player, "command.permission", Placeholder.parsed("permission", permission)); else commander.bundle().sendMessage(player, "command.permission.unknown"); event.setCancelled(true); } } + + private String getPermission(String literal) { + return commander.commandRegistry().getCommandInformation(literal) + .map(CommandInfo::permission) + .orElse(null); + } }