/src/main/java/com/sk89q/commandbook/locations/RootLocationManager.java

https://github.com/Noobidoo/commandbook · Java · 120 lines · 85 code · 18 blank · 17 comment · 10 complexity · 4a3cd51ecf23b14ad4e65a8cbb055345 MD5 · raw file

  1. // $Id$
  2. /*
  3. * Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.sk89q.commandbook.locations;
  19. import java.io.IOException;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.logging.Logger;
  23. import org.bukkit.Location;
  24. import org.bukkit.World;
  25. import org.bukkit.entity.Player;
  26. public class RootLocationManager<T> {
  27. private static final Logger logger = Logger.getLogger("Minecraft.CommandBook");
  28. private LocationManager<T> rootManager;
  29. private Map<String, LocationManager<T>> managers;
  30. private LocationManagerFactory<LocationManager<T>> factory;
  31. private boolean perWorld;
  32. public RootLocationManager(LocationManagerFactory<LocationManager<T>> factory, boolean perWorld) {
  33. this.factory = factory;
  34. this.perWorld = perWorld;
  35. if (perWorld) {
  36. managers = new HashMap<String, LocationManager<T>>();
  37. } else {
  38. rootManager = factory.createManager();
  39. try {
  40. rootManager.load();
  41. } catch (IOException e) {
  42. logger.warning("Failed to load warps: " + e.getMessage());
  43. }
  44. }
  45. }
  46. private LocationManager<T> getManager(World world) {
  47. LocationManager<T> manager = managers.get(world.getName());
  48. if (manager != null) {
  49. return manager;
  50. }
  51. manager = factory.createManager(world);
  52. try {
  53. manager.load();
  54. } catch (IOException e) {
  55. logger.warning("Failed to load warps for world " + world.getName()
  56. + ": " + e.getMessage());
  57. }
  58. managers.put(world.getName(), manager);
  59. return manager;
  60. }
  61. public T get(World world, String id) {
  62. if (perWorld) {
  63. return getManager(world).get(id);
  64. } else {
  65. return rootManager.get(id);
  66. }
  67. }
  68. public T create(String id, Location loc, Player player) {
  69. if (perWorld) {
  70. LocationManager<T> manager = getManager(loc.getWorld());
  71. T ret = manager.create(id, loc, player);
  72. save(manager);
  73. return ret;
  74. } else {
  75. T ret = rootManager.create(id, loc, player);
  76. save(rootManager);
  77. return ret;
  78. }
  79. }
  80. public boolean remove(World world, String id) {
  81. if (perWorld) {
  82. LocationManager<T> manager = getManager(world);
  83. boolean ret = getManager(world).remove(id);
  84. save(manager);
  85. return ret;
  86. } else {
  87. boolean ret = rootManager.remove(id);
  88. save(rootManager);
  89. return ret;
  90. }
  91. }
  92. private void save(LocationManager<T> manager) {
  93. try {
  94. manager.save();
  95. } catch (IOException e) {
  96. logger.warning("Failed to save warps: " + e.getMessage());
  97. }
  98. }
  99. public boolean isPerWorld() {
  100. return perWorld;
  101. }
  102. }