Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

OnInteract

Comments in 'Plugin Development' started by Marcelo234, Jan 29, 2016.

  1. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Hey Guys....This I want to code:
    If a player tap with a Stone on other Players the other Player should get a message....And the player how tapped get a Message too
    PHP:
    public function onPlayerInteract(PlayerInteractEvent $event) {
    if(
    $event->getItem()->getId() == 1){
    //So how to do this?
    }
    }
    Thanks Marcelo234
  2. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    use EntityDamageEvent with EntityDamageByEntityEvent
    AndrewBit and PEMapModder like this.
  3. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    @PocketKiller No need for that!
    PHP:
    public function onPlayerInteract(PlayerInteractEvent $event) {
    if(
    $event->getBlock()->getID() == 1){ //Be sure to use getBLOCK instead of getItem
    $PLAYER $event->getPlayer(); //Now you have your player object Have fun with it!
    }
    }
  4. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Read carefully
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Check if event cause is EntityDamageEvent::CAUSE_ENTITY_ATTACK
  6. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    1..As you know I can't make a public function in a public function...
    2..The players should get two different Messages...
    3..EntityDamageEvent::CAUSE_ENTITY_ATTACK->It should be only works if a player was tapped with a stone...I mean onInteractEvent.
  7. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    PlayerInteractEvent is called when a player touches a block. not hit other players.
  8. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    So what should i write now?
  9. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    PHP:
    public function onAttack(EntityDamageEvent $e)
    {
    // $player_inventory_instance->getItemInHand()
    }
    Last edited: Jan 30, 2016
  10. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    And what should I write for this?
    $player1 =??
    $player2 =??
  11. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Nope.
    PHP:
    public function onAttack(\pocketmine\event\entity\EntityDamageEvent $e)
    {
        if(
    $e instanceof \pocketmine\event\entity\EntityDamageByEntityEvent){
                 if(
    $event->getEntity() instanceof Player && $event->getDamager() instanceof Player){
                 if(
    $e->getDamager()->getInventory()->getItemInHand()->getId() == 1){
                         
    $e->getEntity()->sendMessage("You have been spotted with a stone");
                         
    $e->getDamager()->sendMessage("You spot a player with stone!");
                      }
                }
          }
    }
    Last edited: Jan 31, 2016
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The damager may not be a player. Make sure to check that.
  13. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    1. I'm giving he a base, not the full code. We're here to solve (or help with) people questions, not to fix their problems.
    2. Why "Nope"? What if he want check it by item and not item id?

    I can't understand why you're trying to correct me everytime.
    Last edited: Jan 30, 2016
  14. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I suggest you relearn PHP before start with an API, you'll need know at least basic syntax, functions etc...
    You can try with php.net. It has multilanguage support, if english isn't your main language, maybe you'll find your language inside 'languages' var.
  15. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    what you did is.. on EntityDamageEvent (can be caused by fall, fire damage, lava etc) you did not check if that event is instance of EntityDamageByEntityEvent.
  16. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    You think that I didn't know that I need check if it's a instance of EntityDamageByEntityEvent? He said
    And I answer what he should wrote first.
  17. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Your post isnt also correct, your code could cause a crash, because Call to unknown member function Pig::sendMessage(). You see the problem?
    AndrewBit likes this.
  18. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I am pretty sure he knows PHP, he needs to know the API well.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    AndrewBit likes this.

Share This Page

Advertisement