summaryrefslogtreecommitdiffstats
path: root/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java
blob: a14f8bb71b42cdedbd718b84c25db51eed4cd304 (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
package com.earth2me.essentials.spawn;

import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.BedLocationFix;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerRespawnEvent;


public class EssentialsSpawnPlayerListener extends PlayerListener
{
	private final transient IEssentials ess;

	public EssentialsSpawnPlayerListener(final IEssentials ess)
	{
		super();
		this.ess = ess;
	}

	@Override
	public void onPlayerRespawn(final PlayerRespawnEvent event)
	{
		final User user = ess.getUser(event.getPlayer());

		if (ess.getSettings().getRespawnAtHome())
		{
			Location home = user.getHome(user.getLocation());
			if (home == null)
			{
				home = BedLocationFix.getBedSpawnLocation(user);
			}
			if (home != null)
			{
				event.setRespawnLocation(home);
				return;
			}
		}
		final Location spawn = ess.getSpawn().getSpawn(user.getGroup());
		if (spawn != null)
		{
			event.setRespawnLocation(spawn);
		}
	}

	@Override
	public void onPlayerJoin(final PlayerJoinEvent event)
	{
		final User user = ess.getUser(event.getPlayer());

		if (!user.isNew() || BedLocationFix.getBedSpawnLocation(user) != null)
		{
			return;
		}
		user.setNew(false);
		if (!"none".equalsIgnoreCase(ess.getSettings().getNewbieSpawn()))
		{
			ess.scheduleSyncDelayedTask(new Runnable()
			{
				public void run()
				{
					try
					{
						user.getTeleport().now(ess.getSpawn().getSpawn(ess.getSettings().getNewbieSpawn()));
					}
					catch (Exception ex)
					{
						Logger.getLogger("Minecraft").log(Level.WARNING, _("teleportNewPlayerError"), ex);
					}
				}
			});
		}

		if (ess.getSettings().getAnnounceNewPlayers())
		{
			ess.broadcastMessage(user, ess.getSettings().getAnnounceNewPlayerFormat(user));
		}
	}
}