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.trackers.AutoJoinTracker;
|
||||
import com.alttd.afkdectector.trackers.SuspiciousKickTracker;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
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.minimessage.MiniMessage;
|
||||
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.PlayerQuitEvent;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
|
@ -53,11 +58,9 @@ public class AFKDetector extends JavaPlugin implements Listener {
|
|||
miniMessage = MiniMessage.miniMessage();
|
||||
loadConfig(null);
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
//getCommand("afk").setExecutor(new AFKCommand(this));
|
||||
getCommand("afklist").setExecutor(new AFKListCommand(this));
|
||||
getCommand("afkcheck").setExecutor(new AFKCheckCommand(this));
|
||||
getCommand("relogcheck").setExecutor(new RelogCheckCommand(this));
|
||||
getCommand("reloadafkdetector").setExecutor(new ReloadCommand(this));
|
||||
|
||||
registerCommands();
|
||||
|
||||
new AFKCheckTimer(this).init();
|
||||
myPetEnabled = getServer().getPluginManager().isPluginEnabled("MyPet");
|
||||
} 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
|
||||
public void onDisable() {
|
||||
this.getServer().getScheduler().cancelTasks(this);
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@ package com.alttd.afkdectector.command;
|
|||
import com.alttd.afkdectector.AFKDetector;
|
||||
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
||||
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.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
|
@ -21,45 +25,35 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
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;
|
||||
target.showTitle(Title.title(miniMessage.deserialize(Messages.AFK_CHECK_TITLE.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);
|
||||
Bukkit.getPluginManager().callEvent(commandEvent);
|
||||
player.performCommand(cmd);
|
||||
} 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
||||
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;
|
||||
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||
})
|
||||
)
|
||||
.build()
|
||||
, "Perform an afk check on the target."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,33 +4,39 @@ import com.alttd.afkdectector.AFKDetector;
|
|||
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
||||
import com.alttd.afkdectector.config.Config;
|
||||
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.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
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.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AFKListCommand implements CommandExecutor, TabCompleter {
|
||||
public class AFKListCommand {
|
||||
|
||||
private final AFKDetector plugin;
|
||||
|
||||
public AFKListCommand(AFKDetector plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||
public void register(Commands commands) {
|
||||
commands.register(
|
||||
Commands.literal("afklist")
|
||||
.requires(commandSourceStack ->
|
||||
commandSourceStack.getSender().hasPermission("afkdetector.afkcheck"))
|
||||
.executes(commandContext -> {
|
||||
int afkPlayers = 0;
|
||||
Component message = Component.empty();
|
||||
MiniMessage miniMessage = AFKDetector.miniMessage;
|
||||
for (AFKPlayer afkplayer : plugin.players.values()) {
|
||||
for (AFKPlayer afkplayer : AFKDetector.getInstance().players.values()) {
|
||||
long standingTime = afkplayer.getStandingTime();
|
||||
if (System.currentTimeMillis() - standingTime <= TimeUnit.MINUTES.toMillis(Config.TOGGLE_TIME)) {
|
||||
continue;
|
||||
|
|
@ -50,12 +56,12 @@ public class AFKListCommand implements CommandExecutor, TabCompleter {
|
|||
Placeholder.parsed("afkplayers", Integer.toString(afkPlayers))
|
||||
);
|
||||
Component component = miniMessage.deserialize(Messages.AFK_LIST.getMessage(), templates);
|
||||
sender.sendMessage(component.append(message));
|
||||
return true;
|
||||
}
|
||||
commandContext.getSource().getSender().sendMessage(component.append(message));
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
||||
return null;
|
||||
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||
})
|
||||
.build()
|
||||
, "List all players that are afk"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.afkdectector.command;
|
||||
|
||||
import com.alttd.afkdectector.AFKDetector;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
|
@ -9,22 +10,19 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
public class ReloadCommand implements CommandExecutor, TabCompleter {
|
||||
public class ReloadCommand {
|
||||
|
||||
private final AFKDetector plugin;
|
||||
|
||||
public ReloadCommand(AFKDetector plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||
AFKDetector.getInstance().loadConfig(sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
||||
return null;
|
||||
public void register(Commands commands) {
|
||||
commands.register(
|
||||
Commands.literal("reloadafkdetector")
|
||||
.requires(commandSourceStack ->
|
||||
commandSourceStack.getSender().hasPermission("afkdetector.reload"))
|
||||
.executes(commandContext -> {
|
||||
AFKDetector.getInstance().loadConfig(commandContext.getSource().getSender());
|
||||
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||
})
|
||||
.build()
|
||||
, "Reload the plugin configuration."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,10 @@ package com.alttd.afkdectector.command;
|
|||
import com.alttd.afkdectector.AFKDetector;
|
||||
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
||||
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.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
|
@ -21,45 +25,35 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
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;
|
||||
target.showTitle(Title.title(miniMessage.deserialize(Messages.RELOG_CHECK_TITLE.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);
|
||||
Bukkit.getPluginManager().callEvent(commandEvent);
|
||||
player.performCommand(cmd);
|
||||
} 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {
|
||||
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;
|
||||
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||
})
|
||||
)
|
||||
.build()
|
||||
, "Perform a relog check on the target."
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user