Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace FaigerSYS\MapImageDemo;
- use pocketmine\event\Listener;
- use pocketmine\event\player\PlayerInteractEvent;
- use pocketmine\event\server\DataPacketReceiveEvent;
- use pocketmine\event\server\DataPacketSendEvent;
- use pocketmine\nbt\NBT;
- use pocketmine\nbt\tag\ByteTag;
- use pocketmine\nbt\tag\CompoundTag;
- use pocketmine\nbt\tag\ShortTag;
- use pocketmine\nbt\tag\StringTag;
- use pocketmine\network\mcpe\protocol\BlockEntityDataPacket;
- use pocketmine\network\mcpe\protocol\MapInfoRequestPacket;
- use pocketmine\network\mcpe\protocol\PacketPool;
- use pocketmine\Player;
- use pocketmine\plugin\PluginBase;
- class MapImageDemo extends PluginBase implements Listener{
- /** @var int */
- public $mapId;
- public function onEnable(){
- PacketPool::registerPacket(new ClientboundMapItemDataPacket);
- $this->getServer()->getPluginManager()->registerEvents($this, $this);
- }
- public function onTap(PlayerInteractEvent $e){
- $p = $e->getPlayer();
- if($e->getAction() == $e::RIGHT_CLICK_BLOCK && $e->getItem()->getId() == 280){
- $nbtPk = new BlockEntityDataPacket;
- $nbtPk->x = (int) 1;
- $nbtPk->y = (int) 1;
- $nbtPk->z = (int) 1;
- $nbt = new NBT(NBT::LITTLE_ENDIAN);
- $nbt->setData(new CompoundTag("", [
- new CompoundTag("Item", [
- new ShortTag("id", 358),
- //new ShortTag("id", 280),
- new ShortTag("Damage", 0),
- new ByteTag("Count", 1),
- new CompoundTag("", [
- new StringTag('map_uuid', $this->mapId = rand())
- ])
- ])
- ]));
- $nbtPk->namedtag = $nbt->write(true);
- $p->dataPacket($nbtPk);
- $this->sendMap($e->getPlayer(), $this->mapId);
- }
- }
- public function onReceivePacket(DataPacketReceiveEvent $e){
- $pk = $e->getPacket();
- if($pk instanceof MapInfoRequestPacket){
- $this->sendMap($e->getPlayer(), $pk->mapId);
- }
- }
- public function sendPacket(DataPacketSendEvent $e){
- $pk = $e->getPacket();
- if($pk instanceof ClientboundMapItemDataPacket){
- var_dump(true);
- }
- }
- public function sendMap(Player $player, int $mapId){
- if(!$player->isOnline()){
- return;
- }
- $packet = new ClientboundMapItemDataPacket;
- $packet->mapId = $mapId;
- $packet->scale = 0;
- $packet->width = 128;
- $packet->height = 128;
- $packet->colors = $this->getTextureFromFile('C:\Users\alex\Desktop\t.png');
- $player->dataPacket($packet);
- }
- public function getTextureFromFile(string $filename) : array{
- $im = imagecreatefrompng($filename);
- $data = getimagesize($filename);
- $width = $data[0];
- $height = $data[1];
- $colors = [];
- for($y = 0; $y < $height; $y++){
- for($x = 0; $x < $width; $x++){
- $raw_color = imagecolorsforindex($im, imagecolorat($im, $x, $y));
- $r = $raw_color['red'];
- $g = $raw_color['green'];
- $b = $raw_color['blue'];
- $a = $raw_color['alpha'] === 0 ? 255 : ~$raw_color['alpha'] << 1 & 0xFF;
- $colors[$y][$x] = ($a << 24) | ($b << 16) | ($g << 8) | $r;
- }
- }
- imagedestroy($im);
- return $colors;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement