Refactor commands to use Brigadier.
This commit is contained in:
parent
f82160d7d3
commit
b9a7e6a7e4
|
|
@ -10,7 +10,10 @@ import com.alttd.afkdectector.config.Messages;
|
||||||
import com.alttd.afkdectector.config.MessagesConfig;
|
import com.alttd.afkdectector.config.MessagesConfig;
|
||||||
import com.alttd.afkdectector.trackers.AutoJoinTracker;
|
import com.alttd.afkdectector.trackers.AutoJoinTracker;
|
||||||
import com.alttd.afkdectector.trackers.SuspiciousKickTracker;
|
import com.alttd.afkdectector.trackers.SuspiciousKickTracker;
|
||||||
|
import io.papermc.paper.command.brigadier.Commands;
|
||||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||||
|
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
||||||
|
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||||
|
|
@ -25,10 +28,12 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
@ -53,11 +58,9 @@ public class AFKDetector extends JavaPlugin implements Listener {
|
||||||
miniMessage = MiniMessage.miniMessage();
|
miniMessage = MiniMessage.miniMessage();
|
||||||
loadConfig(null);
|
loadConfig(null);
|
||||||
getServer().getPluginManager().registerEvents(this, this);
|
getServer().getPluginManager().registerEvents(this, this);
|
||||||
//getCommand("afk").setExecutor(new AFKCommand(this));
|
|
||||||
getCommand("afklist").setExecutor(new AFKListCommand(this));
|
registerCommands();
|
||||||
getCommand("afkcheck").setExecutor(new AFKCheckCommand(this));
|
|
||||||
getCommand("relogcheck").setExecutor(new RelogCheckCommand(this));
|
|
||||||
getCommand("reloadafkdetector").setExecutor(new ReloadCommand(this));
|
|
||||||
new AFKCheckTimer(this).init();
|
new AFKCheckTimer(this).init();
|
||||||
myPetEnabled = getServer().getPluginManager().isPluginEnabled("MyPet");
|
myPetEnabled = getServer().getPluginManager().isPluginEnabled("MyPet");
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
|
|
@ -69,6 +72,17 @@ public class AFKDetector extends JavaPlugin implements Listener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void registerCommands() {
|
||||||
|
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
|
||||||
|
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
|
||||||
|
final Commands commands = event.registrar();
|
||||||
|
new AFKListCommand().register(commands);
|
||||||
|
new AFKCheckCommand().register(commands);
|
||||||
|
new RelogCheckCommand().register(commands);
|
||||||
|
new ReloadCommand().register(commands);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
this.getServer().getScheduler().cancelTasks(this);
|
this.getServer().getScheduler().cancelTasks(this);
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,10 @@ package com.alttd.afkdectector.command;
|
||||||
import com.alttd.afkdectector.AFKDetector;
|
import com.alttd.afkdectector.AFKDetector;
|
||||||
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
||||||
import com.alttd.afkdectector.config.Messages;
|
import com.alttd.afkdectector.config.Messages;
|
||||||
|
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||||
|
import io.papermc.paper.command.brigadier.Commands;
|
||||||
|
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
|
||||||
|
import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
|
|
@ -21,45 +25,35 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class AFKCheckCommand implements CommandExecutor, TabCompleter {
|
public class AFKCheckCommand {
|
||||||
|
|
||||||
private final AFKDetector plugin;
|
public void register(Commands commands) {
|
||||||
|
commands.register(
|
||||||
|
Commands.literal("afkcheck")
|
||||||
|
.requires(commandSourceStack ->
|
||||||
|
commandSourceStack.getSender().hasPermission("afkdetector.afkcheck"))
|
||||||
|
.then(Commands.argument("player", ArgumentTypes.player())
|
||||||
|
.executes(commandContext -> {
|
||||||
|
CommandSourceStack sourceStack = commandContext.getSource();
|
||||||
|
Player target = commandContext.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(sourceStack).getFirst();
|
||||||
|
|
||||||
public AFKCheckCommand(AFKDetector plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
|
||||||
if (!(args.length > 0)) {
|
|
||||||
sender.sendMessage(Component.text(command.getUsage(), NamedTextColor.RED));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Player target = Bukkit.getPlayer(args[0]);
|
|
||||||
if (target == null) {
|
|
||||||
sender.sendMessage(Component.text(command.getUsage(), NamedTextColor.RED));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
MiniMessage miniMessage = AFKDetector.miniMessage;
|
MiniMessage miniMessage = AFKDetector.miniMessage;
|
||||||
target.showTitle(Title.title(miniMessage.deserialize(Messages.AFK_CHECK_TITLE.getMessage()),
|
target.showTitle(Title.title(miniMessage.deserialize(Messages.AFK_CHECK_TITLE.getMessage()),
|
||||||
miniMessage.deserialize(Messages.AFK_CHECK_SUBTITLE.getMessage())));
|
miniMessage.deserialize(Messages.AFK_CHECK_SUBTITLE.getMessage())));
|
||||||
if (sender instanceof Player player) {
|
|
||||||
String cmd = "msg " + args[0] + " " + Messages.AFK_CHECK_MESSAGE.getMessage();
|
if (commandContext.getSource().getSender() instanceof Player player) {
|
||||||
|
String cmd = "msg " + target.getName() + " " + Messages.AFK_CHECK_MESSAGE.getMessage();
|
||||||
PlayerCommandPreprocessEvent commandEvent = new PlayerCommandPreprocessEvent(player, "/" + cmd);
|
PlayerCommandPreprocessEvent commandEvent = new PlayerCommandPreprocessEvent(player, "/" + cmd);
|
||||||
Bukkit.getPluginManager().callEvent(commandEvent);
|
Bukkit.getPluginManager().callEvent(commandEvent);
|
||||||
player.performCommand(cmd);
|
player.performCommand(cmd);
|
||||||
} else {
|
} else {
|
||||||
Bukkit.dispatchCommand(sender, "msg " + args[0] + " " + Messages.AFK_CHECK_MESSAGE.getMessage());
|
Bukkit.dispatchCommand(commandContext.getSource().getSender(), "msg " + target.getName() + " " + Messages.AFK_CHECK_MESSAGE.getMessage());
|
||||||
}
|
}
|
||||||
return true;
|
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||||
}
|
})
|
||||||
|
)
|
||||||
@Override
|
.build()
|
||||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
, "Perform an afk check on the target."
|
||||||
List<String> completions = new ArrayList<>();
|
);
|
||||||
if (strings.length == 1) {
|
|
||||||
StringUtil.copyPartialMatches(strings[0], plugin.players.values().stream().filter(AFKPlayer::isAFK).map(AFKPlayer::getPlayerName).collect(Collectors.toList()), completions);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,33 +4,39 @@ import com.alttd.afkdectector.AFKDetector;
|
||||||
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
||||||
import com.alttd.afkdectector.config.Config;
|
import com.alttd.afkdectector.config.Config;
|
||||||
import com.alttd.afkdectector.config.Messages;
|
import com.alttd.afkdectector.config.Messages;
|
||||||
|
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||||
|
import io.papermc.paper.command.brigadier.Commands;
|
||||||
|
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
|
||||||
|
import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||||
|
import net.kyori.adventure.title.Title;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabCompleter;
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class AFKListCommand implements CommandExecutor, TabCompleter {
|
public class AFKListCommand {
|
||||||
|
|
||||||
private final AFKDetector plugin;
|
public void register(Commands commands) {
|
||||||
|
commands.register(
|
||||||
public AFKListCommand(AFKDetector plugin) {
|
Commands.literal("afklist")
|
||||||
this.plugin = plugin;
|
.requires(commandSourceStack ->
|
||||||
}
|
commandSourceStack.getSender().hasPermission("afkdetector.afkcheck"))
|
||||||
|
.executes(commandContext -> {
|
||||||
@Override
|
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
|
||||||
int afkPlayers = 0;
|
int afkPlayers = 0;
|
||||||
Component message = Component.empty();
|
Component message = Component.empty();
|
||||||
MiniMessage miniMessage = AFKDetector.miniMessage;
|
MiniMessage miniMessage = AFKDetector.miniMessage;
|
||||||
for (AFKPlayer afkplayer : plugin.players.values()) {
|
for (AFKPlayer afkplayer : AFKDetector.getInstance().players.values()) {
|
||||||
long standingTime = afkplayer.getStandingTime();
|
long standingTime = afkplayer.getStandingTime();
|
||||||
if (System.currentTimeMillis() - standingTime <= TimeUnit.MINUTES.toMillis(Config.TOGGLE_TIME)) {
|
if (System.currentTimeMillis() - standingTime <= TimeUnit.MINUTES.toMillis(Config.TOGGLE_TIME)) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -50,12 +56,12 @@ public class AFKListCommand implements CommandExecutor, TabCompleter {
|
||||||
Placeholder.parsed("afkplayers", Integer.toString(afkPlayers))
|
Placeholder.parsed("afkplayers", Integer.toString(afkPlayers))
|
||||||
);
|
);
|
||||||
Component component = miniMessage.deserialize(Messages.AFK_LIST.getMessage(), templates);
|
Component component = miniMessage.deserialize(Messages.AFK_LIST.getMessage(), templates);
|
||||||
sender.sendMessage(component.append(message));
|
commandContext.getSource().getSender().sendMessage(component.append(message));
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
})
|
||||||
return null;
|
.build()
|
||||||
|
, "List all players that are afk"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.alttd.afkdectector.command;
|
package com.alttd.afkdectector.command;
|
||||||
|
|
||||||
import com.alttd.afkdectector.AFKDetector;
|
import com.alttd.afkdectector.AFKDetector;
|
||||||
|
import io.papermc.paper.command.brigadier.Commands;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
@ -9,22 +10,19 @@ import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ReloadCommand implements CommandExecutor, TabCompleter {
|
public class ReloadCommand {
|
||||||
|
|
||||||
private final AFKDetector plugin;
|
public void register(Commands commands) {
|
||||||
|
commands.register(
|
||||||
public ReloadCommand(AFKDetector plugin) {
|
Commands.literal("reloadafkdetector")
|
||||||
this.plugin = plugin;
|
.requires(commandSourceStack ->
|
||||||
}
|
commandSourceStack.getSender().hasPermission("afkdetector.reload"))
|
||||||
|
.executes(commandContext -> {
|
||||||
@Override
|
AFKDetector.getInstance().loadConfig(commandContext.getSource().getSender());
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||||
AFKDetector.getInstance().loadConfig(sender);
|
})
|
||||||
return true;
|
.build()
|
||||||
}
|
, "Reload the plugin configuration."
|
||||||
|
);
|
||||||
@Override
|
|
||||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,6 +3,10 @@ package com.alttd.afkdectector.command;
|
||||||
import com.alttd.afkdectector.AFKDetector;
|
import com.alttd.afkdectector.AFKDetector;
|
||||||
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
||||||
import com.alttd.afkdectector.config.Messages;
|
import com.alttd.afkdectector.config.Messages;
|
||||||
|
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||||
|
import io.papermc.paper.command.brigadier.Commands;
|
||||||
|
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
|
||||||
|
import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
|
|
@ -21,45 +25,35 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class RelogCheckCommand implements CommandExecutor, TabCompleter {
|
public class RelogCheckCommand {
|
||||||
|
|
||||||
private final AFKDetector plugin;
|
public void register(Commands commands) {
|
||||||
|
commands.register(
|
||||||
|
Commands.literal("relogcheck")
|
||||||
|
.requires(commandSourceStack ->
|
||||||
|
commandSourceStack.getSender().hasPermission("afkdetector.relogcheck"))
|
||||||
|
.then(Commands.argument("player", ArgumentTypes.player())
|
||||||
|
.executes(commandContext -> {
|
||||||
|
CommandSourceStack sourceStack = commandContext.getSource();
|
||||||
|
Player target = commandContext.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(sourceStack).getFirst();
|
||||||
|
|
||||||
public RelogCheckCommand(AFKDetector plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
|
||||||
if (!(args.length > 0)) {
|
|
||||||
sender.sendMessage(Component.text(command.getUsage(), NamedTextColor.RED));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Player target = Bukkit.getPlayer(args[0]);
|
|
||||||
if (target == null) {
|
|
||||||
sender.sendMessage(Component.text(command.getUsage(), NamedTextColor.RED));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
MiniMessage miniMessage = AFKDetector.miniMessage;
|
MiniMessage miniMessage = AFKDetector.miniMessage;
|
||||||
target.showTitle(Title.title(miniMessage.deserialize(Messages.RELOG_CHECK_TITLE.getMessage()),
|
target.showTitle(Title.title(miniMessage.deserialize(Messages.RELOG_CHECK_TITLE.getMessage()),
|
||||||
miniMessage.deserialize(Messages.RELOG_CHECK_SUBTITLE.getMessage())));
|
miniMessage.deserialize(Messages.RELOG_CHECK_SUBTITLE.getMessage())));
|
||||||
if (sender instanceof Player player) {
|
|
||||||
String cmd = "msg " + args[0] + " " + Messages.RELOG_CHECK_MESSAGE.getMessage();
|
if (commandContext.getSource().getSender() instanceof Player player) {
|
||||||
|
String cmd = "msg " + target.getName() + " " + Messages.RELOG_CHECK_MESSAGE.getMessage();
|
||||||
PlayerCommandPreprocessEvent commandEvent = new PlayerCommandPreprocessEvent(player, "/" + cmd);
|
PlayerCommandPreprocessEvent commandEvent = new PlayerCommandPreprocessEvent(player, "/" + cmd);
|
||||||
Bukkit.getPluginManager().callEvent(commandEvent);
|
Bukkit.getPluginManager().callEvent(commandEvent);
|
||||||
player.performCommand(cmd);
|
player.performCommand(cmd);
|
||||||
} else {
|
} else {
|
||||||
Bukkit.dispatchCommand(sender, "msg " + args[0] + " " + Messages.RELOG_CHECK_MESSAGE.getMessage());
|
Bukkit.dispatchCommand(commandContext.getSource().getSender(), "msg " + target.getName() + " " + Messages.RELOG_CHECK_MESSAGE.getMessage());
|
||||||
}
|
}
|
||||||
return true;
|
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||||
}
|
})
|
||||||
|
)
|
||||||
@Override
|
.build()
|
||||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
, "Perform a relog check on the target."
|
||||||
List<String> completions = new ArrayList<>();
|
);
|
||||||
if (strings.length == 1) {
|
|
||||||
StringUtil.copyPartialMatches(strings[0], plugin.players.values().stream().filter(AFKPlayer::isAFK).map(AFKPlayer::getPlayerName).collect(Collectors.toList()), completions);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user