From 83cb87dde2baab28b569c6be1754448dd32bca2d Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 20 Aug 2024 10:33:22 +0200 Subject: [PATCH 01/42] Move Command registration to its own function --- .../net/countercraft/movecraft/Movecraft.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index fe69859c1..a9c397067 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -198,16 +198,7 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new InteractListener(), this); - getCommand("movecraft").setExecutor(new MovecraftCommand()); - getCommand("release").setExecutor(new ReleaseCommand()); - getCommand("pilot").setExecutor(new PilotCommand()); - getCommand("rotate").setExecutor(new RotateCommand()); - getCommand("cruise").setExecutor(new CruiseCommand()); - getCommand("craftreport").setExecutor(new CraftReportCommand()); - getCommand("manoverboard").setExecutor(new ManOverboardCommand()); - getCommand("scuttle").setExecutor(new ScuttleCommand()); - getCommand("crafttype").setExecutor(new CraftTypeCommand()); - getCommand("craftinfo").setExecutor(new CraftInfoCommand()); + initializeCommands(); getServer().getPluginManager().registerEvents(new BlockListener(), this); getServer().getPluginManager().registerEvents(new PlayerListener(), this); @@ -244,6 +235,19 @@ public void onEnable() { logger.info("[V " + getDescription().getVersion() + "] has been enabled."); } + private void initializeCommands() { + getCommand("movecraft").setExecutor(new MovecraftCommand()); + getCommand("release").setExecutor(new ReleaseCommand()); + getCommand("pilot").setExecutor(new PilotCommand()); + getCommand("rotate").setExecutor(new RotateCommand()); + getCommand("cruise").setExecutor(new CruiseCommand()); + getCommand("craftreport").setExecutor(new CraftReportCommand()); + getCommand("manoverboard").setExecutor(new ManOverboardCommand()); + getCommand("scuttle").setExecutor(new ScuttleCommand()); + getCommand("crafttype").setExecutor(new CraftTypeCommand()); + getCommand("craftinfo").setExecutor(new CraftInfoCommand()); + } + @Override public void onLoad() { super.onLoad(); From 91bd35cf1518a6cf73b97525577888e895a4d594 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 20 Aug 2024 11:51:47 +0200 Subject: [PATCH 02/42] Add New API Dependency --- Movecraft/build.gradle.kts | 1 + buildSrc/src/main/kotlin/buildlogic.java-conventions.gradle.kts | 1 + 2 files changed, 2 insertions(+) diff --git a/Movecraft/build.gradle.kts b/Movecraft/build.gradle.kts index 683722e23..2ea1b250d 100644 --- a/Movecraft/build.gradle.kts +++ b/Movecraft/build.gradle.kts @@ -13,6 +13,7 @@ dependencies { runtimeOnly(project(":movecraft-v1_21", "reobf")) implementation(project(":movecraft-api")) compileOnly("org.yaml:snakeyaml:2.0") + implementation("co.aikar:acf-paper:0.5.1-SNAPSHOT") } tasks.shadowJar { diff --git a/buildSrc/src/main/kotlin/buildlogic.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/buildlogic.java-conventions.gradle.kts index 489d005a7..54245f6e9 100644 --- a/buildSrc/src/main/kotlin/buildlogic.java-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/buildlogic.java-conventions.gradle.kts @@ -7,6 +7,7 @@ repositories { maven("https://repo.maven.apache.org/maven2/") maven("https://oss.sonatype.org/content/repositories/snapshots/") maven("https://repo.papermc.io/repository/maven-public/") + maven("https://repo.aikar.co/content/groups/aikar/") } group = "net.countercraft" From ed6ee2058208a50aacfa9609f48f8af4f6292644 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 20 Aug 2024 12:14:55 +0200 Subject: [PATCH 03/42] MovecraftCommand adapted to new API --- .../net/countercraft/movecraft/Movecraft.java | 5 +- .../movecraft/commands/MovecraftCommand.java | 56 +++++-------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index a9c397067..5cb2cad14 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -17,13 +17,13 @@ package net.countercraft.movecraft; +import co.aikar.commands.PaperCommandManager; import io.papermc.paper.datapack.Datapack; import net.countercraft.movecraft.async.AsyncManager; import net.countercraft.movecraft.commands.*; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.ChunkManager; import net.countercraft.movecraft.craft.CraftManager; -import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry; import net.countercraft.movecraft.features.contacts.ContactsCommand; import net.countercraft.movecraft.features.contacts.ContactsManager; import net.countercraft.movecraft.features.contacts.ContactsSign; @@ -236,7 +236,8 @@ public void onEnable() { } private void initializeCommands() { - getCommand("movecraft").setExecutor(new MovecraftCommand()); + PaperCommandManager pcm = new PaperCommandManager(this); + pcm.registerCommand(new MovecraftCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java index 6bb739cbd..d9e1138cc 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java @@ -1,56 +1,30 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.*; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.localisation.I18nSupport; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.plugin.PluginDescriptionFile; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; -public class MovecraftCommand implements TabExecutor { - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { - if (!command.getName().equalsIgnoreCase("movecraft")) { - return false; - } - if(!commandSender.hasPermission("movecraft.commands.movecraft")){ - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; - } - - if(args.length == 0){ - PluginDescriptionFile descriptionFile = Movecraft.getInstance().getDescription(); - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + "Movecraft " + descriptionFile.getVersion() + " by " + descriptionFile.getAuthors()); - return true; - } - - if(args.length==1 && args[0].equalsIgnoreCase("reloadtypes") && commandSender.hasPermission("movecraft.commands.movecraft.reloadtypes")){ - CraftManager.getInstance().reloadCraftTypes(); - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Movecraft - Reloaded Types")); - return true; - } - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Movecraft - Invalid Argument")); - return true; +@CommandAlias("movecraft") +@CommandPermission("movecraft.commands.movecraft") +public class MovecraftCommand extends BaseCommand { + @Default + @Syntax("") + public static void displayAuthors(CommandSender commandSender) { + PluginDescriptionFile descriptionFile = Movecraft.getInstance().getDescription(); + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + "Movecraft " + descriptionFile.getVersion() + " by " + descriptionFile.getAuthors()); } - @Override - public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { - if(args.length !=1 || !commandSender.hasPermission("movecraft.commands") || !commandSender.hasPermission("movecraft.commands.movecraft")) - return Collections.emptyList(); - List completions = new ArrayList<>(); - completions.add("reloadtypes"); - List returnValues = new ArrayList<>(); - for(String completion : completions) - if(completion.toLowerCase().startsWith(args[args.length-1].toLowerCase())) - returnValues.add(completion); - return returnValues; + @CommandPermission("movecraft.commands.movecraft.reloadtypes") + @Subcommand("reloadtypes") + public static void reloadTypes(CommandSender commandSender) { + CraftManager.getInstance().reloadCraftTypes(); + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Movecraft - Reloaded Types")); } } From 711020fbc4f8406bbe2de9acdbd4383ff1e32a03 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 20 Aug 2024 15:45:28 +0200 Subject: [PATCH 04/42] Shade aikar's ACF --- Movecraft/build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Movecraft/build.gradle.kts b/Movecraft/build.gradle.kts index 2ea1b250d..315178ecc 100644 --- a/Movecraft/build.gradle.kts +++ b/Movecraft/build.gradle.kts @@ -26,6 +26,8 @@ tasks.shadowJar { include(project(":movecraft-v1_18")) include(project(":movecraft-v1_20")) include(project(":movecraft-v1_21")) + relocate("co.aikar.commands", "Movecraft.acf") + relocate("co.aikar.locales", "Movecraft.locales") } } From fe417e9b4641608cde740833fc9c3a0a1310285f Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 20 Aug 2024 16:04:37 +0200 Subject: [PATCH 05/42] Add description to MovecraftCommand --- .../net/countercraft/movecraft/commands/MovecraftCommand.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java index d9e1138cc..cd8b1c184 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java @@ -16,6 +16,7 @@ public class MovecraftCommand extends BaseCommand { @Default @Syntax("") + @Description("Get the authors list of the plugin.") public static void displayAuthors(CommandSender commandSender) { PluginDescriptionFile descriptionFile = Movecraft.getInstance().getDescription(); commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + "Movecraft " + descriptionFile.getVersion() + " by " + descriptionFile.getAuthors()); @@ -23,6 +24,7 @@ public static void displayAuthors(CommandSender commandSender) { @CommandPermission("movecraft.commands.movecraft.reloadtypes") @Subcommand("reloadtypes") + @Description("Reload craft types from file.") public static void reloadTypes(CommandSender commandSender) { CraftManager.getInstance().reloadCraftTypes(); commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Movecraft - Reloaded Types")); From 8c5b9ae557b609452a3d05c08311e0681f8b1ea8 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 20 Aug 2024 17:49:23 +0200 Subject: [PATCH 06/42] Shade attempt 2 --- Movecraft/build.gradle.kts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Movecraft/build.gradle.kts b/Movecraft/build.gradle.kts index 315178ecc..e5b93d389 100644 --- a/Movecraft/build.gradle.kts +++ b/Movecraft/build.gradle.kts @@ -26,9 +26,11 @@ tasks.shadowJar { include(project(":movecraft-v1_18")) include(project(":movecraft-v1_20")) include(project(":movecraft-v1_21")) - relocate("co.aikar.commands", "Movecraft.acf") - relocate("co.aikar.locales", "Movecraft.locales") + include(dependency("co.aikar:acf-paper:0.5.1-SNAPSHOT")) } + + relocate("co.aikar.commands", "Movecraft.acf") + relocate("co.aikar.locales", "Movecraft.locales") } tasks.processResources { From 02abbc3b2438e126ab82cca8e73b55de50e22bfa Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 11:41:53 +0200 Subject: [PATCH 07/42] Update command description --- .../net/countercraft/movecraft/commands/MovecraftCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java index cd8b1c184..993b88534 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommand.java @@ -16,7 +16,7 @@ public class MovecraftCommand extends BaseCommand { @Default @Syntax("") - @Description("Get the authors list of the plugin.") + @Description("Base movecraft command") public static void displayAuthors(CommandSender commandSender) { PluginDescriptionFile descriptionFile = Movecraft.getInstance().getDescription(); commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + "Movecraft " + descriptionFile.getVersion() + " by " + descriptionFile.getAuthors()); From cf7d2889910169c93ddec31084b61506ddd14f57 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 12:15:13 +0200 Subject: [PATCH 08/42] Add CraftInfoCommand --- .../net/countercraft/movecraft/Movecraft.java | 2 +- .../movecraft/commands/CraftInfoCommand.java | 100 +++++++++++------- 2 files changed, 60 insertions(+), 42 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 5cb2cad14..dc7a419b3 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -238,6 +238,7 @@ public void onEnable() { private void initializeCommands() { PaperCommandManager pcm = new PaperCommandManager(this); pcm.registerCommand(new MovecraftCommand()); + pcm.registerCommand(new CraftInfoCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); @@ -246,7 +247,6 @@ private void initializeCommands() { getCommand("manoverboard").setExecutor(new ManOverboardCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); getCommand("crafttype").setExecutor(new CraftTypeCommand()); - getCommand("craftinfo").setExecutor(new CraftInfoCommand()); } @Override diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java index 92fd2cfe3..92fb8a081 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java @@ -1,14 +1,16 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Syntax; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.util.MathUtils; import net.countercraft.movecraft.util.TopicPaginator; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -17,7 +19,7 @@ import java.util.OptionalInt; import java.util.function.Function; -public class CraftInfoCommand implements TabExecutor { +public class CraftInfoCommand extends BaseCommand { private static final List>> providers = new ArrayList<>(); static { registerMultiProvider(CraftInfoCommand::allowedBlockProvider); @@ -48,51 +50,72 @@ public static void registerProvider(@NotNull Function provider){ providers.add(provider.andThen(List::of)); } - - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + @Default + @Syntax(" ") + @Description("Get information on a piloted craft") + public static void onCommand(Player player, String[] args) { if(args.length == 0){ - if (!(sender instanceof Player)){ - sender.sendMessage("Supply a parameter."); - return true; - } - Craft craft = CraftManager.getInstance().getCraftByPlayer(((Player) sender)); + Craft craft = CraftManager.getInstance().getCraftByPlayer(player); if(craft == null){ - sender.sendMessage("You must be piloting a craft."); - return true; + player.sendMessage("You must be piloting a craft."); + return; } - craftInfo(sender, craft, 1); - return true; + craftInfo(player, craft, 1); + return; } - OptionalInt pageQuery; - if (sender instanceof Player && (pageQuery = MathUtils.parseInt(args[0])).isPresent()){ - Craft craft = CraftManager.getInstance().getCraftByPlayer(((Player) sender)); - if(craft == null){ - sender.sendMessage("You must be piloting a craft."); - return true; - } - craftInfo(sender, craft, pageQuery.getAsInt()); + + //if first argument is an integer, get your own craft's info + if (handleSelf(player, args[0])) + return; + + handleOther(player, args); + } + + private static boolean handleSelf(Player player, String page) { + OptionalInt pageQuery = MathUtils.parseInt(page); + + if (pageQuery.isEmpty()) { + return false; + } + + Craft craft = CraftManager.getInstance().getCraftByPlayer(player); + if(craft == null){ + player.sendMessage("You must be piloting a craft."); return true; } - var craft = CraftManager.getInstance().getCraftByPlayerName(args[0]); + + craftInfo(player, craft, pageQuery.getAsInt()); + return true; + } + + private static void handleOther(Player player, String[] args) { + OptionalInt pageQuery; + final String lookupPlayer = args[0]; + + var craft = CraftManager.getInstance().getCraftByPlayerName(lookupPlayer); if (craft == null) { - sender.sendMessage("No player found"); - return true; + //maybe no craft found would be more correct + player.sendMessage("No player found"); + return; } - if(args.length > 1){ - pageQuery = MathUtils.parseInt(args[1]); - if(pageQuery.isEmpty()){ - sender.sendMessage("Parameter " + args[1] + " must be a page number."); - return true; - } - } else { + + if (args.length == 1) { pageQuery = OptionalInt.of(1); + craftInfo(player, craft, pageQuery.getAsInt()); + return; } - craftInfo(sender, craft, pageQuery.getAsInt()); - return true; + + final String page = args[1]; + pageQuery = MathUtils.parseInt(page); + if(pageQuery.isEmpty()){ + player.sendMessage("Parameter " + page + " must be a page number."); + return; + } + + craftInfo(player, craft, pageQuery.getAsInt()); } - public void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft, int page){ + public static void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft, int page){ TopicPaginator paginator = new TopicPaginator("Craft Info"); for(var provider : providers){ for(var line : provider.apply(craft)){ @@ -106,9 +129,4 @@ public void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft for(String line : paginator.getPage(page)) commandSender.sendMessage(line); } - - @Override - public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { - return null; - } } From c2924adb9803e02363360079261991218bfe06f6 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 12:18:21 +0200 Subject: [PATCH 09/42] Forgot this --- .../net/countercraft/movecraft/commands/CraftInfoCommand.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java index 92fb8a081..1a727e51a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java @@ -1,6 +1,7 @@ package net.countercraft.movecraft.commands; import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.Default; import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Syntax; @@ -19,6 +20,7 @@ import java.util.OptionalInt; import java.util.function.Function; +@CommandAlias("craftinfo") public class CraftInfoCommand extends BaseCommand { private static final List>> providers = new ArrayList<>(); static { From 229043635fb5377c1ff8a1547c88281f9f297def Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 12:37:45 +0200 Subject: [PATCH 10/42] Add CraftReportCommand --- .../net/countercraft/movecraft/Movecraft.java | 2 +- .../commands/CraftReportCommand.java | 36 ++++++++----------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index dc7a419b3..5f7867de7 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -239,11 +239,11 @@ private void initializeCommands() { PaperCommandManager pcm = new PaperCommandManager(this); pcm.registerCommand(new MovecraftCommand()); pcm.registerCommand(new CraftInfoCommand()); + pcm.registerCommand(new CraftReportCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); getCommand("cruise").setExecutor(new CruiseCommand()); - getCommand("craftreport").setExecutor(new CraftReportCommand()); getCommand("manoverboard").setExecutor(new ManOverboardCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); getCommand("crafttype").setExecutor(new CraftTypeCommand()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java index 8fcddb9e0..90f053038 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java @@ -1,5 +1,7 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.*; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.PilotedCraft; @@ -11,45 +13,38 @@ import net.countercraft.movecraft.util.hitboxes.HitBox; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; -public class CraftReportCommand implements CommandExecutor { - @Override - public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] args) { - if (commandSender.getName().equalsIgnoreCase("craftreport")) - return false; +@CommandAlias("craftreport") +@CommandPermission("movecraft.commands") +public class CraftReportCommand extends BaseCommand { + + @Default + @Syntax("") + @CommandPermission("movecraft.commands.craftreport") + @Description("Reports on all active craft") + public static void onCommand(CommandSender commandSender, String[] args) { - if (!commandSender.hasPermission("movecraft.commands") - || !commandSender.hasPermission("movecraft.commands.craftreport")) { - commandSender.sendMessage(Component.empty() - .append(ChatUtils.errorPrefix()) - .append(I18nSupport.getInternationalisedComponent("Insufficient Permissions"))); - return true; - } int page; try { if (args.length == 0) page = 1; else page = Integer.parseInt(args[0]); - } - catch (NumberFormatException e) { + } catch (NumberFormatException e) { commandSender.sendMessage(Component.empty() .append(ChatUtils.commandPrefix()) .append(I18nSupport.getInternationalisedComponent("Paginator - Invalid Page")) .append(Component.text("\"")) .append(Component.text(args[0])) .append(Component.text("\""))); - return true; + return; } if (CraftManager.getInstance().isEmpty()) { commandSender.sendMessage(Component.empty() .append(ChatUtils.commandPrefix()) .append(I18nSupport.getInternationalisedComponent("Craft Report - None Found"))); - return true; + return; } ComponentPaginator paginator = new ComponentPaginator( I18nSupport.getInternationalisedComponent("Craft Report"), @@ -89,10 +84,9 @@ else if (craft.getDisabled()) .append(Component.text(" \"")) .append(Component.text(args[0])) .append(Component.text("\""))); - return true; + return; } for (Component line : paginator.getPage(page)) commandSender.sendMessage(line); - return true; } } From 3f3d00ca5491dc17fade56ae5c7962bd8371ac12 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 13:49:23 +0200 Subject: [PATCH 11/42] Add CraftTypeCommand pt1 --- .../net/countercraft/movecraft/Movecraft.java | 10 +- .../movecraft/commands/CraftTypeCommand.java | 92 ++++++++++--------- 2 files changed, 59 insertions(+), 43 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 5f7867de7..1b1a4c787 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -24,6 +24,7 @@ import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.ChunkManager; import net.countercraft.movecraft.craft.CraftManager; +import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.features.contacts.ContactsCommand; import net.countercraft.movecraft.features.contacts.ContactsManager; import net.countercraft.movecraft.features.contacts.ContactsSign; @@ -46,6 +47,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Logger; @@ -237,16 +239,22 @@ public void onEnable() { private void initializeCommands() { PaperCommandManager pcm = new PaperCommandManager(this); + pcm.getCommandCompletions().registerCompletion("crafttypes", c -> { + Set craftTypes = CraftManager.getInstance().getCraftTypes(); + List craftNames = craftTypes.stream().map(type -> type.getStringProperty(CraftType.NAME)).toList(); + return craftNames; + }); + pcm.registerCommand(new MovecraftCommand()); pcm.registerCommand(new CraftInfoCommand()); pcm.registerCommand(new CraftReportCommand()); + pcm.registerCommand(new CraftTypeCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); getCommand("cruise").setExecutor(new CruiseCommand()); getCommand("manoverboard").setExecutor(new ManOverboardCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); - getCommand("crafttype").setExecutor(new CraftTypeCommand()); } @Override diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java index 3b08b64b6..9eac47610 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java @@ -1,5 +1,7 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.*; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; @@ -7,7 +9,6 @@ import net.countercraft.movecraft.util.TopicPaginator; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.util.ChatPaginator; import org.jetbrains.annotations.NotNull; @@ -20,80 +21,87 @@ import java.util.Optional; import java.util.OptionalInt; -public class CraftTypeCommand implements TabExecutor { +@CommandAlias("crafttype") +@CommandPermission("movecraft.commands") +public class CraftTypeCommand extends BaseCommand { private static final Field[] craftTypeFields; + static { craftTypeFields = CraftType.class.getDeclaredFields(); - for(var field : craftTypeFields){ + for (var field : craftTypeFields) { field.setAccessible(true); } } - @Override - public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + @Syntax("") + @Subcommand("list") + public static void listCrafts(CommandSender commandSender, @Default("1") int page) { + sendTypeListPage(page, commandSender); + } + + @Default + @Syntax("[list|crafttype]") + @Description("Get information on a specific craft type") + @CommandCompletion("@crafttypes") + public static void onCommand(@NotNull CommandSender commandSender, String[] args) { CraftType type; int page; - if(args.length == 0 || (args.length == 1 && MathUtils.parseInt(args[0]).isPresent())) { + + if (args.length == 0 || (args.length == 1 && MathUtils.parseInt(args[0]).isPresent())) { Optional typeQuery = tryGetCraftFromPlayer(commandSender); - if(typeQuery.isEmpty()){ + if (typeQuery.isEmpty()) { commandSender.sendMessage("You must supply a craft type!"); - return true; + return; } type = typeQuery.get(); page = args.length == 0 ? 1 : MathUtils.parseInt(args[0]).getAsInt(); - } - else { - if(args.length > 1) { + } else { + if (args.length > 1) { OptionalInt pageQuery = MathUtils.parseInt(args[1]); - if(pageQuery.isEmpty()){ + if (pageQuery.isEmpty()) { commandSender.sendMessage("Argument " + args[1] + " must be a page number"); - return true; + return; } page = pageQuery.getAsInt(); - } - else { + } else { page = 1; } - if(args[0].equalsIgnoreCase("list")) { - sendTypeListPage(page, commandSender); - return true; - } + type = CraftManager.getInstance().getCraftTypeFromString(args[0]); } - if(type == null) { + if (type == null) { commandSender.sendMessage("You must supply a craft type!"); - return true; + return; } - if(!commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) { + if (!commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) { commandSender.sendMessage("You don't have permission for that craft type!"); - return true; + return; } sendTypePage(type, page, commandSender); - return true; + return; } @Nullable - @Override public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { - if(strings.length !=1 || !commandSender.hasPermission("movecraft.commands") || !commandSender.hasPermission("movecraft.commands.crafttype")) + if (strings.length != 1 || !commandSender.hasPermission("movecraft.commands") || !commandSender.hasPermission("movecraft.commands.crafttype")) return Collections.emptyList(); List completions = new ArrayList<>(); - for(CraftType type : CraftManager.getInstance().getCraftTypes()) - if(commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) + for (CraftType type : CraftManager.getInstance().getCraftTypes()) + if (commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) completions.add(type.getStringProperty(CraftType.NAME)); completions.add("list"); List returnValues = new ArrayList<>(); - for(String completion : completions) - if(completion.toLowerCase().startsWith(strings[strings.length-1].toLowerCase())) + for (String completion : completions) + if (completion.toLowerCase().startsWith(strings[strings.length - 1].toLowerCase())) returnValues.add(completion); return returnValues; } - private void sendTypePage(@NotNull CraftType type, int page, @NotNull CommandSender commandSender){ + private static void sendTypePage(@NotNull CraftType type, int page, @NotNull CommandSender commandSender) { TopicPaginator paginator = new TopicPaginator("Type Info"); - for(var field : craftTypeFields){ - if(field.getName().equals("data")){ // don't include the backing data object + for (var field : craftTypeFields) { + if (field.getName().equals("data")) { // don't include the backing data object continue; } Object value; @@ -104,40 +112,40 @@ private void sendTypePage(@NotNull CraftType type, int page, @NotNull CommandSe continue; } var repr = field.getName() + ": " + value; - if(repr.length() > ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH){ + if (repr.length() > ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH) { paginator.addLine(field.getName() + ": too long"); } else { paginator.addLine(field.getName() + ": " + value); } } - if(!paginator.isInBounds(page)){ + if (!paginator.isInBounds(page)) { commandSender.sendMessage(String.format("Page %d is out of bounds.", page)); return; } - for(String line : paginator.getPage(page)) + for (String line : paginator.getPage(page)) commandSender.sendMessage(line); } - private void sendTypeListPage(int page, @NotNull CommandSender commandSender){ + private static void sendTypeListPage(int page, @NotNull CommandSender commandSender) { TopicPaginator paginator = new TopicPaginator("Type Info"); - for(var entry : CraftManager.getInstance().getCraftTypes()){ + for (var entry : CraftManager.getInstance().getCraftTypes()) { paginator.addLine(entry.getStringProperty(CraftType.NAME)); } - if(!paginator.isInBounds(page)){ + if (!paginator.isInBounds(page)) { commandSender.sendMessage(String.format("Page %d is out of bounds.", page)); return; } - for(String line : paginator.getPage(page)) + for (String line : paginator.getPage(page)) commandSender.sendMessage(line); } @NotNull - private Optional tryGetCraftFromPlayer(CommandSender commandSender){ + private static Optional tryGetCraftFromPlayer(CommandSender commandSender) { if (!(commandSender instanceof Player)) { return Optional.empty(); } Craft c = CraftManager.getInstance().getCraftByPlayer((Player) commandSender); - if(c == null){ + if (c == null) { return Optional.empty(); } return Optional.of(c.getType()); From 5ee2fadb70393e56fd1861cbcf2010a2d76e5b20 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 15:33:30 +0200 Subject: [PATCH 12/42] Add CraftTypeCommand pt2 --- .../net/countercraft/movecraft/Movecraft.java | 4 ++ .../movecraft/commands/CraftTypeCommand.java | 66 +++++-------------- 2 files changed, 21 insertions(+), 49 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 1b1a4c787..830a2e445 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -245,6 +245,10 @@ private void initializeCommands() { return craftNames; }); + pcm.getCommandConditions().addCondition("craft_or_list", context -> { + + }); + pcm.registerCommand(new MovecraftCommand()); pcm.registerCommand(new CraftInfoCommand()); pcm.registerCommand(new CraftReportCommand()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java index 9eac47610..1a1a97ea6 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java @@ -5,21 +5,14 @@ import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; -import net.countercraft.movecraft.util.MathUtils; import net.countercraft.movecraft.util.TopicPaginator; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.util.ChatPaginator; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import java.util.Optional; -import java.util.OptionalInt; @CommandAlias("crafttype") @CommandPermission("movecraft.commands") @@ -40,62 +33,37 @@ public static void listCrafts(CommandSender commandSender, @Default("1") int pag sendTypeListPage(page, commandSender); } + @Syntax("") + @Subcommand("self") + public static void selfCommand(Player player, @Default("1") int page) { + Optional typeQuery = tryGetCraftFromPlayer(player); + if (typeQuery.isEmpty()) { + player.sendMessage("You aren't piloting any craft!"); + return; + } + + sendTypePage(typeQuery.get(), page, player); + } + @Default - @Syntax("[list|crafttype]") + @Syntax("[list|self|CRAFTTYPE] ") @Description("Get information on a specific craft type") @CommandCompletion("@crafttypes") - public static void onCommand(@NotNull CommandSender commandSender, String[] args) { + public static void onCommand(@NotNull CommandSender commandSender, String craft, @Default("1") int page) { CraftType type; - int page; - if (args.length == 0 || (args.length == 1 && MathUtils.parseInt(args[0]).isPresent())) { - Optional typeQuery = tryGetCraftFromPlayer(commandSender); - if (typeQuery.isEmpty()) { - commandSender.sendMessage("You must supply a craft type!"); - return; - } - type = typeQuery.get(); - page = args.length == 0 ? 1 : MathUtils.parseInt(args[0]).getAsInt(); - } else { - if (args.length > 1) { - OptionalInt pageQuery = MathUtils.parseInt(args[1]); - if (pageQuery.isEmpty()) { - commandSender.sendMessage("Argument " + args[1] + " must be a page number"); - return; - } - page = pageQuery.getAsInt(); - } else { - page = 1; - } - - type = CraftManager.getInstance().getCraftTypeFromString(args[0]); - } + type = CraftManager.getInstance().getCraftTypeFromString(craft); if (type == null) { commandSender.sendMessage("You must supply a craft type!"); return; } + if (!commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) { commandSender.sendMessage("You don't have permission for that craft type!"); return; } - sendTypePage(type, page, commandSender); - return; - } - @Nullable - public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { - if (strings.length != 1 || !commandSender.hasPermission("movecraft.commands") || !commandSender.hasPermission("movecraft.commands.crafttype")) - return Collections.emptyList(); - List completions = new ArrayList<>(); - for (CraftType type : CraftManager.getInstance().getCraftTypes()) - if (commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) - completions.add(type.getStringProperty(CraftType.NAME)); - completions.add("list"); - List returnValues = new ArrayList<>(); - for (String completion : completions) - if (completion.toLowerCase().startsWith(strings[strings.length - 1].toLowerCase())) - returnValues.add(completion); - return returnValues; + sendTypePage(type, page, commandSender); } private static void sendTypePage(@NotNull CraftType type, int page, @NotNull CommandSender commandSender) { From f59cc642ca080b2b4ac03b414dac3e46096228f5 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 15:33:57 +0200 Subject: [PATCH 13/42] This wasn't needed --- .../src/main/java/net/countercraft/movecraft/Movecraft.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 830a2e445..1b1a4c787 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -245,10 +245,6 @@ private void initializeCommands() { return craftNames; }); - pcm.getCommandConditions().addCondition("craft_or_list", context -> { - - }); - pcm.registerCommand(new MovecraftCommand()); pcm.registerCommand(new CraftInfoCommand()); pcm.registerCommand(new CraftReportCommand()); From e14dd754fd3f2df501399f7f44194ef512583e94 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 15:42:15 +0200 Subject: [PATCH 14/42] Small change to CraftReportCommand --- .../movecraft/commands/CraftReportCommand.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java index 90f053038..a438c9c5b 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java @@ -23,9 +23,10 @@ public class CraftReportCommand extends BaseCommand { @Syntax("") @CommandPermission("movecraft.commands.craftreport") @Description("Reports on all active craft") - public static void onCommand(CommandSender commandSender, String[] args) { - - int page; + public static void onCommand(CommandSender commandSender, @Default("1") int page) { + // TODO: This is ugly to read, maybe better make a component concatenator method in ChatUtils? + // TODO: Should we keep the error for invalid page or just default to 1 in this case? + /* try { if (args.length == 0) page = 1; @@ -39,7 +40,7 @@ public static void onCommand(CommandSender commandSender, String[] args) { .append(Component.text(args[0])) .append(Component.text("\""))); return; - } + }*/ if (CraftManager.getInstance().isEmpty()) { commandSender.sendMessage(Component.empty() .append(ChatUtils.commandPrefix()) @@ -82,7 +83,7 @@ else if (craft.getDisabled()) .append(ChatUtils.commandPrefix()) .append(I18nSupport.getInternationalisedComponent("Paginator - Invalid page")) .append(Component.text(" \"")) - .append(Component.text(args[0])) + .append(Component.text(page)) .append(Component.text("\""))); return; } From 76a2385d8a26d07157c78deb5721ebc3cf9a6f47 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 15:53:06 +0200 Subject: [PATCH 15/42] Apply similar changes to CraftInfoCommand --- .../movecraft/commands/CraftInfoCommand.java | 70 ++++--------------- 1 file changed, 14 insertions(+), 56 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java index 1a727e51a..726aa5d2a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java @@ -1,15 +1,12 @@ package net.countercraft.movecraft.commands; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.CommandAlias; -import co.aikar.commands.annotation.Default; -import co.aikar.commands.annotation.Description; -import co.aikar.commands.annotation.Syntax; +import co.aikar.commands.annotation.*; +import co.aikar.commands.bukkit.contexts.OnlinePlayer; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; -import net.countercraft.movecraft.util.MathUtils; import net.countercraft.movecraft.util.TopicPaginator; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -17,7 +14,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.OptionalInt; import java.util.function.Function; @CommandAlias("craftinfo") @@ -53,68 +49,30 @@ public static void registerProvider(@NotNull Function provider){ } @Default - @Syntax(" ") + @Syntax("[player|self] ") @Description("Get information on a piloted craft") - public static void onCommand(Player player, String[] args) { - if(args.length == 0){ - Craft craft = CraftManager.getInstance().getCraftByPlayer(player); - if(craft == null){ - player.sendMessage("You must be piloting a craft."); - return; - } - craftInfo(player, craft, 1); + @CommandCompletion("@players") + public static void onCommand(Player player, OnlinePlayer subject, @Default("1") int page) { + var craft = CraftManager.getInstance().getCraftByPlayer(subject.getPlayer()); + if (craft == null) { + //maybe no craft found would be more correct + player.sendMessage("No player found"); return; } - //if first argument is an integer, get your own craft's info - if (handleSelf(player, args[0])) - return; - - handleOther(player, args); + craftInfo(player, craft, page); } - private static boolean handleSelf(Player player, String page) { - OptionalInt pageQuery = MathUtils.parseInt(page); - - if (pageQuery.isEmpty()) { - return false; - } - + @Subcommand("self") + @Syntax("") + public static void selfCraftInfo(Player player, @Default("1") int page) { Craft craft = CraftManager.getInstance().getCraftByPlayer(player); if(craft == null){ player.sendMessage("You must be piloting a craft."); - return true; - } - - craftInfo(player, craft, pageQuery.getAsInt()); - return true; - } - - private static void handleOther(Player player, String[] args) { - OptionalInt pageQuery; - final String lookupPlayer = args[0]; - - var craft = CraftManager.getInstance().getCraftByPlayerName(lookupPlayer); - if (craft == null) { - //maybe no craft found would be more correct - player.sendMessage("No player found"); - return; - } - - if (args.length == 1) { - pageQuery = OptionalInt.of(1); - craftInfo(player, craft, pageQuery.getAsInt()); - return; - } - - final String page = args[1]; - pageQuery = MathUtils.parseInt(page); - if(pageQuery.isEmpty()){ - player.sendMessage("Parameter " + page + " must be a page number."); return; } - craftInfo(player, craft, pageQuery.getAsInt()); + craftInfo(player, craft, page); } public static void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft, int page){ From ee05772bbc847f471fbe54d4ee97b4fa04da6a06 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 16:19:20 +0200 Subject: [PATCH 16/42] Add CruiseCommand pt1 --- .../net/countercraft/movecraft/Movecraft.java | 19 +++- .../movecraft/commands/CruiseCommand.java | 101 ++++++++++-------- 2 files changed, 71 insertions(+), 49 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 1b1a4c787..61cbca6fe 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -47,6 +47,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -239,20 +240,34 @@ public void onEnable() { private void initializeCommands() { PaperCommandManager pcm = new PaperCommandManager(this); - pcm.getCommandCompletions().registerCompletion("crafttypes", c -> { + var commandCompletions = pcm.getCommandCompletions(); + + commandCompletions.registerCompletion("crafttypes", c -> { Set craftTypes = CraftManager.getInstance().getCraftTypes(); List craftNames = craftTypes.stream().map(type -> type.getStringProperty(CraftType.NAME)).toList(); return craftNames; }); + commandCompletions.registerCompletion("directions", c -> { + LinkedList directionStrings = new LinkedList<>(); + for (CruiseDirection direction : CruiseDirection.values()) { + if (direction == CruiseDirection.NONE) + continue; + + directionStrings.add(direction.toString()); + } + + return directionStrings; + }); + pcm.registerCommand(new MovecraftCommand()); pcm.registerCommand(new CraftInfoCommand()); pcm.registerCommand(new CraftReportCommand()); pcm.registerCommand(new CraftTypeCommand()); + pcm.registerCommand(new CruiseCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); - getCommand("cruise").setExecutor(new CruiseCommand()); getCommand("manoverboard").setExecutor(new ManOverboardCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index 2e890508a..6c9e5293e 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -1,5 +1,7 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.*; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -7,7 +9,6 @@ import net.countercraft.movecraft.localisation.I18nSupport; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import java.util.ArrayList; @@ -16,32 +17,52 @@ import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; -public class CruiseCommand implements TabExecutor { - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { - if(!command.getName().equalsIgnoreCase("cruise")){ +@CommandAlias("cruise") +@CommandPermission("movecraft.commands.cruise") +public class CruiseCommand extends BaseCommand { + + @PreCommand + public static boolean preCommand(CommandSender sender, String[] args) { + if(!(sender instanceof Player player)) + return false; + + final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + if (craft == null) { + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); + return false; + } + + if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".move")) { + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); return false; } - if(!(commandSender instanceof Player)){ - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Cruise - Must Be Player")); - return true; + if (!craft.getType().getBoolProperty(CraftType.CAN_CRUISE)) { + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Cruise - Craft Cannot Cruise")); + return false; } - Player player = (Player) commandSender; - if(args.length<1){ + return true; + } + + @Default + @Syntax("[on|off|DIRECTION]") + @CommandCompletion("@directions") + @Description("Starts your craft cruising") + public static void onCommand(Player player, String[] args) { + if(args.length<1){ //same as no argument final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); if (craft == null) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return true; + return; } if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.cruise")) { craft.setCruising(false); - return true; + return; } if(craft.getCruising()){ craft.setCruising(false); - return true; + return; } // Normalize yaw from [-360, 360] to [0, 360] float yaw = (player.getLocation().getYaw() + 360.0f); @@ -58,37 +79,14 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, craft.setCruiseDirection(CruiseDirection.SOUTH); } craft.setCruising(true); - return true; - } - if (args[0].equalsIgnoreCase("off")) { //This goes before because players can sometimes freeze while cruising - final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if (craft == null) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return true; - } - craft.setCruising(false); - return true; + return; } if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.cruise")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if (craft == null) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return true; - } - - if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".move")) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; - } - if (!craft.getType().getBoolProperty(CraftType.CAN_CRUISE)) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Cruise - Craft Cannot Cruise")); - return true; - } - if (args[0].equalsIgnoreCase("on")) { float yaw = (player.getLocation().getYaw() + 360.0f); @@ -105,43 +103,52 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, craft.setCruiseDirection(CruiseDirection.SOUTH); } craft.setCruising(true); - return true; + return; } if (args[0].equalsIgnoreCase("north") || args[0].equalsIgnoreCase("n")) { craft.setCruiseDirection(CruiseDirection.NORTH); craft.setCruising(true); - return true; + return; } if (args[0].equalsIgnoreCase("south") || args[0].equalsIgnoreCase("s")) { craft.setCruiseDirection(CruiseDirection.SOUTH); craft.setCruising(true); - return true; + return; } if (args[0].equalsIgnoreCase("east") || args[0].equalsIgnoreCase("e")) { craft.setCruiseDirection(CruiseDirection.EAST); craft.setCruising(true); - return true; + return; } if (args[0].equalsIgnoreCase("west") || args[0].equalsIgnoreCase("w")) { craft.setCruiseDirection(CruiseDirection.WEST); craft.setCruising(true); - return true; + return; } if (args[0].equalsIgnoreCase("up") || args[0].equalsIgnoreCase("u")) { craft.setCruiseDirection(CruiseDirection.UP); craft.setCruising(true); - return true; + return; } if (args[0].equalsIgnoreCase("down") || args[0].equalsIgnoreCase("d")) { craft.setCruiseDirection(CruiseDirection.DOWN); craft.setCruising(true); - return true; + return; } - return false; + } + + @Subcommand("off") + public static void stopCruising(Player player) { + final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + if (craft == null) { + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); + return; + } + + craft.setCruising(false); } private final String[] completions = {"North", "East", "South", "West", "Up", "Down", "On", "Off"}; - @Override public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { if(strings.length !=1) return Collections.emptyList(); From 91398c7d994c27ec0261f9940389ed0482f5fa11 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 16:54:25 +0200 Subject: [PATCH 17/42] Add CruiseCommand pt2 --- .../movecraft/commands/CruiseCommand.java | 65 +++++++------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index 6c9e5293e..2d6f13d7a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -65,20 +65,7 @@ public static void onCommand(Player player, String[] args) { return; } // Normalize yaw from [-360, 360] to [0, 360] - float yaw = (player.getLocation().getYaw() + 360.0f); - if (yaw >= 360.0f) { - yaw %= 360.0f; - } - if (yaw >= 45 && yaw < 135) { // west - craft.setCruiseDirection(CruiseDirection.WEST); - } else if (yaw >= 135 && yaw < 225) { // north - craft.setCruiseDirection(CruiseDirection.NORTH); - } else if (yaw >= 225 && yaw <= 315){ // east - craft.setCruiseDirection(CruiseDirection.EAST); - } else { // default south - craft.setCruiseDirection(CruiseDirection.SOUTH); - } - craft.setCruising(true); + yawLocationCruising(player); return; } if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.cruise")) { @@ -88,23 +75,6 @@ public static void onCommand(Player player, String[] args) { final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if (args[0].equalsIgnoreCase("on")) { - float yaw = (player.getLocation().getYaw() + 360.0f); - if (yaw >= 360.0f) { - yaw %= 360.0f; - } - if (yaw >= 45 && yaw < 135) { // west - craft.setCruiseDirection(CruiseDirection.WEST); - } else if (yaw >= 135 && yaw < 225) { // north - craft.setCruiseDirection(CruiseDirection.NORTH); - } else if (yaw >= 225 && yaw <= 315){ // east - craft.setCruiseDirection(CruiseDirection.EAST); - } else { // default south - craft.setCruiseDirection(CruiseDirection.SOUTH); - } - craft.setCruising(true); - return; - } if (args[0].equalsIgnoreCase("north") || args[0].equalsIgnoreCase("n")) { craft.setCruiseDirection(CruiseDirection.NORTH); craft.setCruising(true); @@ -138,7 +108,7 @@ public static void onCommand(Player player, String[] args) { } @Subcommand("off") - public static void stopCruising(Player player) { + public static void offCruising(Player player) { final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); if (craft == null) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); @@ -148,14 +118,27 @@ public static void stopCruising(Player player) { craft.setCruising(false); } - private final String[] completions = {"North", "East", "South", "West", "Up", "Down", "On", "Off"}; - public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { - if(strings.length !=1) - return Collections.emptyList(); - List returnValues = new ArrayList<>(); - for(String completion : completions) - if(completion.toLowerCase().startsWith(strings[strings.length-1].toLowerCase())) - returnValues.add(completion); - return returnValues; + @Subcommand("on") + public static void onCruising(Player player) { + yawLocationCruising(player); + } + + private static void yawLocationCruising(Player player) { + final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + + float yaw = (player.getLocation().getYaw() + 360.0f); + if (yaw >= 360.0f) { + yaw %= 360.0f; + } + if (yaw >= 45 && yaw < 135) { // west + craft.setCruiseDirection(CruiseDirection.WEST); + } else if (yaw >= 135 && yaw < 225) { // north + craft.setCruiseDirection(CruiseDirection.NORTH); + } else if (yaw >= 225 && yaw <= 315){ // east + craft.setCruiseDirection(CruiseDirection.EAST); + } else { // default south + craft.setCruiseDirection(CruiseDirection.SOUTH); + } + craft.setCruising(true); } } From 911aa4df7d10130d175d3fe07857b0660ee7eedb Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 17:20:05 +0200 Subject: [PATCH 18/42] Add CruiseDirection resolver for ACF --- .../net/countercraft/movecraft/Movecraft.java | 22 ++++++++++++------ .../movecraft/CruiseDirection.java | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 61cbca6fe..1dbf6139f 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -17,6 +17,7 @@ package net.countercraft.movecraft; +import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.PaperCommandManager; import io.papermc.paper.datapack.Datapack; import net.countercraft.movecraft.async.AsyncManager; @@ -53,6 +54,8 @@ import java.util.Set; import java.util.logging.Logger; +import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; + public class Movecraft extends JavaPlugin { private static Movecraft instance; @@ -249,15 +252,20 @@ private void initializeCommands() { }); commandCompletions.registerCompletion("directions", c -> { - LinkedList directionStrings = new LinkedList<>(); - for (CruiseDirection direction : CruiseDirection.values()) { - if (direction == CruiseDirection.NONE) - continue; + var allDirections = CruiseDirection.valuesString(); + var allButNone = allDirections.stream().filter(p -> !p.equals("none")).toList(); + return allButNone; + }); - directionStrings.add(direction.toString()); - } + pcm.getCommandContexts().registerContext(CruiseDirection.class, (c) -> { + String data = c.popFirstArg(); + CruiseDirection direction = CruiseDirection.fromString(data); + + if (direction == CruiseDirection.NONE) { + throw new InvalidCommandArgument(MOVECRAFT_COMMAND_PREFIX + "You must input a direction"); + } - return directionStrings; + return direction; }); pcm.registerCommand(new MovecraftCommand()); diff --git a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java index b569903a3..f6a8c5645 100644 --- a/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java +++ b/api/src/main/java/net/countercraft/movecraft/CruiseDirection.java @@ -2,6 +2,11 @@ import org.bukkit.block.BlockFace; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.Optional; + public enum CruiseDirection { NORTH((byte) 0x3), //0x3 SOUTH((byte) 0x2), //0x2 @@ -74,5 +79,23 @@ public CruiseDirection getRotated(MovecraftRotation rotation) { case NONE -> this; }; } + + public static CruiseDirection fromString(String input) { + final String sanitized = input.toLowerCase(Locale.ROOT); + + return switch (sanitized) { + case "north", "n" -> CruiseDirection.NORTH; + case "south", "s" -> CruiseDirection.SOUTH; + case "east", "e" -> CruiseDirection.EAST; + case "west", "w" -> CruiseDirection.WEST; + case "up", "u" -> CruiseDirection.UP; + case "down", "d" -> CruiseDirection.DOWN; + default -> CruiseDirection.NONE; + }; + } + + public static List valuesString() { + return Arrays.stream(values()).map(v -> v.toString().toLowerCase(Locale.ROOT)).toList(); + } } From 1099193bc21e1c97062f16b1085428438b750315 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 17:27:37 +0200 Subject: [PATCH 19/42] Add CruiseCommand pt3 --- .../net/countercraft/movecraft/Movecraft.java | 9 --- .../movecraft/commands/CruiseCommand.java | 69 ++++--------------- 2 files changed, 15 insertions(+), 63 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 1dbf6139f..3946ab140 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -17,7 +17,6 @@ package net.countercraft.movecraft; -import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.PaperCommandManager; import io.papermc.paper.datapack.Datapack; import net.countercraft.movecraft.async.AsyncManager; @@ -48,14 +47,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Logger; -import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; - public class Movecraft extends JavaPlugin { private static Movecraft instance; @@ -260,11 +256,6 @@ private void initializeCommands() { pcm.getCommandContexts().registerContext(CruiseDirection.class, (c) -> { String data = c.popFirstArg(); CruiseDirection direction = CruiseDirection.fromString(data); - - if (direction == CruiseDirection.NONE) { - throw new InvalidCommandArgument(MOVECRAFT_COMMAND_PREFIX + "You must input a direction"); - } - return direction; }); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index 2d6f13d7a..19696659a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -7,14 +7,9 @@ import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.localisation.I18nSupport; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; @CommandAlias("cruise") @@ -48,63 +43,29 @@ public static boolean preCommand(CommandSender sender, String[] args) { @Syntax("[on|off|DIRECTION]") @CommandCompletion("@directions") @Description("Starts your craft cruising") - public static void onCommand(Player player, String[] args) { - if(args.length<1){ //same as no argument - final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if (craft == null) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return; - } - if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.cruise")) { - craft.setCruising(false); - return; - } - - if(craft.getCruising()){ - craft.setCruising(false); - return; - } - // Normalize yaw from [-360, 360] to [0, 360] - yawLocationCruising(player); - return; - } - if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.cruise")) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return; - } - + public static void onCommand(Player player, CruiseDirection direction) { final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if (args[0].equalsIgnoreCase("north") || args[0].equalsIgnoreCase("n")) { - craft.setCruiseDirection(CruiseDirection.NORTH); - craft.setCruising(true); - return; - } - if (args[0].equalsIgnoreCase("south") || args[0].equalsIgnoreCase("s")) { - craft.setCruiseDirection(CruiseDirection.SOUTH); - craft.setCruising(true); - return; - } - if (args[0].equalsIgnoreCase("east") || args[0].equalsIgnoreCase("e")) { - craft.setCruiseDirection(CruiseDirection.EAST); - craft.setCruising(true); - return; - } - if (args[0].equalsIgnoreCase("west") || args[0].equalsIgnoreCase("w")) { - craft.setCruiseDirection(CruiseDirection.WEST); - craft.setCruising(true); + + /* Might use this over aikar's permission annotation after confirm it doesn't support multiple permission checks + if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.cruise")) { + craft.setCruising(false); return; - } - if (args[0].equalsIgnoreCase("up") || args[0].equalsIgnoreCase("u")) { - craft.setCruiseDirection(CruiseDirection.UP); + }*/ + + //Resolver returns NONE on fail + if (direction != CruiseDirection.NONE) { + craft.setCruiseDirection(direction); craft.setCruising(true); return; } - if (args[0].equalsIgnoreCase("down") || args[0].equalsIgnoreCase("d")) { - craft.setCruiseDirection(CruiseDirection.DOWN); - craft.setCruising(true); + + if(craft.getCruising()){ + craft.setCruising(false); return; } + // Normalize yaw from [-360, 360] to [0, 360] + yawLocationCruising(player); } @Subcommand("off") From c4efaf25a4d02f20fd224a171fcf2d7de1eb6081 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 19:18:16 +0200 Subject: [PATCH 20/42] Made custom command manager to handle or operations --- .../net/countercraft/movecraft/Movecraft.java | 17 +++--- .../commands/MovecraftCommandManager.java | 61 +++++++++++++++++++ 2 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 3946ab140..44864e9a2 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -17,7 +17,6 @@ package net.countercraft.movecraft; -import co.aikar.commands.PaperCommandManager; import io.papermc.paper.datapack.Datapack; import net.countercraft.movecraft.async.AsyncManager; import net.countercraft.movecraft.commands.*; @@ -238,8 +237,8 @@ public void onEnable() { } private void initializeCommands() { - PaperCommandManager pcm = new PaperCommandManager(this); - var commandCompletions = pcm.getCommandCompletions(); + MovecraftCommandManager movecraftCommandManager = new MovecraftCommandManager(this); + var commandCompletions = movecraftCommandManager.getCommandCompletions(); commandCompletions.registerCompletion("crafttypes", c -> { Set craftTypes = CraftManager.getInstance().getCraftTypes(); @@ -253,17 +252,17 @@ private void initializeCommands() { return allButNone; }); - pcm.getCommandContexts().registerContext(CruiseDirection.class, (c) -> { + movecraftCommandManager.getCommandContexts().registerContext(CruiseDirection.class, (c) -> { String data = c.popFirstArg(); CruiseDirection direction = CruiseDirection.fromString(data); return direction; }); - pcm.registerCommand(new MovecraftCommand()); - pcm.registerCommand(new CraftInfoCommand()); - pcm.registerCommand(new CraftReportCommand()); - pcm.registerCommand(new CraftTypeCommand()); - pcm.registerCommand(new CruiseCommand()); + movecraftCommandManager.registerCommand(new MovecraftCommand()); + movecraftCommandManager.registerCommand(new CraftInfoCommand()); + movecraftCommandManager.registerCommand(new CraftReportCommand()); + movecraftCommandManager.registerCommand(new CraftTypeCommand()); + movecraftCommandManager.registerCommand(new CruiseCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java new file mode 100644 index 000000000..276d34247 --- /dev/null +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java @@ -0,0 +1,61 @@ +package net.countercraft.movecraft.commands; + +import co.aikar.commands.CommandIssuer; +import co.aikar.commands.PaperCommandManager; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.regex.Pattern; + +public class MovecraftCommandManager extends PaperCommandManager { + public MovecraftCommandManager(Plugin plugin) { + super(plugin); + } + + private static final Pattern COMMA = Pattern.compile(","); + private static final Pattern PIPE = Pattern.compile("\\|"); + + @Override + public boolean hasPermission(CommandIssuer issuer, String permission) { + if (permission == null || permission.isEmpty()) { + return true; + } + + //handle AND like normal using comma "," + String[] perms = COMMA.split(permission); + if (perms.length > 1) { + return super.hasPermission(issuer, new HashSet<>(Arrays.asList(perms))); + } + + //handle OR using pipe "|" + CommandSender sender = issuer.getIssuer(); + for (String perm : PIPE.split(permission)) { + perm = perm.trim(); + if (!perm.isEmpty() && senderHasPermission(sender, perm)) { + return true; + } + } + + return false; + } + + public static boolean senderHasPermission(@NotNull CommandSender sender, @NotNull String perm) { + if (!(sender instanceof Player p)) { + return sender.isOp(); + } + + if (p.isOp()) + return true; + + if (p.hasPermission(perm) || p.hasPermission("movecraft.all")) { + return true; + } + + return false; + } + +} From c669efd2ab13a78f1767d6c3e677052adff74d36 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 19:22:38 +0200 Subject: [PATCH 21/42] Fix permissions --- .../movecraft/commands/CraftReportCommand.java | 3 +-- .../countercraft/movecraft/commands/CruiseCommand.java | 9 +-------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java index a438c9c5b..4b9c0c5b9 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java @@ -16,12 +16,11 @@ import org.bukkit.command.CommandSender; @CommandAlias("craftreport") -@CommandPermission("movecraft.commands") +@CommandPermission("movecraft.commands|movecraft.commands.craftreport") public class CraftReportCommand extends BaseCommand { @Default @Syntax("") - @CommandPermission("movecraft.commands.craftreport") @Description("Reports on all active craft") public static void onCommand(CommandSender commandSender, @Default("1") int page) { // TODO: This is ugly to read, maybe better make a component concatenator method in ChatUtils? diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index 19696659a..7934f13f6 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -13,7 +13,7 @@ import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; @CommandAlias("cruise") -@CommandPermission("movecraft.commands.cruise") +@CommandPermission("movecraft.commands|movecraft.commands.cruise") public class CruiseCommand extends BaseCommand { @PreCommand @@ -46,13 +46,6 @@ public static boolean preCommand(CommandSender sender, String[] args) { public static void onCommand(Player player, CruiseDirection direction) { final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - - /* Might use this over aikar's permission annotation after confirm it doesn't support multiple permission checks - if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.cruise")) { - craft.setCruising(false); - return; - }*/ - //Resolver returns NONE on fail if (direction != CruiseDirection.NONE) { craft.setCruiseDirection(direction); From cf865dbd0abaf323e772df74ecd0603a662b443f Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 19:44:05 +0200 Subject: [PATCH 22/42] Fix CruiseCommand --- .../net/countercraft/movecraft/commands/CruiseCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index 7934f13f6..75e2925bb 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -43,11 +43,11 @@ public static boolean preCommand(CommandSender sender, String[] args) { @Syntax("[on|off|DIRECTION]") @CommandCompletion("@directions") @Description("Starts your craft cruising") - public static void onCommand(Player player, CruiseDirection direction) { + public static void onCommand(Player player, @Optional CruiseDirection direction) { final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - //Resolver returns NONE on fail - if (direction != CruiseDirection.NONE) { + //Resolver returns NONE on resolver fail, null if no argument at all + if (direction != null && direction != CruiseDirection.NONE) { craft.setCruiseDirection(direction); craft.setCruising(true); return; From ad84cc87a5ae4684d62d01a4fc457362ed32da7f Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 21 Aug 2024 20:36:26 +0200 Subject: [PATCH 23/42] Change to yawLocationCruising --- .../movecraft/commands/CruiseCommand.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index 75e2925bb..eaa0c70ea 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -57,8 +57,8 @@ public static void onCommand(Player player, @Optional CruiseDirection direction) craft.setCruising(false); return; } - // Normalize yaw from [-360, 360] to [0, 360] - yawLocationCruising(player); + + yawLocationCruising(player, craft); } @Subcommand("off") @@ -74,16 +74,15 @@ public static void offCruising(Player player) { @Subcommand("on") public static void onCruising(Player player) { - yawLocationCruising(player); + final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + yawLocationCruising(player, craft); } - private static void yawLocationCruising(Player player) { - final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + private static void yawLocationCruising(Player player, Craft craft) { + + // Normalize yaw from [-360, 360] to [0, 360] + float yaw = (player.getLocation().getYaw() + 360.0f) % 360.0f; - float yaw = (player.getLocation().getYaw() + 360.0f); - if (yaw >= 360.0f) { - yaw %= 360.0f; - } if (yaw >= 45 && yaw < 135) { // west craft.setCruiseDirection(CruiseDirection.WEST); } else if (yaw >= 135 && yaw < 225) { // north @@ -93,6 +92,7 @@ private static void yawLocationCruising(Player player) { } else { // default south craft.setCruiseDirection(CruiseDirection.SOUTH); } + craft.setCruising(true); } } From 8171ade3aceede633e1e0671a0bd0538ee5fa9e9 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Thu, 22 Aug 2024 18:27:31 +0200 Subject: [PATCH 24/42] Add CraftType context resolver --- .../net/countercraft/movecraft/Movecraft.java | 19 ++++++++++++++++--- .../movecraft/commands/CraftTypeCommand.java | 9 +-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 44864e9a2..87ebef1f7 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -17,6 +17,7 @@ package net.countercraft.movecraft; +import co.aikar.commands.InvalidCommandArgument; import io.papermc.paper.datapack.Datapack; import net.countercraft.movecraft.async.AsyncManager; import net.countercraft.movecraft.commands.*; @@ -252,10 +253,22 @@ private void initializeCommands() { return allButNone; }); - movecraftCommandManager.getCommandContexts().registerContext(CruiseDirection.class, (c) -> { + var commandContexts = movecraftCommandManager.getCommandContexts(); + + commandContexts.registerContext(CruiseDirection.class, (c) -> { + String data = c.popFirstArg(); + return CruiseDirection.fromString(data); + }); + + commandContexts.registerContext(CraftType.class, (c) -> { String data = c.popFirstArg(); - CruiseDirection direction = CruiseDirection.fromString(data); - return direction; + CraftType type = CraftManager.getInstance().getCraftTypeFromString(data); + + if (type == null) { + throw new InvalidCommandArgument("You must supply a craft type!"); + } + + return type; }); movecraftCommandManager.registerCommand(new MovecraftCommand()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java index 1a1a97ea6..6639f5d7b 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java @@ -49,14 +49,7 @@ public static void selfCommand(Player player, @Default("1") int page) { @Syntax("[list|self|CRAFTTYPE] ") @Description("Get information on a specific craft type") @CommandCompletion("@crafttypes") - public static void onCommand(@NotNull CommandSender commandSender, String craft, @Default("1") int page) { - CraftType type; - - type = CraftManager.getInstance().getCraftTypeFromString(craft); - if (type == null) { - commandSender.sendMessage("You must supply a craft type!"); - return; - } + public static void onCommand(@NotNull CommandSender commandSender, CraftType type, @Default("1") int page) { if (!commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) { commandSender.sendMessage("You don't have permission for that craft type!"); From 27b9b8b27c239cd917dc74cf168e2788a1df36eb Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 24 Aug 2024 21:30:55 +0200 Subject: [PATCH 25/42] Use string parsing --- .../movecraft/commands/CraftReportCommand.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java index 4b9c0c5b9..7e7c2e1c1 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java @@ -22,24 +22,22 @@ public class CraftReportCommand extends BaseCommand { @Default @Syntax("") @Description("Reports on all active craft") - public static void onCommand(CommandSender commandSender, @Default("1") int page) { - // TODO: This is ugly to read, maybe better make a component concatenator method in ChatUtils? - // TODO: Should we keep the error for invalid page or just default to 1 in this case? - /* + public static void onCommand(CommandSender commandSender, @Optional String pageString) { + int page; try { - if (args.length == 0) + if (pageString == null) page = 1; else - page = Integer.parseInt(args[0]); + page = Integer.parseInt(pageString); } catch (NumberFormatException e) { commandSender.sendMessage(Component.empty() .append(ChatUtils.commandPrefix()) .append(I18nSupport.getInternationalisedComponent("Paginator - Invalid Page")) .append(Component.text("\"")) - .append(Component.text(args[0])) + .append(Component.text(pageString)) .append(Component.text("\""))); return; - }*/ + } if (CraftManager.getInstance().isEmpty()) { commandSender.sendMessage(Component.empty() .append(ChatUtils.commandPrefix()) From 60386793b5f9e4fffeb5942478e8db7b0513651c Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 24 Aug 2024 21:33:11 +0200 Subject: [PATCH 26/42] byebye self --- .../movecraft/commands/CraftInfoCommand.java | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java index 726aa5d2a..0acd15d74 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java @@ -49,26 +49,25 @@ public static void registerProvider(@NotNull Function provider){ } @Default - @Syntax("[player|self] ") + @Syntax("[player] ") @Description("Get information on a piloted craft") @CommandCompletion("@players") - public static void onCommand(Player player, OnlinePlayer subject, @Default("1") int page) { - var craft = CraftManager.getInstance().getCraftByPlayer(subject.getPlayer()); - if (craft == null) { - //maybe no craft found would be more correct - player.sendMessage("No player found"); + public static void onCommand(Player player, @Optional OnlinePlayer subject, @Default("1") int page) { + if(subject == null) { + Craft craft = CraftManager.getInstance().getCraftByPlayer(player); + if(craft == null){ + player.sendMessage("You must be piloting a craft."); + return; + } + + craftInfo(player, craft, page); return; } - craftInfo(player, craft, page); - } - - @Subcommand("self") - @Syntax("") - public static void selfCraftInfo(Player player, @Default("1") int page) { - Craft craft = CraftManager.getInstance().getCraftByPlayer(player); - if(craft == null){ - player.sendMessage("You must be piloting a craft."); + Craft craft = CraftManager.getInstance().getCraftByPlayer(subject.getPlayer()); + if (craft == null) { + //maybe no craft found would be more correct + player.sendMessage("No player found"); return; } From aa6716c54e3fca1690fea201cbebbae6b3d9c131 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 24 Aug 2024 22:07:40 +0200 Subject: [PATCH 27/42] Simplify --- .../movecraft/commands/MovecraftCommandManager.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java index 276d34247..b73e7e23f 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java @@ -48,14 +48,7 @@ public static boolean senderHasPermission(@NotNull CommandSender sender, @NotNul return sender.isOp(); } - if (p.isOp()) - return true; - - if (p.hasPermission(perm) || p.hasPermission("movecraft.all")) { - return true; - } - - return false; + return p.hasPermission(perm); } } From 72c8843c206d550e7975cd1af3e52b46b6f685b3 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 24 Aug 2024 22:19:28 +0200 Subject: [PATCH 28/42] Simplified Permission check --- .../commands/MovecraftCommandManager.java | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java index b73e7e23f..2201d8652 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java @@ -3,12 +3,9 @@ import co.aikar.commands.CommandIssuer; import co.aikar.commands.PaperCommandManager; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -import org.jetbrains.annotations.NotNull; -import java.util.Arrays; -import java.util.HashSet; +import java.util.Set; import java.util.regex.Pattern; public class MovecraftCommandManager extends PaperCommandManager { @@ -28,27 +25,18 @@ public boolean hasPermission(CommandIssuer issuer, String permission) { //handle AND like normal using comma "," String[] perms = COMMA.split(permission); if (perms.length > 1) { - return super.hasPermission(issuer, new HashSet<>(Arrays.asList(perms))); + return super.hasPermission(issuer, Set.of(perms)); } //handle OR using pipe "|" CommandSender sender = issuer.getIssuer(); for (String perm : PIPE.split(permission)) { perm = perm.trim(); - if (!perm.isEmpty() && senderHasPermission(sender, perm)) { + if (!perm.isEmpty() && sender.hasPermission(perm)) { return true; } } return false; } - - public static boolean senderHasPermission(@NotNull CommandSender sender, @NotNull String perm) { - if (!(sender instanceof Player p)) { - return sender.isOp(); - } - - return p.hasPermission(perm); - } - } From a254a3a71b952843d1bbdcf71c3660b4eed50b53 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sun, 25 Aug 2024 19:49:34 +0200 Subject: [PATCH 29/42] Permission fixes --- .../net/countercraft/movecraft/commands/CraftReportCommand.java | 2 +- .../java/net/countercraft/movecraft/commands/CruiseCommand.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java index 7e7c2e1c1..f3d685ad5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftReportCommand.java @@ -16,7 +16,7 @@ import org.bukkit.command.CommandSender; @CommandAlias("craftreport") -@CommandPermission("movecraft.commands|movecraft.commands.craftreport") +@CommandPermission("movecraft.commands,movecraft.commands.craftreport") public class CraftReportCommand extends BaseCommand { @Default diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index eaa0c70ea..ace094e06 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -13,7 +13,7 @@ import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; @CommandAlias("cruise") -@CommandPermission("movecraft.commands|movecraft.commands.cruise") +@CommandPermission("movecraft.commands,movecraft.commands.cruise") public class CruiseCommand extends BaseCommand { @PreCommand From c3c792258c1b1d5871c995ea5ac57efc152b5407 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 31 Aug 2024 09:42:06 +0200 Subject: [PATCH 30/42] Move into command manager --- .../net/countercraft/movecraft/Movecraft.java | 36 +---------------- .../commands/MovecraftCommandManager.java | 39 ++++++++++++++++++- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 87ebef1f7..fb72d24f5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -17,14 +17,12 @@ package net.countercraft.movecraft; -import co.aikar.commands.InvalidCommandArgument; import io.papermc.paper.datapack.Datapack; import net.countercraft.movecraft.async.AsyncManager; import net.countercraft.movecraft.commands.*; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.ChunkManager; import net.countercraft.movecraft.craft.CraftManager; -import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.features.contacts.ContactsCommand; import net.countercraft.movecraft.features.contacts.ContactsManager; import net.countercraft.movecraft.features.contacts.ContactsSign; @@ -47,7 +45,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Logger; @@ -239,37 +236,8 @@ public void onEnable() { private void initializeCommands() { MovecraftCommandManager movecraftCommandManager = new MovecraftCommandManager(this); - var commandCompletions = movecraftCommandManager.getCommandCompletions(); - - commandCompletions.registerCompletion("crafttypes", c -> { - Set craftTypes = CraftManager.getInstance().getCraftTypes(); - List craftNames = craftTypes.stream().map(type -> type.getStringProperty(CraftType.NAME)).toList(); - return craftNames; - }); - - commandCompletions.registerCompletion("directions", c -> { - var allDirections = CruiseDirection.valuesString(); - var allButNone = allDirections.stream().filter(p -> !p.equals("none")).toList(); - return allButNone; - }); - - var commandContexts = movecraftCommandManager.getCommandContexts(); - - commandContexts.registerContext(CruiseDirection.class, (c) -> { - String data = c.popFirstArg(); - return CruiseDirection.fromString(data); - }); - - commandContexts.registerContext(CraftType.class, (c) -> { - String data = c.popFirstArg(); - CraftType type = CraftManager.getInstance().getCraftTypeFromString(data); - - if (type == null) { - throw new InvalidCommandArgument("You must supply a craft type!"); - } - - return type; - }); + movecraftCommandManager.registerMovecraftCompletions(); + movecraftCommandManager.registerMovecraftContexts(); movecraftCommandManager.registerCommand(new MovecraftCommand()); movecraftCommandManager.registerCommand(new CraftInfoCommand()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java index 2201d8652..59fbddc15 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java @@ -1,10 +1,15 @@ package net.countercraft.movecraft.commands; import co.aikar.commands.CommandIssuer; +import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.PaperCommandManager; +import net.countercraft.movecraft.CruiseDirection; +import net.countercraft.movecraft.craft.CraftManager; +import net.countercraft.movecraft.craft.type.CraftType; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; +import java.util.List; import java.util.Set; import java.util.regex.Pattern; @@ -16,7 +21,7 @@ public MovecraftCommandManager(Plugin plugin) { private static final Pattern COMMA = Pattern.compile(","); private static final Pattern PIPE = Pattern.compile("\\|"); - @Override + @Override //gonna keep this in case it is actually used public boolean hasPermission(CommandIssuer issuer, String permission) { if (permission == null || permission.isEmpty()) { return true; @@ -39,4 +44,36 @@ public boolean hasPermission(CommandIssuer issuer, String permission) { return false; } + + public void registerMovecraftCompletions() { + getCommandCompletions().registerCompletion("crafttypes", c -> { + Set craftTypes = CraftManager.getInstance().getCraftTypes(); + List craftNames = craftTypes.stream().map(type -> type.getStringProperty(CraftType.NAME)).toList(); + return craftNames; + }); + + getCommandCompletions().registerCompletion("directions", c -> { + var allDirections = CruiseDirection.valuesString(); + var allButNone = allDirections.stream().filter(p -> !p.equals("none")).toList(); + return allButNone; + }); + } + + public void registerMovecraftContexts() { + getCommandContexts().registerContext(CruiseDirection.class, (c) -> { + String data = c.popFirstArg(); + return CruiseDirection.fromString(data); + }); + + getCommandContexts().registerContext(CraftType.class, (c) -> { + String data = c.popFirstArg(); + CraftType type = CraftManager.getInstance().getCraftTypeFromString(data); + + if (type == null) { + throw new InvalidCommandArgument("You must supply a craft type!"); + } + + return type; + }); + } } From 281446396bb0a489e6ec579d3458cf7db9366a21 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 31 Aug 2024 10:48:14 +0200 Subject: [PATCH 31/42] Fixing craftInfo, this requires either restoring self or reverting --- .../movecraft/commands/CraftInfoCommand.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java index 0acd15d74..f7e352ce4 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java @@ -48,30 +48,33 @@ public static void registerProvider(@NotNull Function provider){ providers.add(provider.andThen(List::of)); } - @Default - @Syntax("[player] ") - @Description("Get information on a piloted craft") + @Syntax("") + @Description("Get information on a your craft") @CommandCompletion("@players") - public static void onCommand(Player player, @Optional OnlinePlayer subject, @Default("1") int page) { - if(subject == null) { - Craft craft = CraftManager.getInstance().getCraftByPlayer(player); - if(craft == null){ - player.sendMessage("You must be piloting a craft."); - return; - } - - craftInfo(player, craft, page); + public static void onSelf(Player player, @Default("1") int page) { + Craft craft = CraftManager.getInstance().getCraftByPlayer(player); + if (craft == null) { + //maybe no craft found would be more correct + player.sendMessage("You aren't piloting any craft"); return; } + craftInfo(player, craft, page); + } + + @Default + @Syntax("[player] ") + @Description("Get information on a piloted craft") + @CommandCompletion("@players") + public static void onOtherPlayer(Player player, OnlinePlayer subject, @Default("1") int page) { Craft craft = CraftManager.getInstance().getCraftByPlayer(subject.getPlayer()); if (craft == null) { //maybe no craft found would be more correct - player.sendMessage("No player found"); + player.sendMessage("No player craft found"); return; } - craftInfo(player, craft, page); + craftInfo(subject.getPlayer(), craft, page); } public static void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft, int page){ From 0ea58e8c68f2b5d8761a8665cec3b569c522d975 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sat, 31 Aug 2024 12:09:47 +0200 Subject: [PATCH 32/42] @PreCommand works with inverted logic --- .../movecraft/commands/CruiseCommand.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java index ace094e06..112aca1b7 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CruiseCommand.java @@ -17,26 +17,26 @@ public class CruiseCommand extends BaseCommand { @PreCommand - public static boolean preCommand(CommandSender sender, String[] args) { + public static boolean preCommand(CommandSender sender) { if(!(sender instanceof Player player)) - return false; + return true; final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); if (craft == null) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return false; + return true; } if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".move")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return false; + return true; } if (!craft.getType().getBoolProperty(CraftType.CAN_CRUISE)) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Cruise - Craft Cannot Cruise")); - return false; + return true; } - return true; + return false; } @Default @@ -51,9 +51,7 @@ public static void onCommand(Player player, @Optional CruiseDirection direction) craft.setCruiseDirection(direction); craft.setCruising(true); return; - } - - if(craft.getCruising()){ + } else if(craft.getCruising()) { craft.setCruising(false); return; } From fd8ab3b7b20e8f529e0a97d5d1bd1498ef8d60ae Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 11:12:37 +0200 Subject: [PATCH 33/42] Remove "self" from CraftInfoCommand --- .../movecraft/commands/CraftInfoCommand.java | 65 ++++++++++++------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java index f7e352ce4..d6620dcb4 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java @@ -1,13 +1,14 @@ package net.countercraft.movecraft.commands; import co.aikar.commands.BaseCommand; +import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.annotation.*; -import co.aikar.commands.bukkit.contexts.OnlinePlayer; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.util.TopicPaginator; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -48,33 +49,45 @@ public static void registerProvider(@NotNull Function provider){ providers.add(provider.andThen(List::of)); } - @Syntax("") - @Description("Get information on a your craft") - @CommandCompletion("@players") - public static void onSelf(Player player, @Default("1") int page) { - Craft craft = CraftManager.getInstance().getCraftByPlayer(player); - if (craft == null) { - //maybe no craft found would be more correct - player.sendMessage("You aren't piloting any craft"); - return; - } - - craftInfo(player, craft, page); - } - @Default @Syntax("[player] ") @Description("Get information on a piloted craft") @CommandCompletion("@players") - public static void onOtherPlayer(Player player, OnlinePlayer subject, @Default("1") int page) { - Craft craft = CraftManager.getInstance().getCraftByPlayer(subject.getPlayer()); - if (craft == null) { - //maybe no craft found would be more correct - player.sendMessage("No player craft found"); - return; + public static void onOtherPlayer(CommandSender commandSender, String[] args) { + if (args.length > 2) { + throw new InvalidCommandArgument("Max allowed arguments: 2"); } - craftInfo(subject.getPlayer(), craft, page); + try { + int page = args.length == 0 ? 1 : Integer.parseInt(args[0]); + if (!(commandSender instanceof Player player)) { + commandSender.sendMessage("You can't run this command on yourself as console"); + return; + } + + Craft craft = CraftManager.getInstance().getCraftByPlayer(player); + if (craft == null) { + commandSender.sendMessage("No player craft found"); + return; + } + + craftInfo(commandSender, craft, page); + + } catch (NumberFormatException e) { + Player playerSubject = Bukkit.getPlayer(args[0]); + if (playerSubject == null) { + commandSender.sendMessage("First argument must be either a number or a player"); + return; + } + + Craft craft = CraftManager.getInstance().getCraftByPlayer(playerSubject); + if (craft == null) { + commandSender.sendMessage("No player craft found"); + return; + } + + craftInfo(commandSender, craft, parseDefaultInt(args[1], 1)); + } } public static void craftInfo(@NotNull CommandSender commandSender, @NotNull Craft craft, int page){ @@ -91,4 +104,12 @@ public static void craftInfo(@NotNull CommandSender commandSender, @NotNull Craf for(String line : paginator.getPage(page)) commandSender.sendMessage(line); } + + public static int parseDefaultInt(String s, int def) { + try { + return Integer.parseInt(s); + } catch (NumberFormatException e) { + return def; + } + } } From f81e45b122016489f45a8405f2166de47dfb7344 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 14:31:03 +0200 Subject: [PATCH 34/42] Remove "self" from CraftTypeCommand --- .../movecraft/commands/CraftInfoCommand.java | 10 +--- .../movecraft/commands/CraftTypeCommand.java | 47 ++++++++++++------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java index d6620dcb4..0e1d69440 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftInfoCommand.java @@ -86,7 +86,7 @@ public static void onOtherPlayer(CommandSender commandSender, String[] args) { return; } - craftInfo(commandSender, craft, parseDefaultInt(args[1], 1)); + craftInfo(commandSender, craft, args.length == 1 ? 1 : Integer.parseInt(args[1])); } } @@ -104,12 +104,4 @@ public static void craftInfo(@NotNull CommandSender commandSender, @NotNull Craf for(String line : paginator.getPage(page)) commandSender.sendMessage(line); } - - public static int parseDefaultInt(String s, int def) { - try { - return Integer.parseInt(s); - } catch (NumberFormatException e) { - return def; - } - } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java index 6639f5d7b..c5bedf070 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/CraftTypeCommand.java @@ -1,6 +1,7 @@ package net.countercraft.movecraft.commands; import co.aikar.commands.BaseCommand; +import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.annotation.*; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -33,30 +34,40 @@ public static void listCrafts(CommandSender commandSender, @Default("1") int pag sendTypeListPage(page, commandSender); } - @Syntax("") - @Subcommand("self") - public static void selfCommand(Player player, @Default("1") int page) { - Optional typeQuery = tryGetCraftFromPlayer(player); - if (typeQuery.isEmpty()) { - player.sendMessage("You aren't piloting any craft!"); - return; - } - - sendTypePage(typeQuery.get(), page, player); - } - @Default - @Syntax("[list|self|CRAFTTYPE] ") + @Syntax("[list|CRAFTTYPE] ") @Description("Get information on a specific craft type") @CommandCompletion("@crafttypes") - public static void onCommand(@NotNull CommandSender commandSender, CraftType type, @Default("1") int page) { + public static void onCommand(@NotNull CommandSender commandSender, String[] args) { + if (args.length > 2) { + throw new InvalidCommandArgument("Max allowed arguments: 2"); + } - if (!commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) { - commandSender.sendMessage("You don't have permission for that craft type!"); - return; + try { + int page = args.length == 0 ? 1 : Integer.parseInt(args[0]); + Optional type = tryGetCraftFromPlayer(commandSender); + if (type.isEmpty()) { + commandSender.sendMessage("Craft not found!"); + return; + } + + sendTypePage(type.get(), page, commandSender); + } catch (NumberFormatException e) { + CraftType type = CraftManager.getInstance().getCraftTypeFromString(args[0]); + if (type == null) { + commandSender.sendMessage("First argument must be either a number or a craft"); + return; + } + + if (!commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) { + commandSender.sendMessage("You don't have permission for that craft type!"); + return; + } + + int page = args.length == 1 ? 1 : Integer.parseInt(args[1]); + sendTypePage(type, page, commandSender); } - sendTypePage(type, page, commandSender); } private static void sendTypePage(@NotNull CraftType type, int page, @NotNull CommandSender commandSender) { From a9846561280bd954f9a3384a8fef209e7037393a Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 14:53:49 +0200 Subject: [PATCH 35/42] Add ManOverboardCommand --- .../net/countercraft/movecraft/Movecraft.java | 2 +- .../commands/ManOverboardCommand.java | 38 ++++++++----------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index fb72d24f5..712c835eb 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -244,10 +244,10 @@ private void initializeCommands() { movecraftCommandManager.registerCommand(new CraftReportCommand()); movecraftCommandManager.registerCommand(new CraftTypeCommand()); movecraftCommandManager.registerCommand(new CruiseCommand()); + movecraftCommandManager.registerCommand(new ManOverboardCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); - getCommand("manoverboard").setExecutor(new ManOverboardCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ManOverboardCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ManOverboardCommand.java index e3118c255..a5dbb1c4a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ManOverboardCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ManOverboardCommand.java @@ -1,5 +1,9 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Description; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -8,44 +12,33 @@ import net.countercraft.movecraft.events.ManOverboardEvent; import net.countercraft.movecraft.localisation.I18nSupport; import net.countercraft.movecraft.util.MathUtils; -import net.countercraft.movecraft.util.ReflectUtils; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; -public class ManOverboardCommand implements CommandExecutor { +@CommandAlias("manoverboard") +public class ManOverboardCommand extends BaseCommand { - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { - if (!command.getName().equalsIgnoreCase("manOverBoard")) - return false; + @Default + @Description("If enabled, returns you to a craft you have fallen out of") + public static void onCommand(Player player) { - if (!(commandSender instanceof Player)) { - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("ManOverboard - Must Be Player")); - return true; - } - Player player = (Player) commandSender; Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if (craft == null) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("ManOverboard - No Craft Found")); - return true; + return; } Location telPoint = getCraftTeleportPoint(craft); if (craft.getWorld() != player.getWorld()) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("ManOverboard - Other World")); - return true; + return; } if ((System.currentTimeMillis() - @@ -53,19 +46,19 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, && !MathUtils.locIsNearCraftFast(craft, MathUtils.bukkit2MovecraftLoc(player.getLocation()))) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("ManOverboard - Timed Out")); - return true; + return; } if (telPoint.distanceSquared(player.getLocation()) > Settings.ManOverboardDistSquared) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("ManOverboard - Distance Too Far")); - return true; + return; } if (craft.getDisabled() || craft instanceof SinkingCraft) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("ManOverboard - Disabled")); - return true; + return; } ManOverboardEvent event = new ManOverboardEvent(craft, telPoint); @@ -74,10 +67,9 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, player.setVelocity(new Vector(0, 0, 0)); player.setFallDistance(0); Movecraft.getInstance().getSmoothTeleport().teleport(player, telPoint, 0, 0); - return true; } - private @NotNull Location getCraftTeleportPoint(@NotNull Craft craft) { + private static @NotNull Location getCraftTeleportPoint(@NotNull Craft craft) { double telX = ((craft.getHitBox().getMinX() + craft.getHitBox().getMaxX()) / 2D) + 0.5D; double telZ = ((craft.getHitBox().getMinZ() + craft.getHitBox().getMaxZ()) / 2D) + 0.5D; double telY = craft.getHitBox().getMaxY() + 1; From 16d3af80fa3b05f325ad9281a68d6f77aeeef070 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 15:10:49 +0200 Subject: [PATCH 36/42] Add PilotCommand pt1 --- .../net/countercraft/movecraft/Movecraft.java | 2 +- .../movecraft/commands/PilotCommand.java | 53 +++++++------------ 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 712c835eb..2a89a7c49 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -245,8 +245,8 @@ private void initializeCommands() { movecraftCommandManager.registerCommand(new CraftTypeCommand()); movecraftCommandManager.registerCommand(new CruiseCommand()); movecraftCommandManager.registerCommand(new ManOverboardCommand()); + movecraftCommandManager.registerCommand(new PilotCommand()); getCommand("release").setExecutor(new ReleaseCommand()); - getCommand("pilot").setExecutor(new PilotCommand()); getCommand("rotate").setExecutor(new RotateCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java index 2f0d67747..1bb82de08 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java @@ -1,5 +1,10 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Default; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -22,32 +27,32 @@ import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; -public class PilotCommand implements TabExecutor { - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { +@CommandAlias("pilot") +@CommandPermission("movecraft.commands,movecraft.commands.pilot") +public class PilotCommand extends BaseCommand { + + @Default + @CommandCompletion("@crafttypes") + public static void onCommand(CommandSender commandSender, Command command, String s, String[] args) { if (!command.getName().equalsIgnoreCase("pilot")) - return false; - if (!(commandSender instanceof Player)) { + return; + if (!(commandSender instanceof Player player)) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - Must Be Player")); - return true; - } - Player player = (Player) commandSender; - if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.pilot")) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } + if (args.length < 1) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - No Craft Type")); - return true; + return; } if (!player.hasPermission("movecraft." + args[0] + ".pilot")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(args[0]); if (craftType == null) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - Invalid Craft Type")); - return true; + return; } final World world = player.getWorld(); @@ -72,25 +77,5 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, CraftManager.getInstance().release(oldCraft, CraftReleaseEvent.Reason.PLAYER, false); } ); - return true; - } - - @Override - public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { - if (strings.length != 1 || !commandSender.hasPermission("movecraft.commands") - || !commandSender.hasPermission("movecraft.commands.pilot")) - return Collections.emptyList(); - - List completions = new ArrayList<>(); - for (CraftType type : CraftManager.getInstance().getCraftTypes()) - if (commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot")) - completions.add(type.getStringProperty(CraftType.NAME)); - - List returnValues = new ArrayList<>(); - for (String completion : completions) - if (completion.toLowerCase().startsWith(strings[strings.length-1].toLowerCase())) - returnValues.add(completion); - - return returnValues; } } From bd710bd3c011ec904e6d7b2fc128a58e475559e1 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 15:17:54 +0200 Subject: [PATCH 37/42] Add PilotCommand pt2 --- .../movecraft/commands/PilotCommand.java | 31 +++++-------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java index 1bb82de08..a12f4d8f9 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/PilotCommand.java @@ -5,6 +5,8 @@ import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Syntax; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -16,14 +18,9 @@ import net.countercraft.movecraft.util.MathUtils; import net.countercraft.movecraft.util.Pair; import org.bukkit.World; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.Locale; import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; @@ -33,27 +30,13 @@ public class PilotCommand extends BaseCommand { @Default @CommandCompletion("@crafttypes") - public static void onCommand(CommandSender commandSender, Command command, String s, String[] args) { - if (!command.getName().equalsIgnoreCase("pilot")) - return; - if (!(commandSender instanceof Player player)) { - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - Must Be Player")); - return; - } - - if (args.length < 1) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - No Craft Type")); - return; - } - if (!player.hasPermission("movecraft." + args[0] + ".pilot")) { + @Syntax("[CRAFTTYPE]") + @Description("Pilots the craft at your feet") + public static void onCommand(Player player, CraftType craftType) { + if (!player.hasPermission("movecraft." + craftType.toString().toLowerCase(Locale.ROOT) + ".pilot")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(args[0]); - if (craftType == null) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - Invalid Craft Type")); - return; - } final World world = player.getWorld(); MovecraftLocation startPoint = MathUtils.bukkit2MovecraftLoc(player.getLocation()); From 2f76c8d76574ed44edcb8a8c6120325e91887615 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 15:35:52 +0200 Subject: [PATCH 38/42] Add ReleaseCommand pt1 --- .../net/countercraft/movecraft/Movecraft.java | 2 +- .../movecraft/commands/ReleaseCommand.java | 119 ++++++++++-------- 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 2a89a7c49..73324fc18 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -246,7 +246,7 @@ private void initializeCommands() { movecraftCommandManager.registerCommand(new CruiseCommand()); movecraftCommandManager.registerCommand(new ManOverboardCommand()); movecraftCommandManager.registerCommand(new PilotCommand()); - getCommand("release").setExecutor(new ReleaseCommand()); + movecraftCommandManager.registerCommand(new ReleaseCommand()); getCommand("rotate").setExecutor(new RotateCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java index 0bb0877d8..9d607c86e 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java @@ -1,5 +1,11 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.PilotedCraft; @@ -8,7 +14,6 @@ import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import java.util.ArrayList; @@ -17,24 +22,71 @@ import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; -public class ReleaseCommand implements TabExecutor { - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { - if (!command.getName().equalsIgnoreCase("release")) - return false; +@CommandAlias("release") +@CommandPermission("movecraft.commands,movecraft.commands.release") +public class ReleaseCommand extends BaseCommand { - if (!commandSender.hasPermission("movecraft.commands") - || !commandSender.hasPermission("movecraft.commands.release")) { + @Subcommand("-p") + public static void releaseAllPlayers(CommandSender commandSender) { + if (!commandSender.hasPermission("movecraft.commands.release.others")) { + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + + I18nSupport.getInternationalisedString("Release - No Force Release")); + return; + } + + for (Player p : Bukkit.getOnlinePlayers()) { + String name = p.getName(); + final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(name); + if (pCraft != null) + CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.FORCE, false); + } + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + + I18nSupport.getInternationalisedString("Release - Released All Player Crafts")); + } + + @Subcommand("-a") + public static void releaseAllCrafts(CommandSender commandSender) { + if (!commandSender.hasPermission("movecraft.commands.release.others")) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + + I18nSupport.getInternationalisedString("Release - No Force Release")); + return; + } + + final List craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts()); + for (Craft craft : craftsToRelease) { + CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false); + } + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + + I18nSupport.getInternationalisedString("Release - Released All Crafts")); + } + + @Subcommand("-n") + public static void releaseAllNullCrafts(CommandSender commandSender) { + if (!commandSender.hasPermission("movecraft.commands.release.others")) { + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + + I18nSupport.getInternationalisedString("Release - No Force Release")); + return; } + + final List craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts()); + for (Craft craft : craftsToRelease) { + if (!(craft instanceof PilotedCraft)) + CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false); + } + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + + I18nSupport.getInternationalisedString("Release - Released All Null Crafts")); + } + + @Default + @Syntax("<-p|-a|-n|player>") + public static void onCommand(CommandSender commandSender, String[] args) { + if (args.length == 0) { if(!(commandSender instanceof Player)) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString( "Player - Error - You do not have a craft to release!")); - return true; + return; } Player player = (Player) commandSender; final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); @@ -43,68 +95,35 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString( "Player - Error - You do not have a craft to release!")); - return true; + return; } CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.PLAYER, false); - return true; + return; } + if (!commandSender.hasPermission("movecraft.commands.release.others")) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - No Force Release")); - return true; - } - if (args[0].equalsIgnoreCase("-p")) { - for (Player p : Bukkit.getOnlinePlayers()) { - String name = p.getName(); - final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(name); - if (pCraft != null) - CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.FORCE, false); - } - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("Release - Released All Player Crafts")); - return true; - } - - if (args[0].equalsIgnoreCase("-a")) { - final List craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts()); - for (Craft craft : craftsToRelease) { - CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false); - } - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("Release - Released All Crafts")); - return true; - } - - if (args[0].equalsIgnoreCase("-n")) { - final List craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts()); - for (Craft craft : craftsToRelease) { - if (!(craft instanceof PilotedCraft)) - CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false); - } - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("Release - Released All Null Crafts")); - return true; + return; } Player target = Bukkit.getPlayer(args[0]); if (target == null) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Player - Not Found")); - return true; + return; } final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(args[0]); if (pCraft != null) { CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.FORCE, false); commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Successful Force Release")); - return true; + return; } commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Player - Not Piloting")); - return true; } - @Override public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { if(strings.length !=1 || !commandSender.hasPermission("movecraft.commands.release.others")) return Collections.emptyList(); From 105d21cda06c1d8b2801df92b10f574a73e0dbc8 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 15:49:09 +0200 Subject: [PATCH 39/42] Add ReleaseCommand pt2 --- .../net/countercraft/movecraft/Movecraft.java | 2 - .../commands/MovecraftCommandManager.java | 22 +++++++- .../movecraft/commands/ReleaseCommand.java | 54 +++++-------------- 3 files changed, 32 insertions(+), 46 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 73324fc18..58fdea6f0 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -236,8 +236,6 @@ public void onEnable() { private void initializeCommands() { MovecraftCommandManager movecraftCommandManager = new MovecraftCommandManager(this); - movecraftCommandManager.registerMovecraftCompletions(); - movecraftCommandManager.registerMovecraftContexts(); movecraftCommandManager.registerCommand(new MovecraftCommand()); movecraftCommandManager.registerCommand(new CraftInfoCommand()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java index 59fbddc15..eb9215832 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/MovecraftCommandManager.java @@ -1,11 +1,13 @@ package net.countercraft.movecraft.commands; import co.aikar.commands.CommandIssuer; +import co.aikar.commands.ConditionFailedException; import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.PaperCommandManager; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; +import net.countercraft.movecraft.localisation.I18nSupport; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; @@ -13,9 +15,14 @@ import java.util.Set; import java.util.regex.Pattern; +import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; + public class MovecraftCommandManager extends PaperCommandManager { public MovecraftCommandManager(Plugin plugin) { super(plugin); + this.registerMovecraftCompletions(); + this.registerMovecraftContexts(); + this.registerMovecraftConditions(); } private static final Pattern COMMA = Pattern.compile(","); @@ -45,7 +52,7 @@ public boolean hasPermission(CommandIssuer issuer, String permission) { return false; } - public void registerMovecraftCompletions() { + private void registerMovecraftCompletions() { getCommandCompletions().registerCompletion("crafttypes", c -> { Set craftTypes = CraftManager.getInstance().getCraftTypes(); List craftNames = craftTypes.stream().map(type -> type.getStringProperty(CraftType.NAME)).toList(); @@ -59,7 +66,7 @@ public void registerMovecraftCompletions() { }); } - public void registerMovecraftContexts() { + private void registerMovecraftContexts() { getCommandContexts().registerContext(CruiseDirection.class, (c) -> { String data = c.popFirstArg(); return CruiseDirection.fromString(data); @@ -76,4 +83,15 @@ public void registerMovecraftContexts() { return type; }); } + + private void registerMovecraftConditions() { + getCommandConditions().addCondition("pilot_others", (context -> { + var issuer = context.getIssuer(); + if (!issuer.hasPermission("movecraft.commands.release.others")) { + throw new ConditionFailedException(MOVECRAFT_COMMAND_PREFIX + + I18nSupport.getInternationalisedString("Release - No Force Release")); + + } + })); + } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java index 9d607c86e..edf10f424 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ReleaseCommand.java @@ -2,7 +2,9 @@ import co.aikar.commands.BaseCommand; import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; import co.aikar.commands.annotation.Default; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; @@ -12,12 +14,10 @@ import net.countercraft.movecraft.events.CraftReleaseEvent; import net.countercraft.movecraft.localisation.I18nSupport; import org.bukkit.Bukkit; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; @@ -27,76 +27,65 @@ public class ReleaseCommand extends BaseCommand { @Subcommand("-p") + @Conditions("pilot_others") public static void releaseAllPlayers(CommandSender commandSender) { - if (!commandSender.hasPermission("movecraft.commands.release.others")) { - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("Release - No Force Release")); - return; - } - for (Player p : Bukkit.getOnlinePlayers()) { String name = p.getName(); final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(name); if (pCraft != null) CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.FORCE, false); } + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Released All Player Crafts")); } @Subcommand("-a") + @Conditions("pilot_others") public static void releaseAllCrafts(CommandSender commandSender) { - if (!commandSender.hasPermission("movecraft.commands.release.others")) { - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("Release - No Force Release")); - return; - } - final List craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts()); for (Craft craft : craftsToRelease) { CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false); } + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Released All Crafts")); } @Subcommand("-n") + @Conditions("pilot_others") public static void releaseAllNullCrafts(CommandSender commandSender) { - if (!commandSender.hasPermission("movecraft.commands.release.others")) { - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX - + I18nSupport.getInternationalisedString("Release - No Force Release")); - return; - } - final List craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts()); for (Craft craft : craftsToRelease) { if (!(craft instanceof PilotedCraft)) CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false); } + commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Released All Null Crafts")); } @Default @Syntax("<-p|-a|-n|player>") + @CommandCompletion("@players") public static void onCommand(CommandSender commandSender, String[] args) { if (args.length == 0) { - if(!(commandSender instanceof Player)) { + if(!(commandSender instanceof Player player)) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString( "Player - Error - You do not have a craft to release!")); return; } - Player player = (Player) commandSender; - final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); if (pCraft == null) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString( "Player - Error - You do not have a craft to release!")); return; } + CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.PLAYER, false); return; } @@ -123,24 +112,5 @@ public static void onCommand(CommandSender commandSender, String[] args) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Player - Not Piloting")); } - - public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { - if(strings.length !=1 || !commandSender.hasPermission("movecraft.commands.release.others")) - return Collections.emptyList(); - List completions = new ArrayList<>(); - completions.add("-a"); - completions.add("-p"); - completions.add("-n"); - for(Player player : Bukkit.getOnlinePlayers()) { - completions.add(player.getName()); - } - - List returnValues = new ArrayList<>(); - for(String completion : completions) { - if (completion.toLowerCase().startsWith(strings[strings.length - 1].toLowerCase())) - returnValues.add(completion); - } - return returnValues; - } } From 92847086b53f609579dc56af0c2a13931a09f4aa Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 17:20:34 +0200 Subject: [PATCH 40/42] Add RotateCommand pt1 --- .../net/countercraft/movecraft/Movecraft.java | 2 +- .../movecraft/commands/RotateCommand.java | 64 +++++++------------ 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 58fdea6f0..db2c474bf 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -245,7 +245,7 @@ private void initializeCommands() { movecraftCommandManager.registerCommand(new ManOverboardCommand()); movecraftCommandManager.registerCommand(new PilotCommand()); movecraftCommandManager.registerCommand(new ReleaseCommand()); - getCommand("rotate").setExecutor(new RotateCommand()); + movecraftCommandManager.registerCommand(new RotateCommand()); getCommand("scuttle").setExecutor(new ScuttleCommand()); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java index 28d7421a2..ceeeec79a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java @@ -1,84 +1,66 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Default; import net.countercraft.movecraft.MovecraftRotation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.localisation.I18nSupport; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; -public class RotateCommand implements TabExecutor{ - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { - if(!command.getName().equalsIgnoreCase("rotate")){ - return false; - } - if(!(commandSender instanceof Player)){ - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Must Be Player")); - return true; - } - Player player = (Player) commandSender; +@CommandAlias("rotate") +@CommandPermission("movecraft.commands") +public class RotateCommand extends BaseCommand { + + @Default + @CommandCompletion("left|right") + public static void onCommand(Player player, String[] args) { + if(args.length<1){ - commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Specify Direction")); - return true; + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Specify Direction")); + return; } if (args[0].equalsIgnoreCase("left")) { if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.rotateleft")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); if(craft==null){ player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return true; + return; } if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.ANTICLOCKWISE, craft.getHitBox().getMidPoint()); - return true; + return; } if (args[0].equalsIgnoreCase("right")) { if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.rotateright")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); if(craft==null){ player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return true; + return; } if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) { player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.CLOCKWISE, craft.getHitBox().getMidPoint()); - return true; + return; } player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Invalid Direction")); - return true; - } - - private final String[] completions = {"Right", "Left"}; - @Override - public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { - if(strings.length !=1) - return Collections.emptyList(); - List returnValues = new ArrayList<>(); - for(String completion : completions) - if(completion.toLowerCase().startsWith(strings[strings.length-1].toLowerCase())) - returnValues.add(completion); - return returnValues; } } From f5995c8e90f15e6a35e2715f653756c6f699d1b7 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 17:28:47 +0200 Subject: [PATCH 41/42] Add RotateCommand pt2 --- .../movecraft/commands/RotateCommand.java | 72 ++++++++++--------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java index ceeeec79a..49fd6f2f0 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java @@ -5,6 +5,8 @@ import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; import net.countercraft.movecraft.MovecraftRotation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -18,49 +20,49 @@ @CommandPermission("movecraft.commands") public class RotateCommand extends BaseCommand { - @Default - @CommandCompletion("left|right") - public static void onCommand(Player player, String[] args) { + @Subcommand("left") + @CommandPermission("movecraft.commands.rotateleft") + public static void rotateLeft(Player player) { + final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + if(craft==null){ + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); + return; + } - if(args.length<1){ - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Specify Direction")); + if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) { + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); + return; + } + + CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.ANTICLOCKWISE, craft.getHitBox().getMidPoint()); + } + + @Subcommand("right") + @CommandPermission("movecraft.commands.rotateright") + public static void rotateRight(Player player) { + final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); + if(craft==null){ + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); return; } - if (args[0].equalsIgnoreCase("left")) { - if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.rotateleft")) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return; - } - final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if(craft==null){ - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return; - } - if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return; - } - CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.ANTICLOCKWISE, craft.getHitBox().getMidPoint()); + + if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) { + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - if (args[0].equalsIgnoreCase("right")) { - if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.rotateright")) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return; - } - final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName()); - if(craft==null){ - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return; - } - if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) { - player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return; - } - CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.CLOCKWISE, craft.getHitBox().getMidPoint()); + CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.CLOCKWISE, craft.getHitBox().getMidPoint()); + } + + @Default + @Syntax("[left|right]") + public static void onCommand(Player player, String[] args) { + + if(args.length<1){ + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Specify Direction")); return; } + player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Invalid Direction")); } } From bb6f576bb7fd6d8219c2dca5402fbc3866fed43a Mon Sep 17 00:00:00 2001 From: Intybyte Date: Tue, 3 Sep 2024 17:36:23 +0200 Subject: [PATCH 42/42] Add ScuttleCommand --- .../net/countercraft/movecraft/Movecraft.java | 2 +- .../movecraft/commands/RotateCommand.java | 3 +- .../movecraft/commands/ScuttleCommand.java | 41 ++++++++++--------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index db2c474bf..090676184 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -246,7 +246,7 @@ private void initializeCommands() { movecraftCommandManager.registerCommand(new PilotCommand()); movecraftCommandManager.registerCommand(new ReleaseCommand()); movecraftCommandManager.registerCommand(new RotateCommand()); - getCommand("scuttle").setExecutor(new ScuttleCommand()); + movecraftCommandManager.registerCommand(new ScuttleCommand()); } @Override diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java index 49fd6f2f0..57114b822 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/RotateCommand.java @@ -2,9 +2,9 @@ import co.aikar.commands.BaseCommand; import co.aikar.commands.annotation.CommandAlias; -import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import net.countercraft.movecraft.MovecraftRotation; @@ -56,6 +56,7 @@ public static void rotateRight(Player player) { @Default @Syntax("[left|right]") + @Description("Rotates your craft") public static void onCommand(Player player, String[] args) { if(args.length<1){ diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ScuttleCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ScuttleCommand.java index 21ab55248..6399a01b9 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/commands/ScuttleCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/commands/ScuttleCommand.java @@ -1,5 +1,11 @@ package net.countercraft.movecraft.commands; +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Syntax; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.SinkingCraft; @@ -7,22 +13,19 @@ import net.countercraft.movecraft.events.CraftScuttleEvent; import net.countercraft.movecraft.localisation.I18nSupport; import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; -public class ScuttleCommand implements CommandExecutor { +@CommandAlias("scuttle") +public class ScuttleCommand extends BaseCommand { - - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { - - if (!command.getName().equalsIgnoreCase("scuttle")) { - return false; - } + @Default + @Syntax("") + @Description("Sinks piloted craft") + @CommandCompletion("@players") + public static void onCommand(CommandSender commandSender, String[] strings) { Craft craft = null; // Scuttle other player @@ -31,46 +34,44 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, if (player == null) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Must Be Online")); - return true; + return; } craft = CraftManager.getInstance().getCraftByPlayer(player); } else if (commandSender.hasPermission("movecraft.commands.scuttle.self") && strings.length == 0) { - if (!(commandSender instanceof Player)) { + if (!(commandSender instanceof Player player)) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Must Be Player")); - return true; + return; } - craft = CraftManager.getInstance().getCraftByPlayer(Bukkit.getPlayer(commandSender.getName())); + craft = CraftManager.getInstance().getCraftByPlayer(player); } if (craft == null) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); - return true; + return; } if (craft instanceof SinkingCraft) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Craft Already Sinking")); - return true; + return; } if (!commandSender.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".scuttle")) { commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions")); - return true; + return; } CraftScuttleEvent e = new CraftScuttleEvent(craft, (Player) commandSender); Bukkit.getServer().getPluginManager().callEvent(e); if (e.isCancelled()) - return true; + return; craft.setCruising(false); CraftManager.getInstance().sink(craft); commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Scuttle Activated")); - return true; - } }