|
29 | 29 | import com.mojang.brigadier.context.StringRange;
|
30 | 30 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
31 | 31 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 32 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
32 | 33 | import com.mojang.brigadier.tree.LiteralCommandNode;
|
33 | 34 | import de.tr7zw.changeme.nbtapi.NBTContainer;
|
34 | 35 | import dev.jorel.commandapi.*;
|
| 36 | +import dev.jorel.commandapi.annotations.Command; |
| 37 | +import dev.jorel.commandapi.annotations.Alias; |
| 38 | +import dev.jorel.commandapi.annotations.Default; |
| 39 | +import dev.jorel.commandapi.annotations.Help; |
| 40 | +import dev.jorel.commandapi.annotations.NeedsOp; |
| 41 | +import dev.jorel.commandapi.annotations.Permission; |
| 42 | +import dev.jorel.commandapi.annotations.Subcommand; |
| 43 | +import dev.jorel.commandapi.annotations.arguments.ADoubleArgument; |
| 44 | +import dev.jorel.commandapi.annotations.arguments.AEntitySelectorArgument; |
| 45 | +import dev.jorel.commandapi.annotations.arguments.AFloatArgument; |
| 46 | +import dev.jorel.commandapi.annotations.arguments.AIntegerArgument; |
| 47 | +import dev.jorel.commandapi.annotations.arguments.ALiteralArgument; |
| 48 | +import dev.jorel.commandapi.annotations.arguments.ALocation2DArgument; |
| 49 | +import dev.jorel.commandapi.annotations.arguments.ALocationArgument; |
| 50 | +import dev.jorel.commandapi.annotations.arguments.ALongArgument; |
| 51 | +import dev.jorel.commandapi.annotations.arguments.AMultiLiteralArgument; |
| 52 | +import dev.jorel.commandapi.annotations.arguments.APlayerArgument; |
| 53 | +import dev.jorel.commandapi.annotations.arguments.AScoreHolderArgument; |
| 54 | +import dev.jorel.commandapi.annotations.arguments.AStringArgument; |
35 | 55 | import dev.jorel.commandapi.arguments.*;
|
36 | 56 | import dev.jorel.commandapi.arguments.CustomArgument.CustomArgumentException;
|
37 | 57 | import dev.jorel.commandapi.arguments.CustomArgument.MessageBuilder;
|
@@ -151,6 +171,190 @@ void aliases() {
|
151 | 171 | /* ANCHOR_END: aliases1 */
|
152 | 172 | }
|
153 | 173 |
|
| 174 | +class annotations2 { |
| 175 | +/* ANCHOR: annotations2 */ |
| 176 | +@Command("teleport") |
| 177 | +@Alias({"tp", "tele"}) |
| 178 | +public class TeleportCommand { |
| 179 | +/* ANCHOR_END: annotations2 */ |
| 180 | +} |
| 181 | +} |
| 182 | +class annotations3 { |
| 183 | +/* ANCHOR: annotations3 */ |
| 184 | +@Command("teleport") |
| 185 | +@Permission("myplugin.tp") |
| 186 | +class TeleportCommand { |
| 187 | +/* ANCHOR_END: annotations3 */ |
| 188 | +} |
| 189 | +} |
| 190 | +class annotations4 { |
| 191 | +/* ANCHOR: annotations4 */ |
| 192 | +@Command("teleport") |
| 193 | +@NeedsOp |
| 194 | +class TeleportCommand { |
| 195 | +/* ANCHOR_END: annotations4 */ |
| 196 | +} |
| 197 | +} |
| 198 | +class annotations5 { |
| 199 | +/* ANCHOR: annotations5 */ |
| 200 | +@Command("teleport") |
| 201 | +@Help("Teleports yourself to another location") |
| 202 | +class TeleportCommand { |
| 203 | +/* ANCHOR_END: annotations5 */ |
| 204 | +} |
| 205 | +} |
| 206 | +class annotations6 { |
| 207 | +/* ANCHOR: annotations6 */ |
| 208 | +@Command("teleport") |
| 209 | +@Help(value = "Teleports yourself to another location", shortDescription = "TP to a location") |
| 210 | +class TeleportCommand { |
| 211 | +/* ANCHOR_END: annotations6 */ |
| 212 | + |
| 213 | +/* ANCHOR: annotations7 */ |
| 214 | +@Subcommand({"teleport", "tp"}) |
| 215 | +public static void teleport(Player player, @APlayerArgument OfflinePlayer target) { |
| 216 | + if(target.isOnline() && target instanceof Player onlineTarget) { |
| 217 | + player.teleport(onlineTarget); |
| 218 | + } |
| 219 | +} |
| 220 | +/* ANCHOR_END: annotations7 */ |
| 221 | +} |
| 222 | +/* ANCHOR: annotations8 */ |
| 223 | +@Default |
| 224 | +public static void command(CommandSender sender, |
| 225 | + @ADoubleArgument(min = 0.0, max = 10.0) double someDouble, |
| 226 | + @AFloatArgument(min = 5.0f, max = 10.0f) float someFloat, |
| 227 | + @AIntegerArgument(max = 100) int someInt, |
| 228 | + @ALongArgument(min = -10) long someLong |
| 229 | +) { |
| 230 | + // Command implementation here |
| 231 | +} |
| 232 | +/* ANCHOR_END: annotations8 */ |
| 233 | +/* ANCHOR: annotations9 */ |
| 234 | +@Default |
| 235 | +public static void command(CommandSender sender, |
| 236 | + @ALiteralArgument("myliteral") String literal, |
| 237 | + @AMultiLiteralArgument({"literal", "anotherliteral"}) String multipleLiterals |
| 238 | +) { |
| 239 | + // Command implementation here |
| 240 | +} |
| 241 | +/* ANCHOR_END: annotations9 */ |
| 242 | +/* ANCHOR: annotations10 */ |
| 243 | +@Default |
| 244 | +public static void command(CommandSender sender, |
| 245 | + @ALocationArgument(LocationType.BLOCK_POSITION) Location location, |
| 246 | + @ALocation2DArgument(LocationType.PRECISE_POSITION) Location location2d, |
| 247 | + @AEntitySelectorArgument.ManyEntities Collection<Entity> entities, |
| 248 | + @AScoreHolderArgument.Multiple Collection<String> scoreHolders |
| 249 | +) { |
| 250 | + // Command implementation here |
| 251 | +} |
| 252 | +/* ANCHOR_END: annotations10 */ |
| 253 | +} |
| 254 | + |
| 255 | +class annotationsintro { |
| 256 | +void annotationsintro1() { |
| 257 | +/* ANCHOR: annotationsintro1 */ |
| 258 | +Map<String, Location> warps = new HashMap<>(); |
| 259 | + |
| 260 | +// /warp |
| 261 | +new CommandAPICommand("warp") |
| 262 | + .executes((sender, args) -> { |
| 263 | + sender.sendMessage("--- Warp help ---"); |
| 264 | + sender.sendMessage("/warp - Show this help"); |
| 265 | + sender.sendMessage("/warp <warp> - Teleport to <warp>"); |
| 266 | + sender.sendMessage("/warp create <warpname> - Creates a warp at your current location"); |
| 267 | + }) |
| 268 | + .register(); |
| 269 | + |
| 270 | +// /warp <warp> |
| 271 | +new CommandAPICommand("warp") |
| 272 | + .withArguments(new StringArgument("warp").replaceSuggestions(ArgumentSuggestions.strings(info -> |
| 273 | + warps.keySet().toArray(new String[0]) |
| 274 | + ))) |
| 275 | + .executesPlayer((player, args) -> { |
| 276 | + player.teleport(warps.get((String) args.get(0))); |
| 277 | + }) |
| 278 | + .register(); |
| 279 | + |
| 280 | +// /warp create <warpname> |
| 281 | +new CommandAPICommand("warp") |
| 282 | + .withSubcommand( |
| 283 | + new CommandAPICommand("create") |
| 284 | + .withPermission("warps.create") |
| 285 | + .withArguments(new StringArgument("warpname")) |
| 286 | + .executesPlayer((player, args) -> { |
| 287 | + warps.put((String) args.get(0), player.getLocation()); |
| 288 | + }) |
| 289 | + ) |
| 290 | + .register(); |
| 291 | +/* ANCHOR_END: annotationsintro1 */ |
| 292 | +} |
| 293 | + |
| 294 | +/* ANCHOR: annotationsintro2 */ |
| 295 | +/* ANCHOR: annotationsintro4 */ |
| 296 | +@Command("warp") |
| 297 | +public class WarpCommand { |
| 298 | +/* ANCHOR_END: annotationsintro4 */ |
| 299 | + |
| 300 | + // List of warp names and their locations |
| 301 | + static Map<String, Location> warps = new HashMap<>(); |
| 302 | + |
| 303 | + @Default |
| 304 | + public static void warp(CommandSender sender) { |
| 305 | + sender.sendMessage("--- Warp help ---"); |
| 306 | + sender.sendMessage("/warp - Show this help"); |
| 307 | + sender.sendMessage("/warp <warp> - Teleport to <warp>"); |
| 308 | + sender.sendMessage("/warp create <warpname> - Creates a warp at your current location"); |
| 309 | + } |
| 310 | + |
| 311 | + @Default |
| 312 | + public static void warp(Player player, @AStringArgument String warpName) { |
| 313 | + player.teleport(warps.get(warpName)); |
| 314 | + } |
| 315 | + |
| 316 | + @Subcommand("create") |
| 317 | + @Permission("warps.create") |
| 318 | + public static void createWarp(Player player, @AStringArgument String warpName) { |
| 319 | + warps.put(warpName, player.getLocation()); |
| 320 | + } |
| 321 | + |
| 322 | +} |
| 323 | + |
| 324 | +/* ANCHOR_END: annotationsintro2 */ |
| 325 | +class annotationsintro3 { |
| 326 | +static Map<String, Location> warps = new HashMap<>(); |
| 327 | +{ |
| 328 | +/* ANCHOR: annotationsintro3 */ |
| 329 | +CommandAPI.registerCommand(WarpCommand.class); |
| 330 | +/* ANCHOR_END: annotationsintro3 */ |
| 331 | +} |
| 332 | +/* ANCHOR: annotationsintro5 */ |
| 333 | +@Default |
| 334 | +public static void warp(CommandSender sender) { |
| 335 | + sender.sendMessage("--- Warp help ---"); |
| 336 | + sender.sendMessage("/warp - Show this help"); |
| 337 | + sender.sendMessage("/warp <warp> - Teleport to <warp>"); |
| 338 | + sender.sendMessage("/warp create <warpname> - Creates a warp at your current location"); |
| 339 | +} |
| 340 | + |
| 341 | +/* ANCHOR_END: annotationsintro5 */ |
| 342 | +/* ANCHOR: annotationsintro6 */ |
| 343 | +@Default |
| 344 | +public static void warp(Player player, @AStringArgument String warpName) { |
| 345 | + player.teleport(warps.get(warpName)); |
| 346 | +} |
| 347 | +/* ANCHOR_END: annotationsintro6 */ |
| 348 | +/* ANCHOR: annotationsintro7 */ |
| 349 | +@Subcommand("create") |
| 350 | +@Permission("warps.create") |
| 351 | +public static void createWarp(Player player, @AStringArgument String warpName) { |
| 352 | + warps.put(warpName, player.getLocation()); |
| 353 | +} |
| 354 | +/* ANCHOR_END: annotationsintro7 */ |
| 355 | +} |
| 356 | +} |
| 357 | + |
154 | 358 | void argument_angle() {
|
155 | 359 | // NOTE: This example isn't used!
|
156 | 360 | /* ANCHOR: argumentAngle1 */
|
@@ -661,8 +865,23 @@ void argument_lootTable() {
|
661 | 865 | }
|
662 | 866 |
|
663 | 867 | @SuppressWarnings({ "unchecked", "null" })
|
664 |
| -void argument_map() { |
| 868 | +class argument_map { |
665 | 869 | /* ANCHOR: argumentMap1 */
|
| 870 | +@FunctionalInterface |
| 871 | +public interface StringParser<T> { |
| 872 | + /** |
| 873 | + * A method that turns a String into an object of type T. |
| 874 | + * |
| 875 | + * @param s The String to parse |
| 876 | + * @return The resulting parsed object |
| 877 | + * @throws WrapperCommandSyntaxException If there is a problem with the syntax of the String that prevents it from being turned into an object of type T. |
| 878 | + */ |
| 879 | + T parse(String s) throws WrapperCommandSyntaxException; |
| 880 | +} |
| 881 | + |
| 882 | +/* ANCHOR_END: argumentMap1 */ |
| 883 | +void commandReg() { |
| 884 | +/* ANCHOR: argumentMap2 */ |
666 | 885 | new CommandAPICommand("sendmessage")
|
667 | 886 | // Parameter 'delimiter' is missing, delimiter will be a colon
|
668 | 887 | // Parameter 'separator' is missing, separator will be a space
|
@@ -694,7 +913,8 @@ void argument_map() {
|
694 | 913 | }
|
695 | 914 | })
|
696 | 915 | .register();
|
697 |
| -/* ANCHOR_END: argumentMap1 */ |
| 916 | +/* ANCHOR_END: argumentMap2 */ |
| 917 | +} |
698 | 918 | }
|
699 | 919 |
|
700 | 920 | @SuppressWarnings("null")
|
@@ -1194,6 +1414,25 @@ void brigadier(){
|
1194 | 1414 | /* ANCHOR_END: brigadier7 */
|
1195 | 1415 | }
|
1196 | 1416 |
|
| 1417 | +class brigadierSuggestions { |
| 1418 | +/* ANCHOR: brigadierSuggestions0 */ |
| 1419 | +@FunctionalInterface |
| 1420 | +public interface ArgumentSuggestions<CommandSender> { |
| 1421 | + |
| 1422 | + /** |
| 1423 | + * Create a {@link CompletableFuture} resolving onto a brigadier {@link Suggestions} object. |
| 1424 | + * |
| 1425 | + * @param info The suggestions info |
| 1426 | + * @param builder The Brigadier {@link SuggestionsBuilder} object |
| 1427 | + * @return a {@link CompletableFuture} resolving onto a brigadier {@link Suggestions} object. |
| 1428 | + * @throws CommandSyntaxException if there is an error making suggestions |
| 1429 | + */ |
| 1430 | + CompletableFuture<Suggestions> suggest(SuggestionInfo<CommandSender> info, SuggestionsBuilder builder) |
| 1431 | + throws CommandSyntaxException; |
| 1432 | +/* ANCHOR_END: brigadierSuggestions0 */ |
| 1433 | +} |
| 1434 | +} |
| 1435 | + |
1197 | 1436 | void brigadierSuggestions() {
|
1198 | 1437 | /* ANCHOR: brigadierSuggestions1 */
|
1199 | 1438 | Map<String, String> emojis = new HashMap<>();
|
@@ -2077,6 +2316,20 @@ void proxySender() {
|
2077 | 2316 | /* ANCHOR_END: proxySender2 */
|
2078 | 2317 | }
|
2079 | 2318 |
|
| 2319 | +class registeringannotations { |
| 2320 | + class WarpCommand {} |
| 2321 | +/* ANCHOR: registeringannotations2 */ |
| 2322 | +class MyPlugin extends JavaPlugin { |
| 2323 | + |
| 2324 | + @Override |
| 2325 | + public void onLoad() { |
| 2326 | + CommandAPI.registerCommand(WarpCommand.class); |
| 2327 | + } |
| 2328 | + |
| 2329 | +} |
| 2330 | +/* ANCHOR_END: registeringannotations2 */ |
| 2331 | +} |
| 2332 | + |
2080 | 2333 | void requirements() {
|
2081 | 2334 | /* ANCHOR: requirements1 */
|
2082 | 2335 | new CommandAPICommand("repair")
|
|
0 commit comments