Working with embedded resources and the saveResource() method
Resources are files embedded in your plugin that can be extracted to the plugin’s data folder. Common uses include default configs, language files, and data files.
protected function onEnable() : void { // Saves resources/config.yml to plugins/MyPlugin/config.yml // Only saves if the file doesn't already exist $this->saveDefaultConfig();}
protected function onEnable() : void { // Save messages.yml (only if it doesn't exist) $this->saveResource("messages.yml"); // Save file from subdirectory $this->saveResource("data/items.json"); // Force overwrite existing file $this->saveResource("config.yml", true);}
Parameters:
string $filename - Path relative to resources folder
bool $replace - If true, overwrite existing file (default: false)
// Get full path to resource$path = $this->getResourcePath("data/items.json");// Read the file$contents = file_get_contents($path);$data = json_decode($contents, true);// Or use getResource() (deprecated but still works)$resource = $this->getResource("data/items.json");if($resource !== null) { $contents = stream_get_contents($resource); fclose($resource);}
getResource() is deprecated. Use getResourcePath() with standard PHP functions instead.
Read resources that have been saved to the data folder:
use pocketmine\utils\Config;protected function onEnable() : void { // Save resource first $this->saveResource("messages.yml"); // Load from data folder $messagesPath = $this->getDataFolder() . "messages.yml"; $config = new Config($messagesPath, Config::YAML); $welcome = $config->get("welcome", "Welcome!");}
# MyPlugin Configuration File# # This file is automatically generated when the plugin first loads.# You can edit this file to customize the plugin's behavior.# Enable or disable the pluginenabled: true# Maximum number of players# Min: 1, Max: 100max-players: 10# Game settingsgame: # Countdown before game starts (in seconds) countdown: 30 # Game duration (in seconds) duration: 600 # Enable friendly fire friendly-fire: false# Language (available: en_US, es_ES, fr_FR)language: en_US# Debug mode - prints additional informationdebug: false