Skip to content

Commit 7e1d53c

Browse files
author
BadAccuracyID
committed
- Improved PluginManagerUtils
- Improved plugin list message
1 parent 6d3c3ff commit 7e1d53c

File tree

9 files changed

+40
-32
lines changed

9 files changed

+40
-32
lines changed
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
100 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
340 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
18 Bytes
Binary file not shown.

plugin/src/main/java/id/luckynetwork/dev/lyrams/lej/commands/pluginmanager/PluginManagerCommand.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import cloud.commandframework.annotations.Argument;
44
import cloud.commandframework.annotations.CommandDescription;
55
import cloud.commandframework.annotations.CommandMethod;
6-
import cloud.commandframework.annotations.Flag;
76
import cloud.commandframework.annotations.specifier.Greedy;
87
import cloud.commandframework.annotations.suggestions.Suggestions;
98
import cloud.commandframework.context.CommandContext;
@@ -16,7 +15,6 @@
1615
import org.bukkit.command.CommandSender;
1716
import org.bukkit.plugin.Plugin;
1817
import org.checkerframework.checker.nullness.qual.NonNull;
19-
import org.checkerframework.checker.nullness.qual.Nullable;
2018

2119
import java.util.ArrayList;
2220
import java.util.Arrays;
@@ -29,20 +27,31 @@ public class PluginManagerCommand extends CommandClass {
2927
@CommandMethod("luckyessentials pluginmanager|pm list")
3028
@CommandDescription("Lists all loaded plugin")
3129
public void listCommand(
32-
final @NonNull CommandSender sender,
33-
final @Nullable @Flag(value = "fullname", aliases = "f", description = "Should it also show the plugin version?") Boolean fullName
30+
final @NonNull CommandSender sender
3431
) {
3532
if (!Utils.checkPermission(sender, "pluginmanager.list")) {
3633
return;
3734
}
3835

39-
boolean includeName = fullName != null && fullName;
40-
List<String> pluginNames = Arrays.stream(Bukkit.getPluginManager().getPlugins())
41-
.map(it -> PluginManagerUtils.getFormattedName(it, includeName))
42-
.sorted(Comparator.naturalOrder())
36+
List<PluginManagerUtils.PluginInfo> pluginInfos = Arrays.stream(Bukkit.getPluginManager().getPlugins())
37+
.map(PluginManagerUtils::getPluginInfo)
38+
.sorted(Comparator.comparing(PluginManagerUtils.PluginInfo::getRawName))
4339
.collect(Collectors.toCollection(ArrayList::new));
4440

45-
sender.sendMessage(plugin.getMainConfigManager().getPrefix() + "§ePlugins §d(" + pluginNames.size() + ")§e: " + Joiner.on(", ").join(pluginNames));
41+
final int[] size = {pluginInfos.size()};
42+
sender.sendMessage("§ePlugins §d(" + size[0] + ")§e: ");
43+
pluginInfos.forEach(it -> {
44+
boolean last = (--size[0] < 1);
45+
String message;
46+
if (last) {
47+
message = "§8└─ ";
48+
} else {
49+
message = "§8├─ ";
50+
}
51+
52+
message += it.getName() + " §7(§6v§e" + it.getVersion() + "§7)";
53+
sender.sendMessage(message);
54+
});
4655
}
4756

4857
@CommandMethod("luckyessentials pluginmanager|pm info <pluginName>")

plugin/src/main/java/id/luckynetwork/dev/lyrams/lej/utils/pluginmanager/PluginManagerUtils.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import id.luckynetwork.dev.lyrams.lej.LuckyEssentials;
55
import id.luckynetwork.dev.lyrams.lej.enums.TrueFalseType;
66
import id.luckynetwork.dev.lyrams.lej.utils.Utils;
7+
import lombok.Data;
78
import lombok.experimental.UtilityClass;
89
import org.bukkit.Bukkit;
910
import org.bukkit.command.Command;
@@ -106,17 +107,14 @@ public Plugin getPluginByName(String name) {
106107
* @param plugin the plugin to format
107108
* @return the formatted name
108109
*/
109-
public String getFormattedName(@NotNull Plugin plugin, boolean includeVersion) {
110+
public PluginInfo getPluginInfo(@NotNull Plugin plugin) {
110111
TrueFalseType trueFalseType = TrueFalseType.DEFAULT;
111112
trueFalseType.setIfTrue(plugin.getName());
112113
trueFalseType.setIfFalse(plugin.getName());
113114

114115
String pluginName = Utils.colorizeTrueFalse(plugin.isEnabled(), trueFalseType);
115-
if (includeVersion) {
116-
pluginName += " (" + plugin.getDescription().getVersion() + ")";
117-
}
118116

119-
return pluginName;
117+
return new PluginInfo(plugin.getName(), pluginName, plugin.getDescription().getVersion());
120118
}
121119

122120
/**
@@ -182,6 +180,7 @@ public Map<String, Command> getKnownCommands() {
182180
e.printStackTrace();
183181
return null;
184182
}
183+
185184
SimpleCommandMap commandMap;
186185
try {
187186
commandMap = (SimpleCommandMap) commandMapField.get(Bukkit.getServer());
@@ -199,7 +198,6 @@ public Map<String, Command> getKnownCommands() {
199198
}
200199

201200
Map<String, Command> knownCommands;
202-
203201
try {
204202
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
205203
} catch (IllegalAccessException e) {
@@ -245,7 +243,6 @@ public boolean load(String pluginName) {
245243
}
246244
} catch (InvalidDescriptionException e) {
247245
e.printStackTrace();
248-
return false;
249246
}
250247
}
251248
}
@@ -271,10 +268,9 @@ public boolean load(String pluginName) {
271268
luckyEssentials.getVersionSupport().getCommandWrap().wrap(command, alias);
272269
}
273270

274-
if (Bukkit.getOnlinePlayers().size() >= 1)
275-
for (Player player : Bukkit.getOnlinePlayers()) {
276-
luckyEssentials.getVersionSupport().reloadCommands(player);
277-
}
271+
if (Bukkit.getOnlinePlayers().size() >= 1) {
272+
Bukkit.getOnlinePlayers().forEach(player -> luckyEssentials.getVersionSupport().reloadCommands(player));
273+
}
278274
}, 10L);
279275
}
280276

@@ -308,10 +304,8 @@ public boolean unload(Plugin plugin) {
308304
}
309305
}
310306

311-
String name = plugin.getName();
312307

313308
PluginManager pluginManager = Bukkit.getPluginManager();
314-
315309
SimpleCommandMap commandMap = null;
316310

317311
List<Plugin> plugins = null;
@@ -351,33 +345,36 @@ public boolean unload(Plugin plugin) {
351345
e.printStackTrace();
352346
return false;
353347
}
354-
355348
}
356349

350+
assert pluginManager != null;
357351
pluginManager.disablePlugin(plugin);
358352

359353
if (plugins != null) {
360354
plugins.remove(plugin);
361355
}
362356

357+
String name = plugin.getName();
363358
if (names != null) {
364359
names.remove(name);
365360
}
366361

367-
if (listeners != null && reloadlisteners) for (SortedSet<RegisteredListener> set : listeners.values())
368-
set.removeIf(value -> value.getPlugin() == plugin);
362+
if (listeners != null) {
363+
listeners.values().forEach(set -> set.removeIf(value -> value.getPlugin() == plugin));
364+
}
369365

370-
if (commandMap != null)
366+
if (commandMap != null) {
371367
for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext(); ) {
372368
Map.Entry<String, Command> entry = it.next();
373369
if (entry.getValue() instanceof PluginCommand) {
374-
PluginCommand c = (PluginCommand) entry.getValue();
375-
if (c.getPlugin() == plugin) {
376-
c.unregister(commandMap);
370+
PluginCommand command = (PluginCommand) entry.getValue();
371+
if (command.getPlugin() == plugin) {
372+
command.unregister(commandMap);
377373
it.remove();
378374
}
379375
}
380376
}
377+
}
381378

382379
// Attempt to close the classloader to unlock any handles on the plugin's jar file.
383380
ClassLoader classLoader = plugin.getClass().getClassLoader();
@@ -390,18 +387,15 @@ public boolean unload(Plugin plugin) {
390387
Field pluginInitField = classLoader.getClass().getDeclaredField("pluginInit");
391388
pluginInitField.setAccessible(true);
392389
pluginInitField.set(classLoader, null);
393-
394390
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
395391
Logger.getLogger(PluginManagerUtils.class.getName()).log(Level.SEVERE, null, ex);
396392
}
397393

398394
try {
399-
400395
((URLClassLoader) classLoader).close();
401396
} catch (IOException ex) {
402397
Logger.getLogger(PluginManagerUtils.class.getName()).log(Level.SEVERE, null, ex);
403398
}
404-
405399
}
406400

407401
// Will not work on processes started with the -XX:+DisableExplicitGC flag, but let's try it anyway.
@@ -426,4 +420,9 @@ public boolean reload(Plugin plugin) {
426420

427421
return false;
428422
}
423+
424+
@Data
425+
public static class PluginInfo {
426+
private final String rawName, name, version;
427+
}
429428
}

0 commit comments

Comments
 (0)