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

InteractPacket with NPC

Discussion in 'Development' started by RyanIsBack, May 4, 2018.

  1. RyanIsBack

    RyanIsBack Spider

    Messages:
    10
    Hello,
    I spawn a fake player with Player AddPacket, the problem is that I can not do any action when the player clicks on the entity! I try EntityDamageEvent and PacketInteract, PacketInteract works but it is when we fly over the entity and not when clicking!

    Thanks for the help.


    »» CODE FOR SPAWN:
    PHP:
    public function spawn(Player $player, array $infos) {
            
    $pk = new AddPlayerPacket();
            
    $pk->eid = (int)$infos['id'];
            
    $pk->uuid UUID::fromRandom();
            
    $pk->entityUniqueId = (int)$infos['id'];
            
    $pk->entityRuntimeId =  (int)$infos['id'];
            
    $pk->username $infos['name'];
                    
    $pk->skin = clone $player->getSkin();
            
    $pk->position = new Vector3($infos['x'], $infos['ya'], $infos['z']);
            
    $pk->$infos['x'];
            
    $pk->$infos['ya'] + 1;
            
    $pk->$infos['z'];
            
    $pk->item Item::get($infos['item'], $infos['meta'], 1);
            
    $pk->speedX 0;
            
    $pk->speedY 0;
            
    $pk->speedZ 0;
            
    $pk->yaw $infos["yaw"];
            
    $pk->pitch $infos["pitch"];
            
    $pk->metadata = [
                
    Entity::DATA_FLAGS => [Entity::DATA_TYPE_BYTE<< Entity::DATA_FLAG_INVISIBLE],
                
    Entity::DATA_NAMETAG => [Entity::DATA_TYPE_STRING$infos['name']],
                
    Entity::DATA_FLAG_CAN_SHOW_NAMETAG => [Entity::DATA_TYPE_BYTE1],
                
    Entity::DATA_FLAG_NO_AI => [Entity::DATA_TYPE_BYTE1],
                
    Entity::DATA_LEAD_HOLDER_EID => [Entity::DATA_TYPE_LONG, -1],
                
    Entity::DATA_FLAG_LEASHED => [Entity::DATA_TYPE_BYTE0]
            ];
            
    $player->dataPacket($pk);
        }
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    The problem is that you spawn the Entity on client side only and server doesn't keep it's "copy" of the entity on server side so no events/interactions can be made. This is basic game server logic. Take a look how Slapper does it (does Slapper still exist?).

    Hint: extend human class

    Edit: I found it this and also this
     
    HyperxXxHound, Muqsit and RyanShaw like this.
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    use inventory transaction packet instead of interactpacket
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    What it has to do with Inventory Transaction?
     
  5. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
  6. RyanIsBack

    RyanIsBack Spider

    Messages:
    10
    How ?
    PHP:
    public function onReceive(DataPacketReceiveEvent $event)
        {
            
    $pk $event->getPacket();
            if(
    $pk instanceof InteractPacket) {
                if(
    $pk->action instanceof InventoryTransactionPacket ) {
                }
             }
        }
     
  7. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Look how pocketmine does it
    https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/Player.php#2269
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    The mojang people stuffed almost everything into InventoryTransactionPacket including Player->Entity interactions.

    Check if $pk is instanceof InventoryTransactionPacket and $pk->transactionType is InventoryTransactionPacket::USE_ITEM_ON_ENTITY_INTERACT. If so, then $pk->trData->entityRuntimeId will return the packet entity's entityRuntimeId.

    Anyway, what's wrong in using pocketmine's Entity class? :rolleyes:
     
    Primus likes this.
  9. Primus

    Primus Zombie Pigman

    Messages:
    749
    That's what I said. Human is instance of Entity after all.
    Might seem funny to us, but makes sense for them.
     
  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.