The API provides a way to register your own commands. This can be done by using the SuperiorSkyblockAPI.registerCommand(<SuperiorCommand>) method.
In this tutorial, I will add a new command: /is near. It will return which islands are nearby, and in which direction.
First of all, I will create my custom SuperiorCommand class:
publicfinalclassNearCommandimplementsSuperiorCommand { @OverridepublicList<String> getAliases() {// A list of aliases. The first argument will be the label of the subcommand.returnArrays.asList("near","nearby"); } @OverridepublicStringgetPermission() {// The required permission for the command. If you don't want a specific permission, use "".return""; } @OverridepublicStringgetUsage(Locale locale) {// The usage of the command. Should only include the label & arguments of the command.return"near"; } @OverridepublicStringgetDescription(Locale locale) {// The description of the command, which will be shown in /is help.return"Locate nearby islands."; } @OverridepublicintgetMinArgs() {// Minimum arguments for the command, including the label.return1; } @OverridepublicintgetMaxArgs() {// Maximum arguments for the command, including the label.return1; } @OverridepublicbooleancanBeExecutedByConsole() {// Whether or not the command can be executed from Console.returnfalse; } @OverridepublicbooleandisplayCommand() {// Whether or not the command would be displayed in the /is help list.returntrue; } @Overridepublicvoidexecute(SuperiorSkyblock plugin,CommandSender sender,String[] args) {// TODO } @OverridepublicList<String> tabComplete(SuperiorSkyblock plugin,CommandSender sender,String[] args) {// TODOreturnnewArrayList<>(); }}
That's it. The only thing that is left is to execute your code under the execute code, and implement the tab-complete.
The execute() and tabComplete() methods contain 3 parameters:
plugin: The instance of the plugin.
sender: The command sender.
args: The arguments from the sender.
After filling my code, the final version of the command is the following:
publicfinalclassNearCommandimplementsSuperiorCommand { <alltheothermethods>privatestaticfinalint MAX_ISLAND_SIZE =200; @Overridepublicvoidexecute(SuperiorSkyblock plugin,CommandSender sender,String[] args) {// I know that the sender is a player, as I disabled the console from executing.SuperiorPlayer superiorPlayer =SuperiorSkyblockAPI.getPlayer((Player) sender);Island targetIsland =plugin.getGrid().getIslandAt(superiorPlayer.getLocation());if(targetIsland ==null){ sender.sendMessage("" + ChatColor.RED + ChatColor.BOLD + "Error | " + ChatColor.GRAY + "You must stand inside an island.");
return; }StringBuilder message =newStringBuilder();int islandsAmount =0;for(BlockFace blockFace :BlockFace.values()){ // I want only to get north, west, east and south - I will just check if their modX is -+1 or their modZ is -+1, but not both.
int modXAbs =Math.abs(blockFace.getModX()), modZAbs =Math.abs(blockFace.getModZ());if((modXAbs ==1|| modZAbs ==1) && modXAbs + modZAbs !=2){Location targetIslandLocation =targetIsland.getCenter(World.Environment.NORMAL).add(MAX_ISLAND_SIZE *3*blockFace.getModX(),0, MAX_ISLAND_SIZE *3*blockFace.getModZ());if(plugin.getGrid().getIslandAt(targetIslandLocation) !=null) { islandsAmount++;message.append(", ").append(blockFace.name()); } } }String directionsMessage =message.length() ==0?"":message.substring(2);switch (islandsAmount){case0: sender.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "Island | " + ChatColor.GRAY + "No islands were found.");
break;case1: sender.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "Island | " + ChatColor.GRAY + "There is one island at " + directionsMessage + ".");
break;default: sender.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "Island | " + ChatColor.GRAY + "There are islands at " + directionsMessage + ".");
break; } } @OverridepublicList<String> tabComplete(SuperiorSkyblock plugin,CommandSender sender,String[] args) {// I don't want any tab completes for this command.returnnewArrayList<>(); }}
The last thing to do is to call SuperiorSkyblockAPI.registerCommand(<SuperiorCommand>), and that's it! You registered your own command! This command will be supported in all tab completes, /is help and argument restrictions! You can also register the commands without calling the method above. You can extract the class into an external jar, and put it in the commands folder of SuperiorSkyblock2.