summaryrefslogtreecommitdiffstats
path: root/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java
blob: ab7cf986cf450d8fd9d537b30e009788354ae976 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.earth2me.essentials.xmpp;

import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.IEssentials;
import net.ess3.api.IUser;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;


public class EssentialsXMPP extends JavaPlugin implements IEssentialsXMPP
{
	private static final Logger LOGGER = Logger.getLogger("Minecraft");
	private static EssentialsXMPP instance = null;
	private transient UserManager users;
	private transient XMPPManager xmpp;
	private transient IEssentials ess;

	public static IEssentialsXMPP getInstance()
	{
		return instance;
	}

	@Override
	public void onEnable()
	{
		instance = this;

		final PluginManager pluginManager = getServer().getPluginManager();
		ess = (IEssentials)pluginManager.getPlugin("Essentials");
		if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion()))
		{
			LOGGER.log(Level.WARNING, tl("versionMismatchAll"));
		}
		if (!ess.isEnabled())
		{
			this.setEnabled(false);
			return;
		}

		final EssentialsXMPPPlayerListener playerListener = new EssentialsXMPPPlayerListener(ess);
		pluginManager.registerEvents(playerListener, this);

		users = new UserManager(this.getDataFolder());
		xmpp = new XMPPManager(this);

		ess.addReloadListener(users);
		ess.addReloadListener(xmpp);
	}

	@Override
	public void onDisable()
	{
		if (xmpp != null)
		{
			xmpp.disconnect();
		}
		instance = null;
	}

	@Override
	public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args)
	{
		return ess.onCommandEssentials(sender, command, commandLabel, args, EssentialsXMPP.class.getClassLoader(), "com.earth2me.essentials.xmpp.Command", "essentials.", null);
	}

	@Override
	public void setAddress(final Player user, final String address)
	{
		final String username = user.getName().toLowerCase(Locale.ENGLISH);
		instance.users.setAddress(username, address);
	}

	@Override
	public String getAddress(final String name)
	{
		return instance.users.getAddress(name);
	}

	@Override
	public IUser getUserByAddress(final String address)
	{
		String username = instance.users.getUserByAddress(address);
		return username == null ? null : ess.getUser(username);
	}

	@Override
	public boolean toggleSpy(final Player user)
	{
		final String username = user.getName().toLowerCase(Locale.ENGLISH);
		final boolean spy = !instance.users.isSpy(username);
		instance.users.setSpy(username, spy);
		return spy;
	}

	@Override
	public String getAddress(final Player user)
	{
		return instance.users.getAddress(user.getName());
	}

	@Override
	public boolean sendMessage(final Player user, final String message)
	{
		return instance.xmpp.sendMessage(instance.users.getAddress(user.getName()), message);
	}

	@Override
	public boolean sendMessage(final String address, final String message)
	{
		return instance.xmpp.sendMessage(address, message);
	}

	// @Override
	public static boolean updatePresence()
	{
		instance.xmpp.updatePresence();
		return true;
	}

	@Override
	public List<String> getSpyUsers()
	{
		return instance.users.getSpyUsers();
	}

	@Override
	public void broadcastMessage(final IUser sender, final String message, final String xmppAddress)
	{
		ess.broadcastMessage(sender, message);
		try
		{
			for (String address : getSpyUsers())
			{
				if (!address.equalsIgnoreCase(xmppAddress))
				{
					sendMessage(address, message);
				}
			}
		}
		catch (Exception ex)
		{
			// Ignore exceptions
		}
	}
}