• 6/11/14 8:44 pm
- 241 views • 0 today
- 1
- 0
1
Hey guys I'm back with the second tutorial for bukkit plugins, before I start, make sure you do my first tutorial for plugins before this one because what I cover in that I will not cover in this. Also write the code in the same classes so these tutorials won't be unnecessarily long. Before we start what is a listeners? The easiest wany to explain it it it's an event. You can choose between tons of listeners such as Player Join(we will be doing this), player quit, player interact(clicking, moving, etc), and sooo much more. Let's get started!!
******Importent: most code is case-sensitive
*****after 2 slashes "//" the words on that line will not be read as code and the compuder will skip over, you do not need to type them.
Ok make sure you have all the methods and everything from the last tutorial working.
In your onEnable() method lets make a listener. To do this, write:
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new PlayerJoinListener(this), this);
Import the PluginManager and create a new class in SRC, for the package put me.<your name>.Listeners and for the Class put PlayerJoinListener.
Then create a constructor. A constructor is the method that runs when the class is called the so the pm.registerEvents line calls this method to run first. To create a constructor and calling the other part we will need, write:
//also write implements Listener and import is after it says public class PlayerJoinListener
MainClass plugin;
//constructor below
public PlayerJoinListener(MainClass pl){
plugin = pl;
}
Now that we have a constructor, we can now actually do what we want our plugin to listen for. :) Let's make a new method for this:
@EventHandler
//import @EventHandler and PlayerJoinEvent
public void onPlayerJoin(PlayerJoinEvent event){
//geting the player that joined in the statement below
Player player = event.getPlayer();
}
Only a few more lines to go!
What do we want to do to the player when they join? Lets send them a message but not just them the entire server a messgae that they are on! :)
player.sendMessage(ChatColor.RED+"Welcome to the server!");
Bukkit.broadcastMessage(ChatColor.GREEN+"Say hight to the HOT player named"+player.getName())
//import Bukkit and chatcolor :) ^
We are done! since we did not add any commands, we dont need to edit the plugin.yml file export it and see if it works, if it doesn't, feel free to ask any questions :)
Thanks for reading, bye :)
******Importent: most code is case-sensitive
*****after 2 slashes "//" the words on that line will not be read as code and the compuder will skip over, you do not need to type them.
Ok make sure you have all the methods and everything from the last tutorial working.
In your onEnable() method lets make a listener. To do this, write:
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new PlayerJoinListener(this), this);
Import the PluginManager and create a new class in SRC, for the package put me.<your name>.Listeners and for the Class put PlayerJoinListener.
Then create a constructor. A constructor is the method that runs when the class is called the so the pm.registerEvents line calls this method to run first. To create a constructor and calling the other part we will need, write:
//also write implements Listener and import is after it says public class PlayerJoinListener
MainClass plugin;
//constructor below
public PlayerJoinListener(MainClass pl){
plugin = pl;
}
Now that we have a constructor, we can now actually do what we want our plugin to listen for. :) Let's make a new method for this:
@EventHandler
//import @EventHandler and PlayerJoinEvent
public void onPlayerJoin(PlayerJoinEvent event){
//geting the player that joined in the statement below
Player player = event.getPlayer();
}
Only a few more lines to go!
What do we want to do to the player when they join? Lets send them a message but not just them the entire server a messgae that they are on! :)
player.sendMessage(ChatColor.RED+"Welcome to the server!");
Bukkit.broadcastMessage(ChatColor.GREEN+"Say hight to the HOT player named"+player.getName())
//import Bukkit and chatcolor :) ^
We are done! since we did not add any commands, we dont need to edit the plugin.yml file export it and see if it works, if it doesn't, feel free to ask any questions :)
Thanks for reading, bye :)
More like this
2938671
6



Have something to say?