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

https://github.com/Noobidoo/commandbook · Java · 98 lines · 73 code · 13 blank · 12 comment · 4 complexity · 03e28f4301ee50d858a46c0cbd6ca62c MD5 · raw file

  1. package com.sk89q.commandbook.locations;
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.Location;
  4. import org.bukkit.World;
  5. import org.bukkit.util.config.Configuration;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. public class WrappedSpawnManager {
  13. /**
  14. * The configuration that stores world spawn pitches and yaws
  15. */
  16. private final Configuration config;
  17. /**
  18. * The logger
  19. */
  20. private final Logger logger = Logger.getLogger("Minecraft.CommandBook");
  21. /**
  22. * The configuration file's header
  23. */
  24. private static final String CONFIG_HEADER = "#\r\n" +
  25. "# * CommndBook world spawn enrichment file\r\n" +
  26. "#\r\n" +
  27. "# WARNING: THIS FILE IS AUTOMATICALLY GENERATED. If you modify this file by\r\n" +
  28. "# hand, be aware that A SINGLE MISTYPED CHARACTER CAN CORRUPT THE FILE. If\r\n" +
  29. "# CommandBook is unable to parse the file, your world spawns will FAIL TO LOAD and\r\n" +
  30. "# the contents of this file may reset. Please use a YAML validator such as\r\n" +
  31. "# http://yaml-online-parser.appspot.com (for smaller files).\r\n" +
  32. "#\r\n" +
  33. "# REMEMBER TO KEEP PERIODICAL BACKUPS.\r\n" +
  34. "#\r\n";
  35. /**
  36. * The map that stores enriched spawns loaded from the config file
  37. */
  38. private final Map<String, WrappedSpawn> storedSpawns = new HashMap<String, WrappedSpawn>();
  39. public WrappedSpawnManager(File configFile) {
  40. configFile.getParentFile().mkdirs();
  41. if (!configFile.exists())
  42. try {
  43. configFile.createNewFile();
  44. } catch (IOException e) {
  45. logger.log(Level.SEVERE, "CommandBook: Spawn storage file creation error: {0}", e.getMessage());
  46. }
  47. config = new Configuration(configFile);
  48. load();
  49. }
  50. public void load() {
  51. storedSpawns.clear();
  52. config.load();
  53. for (World world : Bukkit.getServer().getWorlds())
  54. loadWorld(world);
  55. }
  56. private WrappedSpawn loadWorld(World world) {
  57. WrappedSpawn wrapper = new WrappedSpawn(world,
  58. Double.valueOf(config.getDouble(world.getName() + ".pitch", 0)).floatValue(),
  59. Double.valueOf(config.getDouble(world.getName() + ".yaw", 0)).floatValue());
  60. storedSpawns.put(world.getName(), wrapper);
  61. return wrapper;
  62. }
  63. public Location getWorldSpawn(World world) {
  64. WrappedSpawn wrapper = getEnrichment(world);
  65. return wrapper.getLocation();
  66. }
  67. public WrappedSpawn setWorldSpawn(Location loc) {
  68. WrappedSpawn spawn = getEnrichment(loc.getWorld());
  69. loc.getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
  70. spawn.setPitch(loc.getPitch());
  71. spawn.setYaw(loc.getYaw());
  72. config.setProperty(spawn.getWorldName() + ".pitch", spawn.getPitch());
  73. config.setProperty(spawn.getWorldName() + ".yaw", spawn.getYaw());
  74. config.setHeader(CONFIG_HEADER);
  75. config.save();
  76. return spawn;
  77. }
  78. private WrappedSpawn getEnrichment(World world) {
  79. WrappedSpawn wrapper = storedSpawns.get(world.getName());
  80. if (wrapper == null) {
  81. wrapper = loadWorld(world);
  82. }
  83. return wrapper;
  84. }
  85. }