[SOLVED]Teleportation Error

Discussion in 'Plugin Development' started by catchaser9620, Jul 10, 2012.

  1. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    if(commandLabel.equalsIgnoreCase("tp")) {
                if(player.hasPermission("BC.tp.*") == true || player.hasPermission("BC.tp.p2p") == true) {
                    if(args.length == 0) {
                        eC.sendMultiColouredMessage(player, "You must select a player");
                        eC.sendColouredMessage(player, "Usage: /tp <player>");
                    }else if(args.length == 1) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        if(targetPlayer.isOnline() == true) {
                        Location loc = targetPlayer.getLocation();
                        player.teleport(loc);
                        player.sendMessage(ChatColor.GOLD + "You teleported to: " + args[0]);
                        }else if(targetPlayer.isOnline() == false){
                            eC.sendColouredMessage(player, "Player is not online!");
                        }
                    }else if(args.length >= 2) {
                        player.sendMessage(ChatColor.WHITE + PREFIX + ChatColor.BLUE + " Not yet supported!");
                    }
                }else if(player.hasPermission("BC.tp.*") == false || player.hasPermission("BC.tp.p2p") == true) {
                    player.sendMessage(PERM);
                }
            }
    code for the command

    Code:
    09:30:52 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tp' in plugin BaseCommands v1.01
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at com.github.catchaser.BaseCommandsCommandExecutor.onCommand(BaseCommandsCommandExecutor.java:69)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
    
    The error


    I have tried changing the targetPlayer.isOnline() thing like 5 times and still cant figure it out any ideas?
  2. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  3. Offline

    andf54

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    null somewhere at BaseCommandsCommandExecutor.java:69

    What is at line 69?
  4. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    if(targetPlayer.isOnline() == true) {
  5. Offline

    mbonachea

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Try
    Code:
    else if(!targetPlayer.isOnline()){
    //Message
    }
    
  6. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    nope thats when it gives me the error
  7. Offline

    andf54

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    targetPlayer is probably null

    player.getServer().getPlayer(args[0])

    You need to check if the player with a name args[0] is online.
  8. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Change this:
    Code:
                  }else if(args.length == 1) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        if(targetPlayer.isOnline() == true) {
                        Location loc = targetPlayer.getLocation();
                        player.teleport(loc);
                        player.sendMessage(ChatColor.GOLD + "You teleported to: " + args[0]);
                        }else if(targetPlayer.isOnline() == false){
                            eC.sendColouredMessage(player, "Player is not online!");
                        }
    to this:
    Code:
                  }else if(args.length == 1) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        Location loc = targetPlayer.getLocation();
                        player.teleport(loc);
                        player.sendMessage(ChatColor.GOLD + "You teleported to: " + "" + targetplayer.getName());
                        return true;
                        if(!targetPlayer.isOnline()){
                            eC.sendColouredMessage(player, "Player is not online!");
                        return false;
                        }
    Lacked the return statments(return true; and return false; )of the boolean;
    for check if a player is offline use the ! before targetplayer.isOnline();
    for send the targetplayer's name in the chat you must write "" before the + targetplayer, and get the targetplayer's name with targetplayer.getName().

    This post has been edited 2 times. It was last edited by mcgamer99 Jul 10, 2012.
  9. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    The Player player is the (Player) sender? If not, type this:
    Code:
    Player player = (Player) sender;
    And for check if player has permission use:
    Code:
    if(player.hasPermissions("permission.node.here")) {
    Don't use == true or == false, for check if is false, use this code(the ! before player.hasPermissions()):
    Code:
    if(!player.hasPermissions("permission.node.here")) {
  10. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)

    if(!targetPlayer.isOnline()){
    eC.sendColouredMessage(player, "Player is not online!");
    return false;
    }

    I get unreachable code in eclipse with this part
  11. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    ok
  12. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    What is eC, a player?

    For send colored message you can do this:
    Code:
    player.sendMessage(ChatColor.GREEN + "Message");
    I use green at default, change it to the color you want.
  13. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  14. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If you want to send a coloured message to the player try this:
    Code:
    if(!targetPlayer.isOnline()){
    player.sendMessage("" + targetplayer.getName() + " is not online!");
    return false;
    }
  15. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    no eclipse is saying that that part of the code is unreachable

  16. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    But the part of code should be correct...
  17. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    i tried moving and got rid of that error but when i try the command it gives me an error
    Code:
    if(commandLabel.equalsIgnoreCase("tp")) {
                if(player.hasPermission("BC.tp.*")|| player.hasPermission("BC.tp.p2p")) {
                    if(args.length == 0) {
                        eC.sendMultiColouredMessage(player, "You must select a player");
                        eC.sendColouredMessage(player, "Usage: /tp <player>");
                    }else if(args.length == 1) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                          if(!targetPlayer.isOnline()){
                                eC.sendColouredMessage(player, "Player is not online!");
                            return true;
                            }
                        Location loc = targetPlayer.getLocation();
                        player.teleport(loc);
                        player.sendMessage(ChatColor.GOLD + "You teleported to: " + "" + targetPlayer.getName());
                        return true;
                    }else if(args.length >= 2) {
                        player.sendMessage(ChatColor.WHITE + PREFIX + ChatColor.BLUE + " Not yet supported!");
                    }
                }else if(!(player.hasPermission("BC.tp.*") || player.hasPermission("BC.tp.p2p"))) {
                    player.sendMessage(PERM);
                }
            }
    error:
    Line 69 is the if(!targetPlayer.isOnline()) thing
    Code:
    10:39:36 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tp' in plugin BaseCommands v1.01
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at com.github.catchaser.BaseCommandsCommandExecutor.onCommand(BaseCommandsCommandExecutor.java:69)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
    
  18. Offline

    catchaser9620

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    nvm fixed it:

    from:
    Code:
    if(commandLabel.equalsIgnoreCase("tp")) {
                if(player.hasPermission("BC.tp.*")|| player.hasPermission("BC.tp.p2p")) {
                    if(args.length == 0) {
                        eC.sendMultiColouredMessage(player, "You must select a player");
                        eC.sendColouredMessage(player, "Usage: /tp <player>");
                    }else if(args.length == 1) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                          if(!targetPlayer.isOnline()){
                                eC.sendColouredMessage(player, "Player is not online!");
                            return true;
                            }
                        Location loc = targetPlayer.getLocation();
                        player.teleport(loc);
                        player.sendMessage(ChatColor.GOLD + "You teleported to: " + "" + targetPlayer.getName());
                        return true;
                    }else if(args.length >= 2) {
                        player.sendMessage(ChatColor.WHITE + PREFIX + ChatColor.BLUE + " Not yet supported!");
                    }
                }else if(!(player.hasPermission("BC.tp.*") || player.hasPermission("BC.tp.p2p"))) {
                    player.sendMessage(PERM);
                }
            }
    to:
    Code:
    if(commandLabel.equalsIgnoreCase("tp")) {
                if(player.hasPermission("BC.tp.*")|| player.hasPermission("BC.tp.p2p")) {
                    if(args.length == 0) {
                        eC.sendMultiColouredMessage(player, "You must select a player");
                        eC.sendColouredMessage(player, "Usage: /tp <player>");
                    }else if(args.length == 1) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                          if(targetPlayer == null){
                                eC.sendMultiColouredMessage(player, "Player is not online!");
                            return true;
                            }else if(targetPlayer != null){
                        Location loc = targetPlayer.getLocation();
                        player.teleport(loc);
                        player.sendMessage(ChatColor.GOLD + "You teleported to: " + "" + targetPlayer.getName());
                        return true;
                            }
                    }else if(args.length >= 2) {
                        player.sendMessage(ChatColor.WHITE + PREFIX + ChatColor.BLUE + " Not yet supported!");
                    }
                }else if(!(player.hasPermission("BC.tp.*") || player.hasPermission("BC.tp.p2p"))) {
                    player.sendMessage(PERM);
                }
            }
    changed the if(!(targetPlayer.isOnline()) to if(targetPlayer == null)

    This post has been edited 1 time. It was last edited by catchaser9620 Jul 10, 2012.

Share This Page