Skip to content

Commit 7872416

Browse files
committed
feat: stuff
1 parent acfd9f7 commit 7872416

8 files changed

Lines changed: 139 additions & 46 deletions

File tree

pom.xml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>xyz.webmc</groupId>
55
<artifactId>wlib</artifactId>
6-
<version>1.1.1-SNAPSHOT</version>
6+
<version>1.1.2-SNAPSHOT</version>
77
<distributionManagement>
88
<repository>
99
<id>github</id>
@@ -86,22 +86,30 @@
8686
</executions>
8787
</plugin>
8888
<plugin>
89-
<groupId>net.revelc.code</groupId>
90-
<artifactId>impsort-maven-plugin</artifactId>
91-
<version>1.13.0</version>
89+
<groupId>com.diffplug.spotless</groupId>
90+
<artifactId>spotless-maven-plugin</artifactId>
91+
<version>3.4.0</version>
92+
<configuration>
93+
<java>
94+
<cleanthat>
95+
<sourceJdk>${maven.compiler.source}</sourceJdk>
96+
</cleanthat>
97+
<removeUnusedImports />
98+
<importOrder>
99+
<order>xyz.webmc,java,javax,</order>
100+
</importOrder>
101+
<trimTrailingWhitespace />
102+
<endWithNewline />
103+
</java>
104+
</configuration>
92105
<executions>
93106
<execution>
94-
<id>sort-imports</id>
107+
<phase>process-sources</phase>
95108
<goals>
96-
<goal>sort</goal>
109+
<goal>apply</goal>
97110
</goals>
98111
</execution>
99112
</executions>
100-
<configuration>
101-
<removeUnused>true</removeUnused>
102-
<staticAfter>true</staticAfter>
103-
<groups>xyz.webmc.,java.,javax.</groups>
104-
</configuration>
105113
</plugin>
106114
</plugins>
107115
</build>

src/main/java/xyz/webmc/wlib/WLIBBukkitPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package xyz.webmc.wlib;
22

3+
import xyz.webmc.wlib.command.WLIBCommand;
34
import xyz.webmc.wlib.util.CommandUtil;
45
import xyz.webmc.wlib.util.EventUtil;
56
import xyz.webmc.wlib.util.SchedulerUtil;
7+
import xyz.webmc.wlib.util.WLIBUtil;
68

79
import org.bukkit.plugin.java.JavaPlugin;
810

@@ -12,6 +14,8 @@ public final void onEnable() {
1214
CommandUtil.init(this);
1315
EventUtil.init(this);
1416
SchedulerUtil.init(this);
17+
WLIBUtil.registerPlugin(this);
18+
CommandUtil.registerCommand(new WLIBCommand());
1519
}
1620

1721
@Override
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package xyz.webmc.wlib.command;
2+
3+
import xyz.webmc.wlib.util.CommandUtil;
4+
import xyz.webmc.wlib.util.WLIBUtil;
5+
6+
import org.bukkit.command.Command;
7+
import org.bukkit.command.CommandSender;
8+
9+
public final class WLIBCommand extends Command {
10+
public WLIBCommand() {
11+
super("wlib");
12+
}
13+
14+
@Override
15+
public final boolean execute(final CommandSender sender, final String label, final String[] args) {
16+
boolean bool = true;
17+
if (args.length > 0) {
18+
final String arg = args[0].trim();
19+
if (arg.equals("plugins") || arg.equals("pl")) {
20+
WLIBUtil.sendStringCountMessage(sender, "WLIB Plugins", WLIBUtil.getWLIBPluginNames());
21+
bool = false;
22+
}
23+
}
24+
25+
if (bool) {
26+
CommandUtil.sendUnknownCommandString(sender);
27+
}
28+
29+
return true;
30+
}
31+
}

src/main/java/xyz/webmc/wlib/util/AlertUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static final void devAlert(final String... msg) {
1414
final String str = ChatColor.DARK_GREEN + "[" + ChatColor.GREEN + "DEV" + ChatColor.DARK_GREEN + "] ["
1515
+ ChatColor.GREEN + clazz.getSimpleName() + ChatColor.DARK_GREEN + "] " + ChatColor.RESET
1616
+ String.join(" ", msg);
17+
1718
for (final Player p : Bukkit.getOnlinePlayers()) {
1819
if (p.hasPermission("wlib.dev-alert")) {
1920
p.sendMessage(str);
Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package xyz.webmc.wlib.util;
22

3+
import java.util.List;
34
import java.util.Map;
45

5-
import dev.colbster937.reflect.Mirror;
6-
6+
import dev.colbster937.reflect.MirrorSafe;
77
import org.bukkit.Bukkit;
88
import org.bukkit.command.Command;
9+
import org.bukkit.command.CommandException;
910
import org.bukkit.command.CommandMap;
11+
import org.bukkit.command.CommandSender;
1012
import org.bukkit.plugin.Plugin;
11-
import org.bukkit.plugin.PluginManager;
1213

1314
public final class CommandUtil {
1415
private static Plugin plugin;
@@ -18,51 +19,60 @@ public static final void init(final Plugin plugin) {
1819
}
1920

2021
public static final void registerCommand(final Plugin plugin, final Command command) {
21-
commandMap().register(plugin.getName(), command);
22+
getCommandMap().register(plugin.getName(), command);
2223
syncCommands();
2324
}
2425

2526
public static final void registerCommand(final Command command) {
2627
registerCommand(plugin, command);
2728
}
2829

30+
public static final void registerCommands(final Plugin plugin, final List<Command> commands) {
31+
getCommandMap().registerAll(plugin.getName(), commands);
32+
syncCommands();
33+
}
34+
35+
public static final void registerCommands(final List<Command> commands) {
36+
registerCommands(plugin, commands);
37+
}
38+
2939
public static final void unregisterCommand(final String commandStr) {
30-
try {
31-
final Command command = knownCommands().remove(commandStr);
32-
if (command != null) {
33-
command.unregister(commandMap());
34-
}
35-
syncCommands();
36-
} catch (final ReflectiveOperationException ex) {
37-
throw new RuntimeException(ex);
40+
final Command command = getKnownCommands().remove(commandStr);
41+
42+
if (command != null) {
43+
command.unregister(getCommandMap());
3844
}
45+
46+
syncCommands();
3947
}
4048

41-
private static final CommandMap commandMap() {
42-
final CommandMap commandMap;
49+
public static final boolean dispatch(final CommandSender sender, final String cmd) throws CommandException {
50+
return getCommandMap().dispatch(sender, cmd);
51+
}
4352

44-
try {
45-
final PluginManager pm = Bukkit.getPluginManager();
46-
commandMap = Mirror.getFieldValue(pm, "commandMap");
47-
} catch (final ReflectiveOperationException ex) {
48-
throw new RuntimeException(ex);
49-
}
53+
public static final List<String> tabComplete(final CommandSender sender, final String cmd) throws IllegalArgumentException {
54+
return getCommandMap().tabComplete(sender, cmd);
55+
}
56+
57+
public static final Command getCommand(final String cmd) {
58+
return getCommandMap().getCommand(cmd);
59+
}
60+
61+
public static final void sendUnknownCommandString(final CommandSender sender) {
62+
final Class<?> clazz = MirrorSafe.getClass("org.spigotmc.SpigotConfig");
63+
final String msg = MirrorSafe.getFieldValue(clazz, "unknownCommandMessage");
64+
sender.sendMessage(msg);
65+
}
5066

51-
return commandMap;
67+
private static CommandMap getCommandMap() {
68+
return MirrorSafe.getFieldValue(Bukkit.getPluginManager(), "commandMap");
5269
}
5370

54-
private static final Map<String, Command> knownCommands()
55-
throws ReflectiveOperationException {
56-
return Mirror.getFieldValue(commandMap(), "knownCommands");
71+
private static Map<String, Command> getKnownCommands() {
72+
return MirrorSafe.getFieldValue(getCommandMap(), "knownCommands");
5773
}
5874

59-
private static final void syncCommands() {
60-
SchedulerUtil.runAsync(() -> {
61-
try {
62-
Mirror.invokeMethod(Bukkit.getServer(), "syncCommands");
63-
} catch (final ReflectiveOperationException ex) {
64-
throw new RuntimeException(ex);
65-
}
66-
});
75+
private static void syncCommands() {
76+
MirrorSafe.invokeMethod(Bukkit.getServer(), "syncCommands");
6777
}
6878
}

src/main/java/xyz/webmc/wlib/util/MagicUtil.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package xyz.webmc.wlib.util;
22

3+
4+
import dev.colbster937.reflect.Mirror;
5+
import dev.colbster937.reflect.MirrorSafe;
36
import org.bukkit.Chunk;
47
import org.bukkit.Location;
58
import org.bukkit.Material;
69
import org.bukkit.World;
710
import org.bukkit.entity.Player;
811

9-
import dev.colbster937.reflect.Mirror;
10-
import dev.colbster937.reflect.MirrorSafe;
11-
1212
@SuppressWarnings({ "deprecation" })
1313
public final class MagicUtil {
1414
public static final void sendFakeBlock(final Location loc, final Material mat, final byte dat) {
@@ -17,9 +17,11 @@ public static final void sendFakeBlock(final Location loc, final Material mat, f
1717

1818
for (final Player plr : wrld.getPlayers()) {
1919
boolean sent = true;
20+
2021
if (Mirror.hasMethod(plr.getClass(), "isChunkSent", Chunk.class)) {
2122
sent = MirrorSafe.invokeMethod(plr, "isChunkSent", chnk);
2223
}
24+
2325
if (sent) {
2426
plr.sendBlockChange(loc, mat.getId(), dat);
2527
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package xyz.webmc.wlib.util;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
import org.bukkit.ChatColor;
8+
import org.bukkit.command.CommandSender;
9+
import org.bukkit.plugin.Plugin;
10+
11+
public final class WLIBUtil {
12+
private static final List<Plugin> plugins = new ArrayList<>();
13+
14+
public static final void registerPlugin(final Plugin plugin) {
15+
plugins.add(plugin);
16+
}
17+
18+
public static final List<Plugin> getWLIBPlugins() {
19+
return plugins;
20+
}
21+
22+
public static final List<String> getWLIBPluginNames() {
23+
final List<String> plugins = new ArrayList<>();
24+
25+
for (final Plugin plugin : getWLIBPlugins()) {
26+
plugins.add(plugin.getName());
27+
}
28+
29+
return plugins;
30+
}
31+
32+
public static final void sendStringCountMessage(final CommandSender sender, final String name, final List<String> lst) {
33+
Collections.sort(lst, String.CASE_INSENSITIVE_ORDER);
34+
sender.sendMessage("WLIB Plugins (" + plugins.size() + "): " + ChatColor.GREEN + String.join(ChatColor.RESET + ", " + ChatColor.GREEN, lst));
35+
}
36+
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ name: ${plugin.name}
22
version: ${plugin.vers}
33
main: ${plugin.pckg}.${plugin.name}BukkitPlugin
44
folia-supported: true
5+
api-version: 1.13
56
load: STARTUP

0 commit comments

Comments
 (0)