Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/main/java/fr/maxlego08/menu/command/ZSubCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package fr.maxlego08.menu.command;

import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import fr.robie.paperdispatch.command.SubCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.List;

/**
* Paper-Dispatch attaches an executor to every required argument node, so a command declaring
* several required arguments is also executable when only the first ones are typed. The command
* then runs with arguments that were never parsed and Brigadier throws
* {@code No such argument '...' exists on this command}.
* <p>
* Only the last required argument may be executable, so this class removes the executor from all
* the previous ones just before the node is built. Brigadier then answers with its usual
* "incomplete command" error instead of running the command.
*/
public abstract class ZSubCommand<T extends Plugin> extends SubCommand<T> {

private final List<ArgumentBuilder<CommandSourceStack, ?>> requiredArgumentBuilders = new ArrayList<>();

protected ZSubCommand(T plugin, String name) {
super(plugin, name);
}

protected ZSubCommand(T plugin, String name, String... aliases) {
super(plugin, name, aliases);
}

@Override
protected void addRequiredArgument(ArgumentBuilder<CommandSourceStack, ?> argument, ArgumentExecutor<T> executor) {
super.addRequiredArgument(argument, executor);
// Paper-Dispatch may wrap the builder before this point, so only the instance received here
// is the one that will end up in the command tree.
this.requiredArgumentBuilders.add(argument);
}

@Override
public LiteralCommandNode<CommandSourceStack> build() {
for (int index = 0; index < this.requiredArgumentBuilders.size() - 1; index++) {
this.requiredArgumentBuilders.get(index).executes(null);
}
return super.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.mojang.brigadier.arguments.StringArgumentType;
import fr.maxlego08.menu.ZMenuPlugin;
import fr.maxlego08.menu.api.utils.Message;
import fr.maxlego08.menu.command.ZSubCommand;
import fr.maxlego08.menu.common.enums.Permission;
import fr.maxlego08.menu.common.utils.MessageUtils;
import fr.robie.paperdispatch.command.CommandDispatch;
import fr.robie.paperdispatch.command.CommandResultType;
import fr.robie.paperdispatch.command.SubCommand;
import io.papermc.paper.command.brigadier.Commands;
import org.jetbrains.annotations.NotNull;

public class CommandMenuCreate extends SubCommand<ZMenuPlugin> {
public class CommandMenuCreate extends ZSubCommand<ZMenuPlugin> {

public CommandMenuCreate(ZMenuPlugin plugin) {
super(plugin, "create");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.maxlego08.menu.api.players.Data;
import fr.maxlego08.menu.api.players.DataManager;
import fr.maxlego08.menu.api.utils.Message;
import fr.maxlego08.menu.command.ZSubCommand;
import fr.maxlego08.menu.common.enums.Permission;
import fr.maxlego08.menu.common.utils.MessageUtils;
import fr.maxlego08.menu.common.utils.command.NonSpaceStringArgumentType;
Expand All @@ -13,14 +14,13 @@
import fr.robie.paperdispatch.cache.OfflinePlayerCache;
import fr.robie.paperdispatch.command.CommandDispatch;
import fr.robie.paperdispatch.command.CommandResultType;
import fr.robie.paperdispatch.command.SubCommand;
import io.papermc.paper.command.brigadier.Commands;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

public class CommandMenuPlayersAdd extends SubCommand<ZMenuPlugin> {
public class CommandMenuPlayersAdd extends ZSubCommand<ZMenuPlugin> {

public CommandMenuPlayersAdd(ZMenuPlugin plugin) {
super(plugin, "add");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
import fr.maxlego08.menu.api.players.DataManager;
import fr.maxlego08.menu.api.players.PlayerData;
import fr.maxlego08.menu.api.utils.Message;
import fr.maxlego08.menu.command.ZSubCommand;
import fr.maxlego08.menu.common.enums.Permission;
import fr.maxlego08.menu.common.utils.MessageUtils;
import fr.maxlego08.menu.common.utils.command.NonSpaceStringArgumentType;
import fr.robie.paperdispatch.argument.OfflinePlayerArgument;
import fr.robie.paperdispatch.command.CommandDispatch;
import fr.robie.paperdispatch.command.CommandResultType;
import fr.robie.paperdispatch.command.SubCommand;
import io.papermc.paper.command.brigadier.Commands;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

public class CommandMenuPlayersGet extends SubCommand<ZMenuPlugin> {
public class CommandMenuPlayersGet extends ZSubCommand<ZMenuPlugin> {

public CommandMenuPlayersGet(ZMenuPlugin plugin) {
super(plugin, "get");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import fr.maxlego08.menu.api.players.DataManager;
import fr.maxlego08.menu.api.players.PlayerData;
import fr.maxlego08.menu.api.utils.Message;
import fr.maxlego08.menu.command.ZSubCommand;
import fr.maxlego08.menu.common.enums.Permission;
import fr.maxlego08.menu.common.utils.MessageUtils;
import fr.maxlego08.menu.common.utils.command.NonSpaceStringArgumentType;
import fr.robie.paperdispatch.argument.OfflinePlayerArgument;
import fr.robie.paperdispatch.command.CommandDispatch;
import fr.robie.paperdispatch.command.CommandResultType;
import fr.robie.paperdispatch.command.SubCommand;
import io.papermc.paper.command.brigadier.Commands;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

public class CommandMenuPlayersRemove extends SubCommand<ZMenuPlugin> {
public class CommandMenuPlayersRemove extends ZSubCommand<ZMenuPlugin> {

public CommandMenuPlayersRemove(ZMenuPlugin plugin) {
super(plugin, "remove", "r");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fr.maxlego08.menu.api.players.Data;
import fr.maxlego08.menu.api.players.DataManager;
import fr.maxlego08.menu.api.utils.Message;
import fr.maxlego08.menu.command.ZSubCommand;
import fr.maxlego08.menu.common.enums.Permission;
import fr.maxlego08.menu.common.utils.MessageUtils;
import fr.maxlego08.menu.common.utils.command.NonSpaceStringArgumentType;
Expand All @@ -14,13 +15,12 @@
import fr.robie.paperdispatch.cache.OfflinePlayerCache;
import fr.robie.paperdispatch.command.CommandDispatch;
import fr.robie.paperdispatch.command.CommandResultType;
import fr.robie.paperdispatch.command.SubCommand;
import io.papermc.paper.command.brigadier.Commands;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class CommandMenuPlayersSet extends SubCommand<ZMenuPlugin> {
public class CommandMenuPlayersSet extends ZSubCommand<ZMenuPlugin> {

public CommandMenuPlayersSet(ZMenuPlugin plugin) {
super(plugin, "set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.maxlego08.menu.api.players.Data;
import fr.maxlego08.menu.api.players.DataManager;
import fr.maxlego08.menu.api.utils.Message;
import fr.maxlego08.menu.command.ZSubCommand;
import fr.maxlego08.menu.common.enums.Permission;
import fr.maxlego08.menu.common.utils.MessageUtils;
import fr.maxlego08.menu.common.utils.command.NonSpaceStringArgumentType;
Expand All @@ -13,14 +14,13 @@
import fr.robie.paperdispatch.cache.OfflinePlayerCache;
import fr.robie.paperdispatch.command.CommandDispatch;
import fr.robie.paperdispatch.command.CommandResultType;
import fr.robie.paperdispatch.command.SubCommand;
import io.papermc.paper.command.brigadier.Commands;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

public class CommandMenuPlayersSubtract extends SubCommand<ZMenuPlugin> {
public class CommandMenuPlayersSubtract extends ZSubCommand<ZMenuPlugin> {

public CommandMenuPlayersSubtract(ZMenuPlugin plugin) {
super(plugin, "subtract", "sub");
Expand Down
Loading