structure class added, phar plugins support

This commit is contained in:
GameHerobrine 2022-10-13 13:38:34 +03:00
parent bc75dd9efa
commit 95dbf588c5
9 changed files with 160 additions and 12 deletions

View file

@ -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(){
}
}
}

View file

@ -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();

View file

@ -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***/

View file

@ -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;
}

View file

@ -0,0 +1,7 @@
<?php
/*
Used in PHAR plugins to load all custom classes
*/
interface IClassLoader{
public function loadAll($pharPath);
}

33
src/phar/PharUtils.php Normal file
View file

@ -0,0 +1,33 @@
<?php
class PharUtils{
public static function readMainConfig($content){
$pluginData = [];
$content = explode("\n", $content);
foreach($content as $id => $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);
}
}

View file

@ -0,0 +1,68 @@
<?php
/*XYZ in Structure String:
|--→x+
|
↓z+
*/
abstract class Structure{
const MAP_NO_KEY = -255;
const LEVEL_RSV1 = 1;
private $structure;
public $api, $pm;
public $mapSymToID, $width, $radius;
public function __construct($struct, $width, $symToID = []){
$this->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;
}
}
}

View file

@ -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;
//}
//}

View file

@ -532,4 +532,4 @@ class Level{
}
return $this->server->api->block->scheduleBlockUpdate($pos, $delay, $type);
}
}
}