-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish SimpleGameMode plugin v1.0.0
- Loading branch information
Showing
4 changed files
with
207 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace ImmoDevs\SimpleGameMode; | ||
|
||
use pocketmine\command\CommandSender; | ||
use pocketmine\command\Command; | ||
use pocketmine\player\GameMode; | ||
use pocketmine\player\Player; | ||
use pocketmine\plugin\PluginBase; | ||
use pocketmine\utils\TextFormat; | ||
use pocketmine\lang\Translatable; | ||
|
||
class Main extends PluginBase { | ||
|
||
private array $configMessages; | ||
|
||
public function onEnable(): void | ||
{ | ||
$this->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(). " <gamemode>"); | ||
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", | ||
}; | ||
} | ||
} |