// Use nested structure$playerData = $this->getConfig()->getNested("players.{$player->getName()}");// Or use string literal for better analysis$data = $this->getConfig()->get("players")[$player->getName()] ?? null;
class Main extends PluginBase { public function onEnable(): void { // Save default config.yml from resources/ $this->saveDefaultConfig(); // Access config $config = $this->getConfig(); }}
// Set a value$this->getConfig()->set("last-update", time());// Set nested value$this->getConfig()->setNested("database.port", 3306);// Remove a key$this->getConfig()->remove("old-setting");// Must save to persist changes$this->saveConfig();
use pocketmine\plugin\PluginBase;class Main extends PluginBase { private bool $economyEnabled; private int $startingBalance; public function onEnable(): void { // Save default config if it doesn't exist $this->saveDefaultConfig(); // Load settings $this->economyEnabled = $this->getConfig()->getNested( "features.economy.enabled", true ); $this->startingBalance = $this->getConfig()->getNested( "features.economy.starting-balance", 100 ); // Validate config if ($this->startingBalance < 0) { $this->getLogger()->warning("Invalid starting balance, using default"); $this->startingBalance = 100; } } public function isEconomyEnabled(): bool { return $this->economyEnabled; } public function getStartingBalance(): int { return $this->startingBalance; } public function updateLastSave(): void { $this->getConfig()->set("last-save", date("Y-m-d H:i:s")); $this->saveConfig(); }}
class ConfigManager { public function __construct( private Config $config ) {} public function getMaxPlayers(): int { return (int) $this->config->get("max-players", 20); } public function isDebugEnabled(): bool { return (bool) $this->config->get("debug", false); } public function getDatabaseHost(): string { return (string) $this->config->getNested("database.host", "localhost"); } public function getBlockedItems(): array { $items = $this->config->get("blocked-items", []); return is_array($items) ? $items : []; }}