From ea3ed14f8e5138805bf347c1e46d10b5293708a6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 29 May 2022 18:41:06 +0300 Subject: [PATCH] baby entities can spawn from eggs, minecarts --- src/Player.php | 44 ++++++++++++++++ src/material/item/generic/Minecart.php | 18 ++++++- src/material/item/generic/SpawnEgg.php | 3 ++ src/network/protocol/ProtocolInfo.php | 2 +- .../protocol/packet/InteractPacket.php | 2 +- .../protocol/packet/SetEntityLinkPacket.php | 29 +++++++++++ src/network/raknet/RakNetParser.php | 2 + src/world/Entity.php | 52 +++++++++++++++++-- 8 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 src/network/protocol/packet/SetEntityLinkPacket.php diff --git a/src/Player.php b/src/Player.php index f8a649b4..85fecc3f 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1833,6 +1833,29 @@ class Player{ $data["entity"] = $this->entity; if($packet->action === InteractPacket::ACTION_HOLD){ switch($target->class){ + case ENTITY_OBJECT: + switch($target->type){ + case OBJECT_MINECART: + if($target->linkedEntity != null){ + break; + } + $players = $this->server->api->player->getAll($this->level); + $pk = new SetEntityLinkPacket; + $pk->rider = $target->eid; + $pk->riding = $this->entity->eid; + $pk->type = 2; + $this->server->api->player->broadcastPacket([$this], $pk); + + $target->linkedEntity = $this->entity; + $this->entity->isInMinecart = true; + $pk = new SetEntityLinkPacket(); + $pk->rider = $target->eid; + $pk->riding = 0; + $pk->type = 2; + $this->dataPacket($pk); + $needsBreak = true; + + } case ENTITY_MOB: $slot = $this->getSlot($this->slot); switch($target->type){ @@ -1856,6 +1879,27 @@ class Player{ } } } + if($packet->action === InteractPacket::ACTION_VEHICLE_EXIT){ + switch($target->class){ + case ENTITY_OBJECT: + switch($target->type){ + case OBJECT_MINECART: + $players = $this->server->api->player->getAll($this->level); + $pk = new SetEntityLinkPacket(); + $pk->rider = targetEntity.getId(); + $pk->riding = this.id; + $pk->type = 3; + $this->server->api->player->broadcastPacket([$this], $pk); + $this->entity->isInMinecart = false; + $target->linkedEntity = null; + $pk = new SetEntityLinkPacket(); + $pk->rider = $target->eid; + $pk->riding = 0; + $pk->type = 3; + $this->dataPacket($pk); + } + } + } if($needsBreak){ break; } diff --git a/src/material/item/generic/Minecart.php b/src/material/item/generic/Minecart.php index 9ceae51f..08f1aa1f 100755 --- a/src/material/item/generic/Minecart.php +++ b/src/material/item/generic/Minecart.php @@ -22,6 +22,22 @@ class MinecartItem extends Item{ public function __construct($meta = 0, $count = 1){ parent::__construct(MINECART, 0, $count, "Minecart"); + $this->isActivable = true; + } + public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + if($target->getID() !== 66 and $target->getID() !== 27){ + return; + } + $server = ServerAPI::request(); + $data = [ + "x" => $target->getX(), + "y" => $target->getY() + 0.8, + "z" => $target->getZ(), + ]; + $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); + $server->api->entity->spawnToAll($e); + if(($player->gamemode & 0x01) === 0x00){ + $player->removeItem($this->getID(), $this->getMetadata(), 1, false); + } } - } \ No newline at end of file diff --git a/src/material/item/generic/SpawnEgg.php b/src/material/item/generic/SpawnEgg.php index a6ce0604..a0f774de 100644 --- a/src/material/item/generic/SpawnEgg.php +++ b/src/material/item/generic/SpawnEgg.php @@ -27,11 +27,13 @@ class SpawnEggItem extends Item{ } public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + $ageable = false; switch($this->meta){ case MOB_CHICKEN: case MOB_SHEEP: case MOB_COW: case MOB_PIG: + $ageable = true; case MOB_ZOMBIE: case MOB_CREEPER: case MOB_SKELETON: @@ -41,6 +43,7 @@ class SpawnEggItem extends Item{ "x" => $block->x + 0.5, "y" => $block->y, "z" => $block->z + 0.5, + "isBaby" => $ageable ? mt_rand(0,20) === 0 ? 1 : 0 : 0, ); $e = ServerAPI::request()->api->entity->add($block->level, ENTITY_MOB, $this->meta, $data); ServerAPI::request()->api->entity->spawnToAll($e); diff --git a/src/network/protocol/ProtocolInfo.php b/src/network/protocol/ProtocolInfo.php index 45c10048..98edc082 100644 --- a/src/network/protocol/ProtocolInfo.php +++ b/src/network/protocol/ProtocolInfo.php @@ -78,7 +78,7 @@ abstract class ProtocolInfo{ const HURT_ARMOR_PACKET = 0xa6; const SET_ENTITY_DATA_PACKET = 0xa7; const SET_ENTITY_MOTION_PACKET = 0xa8; - //const SET_ENTITY_LINK_PACKET = 0xa9; + const SET_ENTITY_LINK_PACKET = 0xa9; const SET_HEALTH_PACKET = 0xaa; const SET_SPAWN_POSITION_PACKET = 0xab; const ANIMATE_PACKET = 0xac; diff --git a/src/network/protocol/packet/InteractPacket.php b/src/network/protocol/packet/InteractPacket.php index ce29b577..62b5ddd5 100644 --- a/src/network/protocol/packet/InteractPacket.php +++ b/src/network/protocol/packet/InteractPacket.php @@ -22,7 +22,7 @@ class InteractPacket extends RakNetDataPacket{ const ACTION_HOLD = 1; //HOLD CLICK ON ENTITY const ACTION_ATTACK = 2; //SIMPLE CLICK(ATTACK) - + const ACTION_VEHICLE_EXIT = 3; //EXIT FROM ENTITY(MINECART) public $action; public $eid; public $target; diff --git a/src/network/protocol/packet/SetEntityLinkPacket.php b/src/network/protocol/packet/SetEntityLinkPacket.php new file mode 100644 index 00000000..2b83f7ba --- /dev/null +++ b/src/network/protocol/packet/SetEntityLinkPacket.php @@ -0,0 +1,29 @@ +reset(); + $this->putLong($this->rider); + $this->putLong($this->riding); + $this->putByte($this->type); + } + + + public function pid(){ + return ProtocolInfo::SET_ENTITY_LINK_PACKET; + } + +} \ No newline at end of file diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 98699be9..5f8f8440 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -346,6 +346,8 @@ class RakNetParser{ case ProtocolInfo::ENTITY_DATA_PACKET: $data = new EntityDataPacket; break; + case ProtocolInfo::SET_ENTITY_LINK_PACKET: + $data = new SetEntityLinkPacket; default: $data = new UnknownPacket(); $data->packetID = $pid; diff --git a/src/world/Entity.php b/src/world/Entity.php index 268df075..60077ae3 100644 --- a/src/world/Entity.php +++ b/src/world/Entity.php @@ -50,7 +50,9 @@ class Entity extends Position{ private $server; private $isStatic; public $level; + public $isInMinecart = false; public $lastUpdate; + public $linkedEntity = null; public $check = true; public $size = 1; public $inAction = false; @@ -144,6 +146,8 @@ class Entity extends Position{ }elseif($this->type === (OBJECT_ARROW or OBJECT_SNOWBALL)){ $this->server->schedule(1210, array($this, "update")); //Despawn $this->update(); + }elseif($this->type == OBJECT_MINECART){ + $this->update(); } break; } @@ -195,6 +199,10 @@ class Entity extends Position{ return array( array(PAINTING, 0, 1), ); + case OBJECT_MINECART: + return array( + array(MINECART, 0, 1), + ); } }elseif($this->class === ENTITY_MOB){ switch($this->type){ @@ -526,7 +534,7 @@ class Entity extends Position{ $this->fallY = $y; $this->fallStart = microtime(true); }elseif($this->class === ENTITY_PLAYER and ($this->fallStart + 5) < microtime(true)){ - if($this->server->api->getProperty("allow-flight") !== true and $this->server->handle("player.flying", $this->player) !== true){ + if($this->server->api->getProperty("allow-flight") !== true and $this->server->handle("player.flying", $this->player) !== true and $this->isInMinecart === false){ $this->player->close("flying"); return; } @@ -625,7 +633,23 @@ class Entity extends Position{ return null; } } - + /*METADATA VALUES(EXPEREMENTAL) + ****************** + Types: Get input type of + 0 -> Byte + 1 -> Short + 2 -> Integer + 3 -> Float + 4 -> Length of , Short + 5 -> array(Short, Byte, Short) + 6 -> arry(Integer, Integer, Integer) + ****************** + 0 => array("type" => 0, "value" => $flags) --> Unknown + 1 => array("type" => 1, "value" => $this->air) --> Entity Air + 14 => array("type" => 0, "value" => 1) --> isBaby, value: 0 => false, 1 => true + 16 => array("type" => 0, "value" => 0) --> Unknown + 17 => array("type" => 6, "value" => array(0, 0, 0)) --> Unknown + */ public function getMetadata(){ $flags = 0; $flags |= $this->fire > 0 ? 1:0; @@ -634,10 +658,14 @@ class Entity extends Position{ $d = array( 0 => array("type" => 0, "value" => $flags), 1 => array("type" => 1, "value" => $this->air), - //14 => array("type" => 0, "value" => 1), wip + 14 => array("type" => 0, "value" => 0), 16 => array("type" => 0, "value" => 0), 17 => array("type" => 6, "value" => array(0, 0, 0)), ); + if(!isset($this->data["isBaby"])){ + $this->data["isBaby"] = 0; + } + $d[14]["value"] = $this->data["isBaby"]; if($this->class === ENTITY_MOB and $this->type === MOB_SHEEP){ if(!isset($this->data["Sheared"])){ $this->data["Sheared"] = 0; @@ -782,6 +810,24 @@ class Entity extends Position{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $player->dataPacket($pk); + }elseif($this->type === OBJECT_MINECART){ + $pk = new AddEntityPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $player->dataPacket($pk); + + $pk = new SetEntityMotionPacket; + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->dataPacket($pk); + } break; case ENTITY_FALLING: