Advertisement
PEMapModder

Untitled

Apr 13th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace legionpe\utils;
  4.  
  5. use legionpe\LegionPE;
  6. use legionpe\session\Session;
  7. use pocketmine\entity\Human;
  8. use pocketmine\event\entity\EntityDamageByEntityEvent;
  9. use pocketmine\event\entity\EntityDamageEvent;
  10. use pocketmine\inventory\PlayerInventory;
  11. use pocketmine\level\format\FullChunk;
  12. use pocketmine\level\Location;
  13. use pocketmine\nbt\tag\Byte;
  14. use pocketmine\nbt\tag\Compound;
  15. use pocketmine\nbt\tag\Double;
  16. use pocketmine\nbt\tag\Enum;
  17. use pocketmine\nbt\tag\Float;
  18. use pocketmine\nbt\tag\Short;
  19. use pocketmine\Player;
  20.  
  21. abstract class SlappableHuman extends Human{
  22.     /** @var LegionPE */
  23.     private $main;
  24.     /** @var int[][][] */
  25.     private $dataFlagsArray = [];
  26.     public function __construct(LegionPE $main, Location $vectors, $displayName){
  27.         $this->main = $main;
  28.         $nbt = new Compound;
  29.         $nbt->Pos = new Enum("Pos", [
  30.             new Double(0, $vectors->x),
  31.             new Double(1, $vectors->y),
  32.             new Double(2, $vectors->z)
  33.         ]);
  34.         $nbt->Motion = new Enum("Motion", [
  35.             new Double(0, 0),
  36.             new Double(1, 0),
  37.             new Double(2, 0)
  38.         ]);
  39.         $nbt->Rotation = new Enum("Rotation", [
  40.             new Float(0, $vectors->yaw),
  41.             new Float(1, $vectors->pitch)
  42.         ]);
  43.         $nbt->FallDistance = new Float("FallDistance", 0);
  44.         $nbt->Fire = new Short("Fire", 0);
  45.         $nbt->Air = new Short("Air", 0);
  46.         $nbt->OnGround = new Byte("OnGround", 1);
  47.         $nbt->Invulnerable = new Byte("Invulnerable", 1);
  48.         $nbt->Health = new Short("Health", 20);
  49.         $nbt->NameTag = $displayName;
  50.         $nbt->Inventory = new Enum("Inventory", [new Compound(false, [
  51.             new Short("id", 0),
  52.             new Short("Damage", 0),
  53.             new Byte("Count", 0),
  54.             new Byte("Slot", 9),
  55.             new Byte("TrueSlot", 9)
  56.         ])]);
  57.         $X = ($vectors->getFloorX()) >> 4;
  58.         $Z = ($vectors->getFloorZ() >> 4);
  59.         if(!$vectors->getLevel()->isChunkLoaded($X, $Z)){
  60.             $vectors->getLevel()->loadChunk($X, $Z);
  61.         }
  62.         $chunk = $vectors->getLevel()->getChunk($X, $Z);
  63.         $this->inventory = new PlayerInventory($this);
  64.         $this->initSkin();
  65.         parent::__construct($chunk, $nbt);
  66.         $this->nameTag = $displayName;
  67.         $this->teleport($this);
  68.     }
  69.     protected function initSkin(){
  70.         $this->setSkin(str_repeat("\x80", 64 * 64 * 4));
  71.     }
  72.     public function attack($damage, EntityDamageEvent $source){
  73.         if(!($source instanceof EntityDamageByEntityEvent)){
  74.             return;
  75.         }
  76.         $damager = $source->getDamager();
  77.         if(!($damager instanceof Player)){
  78.             return;
  79.         }
  80.         $session = $this->main->getSessions()->getSession($damager);
  81.         if($session instanceof Session){
  82.             $this->onSlap($session);
  83.         }
  84.     }
  85.     protected abstract function onSlap(Session $session);
  86.     /**
  87.      * @return LegionPE
  88.      */
  89.     public function getMain(){
  90.         return $this->main;
  91.     }
  92.     public function _kill(){
  93.         $this->getLevel()->removeEntity($this);
  94.     }
  95.     public function badConstruct($chunk){
  96.         if($chunk instanceof FullChunk){
  97.             $this->inventory = new PlayerInventory($this);
  98.             $this->server = $chunk->getProvider()->getLevel()->getServer();
  99.             $this->setLevel($chunk->getProvider()->getLevel());
  100.             $chunk->getProvider()->getLevel()->removeEntity($this);
  101.         }
  102.         else{
  103.             echo "What? SlappableHuman::badConstruct() receives parameter 1 as " . is_object($chunk) ? get_class($chunk):gettype($chunk);
  104.         }
  105.     }
  106.     public function spawnTo(Player $player){
  107.         parent::spawnTo($player);
  108.         $this->dataFlagsArray[$player->getId()] = $this->dataProperties;
  109.     }
  110.     public function despawnFrom(Player $player){
  111.         parent::despawnFrom($player);
  112.         if(isset($this->dataFlagsArray[$player->getId()])){
  113.             unset($this->dataFlagsArray[$player->getId()]);
  114.         }
  115.     }
  116.  
  117.     public function setDataPropertyTo(Player $player, $id, $type, $value){
  118.         if($this->getDataPropertyTo($player, $id) !== $value){
  119.             $this->dataFlagsArray[$player->getId()][$id] = [$type, $value];
  120.             $targets = $this->hasSpawned;
  121.             if($this instanceof Player){
  122.                 if(!$this->spawned){
  123.                     return;
  124.                 }
  125.                 $targets[] = $this;
  126.             }
  127.             $this->sendData($targets, [$id => $this->dataFlagsArray[$player->getId()][$id]]);
  128.         }
  129.     }
  130.     public function getDataPropertyTo(Player $player, $id){
  131.         if(isset($this->dataFlagsArray[$player->getId()])){
  132.             return isset($this->dataFlagsArray[$player->getId()][$id]) ? $this->dataFlagsArray[$player->getId()][$id][1] : null;
  133.         }
  134.         return null;
  135.     }
  136.     public function getDataPropertyTypeTo(Player $player, $id){
  137.         if(isset($this->dataFlagsArray[$player->getId()])){
  138.             return isset($this->dataFlagsArray[$player->getId()][$id]) ? $this->dataFlagsArray[$player->getId()][$id][0] : null;
  139.         }
  140.         return null;
  141.     }
  142.     public function setDataFlagTo(Player $player, $propertyId, $id, $value = true, $type = self::DATA_TYPE_BYTE){
  143.         if($this->getDataFlagTo($player, $propertyId, $id) !== $value){
  144.             $flags = (int) $this->getDataPropertyTo($player, $propertyId);
  145.             $flags ^= 1 << $id;
  146.             $this->setDataPropertyTo($player, $propertyId, $type, $flags);
  147.         }
  148.     }
  149.     public function getDataFlagTo(Player $player, $propertyId, $id){
  150.         return (((int) $this->getDataPropertyTo($player, $propertyId)) & (1 << $id)) > 0;
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement