Solved Player Targets a Player

Discussion in 'Plugin Development' started by Brennian, Feb 20, 2013.

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

    Brennian

    Hello guys i'm trying to make an plugin,
    wich should do : when a player clicks with an item on another player the target player will get an effect

    EXAMPLE:
    Player clicks with paper on another player that player gets Regeneration II for 5 seconds

    The question is: is that possible and how?
     
  2. Offline

    Cjreek

    You have to use the PlayerInteractEntityEvent.
     
  3. Offline

    Brennian

    can you make a small code line?

    View attachment 12800
    View attachment 12801


    can anyone help me with the code?

    can anyone correct my code?

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

    RealDope

    What's wrong with it? The potion affect isn't applied? Add debug messages throughout the code to see how far it gets.
     
  5. Offline

    Brennian

    okay i found it i can't add enchantements to a player
    "The method addPotionEffect(PotionEffect) is undefined for the type Entity"
     
  6. Offline

    lycano

    Brennian first of all: Please dont compare blockIDs always use getType().equals(Material.NAME) if the blockId changes you would have a problem but with equals you don't as it compares the actual type not only the id.

    As for the code: I assume you want to make sure that the target is a player if not do nothing. You can use getType on entity class for that.

    When this check has passed you would then check for equality with getType()

    So the code could be something like that.

    Code:
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
        Player player = event.getPlayer();
        Entity target = event.getRightClicked();
    
        if (!(target.getType().equals(EntityType.PLAYER)))
            return;
    
        if (player.getItemInHand().getType().equals(Material.PAPER)) {
            ((Player) target).addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 2, 100));
        }
    }
    
    If you want you could make more use of the EntityType check. Maybe sending a message to the player when he wants to heal an animal if you want.

    slightly altered code
    Code:
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
        Player player = event.getPlayer();
        Entity target = event.getRightClicked();
    
        if (!(target.getType().equals(EntityType.PLAYER))) {
            player.sendMessage("You can only heal other Players.");
            return;
        }
    
        if (player.getItemInHand().getType().equals(Material.PAPER)) {
            ((Player) target).addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 2, 100));
        }
    }
    
    Edit: The old post actually healed the player himself instead of the target so we have to use Class Casting. (Hope that works, untested)
     
  7. Offline

    Brennian

    hello thanks for helping,
    it makes my code a lot better but i want to do the potion effect on the clicked player (in your code it gives it to the player who clicked)
    if i change it to target.addPotionEffect ....
    it still says : "The method addPotionEffect(PotionEffect) is undefined for the type Entity"
     
  8. Offline

    ZachBora

    Brennian
    lycano's code looks fine, it gives effect to the target
     
  9. Offline

    Brennian

    no it gives it to the player but it's solved !
     
  10. Offline

    ZachBora

    He edited his post since he wrote it. It does now.
     
    Brennian likes this.
Thread Status:
Not open for further replies.

Share This Page