THIS TUTORIAL IS FOR PEOPLE WHO HAVE JAVA EXPERIENCE. LINKS IN PREVIOUS EPISODEIn This EpisodeIn the previous episode, we made the command: "/hello". Now we are going to make it so we can say: "/hello <player>". This will send the hello message to another player (Part 1). We will also add another command with: "/tellme <message...>". That will basically take ALL the arguments after the "/tellme" and send them back to you (Part 2). Lets get started! Bukkit Development Skills (this episode): - Arguments after the command - Using loops to resend a message back to you (or potentially to another player) - Add other players into the command - Checking for online players (null check) Step 1 (Part 1)We are going to make our basic class, just like we did in our previous episode. Lets get started with that. public class Tutorial2 extends JavaPlugin { Step 2 (Part 1)Now that we started with the commands and stuff (from the previous episode), we can add the second argument to our /hello command. Lets split Step 2 into 3 parts: Step 2-1 (Argument Check)We need to check if the player sends no arguments with the command. We don't want that, because we want the player to send 1 argument with the command (the player name). We also don't want the command to have more than 1 argument. We want exactly one. There are 2 ways you can do this.
Of course, there are other ways that you could do this, but I think that the fastest one for me is the one on the right (Way 2). Now let's add the player into the command. Step 2-2 (Adding the Player and checking for Null)We need to add the player into the command, because we are sending the message to a player. I'll show you how to do this, and then explain it afterward. if (args.length == 1){ Basically, when we created a new player called "target". This player is trying to be found on the server. The server is basically looking for a player with the name from "args[0]". This is your first argument. You should know (if you have Java experience) that Java numbers start from 0. So args[0] is the first argument, args[1] is the second, args[2] is the third, and so on. Then we check that if the player "target" is null (not found/not real), then we send the sender a message saying that the player isn't online. Now let's finally send the player the message. Step 2-3 (Sending "target" the message)Now, we will send the player a message, and also send the sender a confirmation message saying that the player received the message. We will also give the target player the sender's name. if (args.length == 1){ What we did there is we sent a message to the "target" player that we created saying: "Hey, <target>! <sender> says hello!" Example: "Hey, Blockhead7360! Notch says hello!" (if Notch sent the command to Blockhead7360) Then we sent a message to the sender saying that "<target> has received your message!" Example: (to Notch) "Blockhead7360 has received your message!" Now: You technically could change the "usage" in the plugin.yml, but there is no need, because we already stated the usage in the main class. Step 3 (Part 2)Let's get into the longer messages. This is useful for commands like "/msg" or "/helpop" or something like that. Lets begin. We need to add the command under our onCommand() method: if (cmd.getName().equalsIgnoreCase("tellme")){ Step 4 (Part 2)Now we will create the usage message and all that: if (args.length >= 1){ Step 5 (Part 2)Its time to create the loop. The loop is going to do the same thing for each argument, combining them all into 1 message. Here it is (place under args.length >= 1): String message = ""; What we did up there is created a new String called message. We didn't make it null, we made it blank. Then we made a "for" loop, saying that our integer "i" is 0, and basically take everything after that. If you want another argument in between (like /tellme <1 word argument> <message...>), then you would need to change "int i = 0" to "int i = 1", and "i > 0" to "i > 1". This may be confusing, so if you have some questions, ask in the comments below. Step 6 (Part 2)Now we just send the message back to the player: sender.sendMessage(message); Step 7 (Part 2)Now, lets add this command into the plugin.yml name: Tutorial2 Step 8 (Part 1 & 2)Export your plugin as a Jar File into your Server's plugins folder, and start up your server. It should work! The EndYou learned how to add arguments and players! Next Episode is coming soon! Did I make any problems? Comment below! (I hope I didn't) Have any suggestions for future episodes (want to learn something else)? Comment below! Wanna talk? Comment below! FINAL CODE (Tutorial2.java): http://pastebin.com/mhNqfirR
0 Comments
Leave a Reply. |
|