Solved Cant Get My Plugin To Work can some one help

Discussion in 'Plugin Development' started by aidenkrz, Aug 19, 2013.

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

    aidenkrz

    I want my plugin to teleport a player to a random spot out of three spots say that the spots are 100x 60z 130y and 120x 65z 160y and 150x 75z 180y and i want it to teleport them to one of tho's spots randomley here is what i have for the code right now publicclass Tele extends JavaPlugin
    {
    public void onEnable()
    {
    }

    public void onDisable()
    {
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;
    if (commandLabel.equalsIgnoreCase("Tr")){
    if(args.length == 0)
    player = player.getServer().getPlayer(args[0]);
    Location targetPlayerLocation = player.getLocation();
    player.teleport(targetPlayerLocation);
    player.teleport(new Location(null, 100, 60, 100));
    player.teleport(new Location(null, 160, 60, 160));
    player.teleport(new Location(null, 120, 60, 120));

    }


    returnfalse;
    }
    }
     
  2. aidenkrz
    - Why do you have onEnable- and Disable() methods? They're empty, therefore just taking space.
    - args.length == 0 returns true if your command has no arguments, therefore your code will throw a NPE. Check if args.length == 1 instead.
    - World can't be null - another NPE.

    Answer to your question: use the Random class, and make it choose from 3 integers.
    Code:
    Random rand = new Random();
    int randInt = rand.nextInt(2);
    
    switch(randInt) {
    case 0: // teleport to first out of three locations
    break;
    
    case 1: // teleport to second out of three locations
    break;
    
    case 2: // teleport to third out of three locations
    break;
    }
     
  3. Offline

    Chinwe


    Well...

    You need to check if sender instanceof Player before casting him to Player.

    if (args.length == 0)...args[0]? args must be at least 1 long for args[0] to exist

    You then teleport that player (who might or might not be online -> NPE) to three null locations straight after each other (will seem like just a tp to last location to the player)

    Why is the world parameter in the Location constructor null? Change null for player.getWorld()

    Have you registered your command in plugin.yml too?

    EDIT: Semi-ninja'd by Assist (revenge eh :>)
     
    aidenkrz likes this.
  4. Offline

    aidenkrz

    Yes I have

    Once i type the command it says an internal error occurred while attempting to perform this command

    Chinwe I was woundering if you could corect my code

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

    Chinwe

    Correction + explanatory comments :>
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if (cmd.getName().equalsIgnoreCase("tr"))
            {
                // Check if sender is not a player
                if (!(sender instanceof Player))
                {
                    sender.sendMessage(ChatColor.RED + "You must be a player to use this!");
                    return true;
                }
             
                // Now we know he is a player, it is safe to cast
                Player p = (Player) sender;
                Player target;
             
                // Is a target defined?
                if (args.length == 1)
                {
                    target = Bukkit.getPlayerExact(args[0]);
     
                    // Check if target is online
                    if (target == null)
                    {
                        // Offline
                        p.sendMessage(ChatColor.RED + target.getName() + " is offline!");
                        return true;
                    }
                }
                // Teleporting himself
                else if (args.length == 0)
                    target = p;
     
                // Invalid arguments
                else
                    return false;
             
                // Now to choose the teleport location
                int selector = new Random().nextInt(3);
                Location loc;
                switch (selector)
                {
                case 0: loc = new Location(target.getWorld(), 100, 60, 100);
                case 1: loc = new Location(target.getWorld(), 160, 60, 160);
                case 2: loc = new Location(target.getWorld(), 120, 60, 120);
                }
             
                // And teleport
                target.teleport(loc);
             
                // Notify
                p.sendMessage(blah blah);
                target.sendMessage(blah blah);
                return true;
             
            }
         
            return false;
        }
     
  6. Offline

    aidenkrz

    Thanks So Much

    But i cant get it to teleport you to a random spot

    .bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page