Skip to content

Commit 87141c0

Browse files
authored
Improve metadata / PDC usage (#183)
1 parent 41b2476 commit 87141c0

File tree

17 files changed

+1594
-436
lines changed

17 files changed

+1594
-436
lines changed

src/main/java/com/extendedclip/deluxemenus/action/ClickActionTask.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.extendedclip.deluxemenus.DeluxeMenus;
44
import com.extendedclip.deluxemenus.menu.Menu;
55
import com.extendedclip.deluxemenus.menu.MenuHolder;
6+
import com.extendedclip.deluxemenus.persistentmeta.PersistentMetaHandler;
67
import com.extendedclip.deluxemenus.utils.AdventureUtils;
78
import com.extendedclip.deluxemenus.utils.DebugLevel;
89
import com.extendedclip.deluxemenus.utils.ExpUtils;
@@ -80,14 +81,24 @@ public void run() {
8081
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Meta action not supported on this server version.");
8182
break;
8283
}
83-
try {
84-
final boolean result = plugin.getPersistentMetaHandler().setMeta(player, executable);
85-
if (!result) {
84+
final PersistentMetaHandler.OperationResult result = plugin.getPersistentMetaHandler().parseAndExecuteMetaActionFromString(player, executable);
85+
switch (result) {
86+
case INVALID_SYNTAX:
8687
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! Make sure you have the right syntax.");
8788
break;
88-
}
89-
} catch (final NumberFormatException exception) {
90-
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid integer value for meta action!");
89+
case NEW_VALUE_IS_DIFFERENT_TYPE:
90+
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! New value is a different type than the old value!");
91+
break;
92+
case INVALID_TYPE:
93+
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! The specified type is not supported for the specified action!");
94+
break;
95+
case EXISTENT_VALUE_IS_DIFFERENT_TYPE:
96+
plugin.debug(DebugLevel.HIGHEST, Level.INFO, "Invalid meta action! Existent value is a different type than the new value!");
97+
break;
98+
case VALUE_NOT_FOUND:
99+
case SUCCESS:
100+
default:
101+
break;
91102
}
92103
break;
93104

src/main/java/com/extendedclip/deluxemenus/command/DeluxeMenusCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import com.extendedclip.deluxemenus.command.subcommand.ExecuteCommand;
66
import com.extendedclip.deluxemenus.command.subcommand.HelpCommand;
77
import com.extendedclip.deluxemenus.command.subcommand.ListCommand;
8+
import com.extendedclip.deluxemenus.command.subcommand.MetaCommand;
89
import com.extendedclip.deluxemenus.command.subcommand.OpenCommand;
910
import com.extendedclip.deluxemenus.command.subcommand.ReloadCommand;
1011
import com.extendedclip.deluxemenus.command.subcommand.SubCommand;
11-
import com.extendedclip.deluxemenus.utils.DebugLevel;
1212
import com.extendedclip.deluxemenus.utils.Messages;
1313
import net.kyori.adventure.text.Component;
1414
import net.kyori.adventure.text.TextReplacementConfig;
@@ -24,7 +24,6 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Objects;
27-
import java.util.logging.Level;
2827
import java.util.stream.Collectors;
2928

3029
import static net.kyori.adventure.text.Component.text;
@@ -101,6 +100,7 @@ private void registerSubCommands() {
101100
new ExecuteCommand(plugin),
102101
new HelpCommand(plugin),
103102
new ListCommand(plugin),
103+
new MetaCommand(plugin),
104104
new OpenCommand(plugin),
105105
new ReloadCommand(plugin)
106106
);

src/main/java/com/extendedclip/deluxemenus/command/subcommand/ExecuteCommand.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,6 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
133133
return null;
134134
}
135135

136-
final List<String> onlinePlayerNames = Bukkit.getOnlinePlayers()
137-
.stream()
138-
.map(Player::getName)
139-
.collect(Collectors.toList());
140-
141-
if (onlinePlayerNames.isEmpty()) {
142-
return null;
143-
}
144-
145-
final String secondArgument = arguments.get(1).toLowerCase();
146-
147-
if (secondArgument.isEmpty()) {
148-
return onlinePlayerNames;
149-
}
150-
151-
return onlinePlayerNames.stream()
152-
.filter(playerName -> playerName.toLowerCase().startsWith(secondArgument))
153-
.collect(Collectors.toList());
136+
return getPlayerNameCompletion(arguments.get(1));
154137
}
155138
}

src/main/java/com/extendedclip/deluxemenus/command/subcommand/HelpCommand.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@ public HelpCommand(final @NotNull DeluxeMenus plugin) {
2323

2424
@Override
2525
public void execute(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
26+
if (sender.isOp()) {
27+
plugin.sms(sender, Messages.HELP_OP);
28+
return;
29+
}
30+
2631
if (sender.hasPermission(ADMIN_PERMISSION)) {
27-
plugin.sms(sender, Messages.HELP_ADMIN);
32+
plugin.sms(sender, Messages.HELP);
2833
return;
2934
}
3035

31-
plugin.sms(sender, Messages.HELP);
36+
plugin.sms(sender, Messages.NO_PERMISSION);
3237
}
3338

3439
@Override
3540
public @Nullable List<String> onTabComplete(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
41+
if (!sender.hasPermission(ADMIN_PERMISSION)) {
42+
return null;
43+
}
44+
3645
if (arguments.size() > 1) {
3746
return null;
3847
}

src/main/java/com/extendedclip/deluxemenus/command/subcommand/ListCommand.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.extendedclip.deluxemenus.DeluxeMenus;
44
import com.extendedclip.deluxemenus.menu.Menu;
55
import com.extendedclip.deluxemenus.utils.Messages;
6-
import com.google.common.primitives.Ints;
6+
import com.extendedclip.deluxemenus.utils.PaginationUtils;
77
import net.kyori.adventure.text.Component;
88
import net.kyori.adventure.text.TextComponent;
99
import net.kyori.adventure.text.event.ClickEvent;
@@ -32,7 +32,7 @@ public class ListCommand extends SubCommand {
3232

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

35-
public ListCommand(final @NotNull DeluxeMenus plugin) {
35+
public ListCommand(@NotNull final DeluxeMenus plugin) {
3636
super(plugin);
3737
}
3838

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

4444
@Override
45-
public void execute(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
45+
public void execute(@NotNull final CommandSender sender, @NotNull final List<String> arguments) {
4646
if (!sender.hasPermission(LIST_PERMISSION)) {
4747
plugin.sms(sender, Messages.NO_PERMISSION);
4848
return;
@@ -71,7 +71,7 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
7171
}
7272

7373
@Override
74-
public @Nullable List<String> onTabComplete(final @NotNull CommandSender sender, final @NotNull List<String> arguments) {
74+
public @Nullable List<String> onTabComplete(@NotNull final CommandSender sender, @NotNull final List<String> arguments) {
7575
if (!sender.hasPermission(LIST_PERMISSION)) {
7676
return null;
7777
}
@@ -124,7 +124,7 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
124124
.collect(Collectors.toList());
125125
}
126126

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

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

159-
private void sendPaginatedMenuList(final @NotNull CommandSender sender, final @NotNull Map<String, List<Menu>> menus,
160-
final @NotNull List<Menu> configMenus, final @NotNull List<String> args) {
161-
final int totalMenusCount = configMenus.size() + menus.values().stream().mapToInt(List::size).sum();
162-
163-
Integer page = null;
164-
if (totalMenusCount > plugin.getGeneralConfig().menusListPageSize() && !args.isEmpty()) {
165-
page = Ints.tryParse(args.get(0));
166-
}
159+
private void sendPaginatedMenuList(@NotNull final CommandSender sender, @NotNull final Map<String, List<Menu>> menus,
160+
@NotNull final List<Menu> configMenus, @NotNull final List<String> args) {
167161

168-
final int maxPages = (int) Math.ceil((double) totalMenusCount / plugin.getGeneralConfig().menusListPageSize());
169-
170-
if (page == null || page < 1) {
171-
page = 1;
172-
}
162+
final int menusPerPage = plugin.getGeneralConfig().menusListPageSize();
163+
final int totalMenusCount = configMenus.size() + menus.values().stream().mapToInt(List::size).sum();
164+
final int pagesCount = PaginationUtils.getPagesCount(menusPerPage, totalMenusCount);
173165

174-
if (page > maxPages) {
175-
page = maxPages;
176-
}
166+
final int page = PaginationUtils.parsePage(
167+
menusPerPage,
168+
totalMenusCount,
169+
pagesCount,
170+
args.isEmpty() ? null : args.get(0)
171+
);
177172

178173
final Map<String, List<Menu>> paginatedMenus = getPaginatedMenus(
179174
menus,
180175
configMenus.stream().collect(TreeMap::new, (map, menu) -> map.put(menu.options().name(), menu), TreeMap::putAll),
181176
page,
182-
plugin.getGeneralConfig().menusListPageSize()
177+
menusPerPage
183178
);
184179

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

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

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

204199
list.append(menuList);
205200

206-
if (page > 1 || page < maxPages) {
201+
if (page > 1 || page < pagesCount) {
207202
list.append(newline());
208203

209204
if (page > 1) {
@@ -214,12 +209,12 @@ private void sendPaginatedMenuList(final @NotNull CommandSender sender, final @N
214209
.append(text("Executes: /dm list " + (page - 1), NamedTextColor.GRAY))
215210
))
216211
.clickEvent(ClickEvent.runCommand("/dm list " + (page - 1))));
217-
if (page < maxPages) {
212+
if (page < pagesCount) {
218213
list.append(text(" | ", NamedTextColor.GREEN));
219214
}
220215
}
221216

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

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

373368
if (useAdminCommand) {

0 commit comments

Comments
 (0)