Published Sep 2nd, 2014, 9/2/14 11:51 am
- 2,634 views, 0 today
- 3
- 1
- 3
205
Ok, so in part 3, I will show you how to add more than one command, and add Events. I will show you how I did my thor plugin basicly, I used the event to call down lightning when the player right clicks with the thor hammer (a diamond axe) and they receive a thor hammer when they type /thor. Shall we get started?
So lets continue from where we left off in the last tutorial, where we added the command. Lets add another command
DO NOT EVER EVER add (and I mean ever) another onCommand meathod, just do this:
That is what the code should look like now. All you have to do to add another command is just add this below the previous command and the previous }
That tests if the player typed the command "Alist" as well as if they typed the command above, if you add another on Comand method, it won't work. And you have to have the '{' bracket below the first command, or else it won't work. it closes the previous '{'
Now that you know how to add more than one command ,lets move on to adding and registering an event.
To add the event, paste this above you onCommand method
That calls an event of lightning striking down when the player right clicks with a diamond axe, it also plays the sound of an enderdragon getting hit when he uses it. It also damages the player (player.damage(5);) when they use the Diamond axe. the Player player = event.getPlayer(); gets the player who used the diamond axe, then executes the red of the event. Now right now, that will not do anything, we still need to register it in the onCommand method, so now you paste this under the onCommand meathod
Now, there will be an error, to fix the error, he have to implent listener, so at the top, do this: If we do not implent the listener, then the plugin will not know when to register the event, or how to call the event, so now we implent the listener:
You have to put that right after "public class YourClassNameHere extends java plugin"
So now it should look like this:
Final Code:
Now you can have fun changing the event to what-ever, and whatnot. Please do not copy my Thor Plugin and re-post it.
If you have any questions, or suggestions, either PM me, or comment them.
Click me to go to part 4!
So lets continue from where we left off in the last tutorial, where we added the command. Lets add another command
DO NOT EVER EVER add (and I mean ever) another onCommand meathod, just do this:
Code - New Command
package com.gabe.tutorial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class MainTutorial extends JavaPlugin {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
Player player = (Player) sender;
if(cmd.getName().equalsIgnoreCase("Hello")){
player.sendMessage(ChatColor.AQUA + "Hello Cheese! I mean...World!");
}
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
} else if (foo()) {
player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /Alist");
}
return true;
}
private boolean foo() {
// TODO Auto-generated method stub
return false;
}
public void onDisable() {
//On Disable method stub
}
@SuppressWarnings("deprecation")
public void onEnable() {
//on Enable Method stub
{
for (Player player: Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("hello.world")) {
player.sendMessage(ChatColor.DARK_AQUA + "You can say hello! Just type /hello");
}
}
}
}
}
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class MainTutorial extends JavaPlugin {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
Player player = (Player) sender;
if(cmd.getName().equalsIgnoreCase("Hello")){
player.sendMessage(ChatColor.AQUA + "Hello Cheese! I mean...World!");
}
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
} else if (foo()) {
player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /Alist");
}
return true;
}
private boolean foo() {
// TODO Auto-generated method stub
return false;
}
public void onDisable() {
//On Disable method stub
}
@SuppressWarnings("deprecation")
public void onEnable() {
//on Enable Method stub
{
for (Player player: Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("hello.world")) {
player.sendMessage(ChatColor.DARK_AQUA + "You can say hello! Just type /hello");
}
}
}
}
}
That is what the code should look like now. All you have to do to add another command is just add this below the previous command and the previous }
Adding another command
}
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
That tests if the player typed the command "Alist" as well as if they typed the command above, if you add another on Comand method, it won't work. And you have to have the '{' bracket below the first command, or else it won't work. it closes the previous '{'
Now that you know how to add more than one command ,lets move on to adding and registering an event.
To add the event, paste this above you onCommand method
Code - Event
@SuppressWarnings("deprecation")
@EventHandler
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
player.damage(5);
player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
}
}
@EventHandler
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
player.damage(5);
player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
}
}
That calls an event of lightning striking down when the player right clicks with a diamond axe, it also plays the sound of an enderdragon getting hit when he uses it. It also damages the player (player.damage(5);) when they use the Diamond axe. the Player player = event.getPlayer(); gets the player who used the diamond axe, then executes the red of the event. Now right now, that will not do anything, we still need to register it in the onCommand method, so now you paste this under the onCommand meathod
Code - Register event
getServer().getPluginManager().registerEvents(this, this);
Code - implenting listener
implements Listener
So now it should look like this:
Code: Implenting Listener
public class MainTutorial extends JavaPlugin implements Listener {
Final Code:
Final Code
package com.gabe.tutorial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MainTutorial extends JavaPlugin implements Listener {
@SuppressWarnings("deprecation")
@EventHandler
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
player.damage(5);
player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
}
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
Player player = (Player) sender;
if(cmd.getName().equalsIgnoreCase("Hello")){
player.sendMessage(ChatColor.AQUA + "Hello Cheese! I mean...World!");
}
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
}
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
} else if (foo()) {
player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /Alist");
}
return true;
}
private boolean foo() {
// TODO Auto-generated method stub
return false;
}
public void onDisable() {
//On Disable method stub
}
@SuppressWarnings("deprecation")
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
//on Enable Method stub
{
for (Player player: Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("hello.world")) {
player.sendMessage(ChatColor.DARK_AQUA + "You can say hello! Just type /hello");
}
}
}
}
}
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MainTutorial extends JavaPlugin implements Listener {
@SuppressWarnings("deprecation")
@EventHandler
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
player.damage(5);
player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
}
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
Player player = (Player) sender;
if(cmd.getName().equalsIgnoreCase("Hello")){
player.sendMessage(ChatColor.AQUA + "Hello Cheese! I mean...World!");
}
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
}
if (cmd.getName().equalsIgnoreCase("Alist")) {
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + " Cheese Commands ");
player.sendMessage(ChatColor.GOLD+ "=================================");
player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
} else if (foo()) {
player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /Alist");
}
return true;
}
private boolean foo() {
// TODO Auto-generated method stub
return false;
}
public void onDisable() {
//On Disable method stub
}
@SuppressWarnings("deprecation")
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
//on Enable Method stub
{
for (Player player: Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("hello.world")) {
player.sendMessage(ChatColor.DARK_AQUA + "You can say hello! Just type /hello");
}
}
}
}
}
Now you can have fun changing the event to what-ever, and whatnot. Please do not copy my Thor Plugin and re-post it.
If you have any questions, or suggestions, either PM me, or comment them.
Click me to go to part 4!
Tags |
1 Update Logs
Update #1 : by gabe4356 09/02/2014 3:56:45 pmSep 2nd, 2014
.Made everything a bit more clear
3063665
6
Create an account or sign in to comment.