Whats wrong?

Discussion in 'Plugin Development' started by Malo, Oct 23, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Malo

    I am trying to learn java, so I watched a few tutorials, and came up with a simple plugin for voting, but it no work O_O

    http://pastebin.com/3U83ehnt
    any problems here?
     
  2. Offline

    Mudsquisher

    It would help if we could see the error log. What exactly is the problem that is occuring?

    Do you have plugin.yml file setup correctly? Have you checked out this wiki: http://wiki.bukkit.org/Plugin_Tutorial

    What I can tell now is the place of your return false statement in the onCommand method will make it always return false, despite if the if statement is true or not. Returning false is primarily used to check for correct arguments and if any exceptions occur to stop. But returning true means everything worked properly, the command was typed in right and the method is done. I suggest changing it like so:

    Code:
    if (commandLabel.equalsIgnoreCase("vote")) {
    player.sendMessage(ChatColor.GOLD + "=================================");
    player.sendMessage(ChatColor.AQUA + "Please Click the link below to vote!:");
    player.sendMessage(ChatColor.GREEN + "link");
    player.sendMessage(ChatColor.GOLD + "=================================");
     
    return true;
    }
     
    bobacadodl likes this.
  3. Offline

    KeybordPiano459

    Do this instead:
    Code:java
    1. package me.malo.votesite;
    2.  
    3. import java.util.logging.Logger;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Main extends JavaPlugin {
    11. public void onEnable() {
    12. getLogger().info("[VoteSite] has been enabled!");
    13. }
    14.  
    15. public void onDisable() {
    16. getLogger().info("[VoteSite] has been disabled!");
    17. }
    18.  
    19. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("vote")) {
    22. player.sendMessage(ChatColor.GOLD + "=================================");
    23. player.sendMessage(ChatColor.AQUA + "Please Click the link below to vote!:");
    24. player.sendMessage(ChatColor.GREEN + "link");
    25. player.sendMessage(ChatColor.GOLD + "=================================");
    26. return true;
    27. }
    28. return false;
    29. }
    30. }

    At least, that's how I would do it. Also, do you have the plugin.yml?

    Also, typing 'vote' in console would give you an error because you're telling it that it's a player, if you don't want that to happen, you'll have to check if the CommandSender is a player, and if it isn't, use System.out.println or java's logger to print messages to the console.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    bobacadodl

    What's the need to casting the sender to player and then sending the messages? You can just do sender.sendmessage("")... Also if you cast sender to player without checking if its a player first then this will throw errors when executed from console.


    Better Version :p
    Code:
    package me.malo.votesite;
     
    import java.util.logging.Logger;
    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 Main extends JavaPlugin {
    public void onEnable() {
        getLogger().info("[VoteSite] has been enabled!");
    }
     
    public void onDisable() {
        getLogger().info("[VoteSite] has been disabled!");
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("vote")) {
                sender.sendMessage(ChatColor.GOLD + "=================================");
                sender.sendMessage(ChatColor.AQUA + "Please Click the link below to vote!:");
                sender.sendMessage(ChatColor.GREEN + "link");
                sender.sendMessage(ChatColor.GOLD + "=================================");
                return true;
            }
            return false;
        }
    }
     
  5. Offline

    KeybordPiano459

    That's what I said...
     
  6. Offline

    bobacadodl

    Sorry o_o I started typing the reply before you posted that
     
  7. Offline

    KeybordPiano459

    It's ok :)

    But you do have a plugin.yml, right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page