PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
ServerConfigGroup.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine;
25
27use function array_key_exists;
28use function getopt;
29use function is_bool;
30use function is_int;
31use function is_string;
32use function strtolower;
33
39 private array $propertyCache = [];
40
41 public function __construct(
42 private Config $pocketmineYml,
43 private Config $serverProperties
44 ){}
45
46 public function getProperty(string $variable, mixed $defaultValue = null) : mixed{
47 if(!array_key_exists($variable, $this->propertyCache)){
48 $v = getopt("", ["$variable::"]);
49 if(isset($v[$variable])){
50 $this->propertyCache[$variable] = $v[$variable];
51 }else{
52 $this->propertyCache[$variable] = $this->pocketmineYml->getNested($variable);
53 }
54 }
55
56 return $this->propertyCache[$variable] ?? $defaultValue;
57 }
58
59 public function getPropertyBool(string $variable, bool $defaultValue) : bool{
60 return (bool) $this->getProperty($variable, $defaultValue);
61 }
62
63 public function getPropertyInt(string $variable, int $defaultValue) : int{
64 return (int) $this->getProperty($variable, $defaultValue);
65 }
66
67 public function getPropertyString(string $variable, string $defaultValue) : string{
68 return (string) $this->getProperty($variable, $defaultValue);
69 }
70
71 public function getConfigString(string $variable, string $defaultValue = "") : string{
72 $v = getopt("", ["$variable::"]);
73 if(isset($v[$variable])){
74 return (string) $v[$variable];
75 }
76
77 return $this->serverProperties->exists($variable) ? (string) $this->serverProperties->get($variable) : $defaultValue;
78 }
79
80 public function setConfigString(string $variable, string $value) : void{
81 $this->serverProperties->set($variable, $value);
82 }
83
84 public function getConfigInt(string $variable, int $defaultValue = 0) : int{
85 $v = getopt("", ["$variable::"]);
86 if(isset($v[$variable])){
87 return (int) $v[$variable];
88 }
89
90 return $this->serverProperties->exists($variable) ? (int) $this->serverProperties->get($variable) : $defaultValue;
91 }
92
93 public function setConfigInt(string $variable, int $value) : void{
94 $this->serverProperties->set($variable, $value);
95 }
96
97 public function getConfigBool(string $variable, bool $defaultValue = false) : bool{
98 $v = getopt("", ["$variable::"]);
99 if(isset($v[$variable])){
100 $value = $v[$variable];
101 }else{
102 $value = $this->serverProperties->exists($variable) ? $this->serverProperties->get($variable) : $defaultValue;
103 }
104 if(is_bool($value)){
105 return $value;
106 }
107 if(is_int($value)){
108 return $value !== 0;
109 }
110 if(is_string($value)){
111 switch(strtolower($value)){
112 case "on":
113 case "true":
114 case "1":
115 case "yes":
116 return true;
117 }
118 }
119
120 return false;
121 }
122
123 public function setConfigBool(string $variable, bool $value) : void{
124 $this->serverProperties->set($variable, $value ? "1" : "0");
125 }
126
127 public function save() : void{
128 if($this->serverProperties->hasChanged()){
129 $this->serverProperties->save();
130 }
131 if($this->pocketmineYml->hasChanged()){
132 $this->pocketmineYml->save();
133 }
134 }
135}