Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2.  
  3. namespace FaigerSYS\MapImageDemo;
  4.  
  5. use pocketmine\event\Listener;
  6. use pocketmine\event\player\PlayerInteractEvent;
  7. use pocketmine\event\server\DataPacketReceiveEvent;
  8. use pocketmine\event\server\DataPacketSendEvent;
  9. use pocketmine\nbt\NBT;
  10. use pocketmine\nbt\tag\ByteTag;
  11. use pocketmine\nbt\tag\CompoundTag;
  12. use pocketmine\nbt\tag\ShortTag;
  13. use pocketmine\nbt\tag\StringTag;
  14. use pocketmine\network\mcpe\protocol\BlockEntityDataPacket;
  15. use pocketmine\network\mcpe\protocol\MapInfoRequestPacket;
  16. use pocketmine\network\mcpe\protocol\PacketPool;
  17. use pocketmine\Player;
  18. use pocketmine\plugin\PluginBase;
  19.  
  20.  
  21. class MapImageDemo extends PluginBase implements Listener{
  22.     /** @var  int */
  23.     public $mapId;
  24.  
  25.     public function onEnable(){
  26.         PacketPool::registerPacket(new ClientboundMapItemDataPacket);
  27.         $this->getServer()->getPluginManager()->registerEvents($this, $this);
  28.     }
  29.  
  30.     public function onTap(PlayerInteractEvent $e){
  31.         $p = $e->getPlayer();
  32.  
  33.         if($e->getAction() == $e::RIGHT_CLICK_BLOCK && $e->getItem()->getId() == 280){
  34.             $nbtPk = new BlockEntityDataPacket;
  35.             $nbtPk->x = (int) 1;
  36.             $nbtPk->y = (int) 1;
  37.             $nbtPk->z = (int) 1;
  38.             $nbt = new NBT(NBT::LITTLE_ENDIAN);
  39.             $nbt->setData(new CompoundTag("", [
  40.                 new CompoundTag("Item", [
  41.                     new ShortTag("id", 358),
  42.                     //new ShortTag("id", 280),
  43.                     new ShortTag("Damage", 0),
  44.                     new ByteTag("Count", 1),
  45.                     new CompoundTag("", [
  46.                         new StringTag('map_uuid', $this->mapId = rand())
  47.                     ])
  48.                 ])
  49.             ]));
  50.             $nbtPk->namedtag = $nbt->write(true);
  51.             $p->dataPacket($nbtPk);
  52.  
  53.             $this->sendMap($e->getPlayer(), $this->mapId);
  54.         }
  55.     }
  56.  
  57.     public function onReceivePacket(DataPacketReceiveEvent $e){
  58.         $pk = $e->getPacket();
  59.         if($pk instanceof MapInfoRequestPacket){
  60.             $this->sendMap($e->getPlayer(), $pk->mapId);
  61.         }
  62.     }
  63.  
  64.     public function sendPacket(DataPacketSendEvent $e){
  65.         $pk = $e->getPacket();
  66.         if($pk instanceof ClientboundMapItemDataPacket){
  67.             var_dump(true);
  68.         }
  69.     }
  70.  
  71.     public function sendMap(Player $player, int $mapId){
  72.         if(!$player->isOnline()){
  73.             return;
  74.         }
  75.         $packet = new ClientboundMapItemDataPacket;
  76.         $packet->mapId = $mapId;
  77.         $packet->scale = 0;
  78.         $packet->width = 128;
  79.         $packet->height = 128;
  80.         $packet->colors = $this->getTextureFromFile('C:\Users\alex\Desktop\t.png');
  81.         $player->dataPacket($packet);
  82.     }
  83.  
  84.     public function getTextureFromFile(string $filename) : array{
  85.         $im = imagecreatefrompng($filename);
  86.         $data = getimagesize($filename);
  87.         $width = $data[0];
  88.         $height = $data[1];
  89.         $colors = [];
  90.         for($y = 0; $y < $height; $y++){
  91.             for($x = 0; $x < $width; $x++){
  92.                 $raw_color = imagecolorsforindex($im, imagecolorat($im, $x, $y));
  93.                 $r = $raw_color['red'];
  94.                 $g = $raw_color['green'];
  95.                 $b = $raw_color['blue'];
  96.                 $a = $raw_color['alpha'] === 0 ? 255 : ~$raw_color['alpha'] << 1 & 0xFF;
  97.                 $colors[$y][$x] = ($a << 24) | ($b << 16) | ($g << 8) | $r;
  98.             }
  99.         }
  100.         imagedestroy($im);
  101.         return $colors;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement