Skip to content

Commit

Permalink
Implement different ratio of width and height, formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
VixikHD committed Oct 20, 2021
1 parent 9bd5df0 commit 72be767
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 324 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ext-yaml": "*",
"ext-pthreads": "*",
"ext-json": "*",
"pocketmine/pocketmine-mp": "4.0.0-BETA3",
"pocketmine/pocketmine-mp": "4.0.0-BETA6",
"ext-gd": "*"
},
"require-dev": {
Expand Down
274 changes: 34 additions & 240 deletions composer.lock

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions src/czechpmdevs/imageonmap/DataProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

use czechpmdevs\imageonmap\image\Image;
use czechpmdevs\imageonmap\utils\ImageLoader;
use InvalidStateException;
use czechpmdevs\imageonmap\utils\PermissionDeniedException;
use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\TreeRoot;
use function array_key_exists;
Expand All @@ -46,15 +46,15 @@ trait DataProviderTrait {
*/
public function loadCachedMaps(string $path): void {
$files = glob($path . "/map_*.dat");
if (!$files) {
if(!$files) {
return;
}

$serializer = new BigEndianNbtSerializer();
foreach ($files as $file) {
foreach($files as $file) {
$content = file_get_contents($file);
if (!$content) {
throw new InvalidStateException("Could not access file $file"); // TODO - Better name
if(!$content) {
throw new PermissionDeniedException("Could not access file $file");
}

$this->cachedMaps[(int)substr(basename($file, ".dat"), 4)] = Image::load($serializer->read($content)->mustGetCompoundTag());
Expand All @@ -66,17 +66,20 @@ public function loadCachedMaps(string $path): void {
*/
public function saveCachedMaps(string $path): void {
$serializer = new BigEndianNbtSerializer();
foreach ($this->cachedMaps as $id => $map) {
if (!file_put_contents($file = "$path/map_$id.dat", $serializer->write(new TreeRoot($map->save())))) {
throw new InvalidStateException("Could not access file $file"); // TODO - Better name
foreach($this->cachedMaps as $id => $map) {
if(!file_put_contents($file = "$path/map_$id.dat", $serializer->write(new TreeRoot($map->save())))) {
throw new PermissionDeniedException("Could not access file $file");
}
}
}

public function getImageFromFile(string $file, int $chunkCount, int $xOffset, int $yOffset): int {
$id = crc32(hash_file("md5", $file) . "$chunkCount:$xOffset:$yOffset");
if (!array_key_exists($id, $this->cachedMaps)) {
$this->cachedMaps[$id] = ImageLoader::loadImage($file, $chunkCount, $xOffset, $yOffset);
/**
* @return int $id Returns id of the image loaded from file.
*/
public function getImageFromFile(string $file, int $xChunkCount, int $yChunkCount, int $xOffset, int $yOffset): int {
$id = crc32(hash_file("md5", $file) . "$xChunkCount:$yChunkCount:$xOffset:$yOffset");
if(!array_key_exists($id, $this->cachedMaps)) {
$this->cachedMaps[$id] = ImageLoader::loadImage($file, $xChunkCount, $yChunkCount, $xOffset, $yOffset);
}

return $id;
Expand Down
4 changes: 2 additions & 2 deletions src/czechpmdevs/imageonmap/ImageOnMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ protected function onDisable(): void {

public function onDataPacketReceive(DataPacketReceiveEvent $event): void {
$packet = $event->getPacket();
if (!$packet instanceof MapInfoRequestPacket) {
if(!$packet instanceof MapInfoRequestPacket) {
return;
}

if (!array_key_exists($packet->mapId, $this->cachedMaps)) {
if(!array_key_exists($packet->mapId, $this->cachedMaps)) {
$event->getOrigin()->sendDataPacket(BlankImage::get()->getPacket($packet->mapId));
$this->getLogger()->debug("Unknown map id $packet->mapId received from {$event->getOrigin()->getDisplayName()}");
return;
Expand Down
53 changes: 27 additions & 26 deletions src/czechpmdevs/imageonmap/ImagePlaceSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function __construct(
private Player $player,
private string $imageFile,
private ImageOnMap $plugin
) {}
) {
}

public function run(): void {
$this->player->sendMessage("§aBreak first ItemFrame.");
Expand All @@ -37,30 +38,30 @@ public function run(): void {

public function onBreak(BlockBreakEvent $event): void {
$player = $event->getPlayer();
if ($player->getId() != $this->player->getId()) {
if($player->getId() != $this->player->getId()) {
$player->sendMessage("{$player->getId()}:{$this->player->getId()}");
return;
}

$event->cancel();

if (!$event->getBlock()->isSameType(VanillaBlocks::ITEM_FRAME())) {
if(!$event->getBlock()->isSameType(VanillaBlocks::ITEM_FRAME())) {
$player->sendMessage("§6Block you want to place map on must be Item Frame.");
return;
}

if (!isset($this->firstPosition)) {
if(!isset($this->firstPosition)) {
$player->sendMessage("§aFirst position set to {$event->getBlock()->getPosition()->getX()}, {$event->getBlock()->getPosition()->getY()}, {$event->getBlock()->getPosition()->getZ()}. Break second block.");
$this->firstPosition = clone $event->getBlock()->getPosition();
return;
}

if ($this->firstPosition->getWorld()->getId() != $event->getBlock()->getPosition()->getWorld()->getId()) {
if($this->firstPosition->getWorld()->getId() != $event->getBlock()->getPosition()->getWorld()->getId()) {
$player->sendMessage("§cSecond positions must be in same world as the first one!");
return;
}

if (
if(
$this->firstPosition->getX() != $event->getBlock()->getPosition()->getX() &&
$this->firstPosition->getZ() != $event->getBlock()->getPosition()->getZ()
) {
Expand All @@ -76,13 +77,13 @@ public function onBreak(BlockBreakEvent $event): void {

public function onChat(PlayerChatEvent $event): void {
$player = $event->getPlayer();
if ($player->getId() != $this->player->getId()) {
if($player->getId() != $this->player->getId()) {
return;
}

$event->cancel();

if ($event->getMessage() == "cancel") {
if($event->getMessage() == "cancel") {
$player->sendMessage("§aImage placing cancelled");
$this->close();
return;
Expand All @@ -92,7 +93,7 @@ public function onChat(PlayerChatEvent $event): void {
}

public function onQuit(PlayerQuitEvent $event): void {
if ($event->getPlayer()->getId() == $this->player->getId()) {
if($event->getPlayer()->getId() == $this->player->getId()) {
$this->close();
}
}
Expand All @@ -114,15 +115,15 @@ private function finish(): void {

$world = $this->player->getPosition()->getWorld();

$getItemFrame = function (int $x, int $y, int $z) use ($world): ItemFrame {
$getItemFrame = function(int $x, int $y, int $z) use ($world): ItemFrame {
$block = $world->getBlockAt($x, $y, $z, true, false);
if ($block instanceof ItemFrame) {
if($block instanceof ItemFrame) {
return $block;
}

$world->setBlockAt($x, $y, $z, VanillaBlocks::ITEM_FRAME());
$block = $world->getBlockAt($x, $y, $z, true, false);
if (!$block instanceof ItemFrame) {
if(!$block instanceof ItemFrame) {
throw new AssumptionFailedError("Block must be item frame");
}

Expand All @@ -136,47 +137,47 @@ private function finish(): void {
$blocks = [];

$height = $maxY - $minY;
if ($minX == $maxX) {
if($minX == $maxX) {
$width = $maxZ - $minZ;
if($pattern->getFacing() == Facing::NORTH) {
for ($x = 0; $x <= $width; ++$x) {
for ($y = 0; $y <= $height; ++$y) {
for($x = 0; $x <= $width; ++$x) {
for($y = 0; $y <= $height; ++$y) {
$blocks[] = $getItemFrame($minX, $minY + $y, $minZ + $x)
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $x, $height - $y)))
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $height + 1, $x, $height - $y)))
->setHasMap(true);
}
}
} else {
for ($x = 0; $x <= $width; ++$x) {
for ($y = 0; $y <= $height; ++$y) {
for($x = 0; $x <= $width; ++$x) {
for($y = 0; $y <= $height; ++$y) {
$blocks[] = $getItemFrame($minX, $minY + $y, $maxZ - $x)
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $x, $height - $y)))
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $height + 1, $x, $height - $y)))
->setHasMap(true);
}
}
}
} else {
$width = $maxX - $minX;
if($pattern->getFacing() == Facing::SOUTH) {
for ($x = 0; $x <= $width; ++$x) {
for ($y = 0; $y <= $height; ++$y) {
for($x = 0; $x <= $width; ++$x) {
for($y = 0; $y <= $height; ++$y) {
$blocks[] = $getItemFrame($minX + $x, $minY + $y, $minZ)
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $x, $height - $y)))
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $height + 1, $x, $height - $y)))
->setHasMap(true);
}
}
} else {
for ($x = 0; $x <= $width; ++$x) {
for ($y = 0; $y <= $height; ++$y) {
for($x = 0; $x <= $width; ++$x) {
for($y = 0; $y <= $height; ++$y) {
$blocks[] = $getItemFrame($maxX - $x, $minY + $y, $minZ)
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $x, $height - $y)))
->setFramedItem(FilledMap::get()->setMapId($this->plugin->getImageFromFile($this->imageFile, $width + 1, $height + 1, $x, $height - $y)))
->setHasMap(true);
}
}
}
}

foreach ($blocks as $block) {
foreach($blocks as $block) {
$world->setBlock($block->getPosition(), $block);
}

Expand Down
43 changes: 23 additions & 20 deletions src/czechpmdevs/imageonmap/command/ImageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,38 @@ public function __construct() {
}

public function execute(CommandSender $sender, string $commandLabel, array $args) {
if (!$this->testPermission($sender)) {
if(!$this->testPermission($sender)) {
return;
}

if (!$sender instanceof Player) {
if(!$sender instanceof Player) {
$sender->sendMessage("§cThis command can be used only in game.");
return;
}

if (!isset($args[0])) {
if(!isset($args[0])) {
$sender->sendMessage("§cUsage: §7/img help");
return;
}

switch (strtolower(array_shift($args))):
switch(strtolower(array_shift($args))):
case "help":
$sender->sendMessage("§2--- §fShowing ImageOnMap Commands page 1 of 1 §2---\n" .
"§2/img help §fShows help\n" .
"§2/img list §fShows available images\n" .
"§2/img obtain <image> [<scale> <x> <y>] §fObtains an image\n" .
"§2/img obtain <image> [<xChunkCount> <yChunkCount> <x> <y>] §fObtains an image\n" .
"§2/img place <image> §fPlaces an image");
break;
case "list":
$files = [];

$pngFiles = glob($this->getOwningPlugin()->getDataFolder() . "images/*.png");
if ($pngFiles) {
if($pngFiles) {
array_push($files, ...array_map(fn(string $file) => basename($file, ".png"), $pngFiles));
}

$jpgFiles = glob($this->getOwningPlugin()->getDataFolder() . "images/*.jpg");
if ($jpgFiles) {
if($jpgFiles) {
array_push($files, ...array_map(fn(string $file) => basename($file, ".jpg"), $jpgFiles));
}

Expand All @@ -89,53 +89,56 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
break;
case "obtain":
case "o":
if (count($args) == 0) {
$sender->sendMessage("§cUsage: §7/img o <image> [<cropSize> <x> <y>]");
if(count($args) == 0) {
$sender->sendMessage("§cUsage: §7/img o <image> [<xChunkCount> <yChunkCount> <x> <y>]");
break;
}

$imageName = ImageLoader::findFile((string)array_shift($args));
if ($imageName === null) {
if($imageName === null) {
$sender->sendMessage("§cImage $imageName was not found");
break;
}

$file = $this->getOwningPlugin()->getDataFolder() . "images/$imageName";
if (count($args) >= 3) {
foreach ($args as $argument) {
if (!is_numeric($argument)) {
if(count($args) >= 4) {
foreach($args as $argument) {
if(!is_numeric($argument)) {
$sender->sendMessage("§cOnly numbers could be used to specify crop information");
break 2;
}
}

$cropSize = (int)array_shift($args);
$xChunkCount = (int)array_shift($args);
$yChunkCount = (int)array_shift($args);
$xOffset = (int)array_shift($args);
$yOffset = (int)array_shift($args);

if ($cropSize < 1) {
if($xChunkCount < 1) {
$sender->sendMessage("§cCrop size could not be lower than 0");
break;
}

if ($xOffset >= $cropSize || $yOffset >= $cropSize) {
$sender->sendMessage("§cIt is not possible to create chunk of the image with crop size $cropSize at the position of $xOffset:$yOffset");
if($xOffset >= $xChunkCount || $yOffset >= $yChunkCount) {
$sender->sendMessage("§cIt is not possible to create chunk of the image in a ration of $xChunkCount:$yChunkCount at the position of $xOffset:$yOffset");
break;
}
} else {
$cropSize = 1;
$xChunkCount = $yChunkCount = 1;
$xOffset = $yOffset = 0;

$sender->sendMessage("§6Using default values to obtain the image.");
}

$sender->getInventory()->addItem(FilledMap::get()->setMapId(ImageOnMap::getInstance()->getImageFromFile($file, $cropSize, $xOffset, $yOffset)));
$sender->getInventory()->addItem(FilledMap::get()->setMapId(ImageOnMap::getInstance()->getImageFromFile($file, $xChunkCount, $yChunkCount, $xOffset, $yOffset)));
$sender->sendMessage("§aMap successfully created from the image.");
break;
case "place":
case "p":
$sender->sendMessage("§cUsage: §7/img p <image>");

$imageName = ImageLoader::findFile((string)array_shift($args));
if ($imageName === null) {
if($imageName === null) {
$sender->sendMessage("§cImage $imageName was not found");
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/czechpmdevs/imageonmap/image/BlankImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public function getPacket(int $id): ClientboundMapItemDataPacket {
}

public static function get(): BlankImage {
if (isset(self::$blankImage)) {
if(isset(self::$blankImage)) {
return self::$blankImage;
}

$image = new BlankImage();

$image->colors = [];
for ($x = 0; $x < 128; ++$x) {
for ($y = 0; $y < 128; ++$y) {
for($x = 0; $x < 128; ++$x) {
for($y = 0; $y < 128; ++$y) {
$image->colors[$x][$y] = new Color(0, 0, 0, 0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/czechpmdevs/imageonmap/image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final protected function __construct() {
* @internal
*/
public function getPacket(int $id): ClientboundMapItemDataPacket {
if (isset($this->packetCache)) {
if(isset($this->packetCache)) {
return $this->packetCache;
}

Expand Down
2 changes: 1 addition & 1 deletion src/czechpmdevs/imageonmap/item/FilledMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function deserializeCompoundTag(CompoundTag $tag): void {

public static function get(): FilledMap {
$item = ItemFactory::getInstance()->get(ItemIds::FILLED_MAP);
if (!$item instanceof FilledMap) {
if(!$item instanceof FilledMap) {
throw new AssumptionFailedError("Item is not registered properly");
}

Expand Down
Loading

0 comments on commit 72be767

Please sign in to comment.