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

Solved PlayerActionPacket Problem

Discussion in 'Development' started by MC ATECH, Apr 13, 2019.

  1. MC ATECH

    MC ATECH Baby Zombie

    Messages:
    117
    GitHub:
    mcpeatech
    Hi all!
    I have recently been experimenting with using the interact packet to send a player a message when they left click an entity. The code below is what I've tried with to make the message "hi" be sent to the player.
    PHP:
    public function onrecieveData(DataPacketReceiveEvent $event){
        
    $pk $event->getPacket();
        
    $player $event->getPlayer();
        
    $id 1111;
            if(
    $pk instanceof InteractPacket){
                if(
    $pk->action === PlayerActionPacket::ACTION_LEFT_CLICK){ //line 69
                    
    if($pk->target == $id){
                        
    $player->sendMessage("hi");
                    }
                }
            }
        }
    Unfortunately this code gives out this error:
    [Server thread/CRITICAL]: Error: "Undefined class constant 'ACTION_LEFT_CLICK'" (EXCEPTION) in "plugins/TestPlugin/src/MCATECH/TestPlugin/Main" at line 69
    I have tried to research a fix but nobody else seems to have this problem. Is this because of a new api change or am I just using the constant incorrectly?
    Any help would be appreciated.
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Neither PlayerActionPacket nor InteractPacket are the ones that will help you here. You need to handle InventoryTransactionPacket. (Yeah, doesn't "sound" right... blame mojang for shoving everything into InventoryTransactionPacket).
    PHP:
    if($pk->transactionType === InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY){
        if(
    $pk->trData->actionType === InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT){
            
    $entityId $pk->trData->entityRuntimeId;
            
    // $entity = $server->findEntity($entityId);
        
    }
    }
     
    MC ATECH likes this.
  3. MC ATECH

    MC ATECH Baby Zombie

    Messages:
    117
    GitHub:
    mcpeatech
    Thank you!
    I shall give this a shot. It was a bit confusing floating around the different packets within pocketmine.. but once you know you know :)
     
  4. MC ATECH

    MC ATECH Baby Zombie

    Messages:
    117
    GitHub:
    mcpeatech
    Hi again,
    I seem to be getting this issue when using the transactionType..
    How do I define a property that doesn't exist within the code of the plugin?
    Code:
    Error: "Undefined property: pocketmine\network\mcpe\protocol\BatchPacket::$transactionType" (EXCEPTION) in "src/pocketmine/network/mcpe/protocol/DataPacket" at line 160
     
  5. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    can you show your full code? it seems like you're not checking if the packet is an instance of InventoryTransactionPacket
     
  6. MC ATECH

    MC ATECH Baby Zombie

    Messages:
    117
    GitHub:
    mcpeatech
    Full Code:
    PHP:
    namespace MCATECH\TestPlugin;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\event\Listener;

    use 
    pocketmine\Server;

    use 
    pocketmine\item\Item;
    use 
    pocketmine\item\ItemFactory;

    use 
    pocketmine\math\Vector3;

    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\MainLogger;
    use 
    pocketmine\utils\UUID;

    use 
    pocketmine\level\Position;

    use 
    pocketmine\network\mcpe\protocol\AddItemEntityPacket;
    use 
    pocketmine\network\mcpe\protocol\InteractPacket;
    use 
    pocketmine\network\protocol\UseItemPacket;
    use 
    pocketmine\event\server\DataPacketReceiveEvent;
    use 
    pocketmine\network\mcpe\protocol\AddPlayerPacket;
    use 
    pocketmine\network\mcpe\protocol\InventoryTransactionPacket;

    use 
    pocketmine\Player;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;

    use 
    pocketmine\event\entity\EntityDamageEvent;

    use 
    pocketmine\entity\Entity;

    class 
    Main extends PluginBase implements listener{
     
        protected static 
    $instance;
       
        public static function 
    getInstance() : Main{
            return 
    self::$instance;
        }
     
        public function 
    onEnable() : void {
            
    self::$instance $this;
        }
     
        public function 
    spawnNpc ($position$player$npcname) {
            
    $id Entity::$entityCount++;
            
    $pk = new AddPlayerPacket();
            
    $pk->entityRuntimeId $id;
            
    $pk->uuid UUID::fromRandom();
            
    $pk->username $npcname;
            
    $pk->position $position;
            
    $pk->item ItemFactory::get(Item::AIR00);
            
    $pk->transactionType TYPE_USE_ITEM_ON_ENTITY;
            
    $pk->yaw 90;
            
    $pk->pitch 0;
            
    self::$instance->getServer()->broadcastPacket([$player], $pk);
        }
       
        public function 
    onJoined(PlayerJoinEvent $event) {
             
    $player $event->getPlayer();
             
    $position =  new Vector3(274.54.2252.5);
             
    $npcname TextFormat::RED "NPC";
             
    $this->spawnNpc($position$player$npcname);
        }
       
        public function 
    onrecieveData(DataPacketReceiveEvent $event){
            
    $pk $event->getPacket();
           
    // $player = $event->getPlayer();
            
    if($pk->transactionType === InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY){
                if(
    $pk->trData->actionType === InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT){
                    
    $entityId $pk->trData->entityRuntimeId;
                    
    // $entity = $server->findEntity($entityId);
                    
    $player->sendMessage("hi");
                }
            }
        }
    }
     
  7. MC ATECH

    MC ATECH Baby Zombie

    Messages:
    117
    GitHub:
    mcpeatech
    How do I check that the packet is an instance of InventoryTransactionPacket correctly?
     
  8. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    PHP:
        public function onrecieveData(DataPacketReceiveEvent $event){
            
    $pk $event->getPacket();
           
    // $player = $event->getPlayer();
            
    if($pk instanceof InventoryTransactionPacket){
                if(
    $pk->transactionType === InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY){
                    if(
    $pk->trData->actionType === InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT){
                        
    $entityId $pk->trData->entityRuntimeId;
                        
    // $entity = $server->findEntity($entityId);
                        
    $player->sendMessage("hi");
                    }
                }
            }
        }

     
    MC ATECH likes this.
  9. MC ATECH

    MC ATECH Baby Zombie

    Messages:
    117
    GitHub:
    mcpeatech
    Works like a charm, thank you!
     
    KielKing 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.