From 95dbf588c554f9d74c750af8a1a6e7ec1212dbc8 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 13 Oct 2022 13:38:34 +0300 Subject: [PATCH] structure class added, phar plugins support --- src/API/PluginAPI.php | 40 ++++++++++++++++++++-- src/Player.php | 6 +--- src/dependencies.php | 4 ++- src/math/Vector3.php | 8 ++++- src/phar/IClassLoader.php | 7 ++++ src/phar/PharUtils.php | 33 ++++++++++++++++++ src/structure/Structure.php | 68 +++++++++++++++++++++++++++++++++++++ src/utils/Utils.php | 4 ++- src/world/Level.php | 2 +- 9 files changed, 160 insertions(+), 12 deletions(-) create mode 100644 src/phar/IClassLoader.php create mode 100644 src/phar/PharUtils.php create mode 100644 src/structure/Structure.php diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index ceb2d1c2..2aaac142 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -11,13 +11,13 @@ interface Plugin{ public function init(); - public function __destruct(); + //public function __destruct(); useless } class PluginAPI extends stdClass{ private $server; - private $plugins = []; + public $plugins = []; //specially for DevTools private $randomNonce; public function __construct(){ @@ -138,9 +138,43 @@ class PluginAPI extends stdClass{ while(false !== ($file = $dir->read())){ if($file[0] !== "."){ $ext = strtolower(substr($file, -3)); + $ext2 = strtolower(substr($file, -4)); if($ext === "php" or $ext === "pmf"){ $this->load($dir->path . $file); } + if($ext2 === "phar"){ + $pluginInfo = []; //TODO: A PluginInfo class? + $filePath = $this->pluginsPath().$file; + $p = new Phar($this->pluginsPath().$file, 0); + foreach (new RecursiveIteratorIterator($p) as $file) { + $name = $file->getFileName(); + $content = file_get_contents($file->getPathName()); + if($name === "plugin.cfg"){ + $pluginInfo = PharUtils::readMainConfig($content); + break; + } + } + console("[INFO] Loading {$pluginInfo["name"]} by {$pluginInfo["author"]}..."); + console("[WARNING] PHAR Plugin format is experemental and might cause bugs."); + + $phr = "phar://$filePath/"; + include($phr."/src/".$pluginInfo["classLoader"]); + $class = $pluginInfo["CLClass"]; + $loader = new $class(); + $loader->loadAll($phr); + + $pluginName = PharUtils::getNameSpaceClass($pluginInfo["mainFile"]); + include($phr."/src/".$pluginInfo["mainFile"]); + $plugin = new $pluginName($this->server->api, false); + if(!($plugin instanceof Plugin)){ + console("[ERROR] Plugin \"" . $pluginInfo["name"] . "\" doesn't use the Plugin Interface"); + $plugin->__destruct(); + unset($plugin); + continue; + } + $identifier = $this->getIdentifier($pluginInfo["name"], $pluginInfo["author"]); + $this->plugins[$identifier] = [$plugin, $pluginInfo]; + } } } } @@ -286,4 +320,4 @@ class DummyPlugin implements Plugin{ public function __destruct(){ } -} +} \ No newline at end of file diff --git a/src/Player.php b/src/Player.php index 3c0295da..0035550f 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1471,11 +1471,7 @@ class Player{ $this->server->schedule(5, [$this->entity, "update"], [], true); $this->server->schedule(2, [$this->entity, "updateMovement"], [], true); $this->sendArmor(); - $array = explode("@n", (string)$this->server->motd); - foreach($array as $msg){ - $this->sendChat($msg."\n"); - } - + $this->sendChat($this->server->motd . "\n"); $this->sendInventory(); $this->sendSettings(); diff --git a/src/dependencies.php b/src/dependencies.php index 21110b9c..01edcccb 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -96,6 +96,8 @@ require_once(FILE_PATH . "/src/material/Item.php"); require_once(FILE_PATH . "/src/material/item/armor/ArmorItem.php"); +require_once(FILE_PATH . "/src/structure/Structure.php"); + require_all(FILE_PATH . "src/"); $inc = get_included_files(); @@ -115,4 +117,4 @@ define("SOURCE_SHA1SUM", bin2hex($sha1sum)); if(!file_exists(DATA_PATH . "server.properties") and arg("no-wizard", false) != true){ $installer = new Installer(); } -/***REM_END***/ +/***REM_END***/ \ No newline at end of file diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 2cfc331e..665f7af2 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -9,7 +9,13 @@ class Vector3{ $this->y = $y; $this->z = $z; } - + + public function setXYZ($x, $y, $z){ + $this->x = $x; + $this->y = $y; + $this->z = $z; + } + public function getFloorX(){ return (int) $this->x; } diff --git a/src/phar/IClassLoader.php b/src/phar/IClassLoader.php new file mode 100644 index 00000000..391042da --- /dev/null +++ b/src/phar/IClassLoader.php @@ -0,0 +1,7 @@ + $line){ + if(!strpos($line, "=")){ + continue; + } + + $line = explode("=", $line); + $content[$line[0]] = $line[1]; + } + + $pluginData["name"] = $content["name"]; + $pluginData["description"] = $content["description"]; + $pluginData["version"] = $content["version"]; + $pluginData["author"] = $content["author"]; + $pluginData["mainFile"] = $content["mainFile"]; + $pluginData["api"] = $content["api"]; + $pluginData["classLoader"] = $content["classLoader"]; + $pluginData["CLClass"] = self::getNameSpaceClass($pluginData["classLoader"]); + return $pluginData; + } + + public static function getNameSpaceClass($content){ + return substr(str_replace("/", "\\", $content), 0, -4); + } + + +} \ No newline at end of file diff --git a/src/structure/Structure.php b/src/structure/Structure.php new file mode 100644 index 00000000..99b25432 --- /dev/null +++ b/src/structure/Structure.php @@ -0,0 +1,68 @@ +structure = $struct; + $this->mapSymToID = $symToID; + $this->pm = ServerAPI::request(); + $this->api = $this->pm->api; + $this->width = $width; + $this->radius = $width / 2; + + } + + public function addMapping($sym, $id){ + $this->mapSymToID[$sym] = $id; + } + public function getMappingFor($sym){ + return isset($this->mapSymToID[$sym]) ? $this->mapSymToID[$sym] : Structure::MAP_NO_KEY; + } + + protected function placeBlock($level, $sym, &$tv){ + console("@@@"); + if($level instanceof Level){ + $idm = $this->getMappingFor($sym); + var_dump($idm); + if($idm === Structure::MAP_NO_KEY){ + console("Failed to receive id"); + return false; + } + $block = $this->api->block->get($idm[0], $idm[1], $tv); //TODO: meta + console($block); + $level->setBlock($tv, $block, true, false, true); + } + + } + + public function build($level, $centerX, $centerY, $centerZ){ + console("b"); + $tempVector = new Vector3(0,0,0); + $x = $centerX - $this->radius; + $z = $centerZ - $this->radius; + foreach($this->structure as $offsetY => $blocksXZ){ + foreach($blocksXZ as $blocks){ + foreach(str_split($blocks) as $block){ + $tempVector->setXYZ($x, $centerY + $offsetY, $z); + console("6#&_+7$:373-"); + $this->placeBlock($level, $block, $tempVector); + ++$x; + } + ++$z; + $x = $centerX - $this->radius; + } + $z = $centerZ - $this->radius; + } + } + +} \ No newline at end of file diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 7d70a65b..e5a8582c 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -38,6 +38,8 @@ class Utils{ } return $id; } + + public static function hasEmoji($s){ return preg_match(Utils::emojiRegex, $s, $matches); } @@ -670,4 +672,4 @@ class Utils{ //if(Utils::isOnline() === false){ //Utils::$online = false; -//} +//} \ No newline at end of file diff --git a/src/world/Level.php b/src/world/Level.php index 1d634876..8bc8f5e4 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -532,4 +532,4 @@ class Level{ } return $this->server->api->block->scheduleBlockUpdate($pos, $delay, $type); } -} +} \ No newline at end of file