Skip to content

Instantly share code, notes, and snippets.

@falkirks
Created March 26, 2014 13:50
Show Gist options
  • Save falkirks/9783554 to your computer and use it in GitHub Desktop.
Save falkirks/9783554 to your computer and use it in GitHub Desktop.
PocketMine fake players
<?php
/*
__PocketMine Plugin__
name=AddPlayer
description=Add a player to the game
version=0.1
author=Falk
class=addP
apiversion=12,13
*/
class addP implements Plugin{
private $api;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
}
public function init(){
$this->api->console->register("addp", "Add a player", array($this, "command"));
$this->api->console->register("delp", "Delete all players linked to a player", array($this, "deleteP"));
$this->api->console->register("clearallp", "Delete all players linked to a player", array($this, "clearP"));
$this->api->addHandler("player.spawn", array($this,"spawnPlayers"),50);
$this->e = array();
$this->api->schedule(2,array($this,"movePlayer"),2,true,"server.schedule");
$this->despawn = true; //Set to false to allow fake players to persist on quit
}
public function __destruct(){}
public function command($cmd, $params, $issuer, $alias, $args, $issuer){
if(!isset($params[1])) return "Player to bind required";
$issuer = $this->api->player->get($params[0]);
if($issuer instanceof Player){
$pk = new AddPlayerPacket;
$pk->clientID = 0;
$pk->username = $params[1];
$pk->eid = $this->api->entity->add($issuer->level,ENTITY_PLAYER)->eid; //Prevent usage of EID
$pk->x = $issuer->entity->x;
$pk->y = $issuer->entity->y;
$pk->z = $issuer->entity->z;
$this->e[] = array($pk->eid,$params[0],$pk->x,$pk->z,true,$params[1],$pk->y,$issuer->entity->level->getName());
$pk->yaw = 0;
$pk->pitch = 0;
$pk->unknown1 = 0;
$pk->unknown2 = 0;
$pk->metadata = array(
0 => array("type" => 0, "value" => 0),
1 => array("type" => 1, "value" => 300), //Air
16 => array("type" => 0, "value" => 0),
17 => array("type" => 6, "value" => array(0, 0, 0)), //Sleep Coords
);
foreach($this->api->player->getAll($issuer->level) as $player){
$player->dataPacket($pk);
}
return "Player sucessfully bound";
}
return "Player is not online.";
}
public function deleteP($cmd, $params, $issuer, $alias, $args, $issuer){
if(!isset($params[0])) return "Must specify player";
$i = 0;
foreach ($this->e as $key => $e) {
if($e[1] == $params[0]){
unset($this->e[$key]);
$this->api->entity->remove($e[0]);
}
$i++;
}
return $i . " players removed";
}
public function movePlayer(){
$set = array();
foreach ($this->e as $key => $e) {
if(($p = $this->api->player->get($e[1])) == false){
if($this->despawn){
unset($this->e[$key]);
$this->api->entity->remove($e[0]);
}
}
else{
if(isset($set[$p->username])) $set[$p->username]++;
else $set[$p->username] = 1;
$pk = new MovePlayerPacket;
$pk->eid = $e[0];
if($p->entity->z != $e[3]) $z = ($p->entity->z > $e[3] ? $e[3] + 0.2 : $e[3] - 0.2);
else $z = $e[3];
if($p->entity->x != $e[2]) $x = ($p->entity->x > $e[2] ? $e[2] + 0.2: $e[2] - 0.2);
else $x = $e[2];
if($p->entity->y != $e[6])
$y = ($p->entity->y > $e[6] ? $e[6] + 1: $e[6] - 1);
else $y = $e[6];
if(abs($x-$p->entity->x) < 0.5) $x = $p->entity->x; //If Stalker is suffciently close just make them jump
if(abs($x-$p->entity->z) < 0.5) $z = $p->entity->z;
if(abs($y-$p->entity->y) < 2) $y = $p->entity->y;
if(abs($x-$p->entity->x) < 1 && abs($z-$p->entity->z) < 1 && abs($y-$p->entity->y) < 1){
if($p->getSlot($p->slot)->getID() == 295) $this->e[$key][4] = false;
if($p->getSlot($p->slot)->getID() == 296) $this->e[$key][4] = true;
}
if(abs($x-$p->entity->x) < $set[$p->username] && abs($z-$p->entity->z) < $set[$p->username] && abs($y-$p->entity->y) < 1) continue;
if(!$e[4]) continue;
$c = $this->getSafeSpawn(new Vector3($x,$y,$z),$p->level);
if(!$c) continue;
$pk->x = $c->x;
$pk->y = $c->y;
$pk->z = $c->z;
$this->e[$key][2] = $c->x;
$this->e[$key][3] = $c->z;
$this->e[$key][6] = $c->y;
$pk->yaw = $p->entity->yaw+180;
$pk->pitch = $p->entity->pitch;
$pk->bodyYaw = $p->entity->yaw+180;
foreach ($this->api->player->getAll($p->level) as $player) {
$player->dataPacket($pk);
}
}
}
}
public function getSafeSpawn($spawn,$level){ //Modified version of getSafeSpawn
$x = round($spawn->x);
$y = $spawn->y;
$z = round($spawn->z);
if($level->getBlock(new Vector3($x,$y,$z))->getHardness() < 1 || $level->getBlock(new Vector3($x,$y,$z)) instanceof DoorBlock){
return $spawn;
}
if($level->getBlock(new Vector3($x,$y-1,$z))->getHardness() < 1 || $level->getBlock(new Vector3($x,$y-1,$z)) instanceof DoorBlock){
$spawn->y -= 1;
return $spawn;
}
if($level->getBlock(new Vector3($x,$y+1,$z))->getHardness() < 1 || $level->getBlock(new Vector3($x,$y+1,$z)) instanceof DoorBlock){
$spawn->y += 1;
return $spawn;
}
return false;
}
public function spawnPlayers($data){
foreach ($this->e as $key => $e) {
if($e[8] != $data->entity->level->getName()) continue;
$pk = new AddPlayerPacket;
$pk->clientID = 0;
$pk->username = $e[5];
$pk->eid = $e[0]; //Prevent usage of EID
$pk->x = $e[2];
$pk->y = $e[6];
$pk->z = $e[3];
$pk->yaw = 0;
$pk->pitch = 0;
$pk->unknown1 = 0;
$pk->unknown2 = 0;
$pk->metadata = array(
0 => array("type" => 0, "value" => 0),
1 => array("type" => 1, "value" => 300), //Air
16 => array("type" => 0, "value" => 0),
17 => array("type" => 6, "value" => array(0, 0, 0)), //Sleep Coords
);
$data->dataPacket($pk);
}
}
public function clearP($cmd, $params, $issuer, $alias, $args, $issuer){
$i = 0;
foreach ($this->e as $key => $e) {
unset($this->e[$key]);
$this->api->entity->remove($e[0]);
$i++;
}
return $i . " players removed";
}
}
@atomicyoyo
Copy link

how do you use this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment