Skip to content

Instantly share code, notes, and snippets.

@Steellgold
Last active November 25, 2023 15:40
Show Gist options
  • Save Steellgold/45ebb76f0ff0d1f624dd1d6c593ce86a to your computer and use it in GitHub Desktop.
Save Steellgold/45ebb76f0ff0d1f624dd1d6c593ce86a to your computer and use it in GitHub Desktop.
Recover a unicode to use it. (From my private project)
<?php
namespace your/namespace;
use JsonException;
use pocketmine\utils\Config;
// use your/plugin/Main
class Unicode {
/**
* @throws JsonException
*/
public function __construct(string $file) {
if (!is_dir(Main::getInstance()->getDataFolder() . "unicodes")) mkdir(Main::getInstance()->getDataFolder() . "unicodes");
foreach ($config as $code) {
$this->glyph(basename($file, ".png"), new Config(Main::getInstance()->getDataFolder() . "unicodes/{$file}.yml", Config::YAML), 256);
}
}
/**
* @param string $resource
* @param Config $config
* @param int $grid
* @return void
* @throws JsonException
*/
private function glyph(string $resource, Config $config, int $grid = 16): void {
$GRID = $grid;
$vv = $this->getArr($resource, $GRID);
for ($a = 1; ; $a++) {
if ($a > 256) {
break;
}
$config->set($a, $vv[$a]);
}
$config->save();
}
/**
* @param string $filename
* @param int $GRID
* @return array
*/
private function getArr(string $filename, int $GRID): array {
$startChar = hexdec(substr($filename, strrpos($filename, "_") + 1) . "00");
$g = 0;
$i = 0;
do {
$g++;
$ci = $startChar + $i;
$char = mb_chr($ci);
$vv[$g] = $char;
} while (++$i < $GRID ** 2);
return $vv;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment