- 13,896 views, 0 today
- 29
- 8
- 31
105
Remember to diamond, favorite, and subscribe for more!
**Plan**
Tutorial #2 http://www.planetminecraft.com/blog/bukkit-tutorial-2---commands-and-permissions/! Tutorial #3 will have some more things on events and Arrays! #4 will probably be some sort of contest - Best plugin? Thoughts on it? Leave a comment!
This is for learning purposes, Please do not take the code that I wrote here and upload it as your own, thanks.
-_-_-Intro-_-_-
Hello people reading this! This is going to be an awesome tutorial for those who want to start writing SERVER plugins for BUKKIT! :D This is Tutorial #1: Starting up and handling events!
-_-_-Step 1-_-_-
Learn Java (Tutorials and youtube always helps!) and have an idea of a plugin you want to make! We will start off simple by making a plugin that will cancel an event, we will cancel the InventoryDropEvent in this tutorial, next time we will add commands!
-_-_-Step 2-_-_-
Prepare your workspace! You need Eclipse or some other Java IDE, I recommend Eclipse (http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/junor)
and you will need the latest bukkit build! Grab one from dl.bukkit.org!
Now that you have all of these things installed, Let's prepare your workspace!
First off, you're going to need to create a new Java Project. In Eclipse, go to File > New > Java Project.You can name the project whatever you want, I named mine "PMC Tutorial!" After you have the project folder, it will be there on the left in the package explorer! Next, We need to reference craftbukkit.jar to it! go to the project folder, right click it, and go all the way down the where it says "Properties"! Once you click properties, it will open a new window. On the left of that window it will have an option that says "Java Build Path". Click it, than on the right there will be an "Add External Jars" button, click that and add the craftbukkit.jar you downloaded earlier. Now, finally, we need to add a package to the project! Drop down the project, you will see a thing that says "src"! Right click it, go to new, than go to package. You will name the package something like a backwards domain name such as, net.endercraftbuild. You can really name it anything, it's a good idea to use a domain or something. If you do not have one, use something like me.yourname. What you CANNOT use is org.bukkit, net.bukkit, or com.bukkit! It should look something like this:
-_-_-Step 3-_-_-
Now, we will actually start coding! Yay! Go to your package that you created in Step 2, right click it, go to new, go to class, and name the class anything you want really, this will be your main class! I named mine "TutMain"!
Now, we start coding! Where it says "public class (classname)" Add this to the end of it: "extends JavaPlugin implements Listener" You will need these for your plugin to start! If they are red underlined, hover over it with your mouse, a quick fix menu will come up, click the one that says "Import" Now, you need your onEnable() and onDisable() methods! Right under the public class thing, add your onEnable() method with this code: "public void onEnable() {" Now, We don't have to do this, but we will, we are going to make the plugin say a message we specify in the console when it starts up! To do this, you want to go right under your onEnable(), and write in: "getLogger().info("MyFirst plugin has been enabled!");" You can change the words within the quotes to be whatever you want! Now, an essential line for your plugin to work, you must register the plugin! Right under that line with the getLogger(), put this under!: "getServer().getPluginManager().registerEvents(this, this);" You NEED this line!! Now you can close this statement and put a } at the end! Now we will write our onDisable() method! It's about the same as the on enable, add this line in: public void onDisable() {! We will also write a disable message! A message that is put in console when the plugin is disabled! It's going to be the same as the onEnable(), here is mine! "getLogger().info("MyFirst plugin has been disabled! :D"); }"
Okay! Now our plugin has enabling and disabling methods! (Make sure you import everything needed!)
-_-_-Step 4-_-_-
Now, I'll show you how to cancel an event. An event is fired every time someone does something! If a player throws an item out of their inventory, the PlayerItemDropEvent is called! So, Under the disable, we will add the event! This is the line we use for events:
public void onDrop(PlayerDropItemEvent event) {
But right ABOVE the public void onDrop, Write @Eventhandler and import it, you NEED this!
The onDrop means very little to you, it can be basically anthing, the PlayerDropEvent is the event we are using, do not change this! the "event" word right next to PlayerDropItemEvent is what we named the event, so every time we see the word "event" in our code, it refers to PlayerDropItemEvent! You can go to jd.bukkit.org to find info on the events! Info on PlayerItemDrop is here! :jd.bukkit.org/apidocs/org/bukkit/event/player/PlayerDropItemEvent.html
Now, What we want to do is if the player throws an item out of their inventory, we want to make it NOT let them throw it, and sends the player a message!
Now, How do we do this? We will be using a variable, "player" which will get that player when sending a message to them, it has to know WHO to send the message to right!? To do this, we will write: Player player = event.getPlayer()! Remember to import Player. This line says that it's a Player, the variable name is "player" and it is event.getPlayer(), which gets the player who performed the event! Every time we see "player" in the code, it will refer to "event.getPlayer()" We could always put if statements to cancel the event under certain circumstances, but that's for a later tutorial. We will now cancel the event with this line right under the variable: event.setCancelled(true); This line will cancel the PlayerDropItemEvent! So if they try to drop items, it won't work! :D Now, we will send them a message telling them they cannot drop that item! So, under the canceling line, We will do: player.sendMessage(ChatColor.YELLOW + "[PMCTutorial]" + ChatColor.GREEN + " You cannot drop items! :D");
This uses the variable "player" infront, getting the player who performed the event, it sends them a message, in the yellow chat color "[PMCTutorial]" and than green chat "You cannot drop items! :D" You will need to import ChatColor! Now, you are done for the actual coding part! Yay! We will now export our plugin! This is my final code:
Yours probably looks similar! :D
Now, we export it into a .jar file! Go to File, Export, than a new screen will pop up! Click the Java folder, than JAR File button! Make sure you select the package, put in where you want the jar to be saved to, than click "Finish"! Now, You are ALMOST done! We just need to add a plugin.yml for the plugin to work! Create a new .txt document, only change it to "plugin.yml"! You can edit it with something like notepad or notpad++! Inside the plugin.yml, We will put some essential info for bukkit to recognize our plugin! We will define the plugin name, I named mine PMCPluginTutorial, This is what will show up when you type /pl or /plugins in-game. You will need to add a pointer to your main class, It will be (packagename).(mainclassname)! Mine is net.endercraftbuild.net.TutMain!
And finally, A version number, I just simply put mine 1.0. This is the most simple possible plugin.yml, as we add commands and permissions in later tutorials the plugin.yml will become more complicated! The format is very simple, Here is mine:
Now just simply drag this into your .jar and you are DONE!
-_-_-Step 5-_-_-
Test your plugin! Get a localhost bukkit server and test your plugin! Here is some screenshots of our beautiful, working plugin! :D (And give a diamond! :D)
Yay, It works! :D
Not working?
Make sure you have the plugin registered
Make sure you have your @EventHandler
Make sure you have a plugin.yml
Make sure you imported everything needed!
Tags |
2 Update Logs
Update #2 : by superpeanut911 10/23/2012 7:23:25 pmOct 23rd, 2012
Made it easier to follow, A thing to remember to people... thingy at the bottom
LOAD MORE LOGS
1553878
6
Create an account or sign in to comment.
but how can i make a public variable
i want something recognized that i recognized in another piece off code
but it says it does not know the variable
while i made it later
please, i need a way to make my 2 strings public for my code to work
annyone know?
If anyone knows please tell me.
@EventHandler
public void onDrop(PlayerDropItemEvent event) {
Player player = event.getPlayer();
if(player.hasPermission("your.permission.here") {
event.setCancelled(false);
}else{
event.setCancelled(true);
player.sendMessage(ChatColor.RED + "you can drop items bro");
Sorry if this is wrong, im not to familiar, but hope it helped.
http://www.planetminecraft.com/blog/bukkit-tutorial-2---commands-and-permissions/