summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/Spawn.java
blob: f3c4de06aa25eddcbfde87ed8073fca6f5fb19d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.earth2me.essentials;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.World.Environment;

public class Spawn implements IConf {

	private static final Logger logger = Logger.getLogger("Minecraft");
	private EssentialsConf config;
	private Server server;

	public Spawn(Server server, File dataFolder) {
		File configFile = new File(dataFolder, "spawn.yml");
		this.server = server;
		config = new EssentialsConf(configFile);
		config.load();
	}

	public void setSpawn(Location loc, String group) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("world", loc.getWorld().getName());
		map.put("x", loc.getX());
		map.put("y", loc.getY());
		map.put("z", loc.getZ());
		map.put("yaw", loc.getYaw());
		map.put("pitch", loc.getPitch());
		config.setProperty(group, map);
		config.save();

		if ("default".equals(group)) {
			loc.getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
		}
	}

	public Location getSpawn(String group) {
		if (config.getProperty(group) == null) {
			group = "default";
		}
		if (config.getProperty(group) == null) {
			for (World w : server.getWorlds()) {
				if (w.getEnvironment() != Environment.NORMAL) {
					continue;
				}
				return w.getSpawnLocation();
			}
		}
		String worldId = config.getString(group + ".world", "");
		World world = server.getWorlds().get(server.getWorlds().size() > 1 ? 1 : 0);
		for (World w : server.getWorlds()) {
			if (w.getEnvironment() != Environment.NORMAL) {
				continue;
			}
			world = w;
			break;
		}
		for (World w : server.getWorlds()) {
			if (!w.getName().equals(worldId)) {
				continue;
			}
			world = w;
			break;
		}

		double x = config.getDouble(group + ".x", config.getDouble("default.x", 0));
		double y = config.getDouble(group + ".y", config.getDouble("default.y", 0));
		double z = config.getDouble(group + ".z", config.getDouble("default.z", 0));
		float yaw = (float) config.getDouble(group + ".yaw", config.getDouble("default.yaw", 0));
		float pitch = (float) config.getDouble(group + ".pitch", config.getDouble("default.pitch", 0));
		Location retval = new Location(world, x, y, z, yaw, pitch);

		if (y < 1) {
			retval.setY(world.getHighestBlockYAt(retval));
		}

		return retval;
	}

	public void reloadConfig() {
		config.load();
	}
}