From b30cdd3b2f79ed6eae816c9dc106f73f0622d6a9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 13 Nov 2022 00:06:12 +0300 Subject: [PATCH] a --- src/API/AchievementAPI.php | 55 ++++++++++++++++++++++++++++++++++++-- src/API/BanAPI.php | 10 ++++--- src/API/ChatAPI.php | 4 +-- src/API/ConsoleAPI.php | 14 +++++++--- src/API/PlayerAPI.php | 12 +++++---- src/API/PluginAPI.php | 5 +++- src/API/ServerAPI.php | 6 ++++- src/utils/Utils.php | 6 ++++- 8 files changed, 94 insertions(+), 18 deletions(-) diff --git a/src/API/AchievementAPI.php b/src/API/AchievementAPI.php index a1b79f7b..538dba8c 100644 --- a/src/API/AchievementAPI.php +++ b/src/API/AchievementAPI.php @@ -81,10 +81,20 @@ class AchievementAPI{ ], ]; - + + private $server; + function __construct(){ + $this->server = ServerAPI::request(); } - + + /** + * Add an achievement + * @param string $achievementId + * @param string $achievementName + * @param array $requires + * @return boolean + */ public static function addAchievement($achievementId, $achievementName, array $requires = []){ if(!isset(self::$achievements[$achievementId])){ self::$achievements[$achievementId] = [ @@ -147,6 +157,47 @@ class AchievementAPI{ } } + public function viewAchievements($cmd, $params, $issuer, $alias) + { + if(!($issuer instanceof Player) && !isset($params[0])){ + return "Please enter a nickname."; + } else{ + if(isset($params[0]) && ($this->server->api->ban->isOp($issuer->username) || ! ($issuer instanceof Player))){ + $player = $this->server->api->player->get($params[0]); + if($player instanceof Player){ + $data = $player->data; + } else{ + $data = $this->server->api->player->getOffline(trim($params[0]), false); + } + } else{ + $data = $issuer->achievements; + } + } + if($data === false){ + return "Player {$params[0]} is not found."; + } + if($data instanceof Config){ + $achs = $data->get("achievements"); + }else{ + $achs = $data; + } + + if(count($achs) <= 0){ + return "Player {$params[0]} unlocked 0 achievements"; + } + $output = "Unlocked Achievements(".count($achs)."), "; + foreach($achs as $achievement => $unlocked){ + if($unlocked && isset(self::$achievements[$achievement])){ + $info = self::$achievements[$achievement]; + $output .= "{$info["name"]}, "; + } + } + return substr($output, 0, - 2); + } + public function init(){ + $this->server->api->console->register("ach", "", [$this, "viewAchievements"]); + $this->server->api->console->alias("getplayerachievements", "ach"); + $this->server->api->console->cmdWhitelist("ach"); } } diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index 8cc5cfdd..9f1e3f40 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -14,7 +14,8 @@ class BanAPI{ private $ops; /** @var Config */ private $bannedIPs; - private $cmdWL = [];//Command WhiteList + + public $cmdWhitelist = [];//Command WhiteList function __construct(){ $this->server = ServerAPI::request(); @@ -43,10 +44,13 @@ class BanAPI{ } /** + * Whitelists a CMD so everyone can issue it - Even non OPs. + * @deprecated use ConsoleAPI::cmdWhitelist + * @see ConsoleAPI::cmdWhitelist * @param string $cmd Command to Whitelist */ - public function cmdWhitelist($cmd){//Whitelists a CMD so everyone can issue it - Even non OPs. - $this->cmdWhitelist[strtolower(trim($cmd))] = true; + public function cmdWhitelist($cmd){// + $this->server->api->console->cmdWhitelist($cmd); } /** diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 789f90a3..d656627a 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -12,8 +12,8 @@ class ChatAPI{ $this->server->api->console->register("tell", " ", [$this, "commandHandler"]); $this->server->api->console->register("me", "", [$this, "commandHandler"]); $this->server->api->console->register("say", "", [$this, "commandHandler"]); - $this->server->api->ban->cmdWhitelist("tell"); - $this->server->api->ban->cmdWhitelist("me"); + $this->server->api->console->cmdWhitelist("tell"); + $this->server->api->console->cmdWhitelist("me"); $this->server->api->console->alias("msg", "tell"); } diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 6da024c4..568a1f5a 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -22,10 +22,18 @@ class ConsoleAPI{ $this->register("difficulty", "<0|1|2|3>", [$this, "defaultCommands"]); $this->register("stop", "", [$this, "defaultCommands"]); $this->register("defaultgamemode", "", [$this, "defaultCommands"]); - $this->server->api->ban->cmdWhitelist("help"); - $this->server->api->ban->cmdWhitelist("status"); + $this->cmdWhitelist("help"); + $this->cmdWhitelist("status"); } - + + /** + * Whitelists a CMD so everyone can issue it - Even non OPs. + * @param string $cmd Command to Whitelist + */ + public function cmdWhitelist($cmd){ + $this->server->api->ban->cmdWhitelist[strtolower(trim($cmd))] = true; + } + public function register($cmd, $help, $callback){ if(!is_callable($callback)){ return false; diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index f939a5d4..8a10eea1 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -23,9 +23,9 @@ class PlayerAPI{ $this->server->api->console->alias("who", "list"); $this->server->api->console->alias("suicide", "kill"); $this->server->api->console->alias("tppos", "tp"); - $this->server->api->ban->cmdWhitelist("list"); - $this->server->api->ban->cmdWhitelist("ping"); - $this->server->api->ban->cmdWhitelist("spawn"); + $this->server->api->console->cmdWhitelist("list"); + $this->server->api->console->cmdWhitelist("ping"); + $this->server->api->console->cmdWhitelist("spawn"); $this->server->preparedSQL->selectPlayersToHeal = $this->server->database->prepare("SELECT EID FROM entities WHERE class = " . ENTITY_PLAYER . " AND health < 20;"); } @@ -360,7 +360,7 @@ class PlayerAPI{ } } - public function getOffline($name){ + public function getOffline($name, $create = true){ $iname = strtolower($name); $default = [ "caseusername" => $name, @@ -387,10 +387,12 @@ class PlayerAPI{ ]; if(!file_exists(DATA_PATH . "players/" . $iname . ".yml")){ - if($this->server->extraprops->get("save-player-data")){ + if($this->server->extraprops->get("save-player-data") && $create){ console("[NOTICE] Player data not found for \"" . $iname . "\", creating new profile"); $data = new Config(DATA_PATH . "players/" . $iname . ".yml", CONFIG_YAML, $default); $data->save(); + }else{ + return false; } } diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index a5c935ab..d743fa7b 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -25,7 +25,10 @@ class PluginAPI extends stdClass{ $this->randomNonce = Utils::getRandomBytes(16, false); $this->server->api->console->register("plugins", "", [$this, "commandHandler"]); $this->server->api->console->register("version", "", [$this, "commandHandler"]); - $this->server->api->ban->cmdWhitelist("version"); + $this->server->api->console->cmdWhitelist("version"); + if($this->server->extraprops->get("query-plugins")){ //allow players to also view plugins ingame, since query makes names public + $this->server->api->console->cmdWhitelist("plugins"); + } $this->server->api->console->alias("pl", "plugins"); $this->server->api->console->alias("ver", "version"); $this->server->api->console->alias("about", "version"); diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 2361285d..17bd5b0f 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -44,6 +44,10 @@ class ServerAPI{ * @var TileAPI */ public $tile; + /** + * @var AchievementAPI + */ + public $achievement; private $asyncCalls = []; private $server; private $config; @@ -144,7 +148,6 @@ class ServerAPI{ console("[INFO] NostalgiaCore is distributed under the LGPL License", true, true, 0); $this->loadProperties(); - $this->loadAPI("console", "ConsoleAPI"); $this->loadAPI("level", "LevelAPI"); $this->loadAPI("block", "BlockAPI"); @@ -155,6 +158,7 @@ class ServerAPI{ $this->loadAPI("player", "PlayerAPI"); $this->loadAPI("time", "TimeAPI"); $this->loadAPI("queryAPI", "QueryAPI"); + $this->loadAPI("achievement", "AchievementAPI"); foreach($this->apiList as $ob){ if(is_callable([$ob, "init"])){ $ob->init(); //Fails sometimes!!! diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 9428b75d..a58183bb 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -39,6 +39,7 @@ class Utils{ return $id; } +<<<<<<< HEAD public static function getSign($v){ return $v > 0 ? 1 : ($v < 0 ? -1 : 0); } @@ -47,8 +48,10 @@ class Utils{ return floor(($v % 360 + 360) % 360); } +======= +>>>>>>> 1042e025 (look player achievements, moved method cmdWhitelist to ConsoleAPI(marked as deprecated in BanAPI to avoid losing compability with old pmmp plugins)) public static function hasEmoji($s){ - return preg_match(Utils::emojiRegex, $s, $matches); + return preg_match(Utils::emojiRegex, $s); } public static function getCallableIdentifier(callable $variable){ if(is_array($variable)){ @@ -81,6 +84,7 @@ class Utils{ } } } + public static function getUniqueID($raw = false, $extra = ""){ $machine = php_uname("a"); $machine .= file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : "";