Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -35,8 +39,6 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
return;
}

boolean player = (sender instanceof Player);

if (arguments.isEmpty()) {
plugin.sms(sender, Messages.WRONG_USAGE_OPEN_COMMAND);
return;
Expand All @@ -47,94 +49,80 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
return;
}

Player viewer;
String placeholderPlayer = null;

if (arguments.size() == 2 && arguments.get(1).startsWith("-p:")) {
if (!sender.hasPermission("deluxemenus.placeholdersfor")) {
plugin.sms(sender, Messages.NO_PERMISSION_PLAYER_ARGUMENT);
return;
}

placeholderPlayer = arguments.get(1).replace("-p:", "");

} else if (arguments.size() >= 3 && arguments.get(2).startsWith("-p:")) {
if (!sender.hasPermission("deluxemenus.placeholdersfor")) {
plugin.sms(sender, Messages.NO_PERMISSION_PLAYER_ARGUMENT);
return;
}

placeholderPlayer = arguments.get(2).replace("-p:", "");
}

if (arguments.size() >= 2) {
if (placeholderPlayer == null) {
if (player && !sender.hasPermission("deluxemenus.open.others")) {
plugin.sms(sender, Messages.NO_PERMISSION);
List<String> actualArgs = new ArrayList<>();
for (String arg : arguments) {
if (arg.startsWith("-p:")) {
if (!sender.hasPermission("deluxemenus.placeholdersfor")) {
plugin.sms(sender, Messages.NO_PERMISSION_PLAYER_ARGUMENT);
return;
}

viewer = Bukkit.getPlayerExact(arguments.get(1));

placeholderPlayer = arg.substring(3);
} else {
Comment on lines +54 to 61
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the last argument starting with -p: is taken as a placeholder player. This will break backwards compatibility, if, for example, one of the arguments is also -p:SOMETHING, for some reason.

The command schema should be strictly respected:

/dm open <menu> - open menu for you
/dm open <menu> -p:<target> - open menu for you, but parse placeholders of another player
/dm open <menu> <viewer> - open menu for a player
/dm open <menu> <viewer> -p:<target> - open menu for a player, but parse placeholders of another player.

So, in the end, it should probably have this structure:
/dm open <menu> [args] - open menu for you
/dm open <menu> -p:<target> [args] - open menu for you, but parse placeholders of another player
/dm open <menu> <viewer> [args] - open menu for a player
/dm open <menu> <viewer> -p:<target> [args] - open menu for a player, but parse placeholders of another player.

I'm not sure how to actually do this, because some arguments here are optional, but are in the middle of the argument list.

Maybe we should have a new command for this? I am open to suggestions.

if (arguments.size() >= 3) {
if (!sender.hasPermission("deluxemenus.open.others")) {
plugin.sms(sender, Messages.NO_PERMISSION);
return;
}
actualArgs.add(arg);
}
}

viewer = Bukkit.getPlayerExact(arguments.get(1));
String menuName = actualArgs.get(0);
Optional<Menu> menu = Menu.getMenuByName(menuName);

} else {
if (!player) {
plugin.sms(sender, Messages.MUST_SPECIFY_PLAYER);
return;
}
if (menu.isEmpty()) {
plugin.sms(sender, Messages.INVALID_MENU.message().replaceText(MENU_REPLACER_BUILDER.replacement(menuName).build()));
return;
}

viewer = (Player) sender;
}
String targetPlayerName = actualArgs.size() > 1 ? actualArgs.get(1) : null;
Player viewer;
boolean isPlayer = (sender instanceof Player);
if (targetPlayerName != null) {
if (isPlayer && !sender.hasPermission("deluxemenus.open.others")) {
plugin.sms(sender, Messages.NO_PERMISSION);
return;
}
viewer = Bukkit.getPlayerExact(targetPlayerName);
if (viewer == null) {
plugin.sms(sender, Messages.PLAYER_IS_NOT_ONLINE.message().replaceText(PLAYER_REPLACER_BUILDER.replacement(targetPlayerName).build()));
return;
}

} else {
if (!player) {
if (!isPlayer) {
plugin.sms(sender, Messages.MUST_SPECIFY_PLAYER);
return;
}

viewer = (Player) sender;
}

if (viewer == null) {
plugin.sms(sender, Messages.PLAYER_IS_NOT_ONLINE.message().replaceText(PLAYER_REPLACER_BUILDER.replacement(arguments.get(1)).build()));
return;
}

Player placeholder = null;

if (placeholderPlayer != null) {
placeholder = Bukkit.getPlayerExact(placeholderPlayer);

if (placeholder == null) {
plugin.sms(sender, Messages.PLAYER_IS_NOT_ONLINE.message().replaceText(PLAYER_REPLACER_BUILDER.replacement(placeholderPlayer).build()));
return;

} else {
if (placeholder.hasPermission("deluxemenus.placeholdersfor.exempt")) {
plugin.sms(sender, Messages.PLAYER_IS_EXEMPT.message().replaceText(PLAYER_REPLACER_BUILDER.replacement(placeholderPlayer).build()));

return;
}
}
if (placeholder.hasPermission("deluxemenus.placeholdersfor.exempt")) {
plugin.sms(sender, Messages.PLAYER_IS_EXEMPT.message().replaceText(PLAYER_REPLACER_BUILDER.replacement(placeholderPlayer).build()));
return;
}
}

Optional<Menu> menu = Menu.getMenuByName(arguments.get(0));

if (menu.isEmpty()) {
plugin.sms(sender, Messages.INVALID_MENU.message().replaceText(MENU_REPLACER_BUILDER.replacement(arguments.get(0)).build()));
List<String> menuArgumentNames = menu.get().options().arguments();
if (menuArgumentNames.isEmpty()) {
menu.get().openMenu(viewer, null, placeholder);
return;
}

menu.get().openMenu(viewer, null, placeholder);
List<String> menuArgs = actualArgs.size() > 2 ? actualArgs.subList(2, actualArgs.size()) : Collections.emptyList();
Map<String, String> argumentsMap = new HashMap<>();
for (int index = 0; index < menuArgumentNames.size() && index < menuArgs.size(); index++) {
String argumentName = menuArgumentNames.get(index);
if (index == menuArgumentNames.size() - 1) {
String lastArgumentValue = String.join(" ", menuArgs.subList(index, menuArgs.size()));
argumentsMap.put(argumentName, lastArgumentValue);
break;
}
argumentsMap.put(argumentName, menuArgs.get(index));
}
menu.get().openMenu(viewer, argumentsMap, placeholder);
}

@Override
Expand Down
Loading