[CODING NOOB NEEDS HELP] sender.(event) target.(event) :How to make those?

Discussion in 'Plugin Development' started by samyounes03, May 28, 2015.

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

    samyounes03

    Hey, I am coding a plugin, and i would like to know how to make something (IDK what you call it XD) instead of typing player.(event) only, i would like somthing to happen to the target to so i can make some event happen to the target when i type in my code target.(EVENT) Help please :)
     
  2. Offline

    Agentleader1

    Can you be a little more specific?
     
  3. Offline

    samyounes03

    @Agentleader1 You know there is: target.(EVENT) to do an event to a target. There is also sender.(EVENT) in plugins people can code. How to I add those?
     
  4. Offline

    Hawktasard

    @samyounes03 What are you talking about?.. do you want to call an event?
     
  5. Offline

    ZackBlazes

    @Hawktasard @samyounes03 I think he means something like sender.sendMessage();
    Sender is default in the onCommand boolean if you set it up correctly. A target is usually the same thing as a player.
    Example:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(label.equalsIgnoreCase("heal")){
    Player p = (Player) sender;
    if(args[1] != null){
    Player target = Bukkit.getPlayer(args[1]);
    target.setHealth(20);
    }
    }
    return true;
    }
     
  6. Offline

    teej107

    Agentleader1 likes this.
  7. Offline

    samyounes03

    Last edited by a moderator: May 28, 2015
  8. I see 4-5 errors instantly...
    1. Use cmd.getName() instead of the alias (label)
    2. Don't blindly cast the sender to a Player
    3. Don't use args[1] != null, use args.length == x
    3.1 args[1] is the second word in the command (/command firstWord secondWord)
    4. The target player can be null or not online.
    5. .setHealth requires a double, not an int
     
    mine-care likes this.
  9. Offline

    ZackBlazes

    @FisheyLP I know. I didn't type that to be correct or to be used anywhere. I just showed a quick example.
     
  10. Offline

    teej107

    That's when you should use pseudo code.
     
  11. Offline

    ZackBlazes

    Ok let me correct it then.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(cmd.getName().equalsIgnoreCase("heal")){
    if (sender instanceof Player){
    Player player = (Player) sender;
    if(args.length > 0){
    Player target = Bukkit.getPlayer(args[0]);
    if(target.isOnline()){
    target.setHealth(target.getMaxHealth())
    }
    }
    }
    }
    return true;
    }
     
    Last edited: May 28, 2015
  12. Offline

    teej107

    That is not pseudo code and that still hasn't fixed many of the problems in the first "example"
     
    FisheyLP and mine-care like this.
  13. Offline

    mine-care

    @ZackBlazes first check then cast,
    Args > 0 ? U mean args >= 0
    Target player will turn to null if it not online

    Also i think the op means he wants to add methods in player interface (and further more implementstions) to call them like player.amethod() but in bukkit that is not possible without editingservers code, editing servers code is not suported here.
     
  14. Offline

    _Cookie_

    Pseudo Code: a notation resembling a simplified programming language, used in program design.
    Basically all Pseudo code is, is writing code into an example which isn't actually code, e.g.
    Check if Player equals null.
    Set integer x to 0

    :)
     
  15. Offline

    ZackBlazes

    @_Cookie_ I didn't mean that I was changing it to pseudo code.
     
Thread Status:
Not open for further replies.

Share This Page