/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
- package com.sk89q.commandbook.locations;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.util.config.Configuration;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class WrappedSpawnManager {
- /**
- * The configuration that stores world spawn pitches and yaws
- */
- private final Configuration config;
- /**
- * The logger
- */
- private final Logger logger = Logger.getLogger("Minecraft.CommandBook");
- /**
- * The configuration file's header
- */
- private static final String CONFIG_HEADER = "#\r\n" +
- "# * CommndBook world spawn enrichment file\r\n" +
- "#\r\n" +
- "# WARNING: THIS FILE IS AUTOMATICALLY GENERATED. If you modify this file by\r\n" +
- "# hand, be aware that A SINGLE MISTYPED CHARACTER CAN CORRUPT THE FILE. If\r\n" +
- "# CommandBook is unable to parse the file, your world spawns will FAIL TO LOAD and\r\n" +
- "# the contents of this file may reset. Please use a YAML validator such as\r\n" +
- "# http://yaml-online-parser.appspot.com (for smaller files).\r\n" +
- "#\r\n" +
- "# REMEMBER TO KEEP PERIODICAL BACKUPS.\r\n" +
- "#\r\n";
- /**
- * The map that stores enriched spawns loaded from the config file
- */
- private final Map<String, WrappedSpawn> storedSpawns = new HashMap<String, WrappedSpawn>();
- public WrappedSpawnManager(File configFile) {
- configFile.getParentFile().mkdirs();
- if (!configFile.exists())
- try {
- configFile.createNewFile();
- } catch (IOException e) {
- logger.log(Level.SEVERE, "CommandBook: Spawn storage file creation error: {0}", e.getMessage());
- }
- config = new Configuration(configFile);
- load();
- }
- public void load() {
- storedSpawns.clear();
- config.load();
- for (World world : Bukkit.getServer().getWorlds())
- loadWorld(world);
- }
- private WrappedSpawn loadWorld(World world) {
- WrappedSpawn wrapper = new WrappedSpawn(world,
- Double.valueOf(config.getDouble(world.getName() + ".pitch", 0)).floatValue(),
- Double.valueOf(config.getDouble(world.getName() + ".yaw", 0)).floatValue());
- storedSpawns.put(world.getName(), wrapper);
- return wrapper;
- }
- public Location getWorldSpawn(World world) {
- WrappedSpawn wrapper = getEnrichment(world);
- return wrapper.getLocation();
- }
- public WrappedSpawn setWorldSpawn(Location loc) {
- WrappedSpawn spawn = getEnrichment(loc.getWorld());
- loc.getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
- spawn.setPitch(loc.getPitch());
- spawn.setYaw(loc.getYaw());
- config.setProperty(spawn.getWorldName() + ".pitch", spawn.getPitch());
- config.setProperty(spawn.getWorldName() + ".yaw", spawn.getYaw());
- config.setHeader(CONFIG_HEADER);
- config.save();
- return spawn;
- }
- private WrappedSpawn getEnrichment(World world) {
- WrappedSpawn wrapper = storedSpawns.get(world.getName());
- if (wrapper == null) {
- wrapper = loadWorld(world);
- }
- return wrapper;
- }
- }