Skip to content

Improve metadata / PDC usage #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 23, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.menu.Menu;
import com.extendedclip.deluxemenus.menu.MenuHolder;
import com.extendedclip.deluxemenus.persistentmeta.PersistentMetaHandler;
import com.extendedclip.deluxemenus.utils.AdventureUtils;
import com.extendedclip.deluxemenus.utils.DebugLevel;
import com.extendedclip.deluxemenus.utils.ExpUtils;
Expand Down Expand Up @@ -80,14 +81,24 @@ public void run() {
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Meta action not supported on this server version.");
break;
}
try {
final boolean result = plugin.getPersistentMetaHandler().setMeta(player, executable);
if (!result) {
final PersistentMetaHandler.OperationResult result = plugin.getPersistentMetaHandler().parseAndExecuteMetaActionFromString(player, executable);
switch (result) {
case INVALID_SYNTAX:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! Make sure you have the right syntax.");
break;
}
} catch (final NumberFormatException exception) {
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid integer value for meta action!");
case NEW_VALUE_IS_DIFFERENT_TYPE:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! New value is a different type than the old value!");
break;
case INVALID_TYPE:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! The specified type is not supported for the specified action!");
break;
case EXISTENT_VALUE_IS_DIFFERENT_TYPE:
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! Existent value is a different type than the new value!");
break;
case VALUE_NOT_FOUND:
case SUCCESS:
default:
break;
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.extendedclip.deluxemenus.command.subcommand.ExecuteCommand;
import com.extendedclip.deluxemenus.command.subcommand.HelpCommand;
import com.extendedclip.deluxemenus.command.subcommand.ListCommand;
import com.extendedclip.deluxemenus.command.subcommand.MetaCommand;
import com.extendedclip.deluxemenus.command.subcommand.OpenCommand;
import com.extendedclip.deluxemenus.command.subcommand.ReloadCommand;
import com.extendedclip.deluxemenus.command.subcommand.SubCommand;
import com.extendedclip.deluxemenus.utils.DebugLevel;
import com.extendedclip.deluxemenus.utils.Messages;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
Expand All @@ -24,7 +24,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;

import static net.kyori.adventure.text.Component.text;
Expand Down Expand Up @@ -101,6 +100,7 @@ private void registerSubCommands() {
new ExecuteCommand(plugin),
new HelpCommand(plugin),
new ListCommand(plugin),
new MetaCommand(plugin),
new OpenCommand(plugin),
new ReloadCommand(plugin)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,6 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
return null;
}

final List<String> onlinePlayerNames = Bukkit.getOnlinePlayers()
.stream()
.map(Player::getName)
.collect(Collectors.toList());

if (onlinePlayerNames.isEmpty()) {
return null;
}

final String secondArgument = arguments.get(1).toLowerCase();

if (secondArgument.isEmpty()) {
return onlinePlayerNames;
}

return onlinePlayerNames.stream()
.filter(playerName -> playerName.toLowerCase().startsWith(secondArgument))
.collect(Collectors.toList());
return getPlayerNameCompletion(arguments.get(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ public HelpCommand(final @NotNull DeluxeMenus plugin) {

@Override
public void execute(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
if (sender.isOp()) {
plugin.sms(sender, Messages.HELP_OP);
return;
}

if (sender.hasPermission(ADMIN_PERMISSION)) {
plugin.sms(sender, Messages.HELP_ADMIN);
plugin.sms(sender, Messages.HELP);
return;
}

plugin.sms(sender, Messages.HELP);
plugin.sms(sender, Messages.NO_PERMISSION);
}

@Override
public @Nullable List<String> onTabComplete(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
if (!sender.hasPermission(ADMIN_PERMISSION)) {
return null;
}

if (arguments.size() > 1) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.menu.Menu;
import com.extendedclip.deluxemenus.utils.Messages;
import com.google.common.primitives.Ints;
import com.extendedclip.deluxemenus.utils.PaginationUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
Expand Down Expand Up @@ -32,7 +32,7 @@ public class ListCommand extends SubCommand {

private static final String LIST_PERMISSION = "deluxemenus.list";

public ListCommand(final @NotNull DeluxeMenus plugin) {
public ListCommand(@NotNull final DeluxeMenus plugin) {
super(plugin);
}

Expand All @@ -42,7 +42,7 @@ public ListCommand(final @NotNull DeluxeMenus plugin) {
}

@Override
public void execute(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
public void execute(@NotNull final CommandSender sender, @NotNull final List<String> arguments) {
if (!sender.hasPermission(LIST_PERMISSION)) {
plugin.sms(sender, Messages.NO_PERMISSION);
return;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
}

@Override
public @Nullable List<String> onTabComplete(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
public @Nullable List<String> onTabComplete(@NotNull final CommandSender sender, @NotNull final List<String> arguments) {
if (!sender.hasPermission(LIST_PERMISSION)) {
return null;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
.collect(Collectors.toList());
}

private void sendSimpleMenuList(final @NotNull CommandSender sender, final @NotNull Collection<Menu> menus) {
private void sendSimpleMenuList(@NotNull final CommandSender sender, @NotNull final Collection<Menu> menus) {
final TextComponent.Builder list = text();
list.append(text("The following " + menus.size() + " menus are loaded on the server:", NamedTextColor.GOLD).append(newline()));

Expand Down Expand Up @@ -156,37 +156,32 @@ private void sendSimpleMenuList(final @NotNull CommandSender sender, final @NotN
plugin.sms(sender, list.build());
}

private void sendPaginatedMenuList(final @NotNull CommandSender sender, final @NotNull Map<String, List<Menu>> menus,
final @NotNull List<Menu> configMenus, final @NotNull List<String> args) {
final int totalMenusCount = configMenus.size() + menus.values().stream().mapToInt(List::size).sum();

Integer page = null;
if (totalMenusCount > plugin.getGeneralConfig().menusListPageSize() && !args.isEmpty()) {
page = Ints.tryParse(args.get(0));
}
private void sendPaginatedMenuList(@NotNull final CommandSender sender, @NotNull final Map<String, List<Menu>> menus,
@NotNull final List<Menu> configMenus, @NotNull final List<String> args) {

final int maxPages = (int) Math.ceil((double) totalMenusCount / plugin.getGeneralConfig().menusListPageSize());

if (page == null || page < 1) {
page = 1;
}
final int menusPerPage = plugin.getGeneralConfig().menusListPageSize();
final int totalMenusCount = configMenus.size() + menus.values().stream().mapToInt(List::size).sum();
final int pagesCount = PaginationUtils.getPagesCount(menusPerPage, totalMenusCount);

if (page > maxPages) {
page = maxPages;
}
final int page = PaginationUtils.parsePage(
menusPerPage,
totalMenusCount,
pagesCount,
args.isEmpty() ? null : args.get(0)
);

final Map<String, List<Menu>> paginatedMenus = getPaginatedMenus(
menus,
configMenus.stream().collect(TreeMap::new, (map, menu) -> map.put(menu.options().name(), menu), TreeMap::putAll),
page,
plugin.getGeneralConfig().menusListPageSize()
menusPerPage
);

final int pageMenusCount = paginatedMenus.values().stream().mapToInt(List::size).sum();
final Map<String, Object> pageMenusTree = convertMenusToTree(paginatedMenus);

final TextComponent.Builder list = text();
list.append(text("Page " + page + "/" + maxPages + " - " + pageMenusCount + " menus:", NamedTextColor.GOLD).append(newline()));
list.append(text("Page " + page + "/" + pagesCount + " - " + pageMenusCount + " menus:", NamedTextColor.GOLD).append(newline()));

if (sender instanceof ConsoleCommandSender) {
final var menuList = createMenuListForConsole(pageMenusTree, 0);
Expand All @@ -203,7 +198,7 @@ private void sendPaginatedMenuList(final @NotNull CommandSender sender, final @N

list.append(menuList);

if (page > 1 || page < maxPages) {
if (page > 1 || page < pagesCount) {
list.append(newline());

if (page > 1) {
Expand All @@ -214,12 +209,12 @@ private void sendPaginatedMenuList(final @NotNull CommandSender sender, final @N
.append(text("Executes: /dm list " + (page - 1), NamedTextColor.GRAY))
))
.clickEvent(ClickEvent.runCommand("/dm list " + (page - 1))));
if (page < maxPages) {
if (page < pagesCount) {
list.append(text(" | ", NamedTextColor.GREEN));
}
}

if (page < maxPages) {
if (page < pagesCount) {
list.append(text("Next >>", NamedTextColor.GOLD)
.hoverEvent(HoverEvent.showText(
text("Click to go to the next page", NamedTextColor.GRAY)
Expand All @@ -234,7 +229,7 @@ private void sendPaginatedMenuList(final @NotNull CommandSender sender, final @N
}

private Map<String, List<Menu>> getPaginatedMenus(final Map<String, List<Menu>> menus,
final @NotNull Map<String, Menu> configMenus,
@NotNull final Map<String, Menu> configMenus,
final int page,
final int pageSize
) {
Expand Down Expand Up @@ -367,7 +362,7 @@ private void addMenuToTreeRecursively(final Map<String, Object> tree, final List
* If the config option to use admin commands in menus list is enabled, the admin "/dm open" command will be returned.
* @return The command that can be used to open this menu.
*/
public @Nullable String getMenuDisplayCommand(final @NotNull Menu menu) {
public @Nullable String getMenuDisplayCommand(@NotNull final Menu menu) {
final boolean useAdminCommand = this.plugin.getGeneralConfig().useAdminCommandsInMenusList();

if (useAdminCommand) {
Expand Down
Loading