Refactor TagCommand to use Brigadier.
This commit is contained in:
parent
960729e0c7
commit
e33fdd71f7
|
|
@ -96,4 +96,25 @@ paper {
|
|||
load = PaperPluginDescription.RelativeLoadOrder.BEFORE
|
||||
}
|
||||
}
|
||||
permissions {
|
||||
register("tag.play") {
|
||||
description = "Base permission for the ${rootProject.name} plugin."
|
||||
default = BukkitPluginDescription.Permission.Default.FALSE
|
||||
}
|
||||
register("tag.commands.admin") {
|
||||
description = "Admin permission for the ${rootProject.name} plugin."
|
||||
default = BukkitPluginDescription.Permission.Default.FALSE
|
||||
children = listOf(
|
||||
"tag.play"
|
||||
)
|
||||
}
|
||||
register("tag.commands.admin.location") {
|
||||
description = "Admin permission for setting the scoreboard location."
|
||||
default = BukkitPluginDescription.Permission.Default.FALSE
|
||||
children = listOf(
|
||||
"tag.play",
|
||||
"tag.commands.admin"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.altitudetag;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
|
@ -12,9 +13,13 @@ import com.alttd.altitudetag.configuration.Config;
|
|||
import com.alttd.altitudetag.configuration.Lang;
|
||||
import com.alttd.altitudetag.listeners.ConnectionListener;
|
||||
import com.alttd.altitudetag.listeners.InteractListener;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
||||
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.boss.BossBar;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
|
@ -71,11 +76,18 @@ public class AltitudeTag extends JavaPlugin
|
|||
|
||||
NotificationHandler.loadBossBar();
|
||||
|
||||
CommandHandler.initialize();
|
||||
CommandHandler.getInstance().registerCommand(new TagCommand(), this);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new ConnectionListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new InteractListener(), this);
|
||||
|
||||
registerCommands();
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
|
||||
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
|
||||
final Commands commands = event.registrar();
|
||||
commands.register(new TagCommand().command(), "Tag, you're it!", List.of("tags", "tagging", "tagger"));
|
||||
});
|
||||
}
|
||||
|
||||
private static void reloadConfiguration()
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
package com.alttd.altitudetag.commands;
|
||||
|
||||
import com.alttd.altitudeapi.commands.ValidBaseCommand;
|
||||
import com.alttd.altitudetag.Permission;
|
||||
import com.alttd.altitudetag.commands.admin.TagAdminLocationCommand;
|
||||
|
||||
public class TagAdminCommand extends ValidBaseCommand
|
||||
{
|
||||
protected TagAdminCommand()
|
||||
{
|
||||
super("admin", "Management commands", Permission.COMMAND_ADMIN.getPermission(), new String[]{ "a" });
|
||||
|
||||
addSubCommand(new TagAdminLocationCommand());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +1,55 @@
|
|||
package com.alttd.altitudetag.commands;
|
||||
|
||||
import com.alttd.altitudeapi.commands.ValidBaseCommand;
|
||||
import com.alttd.altitudetag.AltitudeTag;
|
||||
import com.alttd.altitudetag.Leaderboard;
|
||||
import com.alttd.altitudetag.configuration.Config;
|
||||
import com.alttd.altitudetag.configuration.Lang;
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TagCommand extends ValidBaseCommand
|
||||
public class TagCommand
|
||||
{
|
||||
public TagCommand()
|
||||
{
|
||||
super("tag", "Tag, you're it!", "tag.play", new String[]{ "tags", "tagging", "tagger" });
|
||||
public @NotNull LiteralCommandNode<CommandSourceStack> command() {
|
||||
final LiteralArgumentBuilder<CommandSourceStack> builder =
|
||||
Commands.literal("tag")
|
||||
.requires(commandSourceStack ->
|
||||
commandSourceStack.getSender() instanceof Player player &&
|
||||
player.hasPermission("tag.play")
|
||||
)
|
||||
.then(Commands.literal("admin")
|
||||
.requires(commandSourceStack ->
|
||||
commandSourceStack.getSender() instanceof Player player &&
|
||||
player.hasPermission("tag.commands.admin")
|
||||
)
|
||||
.then(Commands.literal("location")
|
||||
.requires(commandSourceStack ->
|
||||
commandSourceStack.getSender() instanceof Player player &&
|
||||
player.hasPermission("tag.commands.admin.location")
|
||||
)
|
||||
.executes((source) -> {
|
||||
if (!(source.getSource().getSender() instanceof Player player))
|
||||
return Command.SINGLE_SUCCESS;
|
||||
|
||||
addSubCommand(new TagAdminCommand());
|
||||
}
|
||||
Leaderboard.setLocation(player.getLocation());
|
||||
Lang.SET_LEADERBOARD_LOCATION.send(player);
|
||||
|
||||
@Override
|
||||
public void help(CommandSender sender, String[] label)
|
||||
{
|
||||
if (sender.hasPermission(getSubCommand("admin").getPermission()))
|
||||
{
|
||||
sender.sendMessage(Lang.renderString(Config.NOTIFICATION_GLOBAL_BOSS_BAR_MESSAGE.getValue(),
|
||||
"{player}", Bukkit.getOfflinePlayer(AltitudeTag.getTagger()).getName()));
|
||||
}
|
||||
else
|
||||
{
|
||||
super.help(sender, label);
|
||||
}
|
||||
return Command.SINGLE_SUCCESS;
|
||||
})
|
||||
)
|
||||
)
|
||||
.executes((source) -> {
|
||||
source.getSource().getSender()
|
||||
.sendMessage(Lang.renderString(Config.NOTIFICATION_GLOBAL_BOSS_BAR_MESSAGE.getValue(),
|
||||
"{player}", Bukkit.getOfflinePlayer(AltitudeTag.getTagger()).getName()));
|
||||
return Command.SINGLE_SUCCESS;
|
||||
})
|
||||
;
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
package com.alttd.altitudetag.commands.admin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alttd.altitudeapi.commands.CommandArgument;
|
||||
import com.alttd.altitudeapi.commands.ValidCommand;
|
||||
import com.alttd.altitudetag.Leaderboard;
|
||||
import com.alttd.altitudetag.Permission;
|
||||
import com.alttd.altitudetag.configuration.Lang;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class TagAdminLocationCommand extends ValidCommand
|
||||
{
|
||||
public TagAdminLocationCommand()
|
||||
{
|
||||
super("location",
|
||||
"Sets the leaderboard location",
|
||||
Permission.COMMAND_ADMIN_LOCATION.getPermission(),
|
||||
true,
|
||||
new String[]{ "loc", "setloc", "setlocation", "leaderboard", "board" });
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validRun(CommandSender commandSender, String[] strings, List<CommandArgument<?>> list)
|
||||
{
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
Leaderboard.setLocation(player.getLocation());
|
||||
|
||||
Lang.SET_LEADERBOARD_LOCATION.send(player);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
package com.alttd.altitudetag.commands.parsers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alttd.altitudeapi.commands.CommandHandler;
|
||||
import com.alttd.altitudeapi.commands.Parser;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class StringParser implements Parser<String>
|
||||
{
|
||||
@Override
|
||||
public String parseArgument(CommandSender sender, String[] label, String rawArgument)
|
||||
{
|
||||
return rawArgument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRecommendations(CommandSender sender, String lastWord)
|
||||
{
|
||||
return CommandHandler.defaultTabComplete(sender, lastWord);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user