Skip to content

Commit

Permalink
implemented api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Sep 28, 2023
1 parent 4381168 commit 8ebea8c
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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();
Expand Down Expand Up @@ -142,15 +157,15 @@ private void reveal(CommandSender sender, String[] args) {
}

private void notifySyntax(CommandSender sender, String message) {
commander.bundle().sendRawMessage(sender, "%prefix% <red>/command " + message
commander.bundle().sendRawMessage(sender, "<prefix> <red>" + message
.replace("[", "<dark_gray>[<gold>")
.replace("]", "<dark_gray>]")
.replace("|", "<dark_gray>|<red>"));
}

@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
List<String> suggestions = new ArrayList<>();
var suggestions = new ArrayList<String>();
if (args.length <= 1) {
suggestions.add("permission");
suggestions.add("unregister");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Command> {

@Override
public Stream<String> getCommandNames() {
return Stream.concat(
Bukkit.getCommandMap().getKnownCommands().keySet().stream(),
getCommands().stream().map(Command::getLabel)
);
}

@Override
public Collection<Command> getCommands() {
return Bukkit.getCommandMap().getKnownCommands().values();
}

@Override
public Stream<Command> 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<Command> getCommand(String literal) {
return Optional.ofNullable(Bukkit.getCommandMap().getCommand(literal));
}

@Override
public void updateCommands() {
Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ->
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Command> {
private final CraftCommander commander;
private final Map<String, @Nullable String> 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);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8ebea8c

Please sign in to comment.