PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/ConfigurationManager.java

https://gitlab.com/igserfurtmcschulserver/CustomWorldGuard
Java | 296 lines | 177 code | 36 blank | 83 comment | 18 complexity | 4c061162c569c664886027abb6ba3ee1 MD5 | raw file
  1. /*
  2. * WorldGuard, a suite of tools for Minecraft
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldGuard team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.bukkit;
  20. import com.google.common.collect.ImmutableMap;
  21. import com.sk89q.util.yaml.YAMLFormat;
  22. import com.sk89q.util.yaml.YAMLProcessor;
  23. import com.sk89q.worldguard.protection.managers.storage.DriverType;
  24. import com.sk89q.worldguard.protection.managers.storage.RegionDriver;
  25. import com.sk89q.worldguard.protection.managers.storage.file.DirectoryYamlDriver;
  26. import com.sk89q.worldguard.protection.managers.storage.sql.SQLDriver;
  27. import com.sk89q.worldguard.session.handler.WaterBreathing;
  28. import com.sk89q.worldguard.util.report.Unreported;
  29. import com.sk89q.worldguard.util.sql.DataSourceConfig;
  30. import org.bukkit.World;
  31. import org.bukkit.entity.Player;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.util.HashMap;
  35. import java.util.Map;
  36. import java.util.concurrent.ConcurrentHashMap;
  37. import java.util.concurrent.ConcurrentMap;
  38. import java.util.logging.Logger;
  39. /**
  40. * Represents the global configuration and also delegates configuration
  41. * for individual worlds.
  42. *
  43. * @author sk89q
  44. * @author Michael
  45. */
  46. public class ConfigurationManager {
  47. private static final Logger log = Logger.getLogger(ConfigurationManager.class.getCanonicalName());
  48. private static final String CONFIG_HEADER = "#\r\n" +
  49. "# WorldGuard's main configuration file\r\n" +
  50. "#\r\n" +
  51. "# This is the global configuration file. Anything placed into here will\r\n" +
  52. "# be applied to all worlds. However, each world has its own configuration\r\n" +
  53. "# file to allow you to replace most settings in here for that world only.\r\n" +
  54. "#\r\n" +
  55. "# About editing this file:\r\n" +
  56. "# - DO NOT USE TABS. You MUST use spaces or Bukkit will complain. If\r\n" +
  57. "# you use an editor like Notepad++ (recommended for Windows users), you\r\n" +
  58. "# must configure it to \"replace tabs with spaces.\" In Notepad++, this can\r\n" +
  59. "# be changed in Settings > Preferences > Language Menu.\r\n" +
  60. "# - Don't get rid of the indents. They are indented so some entries are\r\n" +
  61. "# in categories (like \"enforce-single-session\" is in the \"protection\"\r\n" +
  62. "# category.\r\n" +
  63. "# - If you want to check the format of this file before putting it\r\n" +
  64. "# into WorldGuard, paste it into http://yaml-online-parser.appspot.com/\r\n" +
  65. "# and see if it gives \"ERROR:\".\r\n" +
  66. "# - Lines starting with # are comments and so they are ignored.\r\n" +
  67. "#\r\n";
  68. @Unreported private WorldGuardPlugin plugin;
  69. @Unreported private ConcurrentMap<String, WorldConfiguration> worlds;
  70. @Unreported private YAMLProcessor config;
  71. private boolean hasCommandBookGodMode = false;
  72. public boolean useRegionsCreatureSpawnEvent;
  73. public boolean activityHaltToggle = false;
  74. public boolean useGodPermission;
  75. public boolean useGodGroup;
  76. public boolean useAmphibiousGroup;
  77. public boolean usePlayerMove;
  78. public boolean usePlayerTeleports;
  79. public boolean deopOnJoin;
  80. public boolean blockInGameOp;
  81. public boolean migrateRegionsToUuid;
  82. public boolean keepUnresolvedNames;
  83. @Unreported public Map<String, String> hostKeys = new HashMap<String, String>();
  84. /**
  85. * Region Storage Configuration method, and config values
  86. */
  87. @Unreported public RegionDriver selectedRegionStoreDriver;
  88. @Unreported public Map<DriverType, RegionDriver> regionStoreDriverMap;
  89. /**
  90. * Construct the object.
  91. *
  92. * @param plugin The plugin instance
  93. */
  94. public ConfigurationManager(WorldGuardPlugin plugin) {
  95. this.plugin = plugin;
  96. this.worlds = new ConcurrentHashMap<String, WorldConfiguration>();
  97. }
  98. /**
  99. * Get the folder for storing data files and configuration.
  100. *
  101. * @return the data folder
  102. */
  103. public File getDataFolder() {
  104. return plugin.getDataFolder();
  105. }
  106. /**
  107. * Get the folder for storing data files and configuration for each
  108. * world.
  109. *
  110. * @return the data folder
  111. */
  112. public File getWorldsDataFolder() {
  113. return new File(getDataFolder(), "worlds");
  114. }
  115. /**
  116. * Load the configuration.
  117. */
  118. @SuppressWarnings("unchecked")
  119. public void load() {
  120. // Create the default configuration file
  121. plugin.createDefaultConfiguration(
  122. new File(plugin.getDataFolder(), "config.yml"), "config.yml");
  123. config = new YAMLProcessor(new File(plugin.getDataFolder(), "config.yml"), true, YAMLFormat.EXTENDED);
  124. try {
  125. config.load();
  126. } catch (IOException e) {
  127. log.severe("Error reading configuration for global config: ");
  128. e.printStackTrace();
  129. }
  130. config.removeProperty("suppress-tick-sync-warnings");
  131. migrateRegionsToUuid = config.getBoolean("regions.uuid-migration.perform-on-next-start", true);
  132. keepUnresolvedNames = config.getBoolean("regions.uuid-migration.keep-names-that-lack-uuids", true);
  133. useRegionsCreatureSpawnEvent = config.getBoolean("regions.use-creature-spawn-event", true);
  134. useGodPermission = config.getBoolean("auto-invincible", config.getBoolean("auto-invincible-permission", false));
  135. useGodGroup = config.getBoolean("auto-invincible-group", false);
  136. useAmphibiousGroup = config.getBoolean("auto-no-drowning-group", false);
  137. config.removeProperty("auto-invincible-permission");
  138. usePlayerMove = config.getBoolean("use-player-move-event", true);
  139. usePlayerTeleports = config.getBoolean("use-player-teleports", true);
  140. deopOnJoin = config.getBoolean("security.deop-everyone-on-join", false);
  141. blockInGameOp = config.getBoolean("security.block-in-game-op-command", false);
  142. hostKeys = new HashMap<String, String>();
  143. Object hostKeysRaw = config.getProperty("host-keys");
  144. if (hostKeysRaw == null || !(hostKeysRaw instanceof Map)) {
  145. config.setProperty("host-keys", new HashMap<String, String>());
  146. } else {
  147. for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) hostKeysRaw).entrySet()) {
  148. String key = String.valueOf(entry.getKey());
  149. String value = String.valueOf(entry.getValue());
  150. hostKeys.put(key.toLowerCase(), value);
  151. }
  152. }
  153. // ====================================================================
  154. // Region store drivers
  155. // ====================================================================
  156. boolean useSqlDatabase = config.getBoolean("regions.sql.use", false);
  157. String sqlDsn = config.getString("regions.sql.dsn", "jdbc:mysql://localhost/worldguard");
  158. String sqlUsername = config.getString("regions.sql.username", "worldguard");
  159. String sqlPassword = config.getString("regions.sql.password", "worldguard");
  160. String sqlTablePrefix = config.getString("regions.sql.table-prefix", "");
  161. DataSourceConfig dataSourceConfig = new DataSourceConfig(sqlDsn, sqlUsername, sqlPassword, sqlTablePrefix);
  162. SQLDriver sqlDriver = new SQLDriver(dataSourceConfig);
  163. DirectoryYamlDriver yamlDriver = new DirectoryYamlDriver(getWorldsDataFolder(), "regions.yml");
  164. this.regionStoreDriverMap = ImmutableMap.<DriverType, RegionDriver>builder()
  165. .put(DriverType.MYSQL, sqlDriver)
  166. .put(DriverType.YAML, yamlDriver)
  167. .build();
  168. this.selectedRegionStoreDriver = useSqlDatabase ? sqlDriver : yamlDriver;
  169. // Load configurations for each world
  170. for (World world : plugin.getServer().getWorlds()) {
  171. get(world);
  172. }
  173. config.setHeader(CONFIG_HEADER);
  174. }
  175. /**
  176. * Unload the configuration.
  177. */
  178. public void unload() {
  179. worlds.clear();
  180. }
  181. public void disableUuidMigration() {
  182. config.setProperty("regions.uuid-migration.perform-on-next-start", false);
  183. if (!config.save()) {
  184. log.severe("Error saving configuration!");
  185. }
  186. }
  187. /**
  188. * Get the configuration for a world.
  189. *
  190. * @param world The world to get the configuration for
  191. * @return {@code world}'s configuration
  192. */
  193. public WorldConfiguration get(World world) {
  194. String worldName = world.getName();
  195. WorldConfiguration config = worlds.get(worldName);
  196. WorldConfiguration newConfig = null;
  197. while (config == null) {
  198. if (newConfig == null) {
  199. newConfig = new WorldConfiguration(plugin, worldName, this.config);
  200. }
  201. worlds.putIfAbsent(world.getName(), newConfig);
  202. config = worlds.get(world.getName());
  203. }
  204. return config;
  205. }
  206. /**
  207. * Check to see if god mode is enabled for a player.
  208. *
  209. * @param player The player to check
  210. * @return Whether the player has godmode through WorldGuard or CommandBook
  211. */
  212. public boolean hasGodMode(Player player) {
  213. return plugin.getSessionManager().get(player).isInvincible(player);
  214. }
  215. /**
  216. * Enable amphibious mode for a player.
  217. *
  218. * @param player The player to enable amphibious mode for
  219. */
  220. public void enableAmphibiousMode(Player player) {
  221. WaterBreathing handler = plugin.getSessionManager().get(player).getHandler(WaterBreathing.class);
  222. if (handler != null) {
  223. handler.setWaterBreathing(true);
  224. }
  225. }
  226. /**
  227. * Disable amphibious mode for a player.
  228. *
  229. * @param player The player to disable amphibious mode for
  230. */
  231. public void disableAmphibiousMode(Player player) {
  232. WaterBreathing handler = plugin.getSessionManager().get(player).getHandler(WaterBreathing.class);
  233. if (handler != null) {
  234. handler.setWaterBreathing(false);
  235. }
  236. }
  237. /**
  238. * Check to see if amphibious mode is enabled for a player.
  239. *
  240. * @param player The player to check
  241. * @return Whether {@code player} has amphibious mode
  242. */
  243. public boolean hasAmphibiousMode(Player player) {
  244. WaterBreathing handler = plugin.getSessionManager().get(player).getHandler(WaterBreathing.class);
  245. return handler != null && handler.hasWaterBreathing();
  246. }
  247. public void updateCommandBookGodMode() {
  248. try {
  249. if (plugin.getServer().getPluginManager().isPluginEnabled("CommandBook")) {
  250. Class.forName("com.sk89q.commandbook.GodComponent");
  251. hasCommandBookGodMode = true;
  252. return;
  253. }
  254. } catch (ClassNotFoundException ignore) {}
  255. hasCommandBookGodMode = false;
  256. }
  257. public boolean hasCommandBookGodMode() {
  258. return hasCommandBookGodMode;
  259. }
  260. }