level properties, moved some settings from server.properties, cmd placeholders

This commit is contained in:
GameHerobrine 2026-04-28 13:57:03 +03:00
parent 581a6a8fb2
commit cf4dc73a6c
10 changed files with 165 additions and 30 deletions

View file

@ -74,7 +74,9 @@ class BanAPI{
if(!$this->isOp($player->iusername) && $player->level->getName() === $this->server->api->level->getDefault()){
$t = new Vector2($data["target"]->x, $data["target"]->z);
$s = new Vector2($this->server->spawn->x, $this->server->spawn->z);
if($t->distance($s) <= $this->server->api->getProperty("spawn-protection") and $this->server->api->dhandle($event . ".spawn", $data) !== true){
$sp = $player->entity->level->getProperty("spawn-protection");
if($t->distance($s) <= $sp and $this->server->api->dhandle($event . ".spawn", $data) !== true){
return false;
}
}
@ -84,7 +86,9 @@ class BanAPI{
if(!$this->isOp($player->iusername) && $player->level->getName() === $this->server->api->level->getDefault()){
$t = new Vector2($data["block"]->x, $data["block"]->z);
$s = new Vector2($this->server->spawn->x, $this->server->spawn->z);
if($t->distance($s) <= $this->server->api->getProperty("spawn-protection") and $this->server->api->dhandle($event . ".spawn", $data) !== true){
$sp = $player->entity->level->getProperty("spawn-protection");
if($t->distance($s) <= $sp and $this->server->api->dhandle($event . ".spawn", $data) !== true){
return false;
}
}

View file

@ -7,13 +7,9 @@ class EntityAPI{
public $entities;
private $server;
private $eCnt = 1;
private $serverSpawnAnimals, $serverSpawnMobs;
function __construct(){
$this->entities = [];
$this->server = ServerAPI::request();
$this->serverSpawnAnimals = $this->server->api->getProperty("spawn-animals");
$this->serverSpawnMobs = $this->server->api->getProperty("spawn-mobs");
}
public function init(){

View file

@ -13,6 +13,28 @@ class LevelAPI{
}
public function init(){
//TODO per-level difficulty, interact, random-ticking
$this->registerProperty("difficulty", 2, desc: "WIP for now, Difficulty level. 0 - Peaceful, 1 - Easy, 2 - Normal, 3 - Hard");
$this->registerProperty("mobs-amount", 50, desc: "Max amount of mobs that can be spawned.");
$this->registerProperty("spawn-animals", true, desc: "Spawn animals.");
$this->registerProperty("spawn-monsters", true, desc: "Spawn monsters.");
$this->registerProperty("pvp", true, desc: "Enable or disable pvp in the level");
$this->registerProperty("random-ticking", "subchunk", desc: [
"WIP", "Affects how random ticking works. Available modes:",
"subchunk - tick 16x16x16 areas",
"fullchunk - tick 16x16x128 areas",
"vanilla - tick 256x256x128 area"
]);
$this->registerProperty("interact", true, desc: "WIP, Enable or disable ability to interact with the level");
$this->registerProperty("spawn-protection", 16, desc: "Prevent player interactions <n> blocks around the spawn.");
$this->server->api->console->register("setworldprop", "[world] <property> <value>", [$this, "commandHandler"]);
$this->server->api->console->register("getworldprop", "[world] <property>", [$this, "commandHandler"]);
$this->server->api->console->register("reloadworldprops", "[world]", [$this, "commandHandler"]);
$this->server->api->console->register("createworld", "<worldname> [type] [seed]", [$this, "commandHandler"]);
$this->server->api->console->register("loadworld", "<worldname>", [$this, "commandHandler"]);
$this->server->api->console->register("seed", "[world]", [$this, "commandHandler"]);
$this->server->api->console->register("save-all", "", [$this, "commandHandler"]);
$this->server->api->console->register("save-on", "", [$this, "commandHandler"]);
@ -26,6 +48,49 @@ class LevelAPI{
}
$this->server->spawn = $this->getDefault()->getSafeSpawn();
}
private $defaultLevelProperties = [];
private $levelPropsInfo = [];
/**
* Registers a level property
*
* @param string $name - property name
* @param mixed $defaultValue - default property value
* @param array[string]|string|boolean $desc=false - property description(false=none, string=single line, array of strings=multiline)
* @return false if the property exists, true if success
*/
public function registerProperty(string $name, $defaultValue, $desc = false){
if(isset($this->defaultLevelProperties[$name])) return false;
$this->defaultLevelProperties[$name] = $defaultValue;
if(is_array($desc) || is_string($desc)) $this->levelPropsInfo[$name] = $desc;
return true;
}
/**
* Gets a default value for property
* @param string $name - property name
* @param mixed $fail=false - return value if the property doesn't exist
* @return string|mixed
*/
public function getDefaultProperty(string $name, $fail=false){
return $this->defaultLevelProperties[$name] ?? $fail;
}
/**
* Loads a properties for a level
* @param Level $level
*/
public function loadLevelProperties(Level $level){
$propsPath = DATA_PATH . "worlds/{$level->getName()}/properties.yml";
$level->levelProperties = new Config($propsPath, CONFIG_YAML, $this->defaultLevelProperties, comments: $this->levelPropsInfo);
foreach($level->levelProperties->getAll() as $k => $v){
$level->setProperty($k, $v);
}
}
public function loadLevel($name){
if($this->get($name) !== false){
@ -50,14 +115,16 @@ class LevelAPI{
}
}
$entities = new Config($path . "entities.yml", CONFIG_YAML);
if(file_exists($path . "tileEntities.yml")){
@rename($path . "tileEntities.yml", $path . "tiles.yml");
}
$tiles = new Config($path . "tiles.yml", CONFIG_YAML);
$blockUpdates = new Config($path . "bupdates.yml", CONFIG_YAML);
$this->levels[$name] = new Level($level, $entities, $tiles, $blockUpdates, $name);
$this->levels[$name] = $lvl = new Level($level, $entities, $tiles, $blockUpdates, $name);
$this->loadLevelProperties($lvl);
foreach($entities->getAll() as $entity){
if(!isset($entity["id"])){
break;
@ -82,15 +149,12 @@ class LevelAPI{
]);
}elseif($entity["id"] === FALLING_SAND){
$e = $this->server->api->entity->add($this->levels[$name], ENTITY_FALLING, $entity["id"], $entity);
//$e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]);
$e->setHealth($entity["Health"]);
}elseif(Utils::getEntityTypeByID($entity["id"]) === ENTITY_OBJECT){ //Object
$e = $this->server->api->entity->add($this->levels[$name], ENTITY_OBJECT, $entity["id"], $entity);
//$e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]);
$e->setHealth(1);
}else{
$e = $this->server->api->entity->add($this->levels[$name], ENTITY_MOB, $entity["id"], $entity);
//$e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]);
$e->setHealth($entity["Health"]);
}
}
@ -166,6 +230,21 @@ class LevelAPI{
public function commandHandler($cmd, $params, $issuer, $alias){
$output = "";
switch($cmd){
case "setworldprop":
return "WIP";
case "getworldprop":
return "WIP";
case "reloadworldprops":
return "WIP";
case "createworld":
return "WIP";
case "loadworld":
return "WIP";
case "setwspawn":
if(!($issuer instanceof Player)){
return ("Please run this command in-game. ");

View file

@ -116,18 +116,18 @@ class ServerAPI{
"memory-limit" => "128M",
"white-list" => false,
"announce-player-achievements" => true,
"spawn-protection" => 16,
//"spawn-protection" => 16,
"view-distance" => 10,
"max-players" => 20,
"allow-flight" => true,
"spawn-animals" => true,
"spawn-mobs" => true,
"mobs-amount" => 50,
//"spawn-animals" => true,
//"spawn-mobs" => true,
//"mobs-amount" => 50,
"gamemode" => SURVIVAL,
"hardcore" => false,
"pvp" => true,
"difficulty" => 2,
//"pvp" => true,
"generator-settings" => "",
"difficulty" => 2,
"level-name" => "world",
"level-seed" => "",
"level-type" => "DEFAULT",
@ -140,7 +140,14 @@ class ServerAPI{
"ticking-mode" => "legacy",
"mushroom-spread" => Block::$mushroomSpread
], comments: [
"level-name" => [
"Default level name"
],
"level-seed" => [
"Default level seed"
],
"level-type" => [
"Default level type",
"Alowed types:",
"FLAT - flat world",
"DEFAULT - use one of the 4 pregenerated worlds from pocketmine",
@ -190,7 +197,6 @@ class ServerAPI{
ConsoleAPI::warn("Fly checking is enabled! Players may experience issues with kicking while not flying!");
}
$this->parseProperties();
MobSpawner::$MOB_LIMIT = $this->getProperty("mobs-amount", 50);
Entity::$allowedAI = $this->getProperty("enable-mob-ai", true);
PocketMinecraftServer::$PACKET_READING_LIMIT = $this->getProperty("abort-reading-after-N-packets", PocketMinecraftServer::$PACKET_READING_LIMIT);
PocketMinecraftServer::$TICKING_MODE = match(strtolower($this->getProperty("ticking-mode"))){
@ -202,8 +208,7 @@ class ServerAPI{
define("DEBUG", $this->getProperty("debug", 1));
define("ADVANCED_CACHE", false);
//define("MAX_CHUNK_RATE", 20 / $this->getProperty("max-chunks-per-second", 8)); //Default rate ~512 kB/s
MobSpawner::$spawnAnimals = $this->getProperty("spawn-animals");
MobSpawner::$spawnMobs = $this->getProperty("spawn-mobs");
if($this->getProperty("upnp-forwarding")){
console("[INFO] [UPnP] Trying to port forward...");
UPnP_PortForward($this->getProperty("server-port"));

View file

@ -19,7 +19,8 @@ class PocketMinecraftServer{
public static $TICKING_MODE = PocketMinecraftServer::TICK_LEGACY;
public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread;
public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $stop, $asyncThread;
public $difficulty;
public static $KEEP_CHUNKS_LOADED = true, $PACKET_READING_LIMIT = 100;
public static $BLOCK_BREAKING_PROGRESS = 0.8;
public static $ENABLE_LIGHT_UPDATES = true;

View file

@ -1078,7 +1078,7 @@ class Entity extends Position
public function interactWith(Entity $e, $action)
{
if($this->isPlayer() and ($this->server->api->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($this->player->gamemode & 0x01) === 0x01)){
if($this->isPlayer() and ($this->level->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($this->player->gamemode & 0x01) === 0x01)){
return false;
}

View file

@ -88,6 +88,7 @@ LICENSE;
$config->set("max-players", (int) $this->getInput(self::DEFAULT_PLAYERS));
echo "[*] ".$this->lang->spawn_protection_info."\n";
echo "[?] ".$this->lang->spawn_protection." (Y/n): ";
//TODO remove spawn prot from here
if(strtolower($this->getInput("y")) == "n"){
$config->set("spawn-protection", -1);
}else{

View file

@ -1,6 +1,7 @@
<?php
/**
* Small class to hopefully optimize entities a bit
* A class that allows to get block properties without having to create a block instance.
* Should be used whenever is possible.
*/
class StaticBlock
{
@ -9,6 +10,8 @@ class StaticBlock
private static $NULL_BOUNDS;
public static $prealloc = [];
//default pmmp block props
public static $isSolid = [];
public static $isTransparent = [];
public static $isFlowable = [];
@ -17,6 +20,8 @@ class StaticBlock
public static $hasPhysics = [];
public static $isLiquid = [];
public static $isFullBlock = [];
public static $material = [];
public static $hardness = [];
public static $destroyTime = [];

View file

@ -6,6 +6,10 @@ class Level{
* @var Config
*/
public $entities;
/**
* @var Config
*/
public $levelProperties;
/**
* This is an array of entities in this world.
* @var Entity[]
@ -27,7 +31,7 @@ class Level{
public $queuedBlockUpdates = [];
public $forceDisableBlockQueue = false;
public static $randomUpdateBlocks = [
FIRE => true,
FARMLAND => true,
@ -73,6 +77,35 @@ class Level{
$this->randInt2 = 0x3C6EF35F;
}
/**
* Gets level property.
*
* @param string $name - property name
* @param mixed $fail=false - return value if the property doesn't exist
* @return string|mixed
*/
public function getProperty(string $name, $fail = false){
return $this->levelProperties->exists($name) ? $this->levelProperties->get($name) : $this->server->api->level->getDefaultProperty($name, $fail);
}
/**
* Sets level property
*
* @param string $name - property name
* @param mixed $value - new property value
*
* @return boolean true if sucess, false if failed
*/
public function setProperty(string $name, $value){
$c = $this->server->api->dhandle("level.property.change", ["level" => $this, "name" => $name, "value" => $value]);
if($c === false) return false;
$this->levelProperties->set($name, $value);
$this->levelProperties->save();
return true;
}
public function close(){
if(isset($this->level)){
$this->save(true, true, true, true);

View file

@ -1,13 +1,12 @@
<?php
class MobSpawner{
public static $spawnAnimals = false, $spawnMobs = false, $maxMobsNearPlayerAtOnce = 10;
public static $maxMobsNearPlayerAtOnce = 10;
private $server;
public $level;
public $totalMobsPerPlayer;
public $playerAffectedEIDS = [];
public $entityAffectedPlayers = [];
public static $MOB_LIMIT = 50;
public function __construct(Level $level){
$this->server = ServerAPI::request();
@ -42,11 +41,13 @@ class MobSpawner{
}
public function handle(){
if($this->countEntities() > self::$MOB_LIMIT || count($this->level->players) <= 0){
$limit = $this->level->getProperty("mobs-amount");
if($this->countEntities() > $limit || count($this->level->players) <= 0){
return false; //not spawning
}
$svd = $this->totalMobsPerPlayer;
$this->totalMobsPerPlayer = min(ceil(self::$MOB_LIMIT / count($this->level->players)), self::$maxMobsNearPlayerAtOnce);
$this->totalMobsPerPlayer = min(ceil($limit / count($this->level->players)), self::$maxMobsNearPlayerAtOnce);
if($svd != $this->totalMobsPerPlayer) ConsoleAPI::debug("Changed total mobs per player from $svd to {$this->totalMobsPerPlayer}.");
return $this->spawnMobs();
@ -54,11 +55,11 @@ class MobSpawner{
public function spawnMobs(){
$phase = $this->server->api->time->getPhase($this->level);
if(self::$spawnAnimals && ($phase == "day" || $phase == "sunrise")){ //Animal
if($this->level->getProperty("spawn-animals") && ($phase == "day" || $phase == "sunrise")){ //Animal
$type = mt_rand(10, 13);
$baby = false;
$grassOnly = true;
}elseif(self::$spawnMobs && ($phase == "night" || $phase == "sunset") && $this->server->difficulty > 0){ //Monster, true night
}elseif($this->level->getProperty("spawn-monsters") && ($phase == "night" || $phase == "sunset") && $this->server->difficulty > 0){ //Monster, true night
$type = mt_rand(32, 35);
$grassOnly = false;
$baby = 2;
@ -145,5 +146,15 @@ class MobSpawner{
return empty($allowed) ? -1 : $allowed[mt_rand(0, count($allowed) - 1)];
}
/**
* @deprecated
* use $level->getProperty("mobs-amount")
*/
public static $MOB_LIMIT = 50;
/**
* @deprecated
* use $level->getProperty("spawn-animals") and $level->getProperty("spawn-monsters")
*/
public static $spawnAnimals = true, $spawnMobs = true;
}