Problem with (Sender instanceof Player)

Discussion in 'Plugin Development' started by nicofig, Mar 3, 2014.

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

    nicofig

    I cant figure out how to solve this. I just get an "unreachable code" message under (Sender instanceof Player). Is there something wrong with the }'s?
    Code:java
    1. package fly;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.plugin.*;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15.  
    16.  
    17. public class Fly extends JavaPlugin implements Listener {
    18. public final static Logger logger = Logger.getLogger("Minecraft");
    19. public Plugin Fly;
    20.  
    21. public void LoadConfiguration(){
    22. Fly.getConfig().options().copyDefaults()
    23. }
    24. @Override
    25. public void onDisable() {PluginDescriptionFile pdfFile = this.getDescription();
    26. logger.info(ChatColor.GOLD + pdfFile.getName() + " Version: " + pdfFile.getVersion() + " Disabled!");
    27. this.saveConfig();
    28. }
    29. @Override
    30. public void onEnable() {PluginDescriptionFile pdfFile = this.getDescription();
    31. logger.info(ChatColor.GOLD + pdfFile.getName() + " Version: " + pdfFile.getVersion() + " Enabled!");
    32. }
    33.  
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. Player p = (Player)sender;
    36. if (!(sender instanceof Player));
    37. sender.sendMessage(ChatColor.GREEN + "You must be a player to use this command!");
    38. return true;
    39.  
    40. if (sender instanceof Player)
    41. if (cmd.getName().equalsIgnoreCase("fly"));
    42. if (args.length == 0)
    43. if (p.isOp());
    44. if (!p.getAllowFlight());{
    45. p.setAllowFlight(true);
    46. p.sendMessage(ChatColor.GOLD + "Flight Enabled.");
    47. return true;
    48. }
    49. if (p.getAllowFlight());{
    50. p.setAllowFlight(false);
    51. p.sendMessage(ChatColor.GOLD + "Flight Disabled.");
    52. return true;
    53. }
    54.  
    55. if (args.length == 1);
    56. Player target = Bukkit.getPlayer(args[0]);
    57. if (p.isOp());
    58. if (target.isOnline());
    59. if (!target.getAllowFlight()){
    60. target.setAllowFlight(true);
    61. return true;
    62. }
    63. if (target.getAllowFlight());{
    64. target.setAllowFlight(false);
    65. return true;
    66. }
    67. }
    68. }
    69.  
     
  2. Offline

    HungerCraftNL

    Have a moment, I'll clean the code up.
    PHP:
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.event.Listener;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.*;
    import org.bukkit.entity.Player;
     
     
    public class 
    Fly extends JavaPlugin implements Listener {
        public final static 
    Logger logger Logger.getLogger("Minecraft");
        public 
    Plugin Fly;
     
        public 
    void LoadConfiguration(){
            
    Fly.getConfig().options().copyDefaults();
        }
        @
    Override
        
    public void onDisable() {PluginDescriptionFile pdfFile this.getDescription();
        
    logger.info(ChatColor.GOLD pdfFile.getName() + " Version: " pdfFile.getVersion() + " Disabled!");
        
    this.saveConfig();
        }
        @
    Override
        
    public void onEnable() {PluginDescriptionFile pdfFile this.getDescription();
        
    logger.info(ChatColor.GOLD pdfFile.getName() + " Version: " pdfFile.getVersion() + " Enabled!");
        }
     
        public 
    boolean onCommand(CommandSender senderCommand cmdString labelString[] args){
            
    Player p = (Player)sender;
     
            if (
    sender instanceof Player){
                if (
    cmd.getName().equalsIgnoreCase("fly")){
                    if (
    args.length == 0){
                        if (
    p.isOp()){
                            if (!
    p.getAllowFlight()){
                                
    p.setAllowFlight(true);
                                
    p.sendMessage(ChatColor.GOLD "Flight Enabled.");
                            }else{
                                
    p.setAllowFlight(false);
                                
    p.sendMessage(ChatColor.GOLD "Flight Disabled.");
                            }
                        }
                    }
                    if (
    args.length == 1);
                    
    Player target =  Bukkit.getPlayer(args[0]);
                    if (
    p.isOp()){
                        if (
    target.isOnline()){
                            if (!
    target.getAllowFlight()){
                                
    target.setAllowFlight(true);
                            }
                        }
                    }
                    if (
    target.getAllowFlight()){
                        
    target.setAllowFlight(false);
                    }
                }
            }
            return 
    false;
        }
    }
     
    nicofig likes this.
  3. Offline

    nicofig

    HungerCraftNL Wow thanks a lot! Could you tell me what I did wrong?
     
  4. Offline

    HungerCraftNL

    nicofig Some arguments wrong and {} misplaced.
     
    nicofig likes this.
  5. Offline

    Wolfey

    You were doing this.
    Code:java
    1. if(!(sender instanceof Player));

    The ';' was suppose to be a curly bracket.
     
    nicofig likes this.
  6. Offline

    HungerCraftNL

    In the rush did I make a mistake, here a better version with noperms message added:
    PHP:
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.event.Listener;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.*;
    import org.bukkit.entity.Player;
     
     
    public class 
    Fly extends JavaPlugin implements Listener {
        public final static 
    Logger logger Logger.getLogger("Minecraft");
        public 
    Plugin Fly;
     
        public 
    void LoadConfiguration(){
            
    Fly.getConfig().options().copyDefaults();
        }
        @
    Override
        
    public void onDisable() {
            
    PluginDescriptionFile pdfFile this.getDescription();
            
    logger.info(ChatColor.GOLD pdfFile.getName() + " Version: " pdfFile.getVersion() + " Disabled!");
            
    this.saveConfig();
        }
        @
    Override
        
    public void onEnable() {
            
    PluginDescriptionFile pdfFile this.getDescription();
            
    logger.info(ChatColor.GOLD pdfFile.getName() + " Version: " pdfFile.getVersion() + " Enabled!");
        }
     
        public 
    boolean onCommand(CommandSender senderCommand cmdString labelString[] args){
            
    Player p = (Player)sender;
            if (
    sender instanceof Player){
                if (
    cmd.getName().equalsIgnoreCase("fly")){
                    if (
    args.length == 0){
                        if (
    p.isOp()){//Check perms
                            
    if (!p.getAllowFlight()){
                                
    p.setAllowFlight(true);
                                
    p.sendMessage(ChatColor.GOLD "Flight Enabled.");
                            }else{
                                
    p.setAllowFlight(false);
                                
    p.sendMessage(ChatColor.GOLD "Flight Disabled.");
                            }
                        } else {
                            
    p.sendMessage(ChatColor.RED "You're not allowed to do this!");
                        }
                    }
                    if (
    args.length == 1);
                    
    Player target =  Bukkit.getPlayer(args[0]);
                    if (
    p.isOp()){//Check perms
                        
    if (target.isOnline()){
                            if (!
    target.getAllowFlight()){
                                
    target.setAllowFlight(true);
                            }
                        }
                        if (
    target.getAllowFlight()){
                            
    target.setAllowFlight(false);
                        }
                    } else {
                        
    p.sendMessage(ChatColor.RED "You're not allowed to do this!");
                    }
                }
            }
            return 
    false;
        }
    }
    Good luck ;)
     
    nicofig likes this.
  7. Offline

    nicofig

    Wolfey HungerCraftNL So im trying to understand this, each curly bracket needs another one after the whole thing but can you put one in between two if statements to prevent the second one from running. Like this:
    Code:
    if (player.getallowflight();
      player.setallowflight(false);
    if (!player.getallowflight);
      player.setallowflight(true);
    Because I wouldnt want the second one to run, as it would undo what the previous line had just done.
     
  8. Offline

    HungerCraftNL

    nicofig there are different ways to do this, you can do it like this (because it's a boolean (true/false))
    PHP:
    if(player.getAllowFlight()){
      
    player.setAllowFlight(true);
    } else {
      
    player.setAllowFlight(false);
    }
    The most gets are with the { and the sets ;. I can't really explain that :/
     
    nicofig likes this.
  9. Offline

    nicofig

    HungerCraftNL Thanks a ton for your help! Sorry if this stuff seems trivial but I gotta start somewhere.
     
  10. Offline

    HungerCraftNL

    nicofig likes this.
  11. Offline

    nicofig

    HungerCraftNL If you could help me with this one more thing that would be fantastic.
    PHP:
    package test1;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class 
    Test1 extends JavaPlugin {
       
        public 
    boolean onCommand(CommandSender senderCommand cmdString labelString[] args){
            if (
    cmd.getName().equalsIgnoreCase("cmd1")){
                
    sender.sendMessage("Cmd1");
            }
            if (
    cmd.getName().equalsIgnoreCase("cmd2")){
                
    sender.sendMessage("Cmd2");
            }else{
                
    sender.sendMessage("Check /help again!");
            }
            return 
    true;
        }
     
    }
    Is there a way to have the else work for both if statements? With this code if I type /cmd1, because it is not equal to cmd2, i get the error message.
     
  12. Offline

    HungerCraftNL

    Remove the else part, it don't belongs there, you need another code for changing the 'help' message.
     
Thread Status:
Not open for further replies.

Share This Page