Solved Need a little bit of help with coding please?

Discussion in 'Plugin Development' started by adyyy1337, Oct 4, 2013.

Thread Status:
Not open for further replies.
  1. i admit it. i am a bit of a 'noob' towards coding, i started 3 days ago. my plugin isnt working and idk why, i asked my friend who also knows about code and he cant seem to find any errors.

    What i want it to do:
    Basically, people just type /p and it returns saying "Pinged!"

    The Error:
    http://gyazo.com/7caaff52a9b43cd91161c3a046f8124b

    Main.java:
    Code:java
    1. package adyyy1337.simpleping;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class Main extends JavaPlugin{
    6.  
    7. public void onEnable(){
    8. System.out.println("SimplePing Has Been Enabled!");
    9. this.getServer().getPluginManager().registerEvents(new Events(), this);
    10. }
    11.  
    12. public void onDisable(){
    13. System.out.println("SimplePing Has Been Disabled!");
    14. }
    15. }
    16.  

    Events.java:
    Code:java
    1. package adyyy1337.simpleping;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10.  
    11. public class Events implements Listener{
    12.  
    13. @EventHandler(priority = EventPriority.NORMAL)
    14. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    15. Player player = (Player) sender;
    16. if(commandLabel.equalsIgnoreCase("P"));{
    17. player.sendMessage(ChatColor.GOLD + "Pinged!");
    18. }
    19. return false;
    20. }
    21.  
    22. }
    23.  


    any help would be appreciated, thanks.
     
  2. Offline

    MelkyPie

  3. Offline

    3ptO

    adyyy1337 onCommand is not an event, you do not need to implement Listener.

    Also, refer to line 16 @ Events class:
    Code:java
    1. if(commandLabel.equalsIgnoreCase("P"));{

    You should not have added ";"

    Move onCommand to the Main class:
    Code:java
    1. package adyyy1337.simpleping;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class Main extends JavaPlugin
    6. {
    7. public void onEnable()
    8. {
    9. System.out.println("SimplePing Has Been Enabled!");
    10. }
    11.  
    12. public void onDisable()
    13. {
    14. System.out.println("SimplePing Has Been Disabled!");
    15. }
    16.  
    17. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    18. {
    19. Player player = (Player) sender;
    20. if (commandLabel.equalsIgnoreCase("P")) {
    21. player.sendMessage(ChatColor.GOLD + "Pinged!");
    22. }
    23. return false;
    24. }
    25. }
     
  4. Offline

    Chinwe

    Also, check "if (sender instanceof Player)" before casting sender to a Player (try running the command from the console as it is), and put a "return true;" after sending the message, otherwise the player will be sent the usage of the command, which you don't want if they did it correctly :)
     
  5. Offline

    JPG2000

  6. thanks all, but it says /p when i type /p.
    i also added the command info to plugin.yml, maybe i did something wrong there?
    when typing /p, this shows up in chat:
    http://gyazo.com/bf05ca1dba91721f3d6bc0b829aeefab
    Main.java:
    Code:java
    1. package adyyy1337.simpleping;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin{
    9.  
    10. public void onEnable()
    11. {
    12. System.out.println("SimplePing Has Been Enabled!");
    13. }
    14.  
    15. public void onDisable()
    16. {
    17. System.out.println("SimplePing Has Been Disabled!");
    18.  
    19. }
    20.  
    21. public boolean onCommand(CommandSender sender, CommandSender cmd, String commandLabel, String[] args)
    22. {
    23. Player player = (Player) sender;
    24. if(commandLabel.equalsIgnoreCase("P")) {
    25. player.sendMessage(ChatColor.GOLD + "Pinged!");
    26. }
    27. return false;
    28. }
    29.  
    30. }
    31.  

    plugin.yml:
    Code:
    name: SimplePing
    version: 1.0
    main: adyyy1337.simpleping.Main
    commands:
      p:
          description: Used For Testing Server Response Time.
          usage: /<command>
          permission: SimplePing.p
          permission-message: You Do Not Have The Required Permissions.
     
  7. Offline

    FlareLine

    Try
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("p"){
    2. player.sendMessage(ChatColor.GOLD + "Pinged!");
    3. return true;
    4. } else {
    5. return false;
    6. }

    instead of
    Code:java
    1. if(commandLabel.equalsIgnoreCase("P")) {
    2. player.sendMessage(ChatColor.GOLD + "Pinged!");
    3. }
     
  8. nope, still gives me same error.
    Main.java(now):
    Code:java
    1. package adyyy1337.simpleping;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin{
    9.  
    10. public void onEnable()
    11. {
    12. System.out.println("SimplePing Has Been Enabled!");
    13. }
    14.  
    15. public void onDisable()
    16. {
    17. System.out.println("SimplePing Has Been Disabled!");
    18.  
    19. }
    20.  
    21. public boolean onCommand(CommandSender sender, CommandSender cmd, String commandLabel, String[] args)
    22. {
    23. Player player = (Player) sender;
    24. if(cmd.getName().equalsIgnoreCase("p")){ // i corrected your typo, 2 close brackets instead of one
    25. player.sendMessage(ChatColor.GOLD + "Pinged!");
    26. return true;
    27. } else {
    28. return false;
    29. }
    30. }
    31.  
    32. }
     
  9. Offline

    FlareLine

    Hmm, maybe it is because you are not returning false at the end...
    Code:java
    1. public boolean onCommand(CommandSender sender, CommandSender cmd, String commandLabel, String[] args)
    2. {
    3. Player player = (Player) sender;
    4. if(cmd.getName().equalsIgnoreCase("p")){ // i corrected your typo, 2 close brackets instead of one
    5. player.sendMessage(ChatColor.GOLD + "Pinged!");
    6. return true;
    7. }
    8. return false;
    9. }
     
  10. Offline

    3ptO

    adyyy1337 The example I provided you was correct, however, you have a typo.

    Change onCommand to:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)

    You had CommandSender sender, CommandSender cmd...

    For the record, as previously suggested by Chinwe, you need to check if the sender is an instance of Player. You can easily do so by adding if (sender instanceof Player) {
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2. {
    3. if (sender instanceof Player) {
    4. //execute
    5. }
    6. }
     
  11. Offline

    FlareLine

    3ptO Derp, I just realised that. I had expected that the method's parameters would be correct - it's pretty hardto stuff up. But it should work with his example adyyy1337
     
  12. thank you! it now works. haha, i didnt notice that typo :confused: thank you all for your help guys!
     
Thread Status:
Not open for further replies.

Share This Page