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: Spoiler: 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.
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); }}
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
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
can you show your full code? it seems like you're not checking if the packet is an instance of InventoryTransactionPacket
Full Code: Spoiler: 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::AIR, 0, 0); $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.5, 4.2, 252.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"); } } }}
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"); } } } }}