1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

DataPacketReceiveEvent - fired twice

Discussion in 'Development' started by Error202, May 21, 2019.

  1. Error202

    Error202 Spider Jockey

    Messages:
    39
    I have two plugins:

    1: Used for right click on NPC
    PHP:
    public function onUseGuide(DataPacketReceiveEvent $event)
        {
            
    $player $event->getPlayer();

            
    $pk $event->getPacket();
            if (
    $pk instanceof InventoryTransactionPacket) {
                if (
    $pk->transactionType === InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY) {
                    if (
    $pk->trData->actionType === InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT) {
    ...
    2: Used for right click on AIR and COMPASS in hand.. Go Home
    PHP:
    public function onUseHubInventory(DataPacketReceiveEvent $event)
        {
            
    $player $event->getPlayer();

            
    $pk $event->getPacket();
            if (
    $pk instanceof InventoryTransactionPacket) {
                if (
    $pk->transactionType === InventoryTransactionPacket::TYPE_USE_ITEM) {
                    if (
    $pk->trData->actionType === InventoryTransactionPacket::USE_ITEM_ACTION_CLICK_AIR) { ...
    Problem:
    I click by NPC and teleport to other world. And there fired #2 (player view - AIR). Player teleport to home...
    How I can prevent twice DataPacketReceiveEvent?
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Because you registered 2 listeners for the same event thats why both will fire, combine them together in 1 function
     
  3. Error202

    Error202 Spider Jockey

    Messages:
    39
    I can't, these are different plugins..
    I can edit only second plugin
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Cancel the event.
    If the second event doesn't ignore cancelled events, it is a bug.
    By the way, cancelled events are planned to be ignored by default as of 4.0.0
     
  5. Error202

    Error202 Spider Jockey

    Messages:
    39
    I set $e->setCancelled(true) in plugin #1, but not effect :(
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Then the second plugin needs to be fixed.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.