Skip to content

Commit

Permalink
extracted arguments into separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Jul 25, 2024
1 parent 95b9606 commit ea3a5a7
Show file tree
Hide file tree
Showing 12 changed files with 434 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void onEnable() {
}

private void registerCommands() {
new CommanderCommand(this).register();
new CommanderCommand().register(this);
}

private void registerListeners() {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class HideCommand {
private final CommanderPlugin plugin;

ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("hide")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
Bukkit.getCommandMap().getKnownCommands().values().stream()
.map(org.bukkit.command.Command::getLabel)
.filter(s -> !plugin.commandRegistry().isHidden(s))
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::hide));
}

private int hide(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var success = plugin.commandRegistry().hide(command);
var message = success ? "command.hidden" : "nothing.changed";
plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command));
if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.builder.ArgumentBuilder;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.thenextlvl.commander.paper.CommanderPlugin;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class PermissionCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("permission")
.then(new PermissionQueryCommand(plugin).create())
.then(new PermissionResetCommand(plugin).create())
.then(new PermissionSetCommand(plugin).create())
.then(new PermissionUnsetCommand(plugin).create());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class PermissionQueryCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("query")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
Bukkit.getCommandMap().getKnownCommands().values().stream()
.map(Command::getLabel)
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::query));
}

private int query(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var registered = Bukkit.getCommandMap().getKnownCommands().get(command);
var permission = registered != null ? registered.getPermission() : null;
var message = registered == null ? "command.unknown" : permission != null ?
"permission.query.defined" : "permission.query.undefined";
plugin.bundle().sendMessage(sender, message,
Placeholder.parsed("permission", String.valueOf(permission)),
Placeholder.parsed("command", command));
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class PermissionResetCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("reset")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
plugin.permissionOverride().originalPermissions().keySet().stream()
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::reset));
}

private int reset(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var success = plugin.permissionOverride().reset(command);
var message = success ? "permission.reset" : "nothing.changed";
plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command));
if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class PermissionSetCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("set")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
Bukkit.getCommandMap().getKnownCommands().values().stream()
.map(Command::getLabel)
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.then(Commands.argument("permission", StringArgumentType.string())
.suggests((context, suggestions) -> {
Bukkit.getPluginManager().getPermissions().stream()
.map(Permission::getName)
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::set)));
}

private int set(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var permission = context.getArgument("permission", String.class);
var success = plugin.permissionOverride().override(command, permission);
var message = success ? "permission.set" : "nothing.changed";
plugin.bundle().sendMessage(sender, message,
Placeholder.parsed("permission", permission),
Placeholder.parsed("command", command));
if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class PermissionUnsetCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("unset")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
Bukkit.getCommandMap().getKnownCommands().values().stream()
.map(Command::getLabel)
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::unset));
}

private int unset(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var success = plugin.permissionOverride().override(command, null);
var message = success ? "permission.set" : "nothing.changed";
plugin.bundle().sendMessage(sender, message,
Placeholder.parsed("permission", "null"),
Placeholder.parsed("command", command));
if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class RegisterCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("register")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
plugin.commandRegistry().unregisteredCommands().stream()
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::register));
}

private int register(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var success = plugin.commandRegistry().register(command);
var message = success ? "command.registered" : "nothing.changed";
plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command));
if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class ResetCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("reset")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
plugin.commandRegistry().hiddenCommands().stream()
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
plugin.commandRegistry().unregisteredCommands().stream()
.filter(s -> s.contains(suggestions.getRemaining()))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::reset));
}

private int reset(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var s1 = plugin.permissionOverride().reset(command);
var s3 = plugin.commandRegistry().register(command);
var s2 = plugin.commandRegistry().reveal(command);
var message = s1 || s2 || s3 ? "command.reset" : "nothing.changed";
plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command));
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class RevealCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("reveal")
.then(Commands.argument("command", StringArgumentType.string())
.suggests((context, suggestions) -> {
plugin.commandRegistry().hiddenCommands().stream()
.map(StringArgumentType::escapeIfRequired)
.filter(s -> s.contains(suggestions.getRemaining()))
.forEach(suggestions::suggest);
return suggestions.buildFuture();
})
.executes(this::reveal));
}

private int reveal(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
var command = context.getArgument("command", String.class);
var success = plugin.commandRegistry().reveal(command);
var message = success ? "command.revealed" : "nothing.changed";
plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command));
if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
return Command.SINGLE_SUCCESS;
}
}
Loading

0 comments on commit ea3a5a7

Please sign in to comment.