Skip to content

Commit

Permalink
Implemented #42 default settings for worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
jarne committed Jul 31, 2019
1 parent 54b4c13 commit e8baa85
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 119 deletions.
3 changes: 3 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ permissions:
worlds.admin.unset:
description: "Unset parameters"
default: op
worlds.admin.defaults:
description: "Set / unset world default values"
default: op
worlds.admin.build:
description: "Allow building in protected worlds"
default: op
Expand Down
30 changes: 20 additions & 10 deletions resources/languages/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# \ V V / (_) | | | | (_| \__ \
# \_/\_/ \___/|_| |_|\__,_|___/
#
# (C) 2018 by surva network | https://github.com/survanetwork
# (C) 2019 by surva network | https://github.com/survanetwork

# German language

Expand All @@ -30,16 +30,16 @@ create:

# Remove command
remove:
success: "§aDie Welt §f{name} §awurde erfolgreich gelöscht."
success: "§aDie Welt §f{name} §awurde erfolgreich gelöscht."

# Copy command
copy:
success: "§aDie Welt §f{name} §wurde erfolgreich nach §f{to} §akopiert."
same: "§cDu kannst eine Welt nicht in sich selbst kopieren."
datfile:
notexist: "§cDie DAT-Datei der Welt konnte nicht gefunden werden."
damaged: "§cDie DAT-Datei der Welt ist beschädigt."
notsave: "§cDie aktualisierte DAT-Datei der Welt konnte nicht gespeichert werden."
success: "§aDie Welt §f{name} §wurde erfolgreich nach §f{to} §akopiert."
same: "§cDu kannst eine Welt nicht in sich selbst kopieren."
datfile:
notexist: "§cDie DAT-Datei der Welt konnte nicht gefunden werden."
damaged: "§cDie DAT-Datei der Welt ist beschädigt."
notsave: "§cDie aktualisierte DAT-Datei der Welt konnte nicht gespeichert werden."

# Rename command
rename:
Expand Down Expand Up @@ -70,8 +70,8 @@ teleport:
set:
success: "§aDu hast §b{key} §ain §f{world} §aauf §c{value} §agesetzt."
list:
values: "§Einstellungen für die Welt §f{name}\n\n§eberechtigung: {permission}\n§egamemode: {gamemode}\n§ebauen: {build}\n§epvp: {pvp}\n§eschaden: {damage}\n§einteragieren: {interact}\n§eexplosionen: {explode}\n§edrop: {drop}\n§ehunger: {hunger}\n§efliegen: {fly}\n"
notset: "§7nicht gesetzt"
values: "§Einstellungen für die Welt §f{name}\n\n§eberechtigung: {permission}\n§egamemode: {gamemode}\n§ebauen: {build}\n§epvp: {pvp}\n§eschaden: {damage}\n§einteragieren: {interact}\n§eexplosionen: {explode}\n§edrop: {drop}\n§ehunger: {hunger}\n§efliegen: {fly}\n"
notset: "§7nicht gesetzt"
permission:
notdefault: "§cDu kannst keine Berechtigung für die Standard-Welt setzen."
gamemode:
Expand All @@ -81,3 +81,13 @@ set:
# Unset command
unset:
success: "§b{key} §ain §f{world} §awurde zurückgesetzt."

# Defaults command
defaults:
list:
values: "§Einstellungen für Welten auf diesem Server\n\n§eberechtigung: {permission}\n§egamemode: {gamemode}\n§ebauen: {build}\n§epvp: {pvp}\n§eschaden: {damage}\n§einteragieren: {interact}\n§eexplosionen: {explode}\n§edrop: {drop}\n§ehunger: {hunger}\n§efliegen: {fly}\n"
notset: "§7nicht gesetzt"
set:
success: "§aDu hast §b{key} §aauf §c{value} §aals Standard-Wert gesetzt."
unset:
success: "§b{key} §awurde als Standard-Wert zurückgesetzt."
26 changes: 18 additions & 8 deletions resources/languages/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# \ V V / (_) | | | | (_| \__ \
# \_/\_/ \___/|_| |_|\__,_|___/
#
# (C) 2018 by surva network | https://github.com/survanetwork
# (C) 2019 by surva network | https://github.com/survanetwork

# English language

Expand All @@ -30,16 +30,16 @@ create:

# Remove command
remove:
success: "§aWorld §f{name} §ahas been removed."
success: "§aWorld §f{name} §ahas been removed."

# Copy command
copy:
success: "§aWorld §f{name} §ahas been copied to §f{to}§a."
same: "§cYou can't copy the world into itself."
datfile:
notexist: "§cThe world's data file does not exist."
damaged: "§cThe world's data file is damaged."
notsave: "§cFailed saving the updated dat file of the world."
success: "§aWorld §f{name} §ahas been copied to §f{to}§a."
same: "§cYou can't copy the world into itself."
datfile:
notexist: "§cThe world's data file does not exist."
damaged: "§cThe world's data file is damaged."
notsave: "§cFailed saving the updated dat file of the world."

# Rename command
rename:
Expand Down Expand Up @@ -81,3 +81,13 @@ set:
# Unset command
unset:
success: "§b{key} §ahas been unset in §f{world}§a."

# Defaults command
defaults:
list:
values: "§cDefault settings for worlds on this server\n\n§epermission: {permission}\n§egamemode: {gamemode}\n§ebuild: {build}\n§epvp: {pvp}\n§edamage: {damage}\n§einteract: {interact}\n§eexplode: {explode}\n§edrop: {drop}\n§ehunger: {hunger}\n§efly: {fly}\n"
notset: "§7not set"
set:
success: "§aYou've set §b{key} §ato §c{value} §aas default value."
unset:
success: "§b{key} §ahas been unset as default value."
17 changes: 17 additions & 0 deletions src/surva/worlds/Worlds.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use surva\worlds\commands\CopyCommand;
use surva\worlds\commands\CreateCommand;
use surva\worlds\commands\CustomCommand;
use surva\worlds\commands\DefaultsCommand;
use surva\worlds\commands\ListCommand;
use surva\worlds\commands\LoadCommand;
use surva\worlds\commands\RemoveCommand;
Expand All @@ -19,6 +20,7 @@
use surva\worlds\commands\TeleportCommand;
use surva\worlds\commands\UnloadCommand;
use surva\worlds\commands\UnsetCommand;
use surva\worlds\types\Defaults;
use surva\worlds\types\World;
use surva\worlds\utils\ArrayList;
use pocketmine\plugin\PluginBase;
Expand All @@ -27,6 +29,9 @@
use pocketmine\utils\Config;

class Worlds extends PluginBase {
/* @var Defaults */
private $defaults;

/* @var ArrayList */
private $worlds;

Expand All @@ -37,6 +42,8 @@ public function onEnable() {
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
$this->saveDefaultConfig();

$this->defaults = new Defaults($this, $this->getCustomConfig($this->getDataFolder() . "defaults.yml"));

$this->worlds = new ArrayList();

foreach($this->getServer()->getLevels() as $level) {
Expand Down Expand Up @@ -102,6 +109,9 @@ public function getCustomCommand(string $name): ?CustomCommand {
case "unset":
case "ust":
return new UnsetCommand($this, "unset", "worlds.admin.unset");
case "defaults":
case "df":
return new DefaultsCommand($this, "defaults", "worlds.admin.defaults");
default:
return null;
}
Expand Down Expand Up @@ -240,4 +250,11 @@ public function getMessages(): Config {
public function getWorlds(): ArrayList {
return $this->worlds;
}

/**
* @return Defaults
*/
public function getDefaults(): Defaults {
return $this->defaults;
}
}
159 changes: 159 additions & 0 deletions src/surva/worlds/commands/DefaultsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
/**
* Worlds | defaults set / unset command
*/

namespace surva\worlds\commands;

use pocketmine\Player;
use pocketmine\Server;
use pocketmine\utils\TextFormat;

class DefaultsCommand extends CustomCommand {
public function do(Player $player, array $args) {
$defaults = $this->getWorlds()->getDefaults();

if(count($args) === 0) {
$player->sendMessage(
$this->getWorlds()->getMessage(
"defaults.list.values",
array(
"permission" => $this->formatText($defaults->getPermission()),
"gamemode" => $this->formatGamemode($defaults->getGamemode()),
"build" => $this->formatBool($defaults->getBuild()),
"pvp" => $this->formatBool($defaults->getPvp()),
"damage" => $this->formatBool($defaults->getDamage()),
"interact" => $this->formatBool($defaults->getInteract()),
"explode" => $this->formatBool($defaults->getExplode()),
"drop" => $this->formatBool($defaults->getDrop()),
"hunger" => $this->formatBool($defaults->getHunger()),
"fly" => $this->formatBool($defaults->getFly())
)
)
);

return true;
}

switch($args[0]) {
case "set":
if(!(count($args) === 3)) {
return false;
}

if(!(in_array(
$args[1],
array("permission", "gamemode", "build", "pvp", "damage", "interact", "explode", "drop", "hunger", "fly")
))) {
return false;
}

if($args[1] === "permission") {
$player->sendMessage($this->getWorlds()->getMessage("set.permission.notdefault"));

return true;
} elseif($args[1] === "gamemode") {
if(($args[2] = Server::getGamemodeFromString($args[1])) === -1) {
$player->sendMessage($this->getWorlds()->getMessage("set.gamemode.notexist"));

return true;
}

$defaults->updateValue($args[1], $args[2]);

$player->sendMessage(
$this->getWorlds()->getMessage(
"defaults.set.success",
array("key" => $args[1], "value" => $args[2])
)
);
} else {
if(!(in_array($args[2], array("true", "false")))) {
$player->sendMessage($this->getWorlds()->getMessage("set.notbool", array("key" => $args[1])));

return true;
}

$defaults->updateValue($args[1], $args[2]);

$player->sendMessage(
$this->getWorlds()->getMessage(
"defaults.set.success",
array("key" => $args[1], "value" => $args[2])
)
);
}
return true;
case "unset":
if(!(count($args) === 2)) {
return false;
}

if(!(in_array(
$args[1],
array("permission", "gamemode", "build", "pvp", "damage", "interact", "explode", "drop", "hunger", "fly")
))) {
return false;
}

$defaults->removeValue($args[1]);

$player->sendMessage(
$this->getWorlds()->getMessage(
"defaults.unset.success",
array("key" => $args[1])
)
);

return true;
default:
return false;
}
}

/**
* Format a text for showing its value
*
* @param string|null $value
* @return string
*/
private function formatText(?string $value): string {
if($value === null) {
return $this->getWorlds()->getMessage("set.list.notset");
}

return TextFormat::WHITE . $value;
}

/**
* Format a gamemode for showing its value
*
* @param int|null $value
* @return string
*/
private function formatGamemode(?int $value): string {
if($value === null) {
return $this->getWorlds()->getMessage("set.list.notset");
}

return $this->getWorlds()->getServer()->getLanguage()->translateString(
TextFormat::WHITE . Server::getGamemodeString($value)
);
}

/**
* Format a boolean for showing its value
*
* @param bool|null $value
* @return string
*/
private function formatBool(?bool $value): string {
if($value === true) {
return TextFormat::GREEN . "true";
} elseif($value === false) {
return TextFormat::RED . "false";
} else {
return $this->getWorlds()->getMessage("set.list.notset");
}
}
}
Loading

0 comments on commit e8baa85

Please sign in to comment.