command to display which plugins have hooks registered

This commit is contained in:
2024-06-05 20:12:24 +02:00
parent 48f5c337d7
commit b7587970c0
3 changed files with 90 additions and 0 deletions
@@ -1,11 +1,13 @@
package me.youhavetrouble.yardwatch; package me.youhavetrouble.yardwatch;
import me.youhavetrouble.yardwatch.commands.YardWatchCommand;
import me.youhavetrouble.yardwatch.hooks.FactionsUUIDProtection; import me.youhavetrouble.yardwatch.hooks.FactionsUUIDProtection;
import me.youhavetrouble.yardwatch.hooks.GriefPreventionProtection; import me.youhavetrouble.yardwatch.hooks.GriefPreventionProtection;
import me.youhavetrouble.yardwatch.hooks.LWCXProtection; import me.youhavetrouble.yardwatch.hooks.LWCXProtection;
import me.youhavetrouble.yardwatch.hooks.SuperiorSkyBlockProtection; import me.youhavetrouble.yardwatch.hooks.SuperiorSkyBlockProtection;
import me.youhavetrouble.yardwatch.hooks.TownyProtection; import me.youhavetrouble.yardwatch.hooks.TownyProtection;
import me.youhavetrouble.yardwatch.hooks.WorldGuardProtection; import me.youhavetrouble.yardwatch.hooks.WorldGuardProtection;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicePriority;
@@ -17,6 +19,12 @@ public final class YardWatch extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
PluginCommand command = getCommand("yardwatch");
if (command != null) {
command.setExecutor(new YardWatchCommand(this));
}
if (shouldRegisterService("WorldGuard")) { if (shouldRegisterService("WorldGuard")) {
getServer().getServicesManager().register( getServer().getServicesManager().register(
Protection.class, new WorldGuardProtection(this), this, ServicePriority.Normal Protection.class, new WorldGuardProtection(this), this, ServicePriority.Normal
@@ -0,0 +1,75 @@
package me.youhavetrouble.yardwatch.commands;
import me.youhavetrouble.yardwatch.Protection;
import me.youhavetrouble.yardwatch.YardWatch;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class YardWatchCommand implements TabExecutor {
private final YardWatch plugin;
private final String version;
public YardWatchCommand(YardWatch plugin) {
this.plugin = plugin;
this.version = plugin.getDescription().getVersion();
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length == 0) {
sendDefault(sender);
return true;
}
if (args[0].equalsIgnoreCase("hooks")) {
sendHooks(sender);
return true;
}
return false;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
List<String> completions = new ArrayList<>();
if (args.length == 1) {
completions.add("hooks");
return StringUtil.copyPartialMatches(args[0], completions, new ArrayList<>());
}
return completions;
}
private void sendDefault(CommandSender sender) {
sender.sendMessage("YardWatch " + version);
}
private void sendHooks(CommandSender sender) {
Map<String, Integer> hooks = new HashMap<>();
Collection<RegisteredServiceProvider<Protection>> protections = plugin.getServer().getServicesManager().getRegistrations(Protection.class);
for (RegisteredServiceProvider<Protection> protection : protections) {
hooks.merge(protection.getPlugin().getName(), 1, Integer::sum);
}
if (hooks.isEmpty()) {
sender.sendMessage("No hooks registered.");
return;
}
sender.sendMessage("Hooks:");
for (Map.Entry<String, Integer> entry : hooks.entrySet()) {
String hook = entry.getValue() == 1 ? "hook" : "hooks";
sender.sendMessage(String.format("%s -> %s %s registered", entry.getKey(), entry.getValue(), hook));
}
}
}
+7
View File
@@ -14,3 +14,10 @@ softdepend:
- "Factions" - "Factions"
- "SuperiorSkyblock2" - "SuperiorSkyblock2"
- "LWC" - "LWC"
commands:
yardwatch:
description: "YardWatch command"
usage: "/yardwatch <arg>"
permission: "yardwatch.command"
aliases:
- "yw"