Solved Why doesn't this Work!!!!

Discussion in 'Plugin Development' started by CheifKeef, Jul 5, 2014.

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

    CheifKeef

  2. Offline

    teej107

    First off, you aren't safely casting sender to a player. Second, use cmd.getName() for the command name instead.
     
  3. Offline

    Kassestral

    I am sure its meant to be

    Code:java
    1. if(cmd.getname().equalsIgnoreCase("apply"){
    2. //DO SOMETHING
    3. }
     
  4. Offline

    CheifKeef

    Tried it right now didn't work. Kassestral

    teej107 How do i safely cast sender to player ?

    Code:
    package me.PokeCoding.instamsg;
     
    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.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class InstaMsg extends JavaPlugin {
        public static Logger logger = Logger.getLogger("Minecraft");
        public static InstaMsg plugin;
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            InstaMsg.logger.info(pdfFile.getName() + "has been enabled!");
        }
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            InstaMsg.logger.info(pdfFile.getName() + "has been disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("Apply")){
                player.sendMessage(ChatColor.RED + "This will be added soon!");
            }
            return false;
        }
     
    }
    Kassestral

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    Kassestral

    CheifKeef Why is your plugin.yml in your Src Folder? thats probably the reason......
     
  6. Offline

    teej107

    CheifKeef Did you directly copy paste his code? Because Kassestral didn't even use a method that exists. It's
    not .getname(). Yes method names are case sensitive.

    EDIT:
    Didn't see that. Idk if that makes a difference but move it into your project folder and try it again.
     
  7. Offline

    CheifKeef

    Kassestral Thats the only way it works for me if i put it anywhere else it doesn't run. It works and loads up fine just when i execute the command it doesn't send me the msg.

    teej107 As you can see in the code above i posted i put getName().

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. Offline

    Kassestral

  9. Offline

    CheifKeef

  10. Offline

    teej107

    CheifKeef Add in debug code. And now that I think about it, I don't think it matters if your plugin.yml is in your src folder. If it did, his plugin wouldn't load.
     
  11. Offline

    CheifKeef

  12. Offline

    teej107

    CheifKeef Code in certain parts of your code to make sure it is being executed. Examples would be
    Code:java
    1. System.out.println(messageHere);
    2. //Or since this is Bukkit, even using something like this.
    3. player.sendMessage(messageHere);
    4. //Or
    5. Bukkit.broadcastMessage(messageHere);
     
  13. Offline

    CheifKeef

    teej107 If i use any of those except player.sendMessage It underlines Player player = (Player) sender; in yellow
     
  14. Offline

    teej107

    CheifKeef What is your IDE telling you? Why is it yellow? I can tell you one thing:
    just check to see if sender is an instance of Player before casting.
    Code:java
    1. if(variable instanceof Class)

    With your current code, if the console ran that command, your plugin would throw an error.
     
  15. Offline

    CheifKeef

    teej107 I did apply in console and it said Unknow command.
     
  16. Offline

    teej107

    CheifKeef What do you mean?
    Are you running the command in the console? Because you are sending the message to a player.
     
  17. Offline

    Kassestral

    CheifKeef You forgot to register the command lol....

    Code:
    name: InstaMsg
    main: me.PokeCoding.instamsg.InstaMsg
    version: 1.0
    commands:
      apply:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    3. Player player = (Player) sender;
    4. if(cmd.getName().equalsIgnoreCase("apply")){
    5. player.sendMessage(ChatColor.RED + "This will be added soon!");
    6. }
    7. return true;
     
  18. Offline

    JBoss925

    Did you register the command in your plugin.yml?
     
  19. Offline

    Kassestral

    JBoss925
    I just told him that lol... and I posted the fix
     
  20. Offline

    CheifKeef

  21. Offline

    Kassestral

  22. Offline

    teej107

    CheifKeef These people Kassestral JBoss925 are probably right Ninja'D. But I do see some bad coding practices in your class that I could help you with too. First being, I don't think you understand static and you are using it in a poor manner. Second you don't need to broadcast to the console that your plugin is loaded. It automatically does that for you. If you follow the advice above and the advice that I suggested earlier, you shouldn't have any problems.
     
  23. Offline

    Kassestral

    teej107
    He probably saw a tutorial from a while ago and followed that :3
     
    CheifKeef likes this.
  24. Offline

    CheifKeef

    teej107 ok thanks for the help, And again I'm really new to coding and i usually follow Bukkit coding tutorials on youtube and they say to do Public static but ill try your methods
     
  25. Offline

    zack6849

    First of all, sender doesn't need to be a player to send a message, second of all, half of you guys are suggesting just casting sender to player, WITHOUT EVEN CHECKING IF IT IS ONE, you know what's going to happen if someone tries to use that command as console? a nice big stack trace spammed in console. There's literally no need to cast sender to a player simply to send them a message.
    Plugin.yml
    Show Spoiler

    Code:
    main: me.PokeCoding.instamsg.InstaMsg
    name: InstaMsg
    version: 1.0
    commands:
      apply:
        description: Example command
        usage: /apply
    

    code
    Show Spoiler

    Code:
    package me.PokeCoding.instamsg;
     
    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.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class InstaMsg extends JavaPlugin {
        public static Logger logger = Logger.getLogger("Minecraft");
        public static InstaMsg plugin;
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            InstaMsg.logger.info(pdfFile.getName() + "has been enabled!");
            //i assume you want to actually set plugin to something.
            this.plugin = this;
      }
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            InstaMsg.logger.info(pdfFile.getName() + "has been disabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            /*
            * If you want to check if a user is a player before casting, use
            * "if(sender instanceof Player){ //do stuff. }"
            */
            //Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("Apply")){
                sender.sendMessage(ChatColor.RED + "This will be added soon!");
                //your command executed successfully, return true.
                return true;
            }
            return false;
        }
     
    }
    
     
  26. Offline

    Necrodoom

    zack6849 expect a copy-paste without understanding of with that spoonfeed, though.
     
  27. Offline

    Kassestral

    Happy now?
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    3. if(cmd.getName().equalsIgnoreCase("apply") && sender instanceof Player){
    4. Player player = (Player) sender;
    5. player.sendMessage(ChatColor.RED + "This will be added soon!");
    6. }
    7. return true;
    8. }

    :D
     
  28. Offline

    teej107

    zack6849 Whether this guy wants console support with his command is up to him.
    I don't see anybody saying that on here.
     
  29. Offline

    zack6849

    Kassestral didn't typecheck before casting in two of his posts
    Necrodoom I'd rather see a correct copypaste than someone with a horrible mess of code without understanding it
     
  30. Offline

    CheifKeef

    Can you guys stop replying to this thread The problem was solved no need for further Chatting k thx
     
    zack6849 likes this.
Thread Status:
Not open for further replies.

Share This Page