Created
January 21, 2018 00:02
-
-
Save BEcraft/567b31baf495a9b7b49b609e9d6bbd09 to your computer and use it in GitHub Desktop.
PocketMine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @name Balloon | |
* @author BEcraft | |
* @version 1.0 | |
* @api 3.0.0 | |
* @main BEcraft\Balloon\Loader | |
*/ | |
namespace BEcraft\Balloon; | |
use pocketmine\plugin\PluginBase; | |
use pocketmine\event\Listener; | |
use pocketmine\Player; | |
use pocketmine\Server; | |
use pocketmine\nbt\tag\CompoundTag; | |
use pocketmine\nbt\tag\ListTag; | |
use pocketmine\nbt\tag\DoubleTag; | |
use pocketmine\nbt\tag\IntTag; | |
use pocketmine\nbt\tag\ByteTag; | |
use pocketmine\nbt\tag\FloatTag; | |
use pocketmine\entity\Entity; | |
use pocketmine\entity\Villager; | |
use pocketmine\entity\FallingSand; | |
use pocketmine\utils\TextFormat; | |
use pocketmine\event\player\PlayerChatEvent; | |
use pocketmine\event\player\PlayerMoveEvent; | |
use pocketmine\network\mcpe\protocol\SetEntityDataPacket; | |
use pocketmine\network\mcpe\protocol\SetEntityLinkPacket; | |
class Loader extends PluginBase implements Listener{ | |
private $balloon = []; | |
public function onEnable(){ | |
$this->getServer()->getPluginManager()->registerEvents($this, $this); | |
Entity::registerEntity(NewVillager::class, true); | |
Entity::registerEntity(NewFallingSand::class, true); | |
} | |
private function hasBalloon(string $player): bool{ | |
return array_key_exists($player, $this->balloon); | |
} | |
private function getBalloonID(string $player): int{ | |
if($this->hasBalloon($player)){ | |
return $this->balloon[$player]["BalloonID"]; | |
} | |
} | |
private function getBaseID(string $player): int{ | |
if($this->hasBalloon($player)){ | |
return $this->balloon[$player]["BaseID"]; | |
} | |
} | |
public function onChat(PlayerChatEvent $event){ | |
if($event->isCancelled()){ | |
return false; | |
} | |
$player = $event->getPlayer(); | |
$command = explode(" ", $event->getMessage()); | |
if($command[0] === "%balloon"){ | |
if($command[1] === "spawn"){ | |
if($this->hasBalloon($player->getName())){ | |
$player->sendMessage(TextFormat::RED."- Ya tienes tu globo, no seas ambicioso D:"); | |
}else{ | |
if($this->addPlayer($player->getName(), Manager::spawnBalloon($player))){ | |
$player->sendMessage(TextFormat::GOLD."- Ahora tienes un globo! :)"); | |
} | |
} | |
}elseif($command[1] === "close"){ | |
if(!$this->hasBalloon($player->getName())){ | |
$player->sendMessage(TextFormat::GOLD."- Aun no tienes un globo, usa %balloon spawn"); | |
}else{ | |
if(!is_null(($balloon = $player->getLevel()->getEntity($this->getBalloonID($player->getName()))))){ | |
$balloon->close(); | |
} | |
if(!is_null(($base = $player->getLevel()->getEntity($this->getBaseID($player->getName()))))){ | |
$base->close(); | |
} | |
unset($this->balloons[$player->getName()]); | |
$player->sendMessage(TextFormat::YELLOW."- Tu globo se ha eliminado"); | |
} | |
}else{ | |
$player->sendMessage(TextFormat::YELLOW."Usa: ".TextFormat::GREEN."%balloon <spawn:close>"); | |
} | |
$event->setCancelled(); | |
} | |
} | |
public function onMove(PlayerMoveEvent $event){ | |
if($event->isCancelled()){ | |
return false; | |
} | |
$player = $event->getPlayer(); | |
if($this->hasBalloon($player->getName())){ | |
$entity = $player->getLevel()->getEntity($this->getBalloonID($player->getName())); | |
if(!is_null($entity)){ | |
$diff = abs($x = $player->getX()-$entity->getX())+abs($z = $player->getZ()-$entity->getZ()); | |
$x = 1.5*($x/$diff+0.5); | |
$z = 1.5*($z/$diff+0.5); | |
$player->getLevel()->addEntityMovement($player->chunk->getX(), $player->chunk->getZ(), $this->getBalloonID($player->getName()), $player->getX()+$x, $player->getY()+5, $player->getZ()+$z, $player->yaw, $player->pitch); | |
} | |
} | |
} | |
private function addPlayer(string $player, array $data): bool{ | |
$this->balloon[$player] = ["BalloonID" => $data["BalloonID"], "BaseID" => $data["BaseID"]]; | |
return $this->hasBalloon($player); | |
} | |
} | |
class Manager{ | |
public static function spawnBalloon(Player $player): array{ | |
$nbt = new CompoundTag(); | |
$nbt->Pos = new ListTag("Pos", [new DoubleTag("", $player->getX()), new DoubleTag("", $player->getY()+5), new DoubleTag("", $player->getZ())]); | |
$nbt->Motion = new ListTag("Motion", [new DoubleTag("", 0), new DoubleTag("", 0), new DoubleTag("", 0)]); | |
$nbt->Rotation = new ListTag("Rotation", [new FloatTag("", 0.0), new FloatTag("", 0.0)]); | |
$entity = Entity::createEntity("NewVillager", $player->getLevel(), $nbt); | |
$entity->setDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_LEASHED, true); | |
$entity->setDataProperty(Entity::DATA_LEAD_HOLDER_EID, Entity::DATA_TYPE_LONG, $player->getId()); | |
$entity->spawnToAll(); | |
$nbt->TileID = new IntTag("TileID", 35); | |
$nbt->Data = new ByteTag("Data", mt_rand(1, 15)); | |
$balloon = Entity::createEntity("NewFallingSand", $player->getLevel(), $nbt); | |
$balloon->spawnToAll(); | |
$pk = new SetEntityLinkPacket(); | |
$pk->from = $entity->getId(); | |
$pk->to = $balloon->getId(); | |
$pk->type = 2; | |
Server::getInstance()->broadcastPacket(Server::getInstance()->getOnlinePlayers(), $pk); | |
return ["BalloonID" => $entity->getId(), "BaseID" => $balloon->getId()]; | |
} | |
} | |
class NewVillager extends Villager{ | |
public function initEntity(){ | |
parent::initEntity(); | |
$this->setDataProperty(self::DATA_SCALE, self::DATA_TYPE_FLOAT, 0.1); | |
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY, true); //shh xD | |
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_INVISIBLE, true); | |
} | |
} | |
class NewFallingSand extends FallingSand{ | |
protected $gravity = 0.0; | |
protected $drag = 0.0; | |
public function onUpdate($currentTick){ | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeError: "Argument 1 passed to pocketmine\network\mcpe\protocol\DataPacket::putEntityLink() must be an instance of pocketmine\network\mcpe\protocol\types\EntityLink, null given, called in phar:///home/dev3/TestServer/PocketMine-MP.phar/src/pocketmine/network/mcpe/protocol/SetEntityLinkPacket.php on line 43" (EXCEPTION) in "src/pocketmine/network/mcpe/protocol/DataPacket" at line 535