From 1e43f14acc69a1b5bcdf8246e409de0039a6ac86 Mon Sep 17 00:00:00 2001 From: Unreall <128391210+ImmoDevs@users.noreply.github.com> Date: Sun, 2 Jun 2024 19:10:30 +0000 Subject: [PATCH] Publish SimpleGameMode plugin v1.0.0 --- README.md | 67 +++++++++------ plugin.yml | 39 +++++++++ resources/config.yml | 9 ++ src/ImmoDevs/SimpleGameMode/Main.php | 119 +++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 27 deletions(-) create mode 100644 plugin.yml create mode 100644 resources/config.yml create mode 100644 src/ImmoDevs/SimpleGameMode/Main.php diff --git a/README.md b/README.md index 59261d8..e83dad8 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,34 @@ -# SimpleGamemode +# SimpleGameMode -![SimpleGamemode](https://img.shields.io/badge/SimpleGamemode-v1.0.0-brightgreen.svg) -![PocketMine-MP](https://img.shields.io/badge/PocketMine--MP-5.0.0-blue.svg) -![License](https://img.shields.io/badge/License-MIT-yellow.svg) +[![SimpleGameMode](https://img.shields.io/badge/SimpleGamemode-v1.0.0-brightgreen.svg)](https://github.com/ImmoDevs/SimpleGameMode) +[![PocketMine-MP](https://img.shields.io/badge/PocketMine--MP-5.0.0-blue.svg)](https://github.com/pmmp/PocketMine-MP) +[![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/ImmoDevs/SimpleGameMode/blob/main/LICENSE) -**SimpleGamemode** is a PocketMine-MP plugin that simplifies changing gamemodes with shorter commands. Easily switch between creative, survival, adventure, and spectator modes, and target other players or use selectors for flexibility. +**SimpleGameMode** is a sleek and powerful PocketMine-MP plugin designed to streamline gamemode switching with intuitive and shortened commands. Effortlessly toggle between creative, survival, adventure, and spectator modes. Target specific players or utilize player selectors for maximum flexibility. -## Features +## ✨ Features -- **Simplified Commands**: Shortened commands for quick gamemode changes. -- **Player Targeting**: Change gamemodes for other players using direct names or selectors (`@a`, `@s`, `@p`). -- **Permission Control**: Restrict command usage based on permissions. +- **✂️ Simplified Commands**: Shortened commands for quick gamemode changes. +- **🎯 Player Targeting**: Change gamemodes for other players using direct names, partial names, or selectors (`@a`, `@s`). +- **🔐 Permission Control**: Restrict command usage based on permissions. +- **⚙️ Configurable Messages**: Customize success messages for gamemode changes via the config file. -## Commands +## 📜 Commands -| Command | Description | Permission |Usage | +| Command | Description | Permission | Usage | |---------|------------------------------------|-----------------------|----------------------------| | `/gmc` | Change to Creative mode | `simplegamemode.gmc` | `/gmc [player/@selector]` | | `/gms` | Change to Survival mode | `simplegamemode.gms` | `/gms [player/@selector]` | | `/gma` | Change to Adventure mode | `simplegamemode.gma` | `/gma [player/@selector]` | | `/gmsp` | Change to Spectator mode | `simplegamemode.gmsp` | `/gmsp [player/@selector]` | -## Installation +## 🚀 Installation -1. Download the `SimpleGamemode.phar` file from the [releases page](#). -2. Place the `SimpleGamemode.phar` file into the `plugins` directory of your PocketMine-MP server. +1. Download the `SimpleGameMode.phar` file from the [releases page](https://github.com/ImmoDevs/SimpleGamemode/releases). +2. Place the `SimpleGameMode.phar` file into the `plugins` directory of your PocketMine-MP server. 3. Restart your server to load the plugin. -## Permissions +## 🔐 Permissions | Permission | Description | Default | |-----------------------|-----------------------------------|---------| @@ -36,26 +37,38 @@ | `simplegamemode.gma` | Allows the use of `/gma` command | `op` | | `simplegamemode.gmsp` | Allows the use of `/gmsp` command | `op` | -## Usage Examples +## 💡 Usage Examples - Change your own gamemode to Creative: - ``` + ```sh /gmc | /gmc @s ``` - Change another player's gamemode to Survival: - ``` + ```sh /gms PlayerName ``` - Change all players' gamemode to Adventure: - ``` + ```sh /gma @a ``` -- Change the nearest player's gamemode to Spectator: - ``` - /gmsp @p - ``` -## Building from Source +## ⚙️ Configuration + +Customize the success messages in `config.yml`: +```yaml +# Configuration for SimpleGameMode Plugin + +# Messages to display when a player's gamemode is changed +messages: + # Message sent to the command sender when they successfully change another player's gamemode + player-gamemode-change: "Successfully changed {player}'s gamemode to {gamemode}." + + # Message sent to the player when they change their own gamemode + personal-gamemode-change: "Gamemode changed to {gamemode}." + +``` + +## 🛠️ Building from Source 1. Clone the repository: ```bash @@ -70,11 +83,11 @@ zip -r SimpleGamemode.phar . -x "*.git*" -x "*.md" -x "tests/*" ``` -## Contributing +## 🤝 Contributing -Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/ImmoDevs/SimpleGamemode). +Contributions are welcome! Please open an [Issues](https://github.com/ImmoDevs/SimpleGameMode/issues) or submit a [Pull Request](https://github.com/ImmoDevs/SimpleGameMode/pulls). -## License +## 📄 License This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..15cc237 --- /dev/null +++ b/plugin.yml @@ -0,0 +1,39 @@ +name: SimpleGameMode +main: ImmoDevs\SimpleGameMode\Main +version: 1.0.0 +api: [5.0.0] +author: ImmoDevs +website: https://github.com/SimpleGameMode +description: A simple plugin to change gamemodes with shorter commands. + +commands: + gmc: + description: Change to Creative mode. + usage: /gmc [player] + permission: simplegamemode.gmc + gms: + description: Change to Survival mode. + usage: /gms [player] + permission: simplegamemode.gms + gma: + description: Change to Adventure mode. + usage: /gma [player] + permission: simplegamemode.gma + gmsp: + description: Change to Spectator mode. + usage: /gmsp [player] + permission: simplegamemode.gmsp + +permissions: + simplegamemode.gmc: + description: Allows the use of /gmc command. + default: op + simplegamemode.gms: + description: Allows the use of /gms command. + default: op + simplegamemode.gma: + description: Allows the use of /gma command. + default: op + simplegamemode.gmsp: + description: Allows the use of /gmsp command. + default: op \ No newline at end of file diff --git a/resources/config.yml b/resources/config.yml new file mode 100644 index 0000000..21b006e --- /dev/null +++ b/resources/config.yml @@ -0,0 +1,9 @@ +# Configuration for SimpleGamemode Plugin + +# Messages to display when a player's gamemode is changed +messages: + # Message sent to the command sender when they successfully change another player's gamemode + player-gamemode-change: "§f[§cSimpleGameMode§f] §6You have §aSuccessfully changed §{player}'s §6gamemode §fto §{gamemode}." + + # Message sent to the player when they change their own gamemode + personal-gamemode-change: "§f[§cSimpleGameMode§f] §6Your gamemode has been changed to §c{gamemode}." diff --git a/src/ImmoDevs/SimpleGameMode/Main.php b/src/ImmoDevs/SimpleGameMode/Main.php new file mode 100644 index 0000000..d348f0a --- /dev/null +++ b/src/ImmoDevs/SimpleGameMode/Main.php @@ -0,0 +1,119 @@ +saveDefaultConfig(); + $this->configMessages = $this->getConfig()->get("messages", []); + $this->getLogger()->info(TextFormat::GREEN . "SimpleGameMode By ImmoDevs enabled"); + } + + public function onDisable(): void + { + $this->getLogger()->info(TextFormat::RED . "SimpleGameMode By ImmoDevs disabled"); + } + + public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool + { + if (!$sender->hasPermission("simplegamemode." . $command->getName())) { + $sender->sendMessage(TextFormat::RED . "You do not have permission to use this command."); + return false; + } + + if (count($args) < 1) { + $sender->sendMessage(TextFormat::RED. "Usage: /". $command->getName(). " "); + return false; + } + + if (!$sender instanceof Player && count($args) === 0) { + $sender->sendMessage(TextFormat::RED. "This command can only be used in-game or with a player/selector."); + return false; + } + + $targetPlayers = [$sender]; + if (count($args) > 0) { + $targetPlayers = $this->resolvePlayer($sender, $args[0]); + if(empty($targetPlayers)) { + $sender->sendMessage(TextFormat::RED . "No players matched the given selector."); + return false; + } + } elseif ($sender instanceof Player) { + $targetPlayers = [$sender]; + } else { + $sender->sendMessage(TextFormat::RED . "You must specify a player or selector when using this command from console."); + return false; + } + + $gamemode = match ($command->getName()) { + "gmc" => GameMode::CREATIVE(), + "gms" => GameMode::SURVIVAL(), + "gma" => GameMode::ADVENTURE(), + "gmsp" => GameMode::SPECTATOR(), + default => null, + }; + + if ($gamemode === null) { + return false; + } + + foreach ($targetPlayers as $player) { + $player->setGamemode($gamemode); + $player->sendMessage($this->configMessages["personal-gamemode-change"]); + if ($sender !== $player) { + $senderMessage = str_replace( + ["{player}", "{gamemode}"], + [$player->getName(), $this->getGamemodeName($gamemode)], + $this->configMessages["player-gamemode-change"] + ); + $sender->sendMessage($senderMessage); + } + } + + return true; + } + + private function resolvePlayer(CommandSender $sender, string $selector): array + { + $server = $this->getServer(); + switch ($selector) { + case "all": + return $server->getOnlinePlayers(); + case "@a": + return $server->getOnlinePlayers(); + case "@s": + return [$sender]; + default: + $selector = strtolower($selector); + $matchedPlayers = []; + foreach ($server->getOnlinePlayers() as $player) { + if (strpos(strtolower($player->getName()), $selector) === 0) { + $matchedPlayers[] = $player; + } + } + return $matchedPlayers; + } + } + + private function getGameModeName(GameMode $gamemode): string { + return match ($gamemode) { + GameMode::CREATIVE() => "Creative", + GameMode::SURVIVAL() => "Survival", + GameMode::ADVENTURE() => "Adventure", + GameMode::SPECTATOR() => "Spectator", + default => "Unknown", + }; + } +} \ No newline at end of file