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

Solved Interact to Entity Id

Discussion in 'Development' started by ItzAribie, Oct 31, 2017.

  1. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    Hello, I'm new I would like to know that the packet InteractPacket had the action of clicking on an Entity Id this is not the car case when it was named from the id he received the order and I do not want not. I want to run it by clicking. If someone can help me.
    PHP:
        public function onPacketReceived(DataPacketReceiveEvent $event)
        {
            
    $pk $event->getPacket();

            if (
    $pk instanceof InteractPacket) {
                if (isset(
    $this->entity[$pk->target])) {
                    
    $entity $this->entity[$pk->target];

                    if (!isset(
    $this->command[$event->getPlayer()->getName()])) {
                        
    $entity->executeCommand($event->getPlayer());
                        return 
    true;
                    }
                }
            }
        }
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    use PlayerInteractEvent / UseItemEvent?
     
  3. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    Why UseItemEvent ?
     
  4. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    oops, that event doesn't exists. I never said such a thing :p (hides)
     
    ItzAribie likes this.
  5. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    Lol are you sure i should use PlayerInteractPacket ?
     
  6. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    NpcRequestPacket maybe ?
     
  7. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Why why why would you use packets directly when there's an entity damage event
     
    Last edited: Oct 31, 2017
    kazuya and WinterBuild7074 like this.
  8. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Also note that for whatever reason you're using DataPacketReceiveEvent, tapping an entity is no longer the Interact packet but an InventoryTransactionPacket.
     
  9. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    I do not understand why her changed and her then becomes InventoryTransactionPacket
     
  10. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Because you're not supposed to use packets directly where possible. Packets and the protocol are bound to change every update, and you can blame the developers at Mojang for that. When API methods are provided you're encouraged to use them. This is one of the reasons you should never use packets when there's API for it.
     
    Jack Noordhuis and ItzAribie like this.
  11. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    Which API is talking to you ? (InventoryTransactionEvent) ?
     
  12. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    Solved Thanks @Sandertv for your help its work ;)

    here the solution.

    PHP:
    /**
    * @param DataPacketReceiveEvent $event
    * @return bool
    */
    public function onPacketReceived(DataPacketReceiveEvent $event)
        {
            
    $pk $event->getPacket();
            if (
    $pk instanceof InventoryTransactionPacket) {
                if (
    $pk->transactionType === InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY && $pk->trData->actionType === InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT) {


                        if (isset(
    $this->entity[$pk->trData->entityRuntimeId])) {
                            
    $entity $this->entity[$pk->trData->entityRuntimeId];

                            if (!isset(
    $this->command[$event->getPlayer()->getName()])) {
                                
    $entity->executeCommand($event->getPlayer());
                                return 
    true;
                            }
                        }
                    }
                }
            }
     
    Last edited: Oct 31, 2017
  13. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    No, no, no!
    Stop your DataPacketReceiveEvent addiction! It's a stupid idea and causes way more load than is needed.

    Why don't you just use the events provided? You tap an entity, so it's an attack, which means you have the EntityDamageEvent. Then check if it's instanceof EntityDamageByEntityEvent and check if the damager is a Player. Forget about this DataPacketReceiveEvent if you can do it without. Which you can most of the time..
     
    iiFlamiinBlaze likes this.
  14. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    No False,
    Why do that so I did not use the nbt I used directly AddPlayerPacket () it can not be taken by an EntityDamageEvent.
     
    Last edited: Oct 31, 2017
  15. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    If that's how you want to approach it, good luck with it. You should know DataPacketReceiveEvent is the most aggressive event in the whole core and it is strongly disadvised to use it if you can simply create an entity and use normal events instead.
     
    EdwardHamHam likes this.
  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.