[solved]Help with Teleport

Discussion in 'Plugin Development' started by herghost, Dec 19, 2011.

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

    herghost

    Hi all

    I am having some problems with teleporting. I have it working if you /tp victim target, however I am aiming at /tp target

    The error I get when you use this is:

    2011-12-20 00:09:37 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tp' in plugin Fiery v0.1
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1

    My code
    Code:
    package me.herghost.Fiery.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    
    public class tpCommand implements CommandExecutor {
    
    	@Override
    	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    	{
    		if(cmd.getName().equalsIgnoreCase("tp")&& sender instanceof Player)
    			{
    			 Player player = (Player) sender;
    			 Player victim = Bukkit.getPlayer(args[0]);
    			 Player target = Bukkit.getPlayer(args[1]);
    			 if(args.length == 2)
    				{
    				 if (victim == null)
    					{
    						sender.sendMessage("No user " + args[0] + ".");
    					}
    					else if (target == null)
    					{
    					sender.sendMessage("No user " + args[1] + ".");
    					}
    					else
    					{
    					  Command.broadcastCommandMessage(sender, "Teleporting " + victim.getName() + " to " + target.getName());
    					  victim.teleport(target);
    					}
    					return true;
    				}
    			 if(args.length == 1)
    			 {
    				 	if (victim == null)
    					{
    					sender.sendMessage("No user " + args[0] + ".");
    					}
    					else
    					{
    					  Command.broadcastCommandMessage(sender, "Teleporting to " + victim.getName());
    					  player.teleport(victim);
    					}
    					return true;
    			 }
    			}
    		return false;
    
    }
    }
    
    What do I have wrong?

    Thanks

    Testing on my local server I have managed to get the full error:

    Code:
    2011-12-19 23:58:08 [INFO] Starting minecraft server version 1.0.1
    2011-12-19 23:58:08 [INFO] Loading properties
    2011-12-19 23:58:08 [INFO] Starting Minecraft server on *:25565
    2011-12-19 23:58:08 [WARNING] **** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!
    2011-12-19 23:58:08 [WARNING] The server will make no attempt to authenticate usernames. Beware.
    2011-12-19 23:58:08 [WARNING] While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.
    2011-12-19 23:58:08 [WARNING] To change this, set "online-mode" to "true" in the server.properties file.
    2011-12-19 23:58:08 [INFO] This server is running Craftbukkit version git-Bukkit-1.8.1-R4-83-g24cb05a-b1556jnks (MC: 1.0.1) (Implementing API version 1.0.0-R1-SNAPSHOT)
    2011-12-19 23:58:08 [INFO] Preparing level "world"
    2011-12-19 23:58:08 [INFO] Default game type: 1
    2011-12-19 23:58:08 [INFO] Preparing start region for level 0 (Seed: -4058256270512872989)
    2011-12-19 23:58:09 [INFO] Preparing start region for level 1 (Seed: 862085803749579590)
    2011-12-19 23:58:09 [INFO] Preparing spawn area: 85%
    2011-12-19 23:58:09 [INFO] Preparing start region for level 2 (Seed: 862085803749579590)
    2011-12-19 23:58:10 [INFO] [Fiery] Fiery Plugin Enabled - Beta
    2011-12-19 23:58:10 [INFO] [Fiery] Fiery Database Tables OK!
    2011-12-19 23:58:10 [INFO] PermissionsBukkit v1.2 is now enabled
    2011-12-19 23:58:10 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2011-12-19 23:58:10 [INFO] Done (0.200s)! For help, type "help" or "?"
    2011-12-19 23:58:17 [INFO] /127.0.0.1:50694 lost connection
    2011-12-19 23:58:18 [INFO] herghost [/127.0.0.1:50696] logged in with entity id 205 at ([world] 0.90625, 64.0, 278.40625)
    2011-12-19 23:58:23 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tp' in plugin Fiery v0.1
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:165)
    	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:372)
    	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:756)
    	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:721)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:714)
    	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    	at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    	at me.herghost.Fiery.commands.tpCommand.onCommand(tpCommand.java:19)
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    	... 12 more
    
    I dont understand, line 19 is
    Code:
     Player target = Bukkit.getPlayer(args[1]); 
    - but I am not using this line?!

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

    Chiller

    Ya it is returning null...
     
    herghost likes this.
  3. Offline

    herghost

    Do'h

    Code:
    package me.herghost.Fiery.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    
    public class tpCommand implements CommandExecutor {
    
    	@Override
    	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    	{
    		if(cmd.getName().equalsIgnoreCase("tp")&& sender instanceof Player)
    			{
    			 if(args.length == 2)
    				{
    				 Player victim = Bukkit.getPlayer(args[0]);
    				 Player target = Bukkit.getPlayer(args[1]);
    				 if (victim == null)
    					{
    						sender.sendMessage("No user " + args[0] + ".");
    					}
    					else if (target == null)
    					{
    					sender.sendMessage("No user " + args[1] + ".");
    					}
    					else
    					{
    					  Command.broadcastCommandMessage(sender, "Teleporting " + victim.getName() + " to " + target.getName());
    					  victim.teleport(target);
    					}
    					return true;
    				}
    			 if(args.length == 1)
    			 {
    				 Player player = (Player) sender;		
    				 Player slap = Bukkit.getPlayerExact(args[0]);
    				 	if (slap == null)
    					{
    					sender.sendMessage("No user " + args[0] + ".");
    					}
    					else
    					{
    					  Command.broadcastCommandMessage(sender, "Teleporting to " + slap.getName());
    					  player.teleport(slap);
    					}
    					return true;
    			 }
    			}
    		return false;
    
    }
    }
    
    
     
  4. Offline

    Chiller

    Easy enough?!
     
  5. Offline

    herghost

    Ha ha, you want to see some of what I tried :p Thanks for pointing me in the right direction, think I was going in circles! My next brain teaser, how to see if an arg[] is an int, and if not display error message!
     
  6. Offline

    Chiller

    Umm... Try:
    Code:java
    1. try {
    2. int x = Integer.parseInt(args[0]);
    3. //Is an Integer.
    4. } catch (NumberFormatException nFE) {
    5. //Is not an Integer
    6. }
     
Thread Status:
Not open for further replies.

Share This Page