Else statement not working?

Discussion in 'Plugin Development' started by tacos1223, Apr 23, 2014.

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

    tacos1223

    I want it so when a player types /flip (his friends name) it will randomly select heads or tails then send a message back to both players on whether the result was heads or tails. I got everything to work fine, but for some reason, whenever I try testing /flip (an offline player) it comes back with an internal error. What is wrong? Is my else statement not working? Please help!

    Code:
    package me.Tacos1223.RandomNumbers;
     
    import java.util.Random;
    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.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class RandomNumbers
    extends JavaPlugin
    implements Listener
    {
          public static RandomNumbers plugin;
          public final Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info("[" + pdffile.getName() + "]" + " v" + pdffile.getVersion() + " is now Enabled!");
          }
     
        public void onDisable() {
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info("[" + pdffile.getName() + "]" + " v" + pdffile.getVersion() + " is now Disabled!");
          }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
        Player player = (Player)sender;
        Player targetPlayer = player.getServer().getPlayer(args[0]);
            if(commandLabel.equalsIgnoreCase("flip"))
            {
                Random object = new Random();
                int tacos1223;
               
                for(int counter = 1; counter<=1; counter++)
                {
                    tacos1223 = 1+object.nextInt(2);
                   
                    if(args.length == 1 && tacos1223 == 1){
                        targetPlayer.sendMessage(ChatColor.AQUA + player.getName() + " has flipped heads!");
                        player.sendMessage(ChatColor.AQUA + "You have flipped heads!");
                    }
                        else if(args.length == 1 && tacos1223 == 2){
                        targetPlayer.sendMessage(ChatColor.AQUA + player.getName() + " has flipped tails!");
                        player.sendMessage(ChatColor.AQUA + "You have flipped tails!");
                    }
                    else
                    {
                    player.sendMessage(ChatColor.DARK_RED + "That player is currently offline!");
                        }
                }
            }
            return false;
        }
    }
     
  2. Offline

    Nateb1121

    Could you post the stack trace please? (While I look through your code)

    Update:
    I think I know your issue, you never check if the player is offline really. One of your if() or elseif() statements fire and they try to mess with the targetPlayer who isn't online, and then you get your error. You might want to try this:

    Code:java
    1. if(targetPlayer != null) { // Make sure the target isn't null! (Offline)
    2.  
    3. if(args.length == 1 && tacos1223 == 1){
    4. targetPlayer.sendMessage(ChatColor.AQUA + player.getName() + " has flipped heads!");
    5. player.sendMessage(ChatColor.AQUA + "You have flipped heads!");
    6. }
    7. else if(args.length == 1 && tacos1223 == 2){
    8. targetPlayer.sendMessage(ChatColor.AQUA + player.getName() + " has flipped tails!");
    9. player.sendMessage(ChatColor.AQUA + "You have flipped tails!");
    10. } else {
    11. //Not sure?
    12. }
    13.  
    14. } else {
    15. player.sendMessage(ChatColor.DARK_RED + "That player is currently offline!");
    16. }


    MooshViolet

    Err... What? The "int counter = 1;" is an assignment, not a boolean...

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

    Deleted user


    Okay so, a few things here bud.

    Code:
    Player player = (Player)sender;
    Player targetPlayer = player.getServer().getPlayer(args[0]);
    
    First, you need to make sure the command is actually being called by a player and not the console.
    second, before casting args[0] to a Player, check to make sure the player even exist.

    I don't have Eclipse open atm or I'd give you some sample code but I'll do my best in GChrome.

    Code:
    if ((!sender instanceof Player) || (getServer().getPlayer(args[0]) == null) {
    return;
    }
    
    Place that before creating your player and targetPlayer. Should fix everything up, please note this will restrict console use of the command /flip


    EDIT (before I post cause I'm gewd like that):
    Check for arguments at the very beginning (after checking to make sure your players are legit, so we can msg em) because this command will only work with 1 arg right? not 0, not 7, not 2. So...

    Code:
    if (args.length != 1) {
    player.sendMessage("Invalid syntax. (/flip [playerName]));
    return;
    }
    
    so, in conclusion, in case I've lost you here's where you should be at:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
     
    if(commandLabel.equalsIgnoreCase("flip")) {
    if (sender instanceof Player)  {
    if (args.length != 1) {
    player.sendMessage("Invalid syntax. (/flip [playerName]));
    return;
    }
    if (Bukkit.getServer().getPlayer(args[0]) == null) {
    player.sendMessage("Could not find player '"+args[0]+"'.");
    return;
    }
    } else {
    return;
    }
    }
        Player player = (Player)sender;
        Player targetPlayer = player.getServer().getPlayer(args[0]);
     
    
    and now that we've already checked to make sure there is only 1 arg and it is a valid player we can replace

    Code:
    if(args.length == 1 && tacos1223 == 1){
                        targetPlayer.sendMessage(ChatColor.AQUA + player.getName() + " has flipped heads!");
                        player.sendMessage(ChatColor.AQUA + "You have flipped heads!");
                    }
                        else if(args.length == 1 && tacos1223 == 2){
                        targetPlayer.sendMessage(ChatColor.AQUA + player.getName() + " has flipped tails!");
                        player.sendMessage(ChatColor.AQUA + "You have flipped tails!");
                    }
                    else
                    {
                    player.sendMessage(ChatColor.DARK_RED + "That player is currently offline!");
                        }
                }
    
    with

    Code:
    if (tacos1223 == 1) {
    targetPlayer.sendMessage(ChatColor.AQUA + player.getName().toString() + " has flipped heads!");
    player.sendMessage(ChatColor.AQUA + "You have flipped heads!");
    } else if(tacos1223 == 2) {
    targetPlayer.sendMessage(ChatColor.AQUA + player.getName() + " has flipped tails!");
    player.sendMessage(ChatColor.AQUA + "You have flipped tails!");
    }
    
    so you gotta code revamp, big whoop. wanna fight bout' it? (need any explaining on how any of this code works just ask)
     
    MooshViolet and tacos1223 like this.
  4. Offline

    tacos1223

    You my sir are a genius.
    Thank you!
     
Thread Status:
Not open for further replies.

Share This Page