/src/main/java/com/sk89q/worldguard/protection/GlobalRegionManager.java

https://github.com/HaxtorMoogle/worldguard · Java · 302 lines · 141 code · 43 blank · 118 comment · 15 complexity · f38fa900647f75c9192c6835e55caa58 MD5 · raw file

  1. // $Id$
  2. /*
  3. * WorldGuard
  4. * Copyright (C) 2010 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the 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,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.protection;
  20. import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.util.HashMap;
  25. import java.util.logging.Logger;
  26. import org.bukkit.Location;
  27. import org.bukkit.World;
  28. import org.bukkit.block.Block;
  29. import org.bukkit.entity.Player;
  30. import com.sk89q.worldguard.LocalPlayer;
  31. import com.sk89q.worldguard.bukkit.BukkitUtil;
  32. import com.sk89q.worldguard.bukkit.ConfigurationManager;
  33. import com.sk89q.worldguard.bukkit.WorldConfiguration;
  34. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
  35. import com.sk89q.worldguard.protection.databases.YAMLDatabase;
  36. import com.sk89q.worldguard.protection.flags.StateFlag;
  37. import com.sk89q.worldguard.protection.managers.FlatRegionManager;
  38. import com.sk89q.worldguard.protection.managers.RegionManager;
  39. /**
  40. * This class keeps track of region information for every world. It loads
  41. * world region information as needed.
  42. *
  43. * @author sk89q
  44. * @author Redecouverte
  45. */
  46. public class GlobalRegionManager {
  47. private static final Logger logger = Logger.getLogger("Minecraft.WorldGuard");
  48. /**
  49. * Reference to the plugin.
  50. */
  51. private WorldGuardPlugin plugin;
  52. /**
  53. * Reference to the global configuration.
  54. */
  55. private ConfigurationManager config;
  56. /**
  57. * Map of managers per-world.
  58. */
  59. private HashMap<String, RegionManager> managers;
  60. /**
  61. * Stores the list of modification dates for the world files. This allows
  62. * WorldGuard to reload files as needed.
  63. */
  64. private HashMap<String, Long> lastModified;
  65. /**
  66. * Construct the object.
  67. *
  68. * @param plugin
  69. */
  70. public GlobalRegionManager(WorldGuardPlugin plugin) {
  71. this.plugin = plugin;
  72. config = plugin.getGlobalStateManager();
  73. managers = new HashMap<String, RegionManager>();
  74. lastModified = new HashMap<String, Long>();
  75. }
  76. /**
  77. * Unload region information.
  78. */
  79. public void unload() {
  80. managers.clear();
  81. lastModified.clear();
  82. }
  83. /**
  84. * Get the path for a world's regions file.
  85. *
  86. * @param name
  87. * @return
  88. */
  89. protected File getPath(String name) {
  90. return new File(plugin.getDataFolder(),
  91. "worlds" + File.separator + name + File.separator + "regions.yml");
  92. }
  93. /**
  94. * Unload region information for a world.
  95. *
  96. * @param name
  97. */
  98. public void unload(String name) {
  99. RegionManager manager = managers.get(name);
  100. if (manager != null) {
  101. managers.remove(name);
  102. lastModified.remove(name);
  103. }
  104. }
  105. /**
  106. * Unload all region information.
  107. */
  108. public void unloadAll() {
  109. managers.clear();
  110. lastModified.clear();
  111. }
  112. /**
  113. * Load region information for a world.
  114. *
  115. * @param world
  116. * @return
  117. */
  118. public RegionManager load(World world) {
  119. String name = world.getName();
  120. File file = getPath(name);
  121. try {
  122. // Create a manager
  123. RegionManager manager = new FlatRegionManager(new YAMLDatabase(file));
  124. managers.put(name, manager);
  125. manager.load();
  126. logger.info("WorldGuard: " + manager.getRegions().size()
  127. + " regions loaded for '" + name + "'");
  128. // Store the last modification date so we can track changes
  129. lastModified.put(name, file.lastModified());
  130. return manager;
  131. } catch (FileNotFoundException e) {
  132. logger.warning("WorldGuard: Region file for world \""
  133. + name + "\" missing or inaccessible.");
  134. } catch (IOException e) {
  135. logger.warning("WorldGuard: Failed to load regions from file "
  136. + file.getAbsolutePath() + " : " + e.getMessage());
  137. } catch (Exception e) {
  138. logger.warning("WorldGuard: Error loading regions for world \""
  139. + name + "\":" + e.getMessage());
  140. }
  141. // @TODO: THIS CREATES PROBLEMS!!
  142. return null;
  143. }
  144. /**
  145. * Preloads region managers for all worlds.
  146. */
  147. public void preload() {
  148. // Load regions
  149. for (World world : plugin.getServer().getWorlds()) {
  150. load(world);
  151. }
  152. }
  153. /**
  154. * Reloads the region information from file when region databases
  155. * have changed.
  156. */
  157. public void reloadChanged() {
  158. for (String name : managers.keySet()) {
  159. File file = getPath(name);
  160. Long oldDate = lastModified.get(name);
  161. if (oldDate == null) {
  162. oldDate = 0L;
  163. }
  164. try {
  165. if (file.lastModified() > oldDate) {
  166. World world = plugin.getServer().getWorld(name);
  167. if (world != null) {
  168. load(world);
  169. }
  170. }
  171. } catch (Exception e) {
  172. }
  173. }
  174. }
  175. /**
  176. * Get the region manager for a particular world.
  177. *
  178. * @param world
  179. * @return
  180. */
  181. public RegionManager get(World world) {
  182. RegionManager manager = managers.get(world.getName());
  183. if (manager == null) {
  184. manager = load(world);
  185. }
  186. return manager;
  187. }
  188. /**
  189. * Returns whether the player can bypass.
  190. *
  191. * @param player
  192. * @param world
  193. * @return
  194. */
  195. public boolean hasBypass(LocalPlayer player, World world) {
  196. return player.hasPermission("worldguard.region.bypass."
  197. + world.getName());
  198. }
  199. /**
  200. * Returns whether the player can bypass.
  201. *
  202. * @param player
  203. * @param world
  204. * @return
  205. */
  206. public boolean hasBypass(Player player, World world) {
  207. return plugin.hasPermission(player, "worldguard.region.bypass."
  208. + world.getName());
  209. }
  210. /**
  211. * Check if a player has permission to build at a block.
  212. *
  213. * @param player
  214. * @param block
  215. * @return
  216. */
  217. public boolean canBuild(Player player, Block block) {
  218. return canBuild(player, block.getLocation());
  219. }
  220. /**
  221. * Check if a player has permission to build at a location.
  222. *
  223. * @param player
  224. * @param loc
  225. * @return
  226. */
  227. public boolean canBuild(Player player, Location loc) {
  228. World world = loc.getWorld();
  229. WorldConfiguration worldConfig = config.get(world);
  230. if (!worldConfig.useRegions) {
  231. return true;
  232. }
  233. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  234. if (!hasBypass(player, world)) {
  235. RegionManager mgr = get(world);
  236. if (!mgr.getApplicableRegions(BukkitUtil.toVector(loc))
  237. .canBuild(localPlayer)) {
  238. return false;
  239. }
  240. }
  241. return true;
  242. }
  243. /**
  244. * Checks to see whether a flag is allowed.
  245. *
  246. * @param flag
  247. * @param loc
  248. * @return
  249. */
  250. public boolean allows(StateFlag flag, Location loc) {
  251. World world = loc.getWorld();
  252. WorldConfiguration worldConfig = config.get(world);
  253. if (!worldConfig.useRegions) {
  254. return true;
  255. }
  256. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  257. return mgr.getApplicableRegions(toVector(loc)).allows(flag);
  258. }
  259. }