Help!

Discussion in 'Spigot Plugin Development' started by ZevoGaems, Jun 7, 2017.

  1. I made a plugin that allows you to do /launch player and it launches the player by how far you specified. I don't want it to work on me but how would I cancel it? Here is what I have
    Code (Text):
            if(cmd.getName().equalsIgnoreCase("launch")) {
                if(!sender.hasPermission("minegrid.launch")) {
                    sender.sendMessage(ChatColor.RED + "Requested Permission Level \n ADMINISTRATOR");
                } else if(args.length != 2) {
                    sender.sendMessage(ChatColor.RED + "Usage, /launch (player) (power)");
                } else if(args.length == 2) {
                    String target1 = args[0];
                    Player target = Bukkit.getPlayer(target1);
                    String amount1 = args[1];
                    int amount = Integer.parseInt(amount1);
                    target.setVelocity(target.getLocation().getDirection().multiply(amount));
                    if(args[0].equals("Flauw")) {
                        sender.sendMessage(ChatColor.RED + "I am not stupid, I blocked it for myself :)");
                    }
                }
            }
     
  2. if target.equals(sender) {
    return;
    }


    Or set the args to lowercase and compares its lowercase name
    Before performing the action
     
  3. then check if the player is offline by using
    Code (Java):
    if(target == null) {
     // the target is offline
    }
     
  4. Code (Text):
    if(!cmd.getName().equalsIgnoreCase("launch")) {
        return false; // this check shouldn't be needed unless you are cramming more than 1 command into your main class, which is bad..
    }

    if(!sender.hasPermission("minegrid.launch")) {
        sender.sendMessage(ChatColor.RED + "Requested Permission Level \n ADMINISTRATOR");
        return true;
    }

    if(args.length != 2) {
        return false; // this shows player the usage from plugin.yml
    }

    Player target = Bukkit.getPlayer(args[0]);
    if (target == null) {
        sender.sendMessage(ChatColor.RED + "Player not found");
        return true;
    }

    int amount;
    try {
        amount = Integer.parseInt(amount1);
    } catch(NumberFormatException e) {
        sender.sendMessage(ChatColor.RED + "Not a valid number");
        return true;
    }

    if (target.hasPermission("no.launch")) {
        sender.sendMessage(ChatColor.RED + "You cannot launch that player");
        return true;
    }

    target.setVelocity(target.getLocation().getDirection().multiply(amount));
     
    Then just give yourself the no.launch permission. You can also give others this permission to protect them too without having to edit your plugin.
     
    • Informative Informative x 1
  5. "target.setVelocity(target.getLocation().getDirection().multiply(amount));"

    It's okay, but i think you should use the sender location's direction because you wanna throw that way whither you look.

    Player p = (Player) sender; //(you need a sender instanceof Player check before
    target.setVelocity(p.getLocation().getDirection().multiply(amount));