Skip to content

Instantly share code, notes, and snippets.

@BEcraft
Last active July 11, 2021 21:43
Show Gist options
  • Save BEcraft/6b949b2c4d868fcd00f8fd6505d6bc37 to your computer and use it in GitHub Desktop.
Save BEcraft/6b949b2c4d868fcd00f8fd6505d6bc37 to your computer and use it in GitHub Desktop.
Maps with custom images PocketMine
<?php
namespace Picture;
use pocketmine\item\Item;
use pocketmine\event\Listener;
use pocketmine\{Server, Player};
use pocketmine\plugin\PluginBase;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\utils\{Color, TextFormat as T};
use pocketmine\nbt\tag\{CompoundTag, StringTag};
use pocketmine\network\mcpe\protocol\ClientboundMapItemDataPacket;
class Main extends PluginBase implements Listener{
public function onEnable(){
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->getServer()->getLogger()->info("enabled");
@mkdir($this->getDataFolder());
@mkdir($this->getDataFolder()."Image/");
}
public function onJoin(PlayerJoinEvent $event){
$player = $event->getPlayer();
$folder = $this->getDataFolder()."Image/";
$colores = [];
$imagen = @imagecreatefrompng($folder."BEcraft.png");
if($imagen === false){
$player->sendMessage(T::RED."Error with image!");
return;
}
$r = 0;
$g = 0;
$b = 0;
$ancho = 128;
$altura = 128;
$imagen = imagescale($imagen, $ancho, $altura, IMG_NEAREST_NEIGHBOUR);
imagepng($imagen, $folder."BEcraft-new.png");
for($y = 0; $y < $altura; ++$y){
for($x = 0; $x < $ancho; ++$x){
$rgb = imagecolorat($imagen, $x, $y);
$color = imagecolorsforindex($imagen, $rgb);
$r = $color["red"];
$g = $color["green"];
$b = $color["blue"];
$colores[$y][$x] = new Color($r, $g, $b, 0xff);
}
}
$map = Item::get(Item::FILLED_MAP);
$map->setCount(1);
$key = 18293883;
$tag = new CompoundTag("", []);
$tag->map_uuid = new StringTag("map_uuid", $key);
$map->setCompoundTag($tag);
$player->getInventory()->setItem(6, $map);
$pk = new ClientboundMapItemDataPacket();
$pk->mapId = $key;
$pk->type = ClientboundMapItemDataPacket::BITFLAG_TEXTURE_UPDATE;
$pk->height = 128;
$pk->width = 128;
$pk->scale = 1;
$pk->colors = $colores;
$player->dataPacket($pk);
$player->sendMessage(T::GREEN."Image completed!");
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment