So I know whats wrong just have no clue how to fix it.

Discussion in 'Plugin Development' started by Fiddy_percent, Jul 24, 2012.

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

    Fiddy_percent

    Code:
    package me.fiddy.jack;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Jack extends JavaPlugin {
     
    public void onEnable(){
    getLogger().info("Jack is enabled");
    }
     
    public void onDisable(){
    getLogger().info("Jack is disabled");
    }
     
    // not sure what to do with this below here where the heck do i put it and why wont it register player?
    String playerName = player.getName();
    public int f;
    f = player.getFoodLevel();
     
     
     
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if(cmd.getName().equalsIgnoreCase("food"))
    sender.sendMessage("your hunger is" + f);
    return true;
     
    }
    }
     
    
     
  2. Offline

    ZeusAllMighty11

    SO uh, what is wrong?
     
  3. Offline

    bajsko

    You haven't even regristrated your player, it should look like this:
    Code:
    package me.fiddy.jack;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Jack extends JavaPlugin {
     
    public void onEnable(){
    getLogger().info("Jack is enabled");
    }
     
    public void onDisable(){
    getLogger().info("Jack is disabled");
    }
     
     
     
     
     
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if(!(sender instanceof Player)) {
    return true;
    }
    Player player = (Player)sender;
    why wont it register player?
    public int f;
    f = player.getFoodLevel();
    if(cmd.getName().equalsIgnoreCase("food"))
    sender.sendMessage("your hunger is" + f);
    return true;
     
    }
     
  4. Offline

    Lolmewn

    If you can't see what's wrong with that code, please, learn java first.

    Fiddy_percent What you are doing is absolutely wrong. You put random code outside a code block, randomly in a class. What you're supposed to do is:
    when onCommand is issued, check if the sender is a player (by doing if(sender instanceof Player){})
    If it is, Cast the sender object to Player, like this: Player p = (Player) sender;
    Then you can do sender.sendMessage("Your hunger is " + p.getFoodLevel());

    bajsko You are instantly casting the sender object to player, which is wrong. What if the console issues the command? Big error ;)

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

    bajsko

    if(!(sender instanceof Player)) {
    return true;
    }
    ;)
     
  6. Offline

    ZeusAllMighty11

    As a matter of fact, I do know java. I've put out 4 plugins, 1 recently. A few hundred downloads, which isn't bad. No need to call me out. I shouldn't have to dig through someone's code, for an error which he has not specified. So before you call people out, realize that not every can take the time out of their day to sit down and read line-by line. Others can, but some can't.


    @OP:

    If you don't intend on the console being a command sender, why not ask the player if he is an instanceof Player ?
     
  7. Offline

    Fiddy_percent

    Oh ok got it thanks for the help (>_> noob to java is noob)
     
  8. Offline

    Lolmewn

    Hey, that wasn't there before!

    Who, no need to start an argument! It's just a bit useless to post "so what's wrong" while it is so obvious.

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

    ZeusAllMighty11

    You snapped at me, it was only fair for me to tell you that I do infact know java.
    As for the 'so whats wrong'...I posted that because it's quite ironic how he said he knew what was wrong, yet, neglected to post it.
     
  10. Offline

    Fiddy_percent

    I did post it in the code which is why I put it out of the {}
    // not sure what to do with this below here where the heck do i put it and why wont it register player?
     
  11. Offline

    bajsko

    :p
     
Thread Status:
Not open for further replies.

Share This Page