1 package org.bukkit.plugin.messaging;
3 import com.google.common.collect.ImmutableSet;
4 import com.google.common.collect.ImmutableSet.Builder;
5 import java.util.HashMap;
6 import java.util.HashSet;
9 import org.bukkit.entity.Player;
10 import org.bukkit.plugin.Plugin;
16 private final Map<String, Set<PluginMessageListenerRegistration>> incomingByChannel =
new HashMap<String, Set<PluginMessageListenerRegistration>>();
17 private final Map<Plugin, Set<PluginMessageListenerRegistration>> incomingByPlugin =
new HashMap<Plugin, Set<PluginMessageListenerRegistration>>();
18 private final Map<String, Set<Plugin>> outgoingByChannel =
new HashMap<String, Set<Plugin>>();
19 private final Map<Plugin, Set<String>> outgoingByPlugin =
new HashMap<Plugin, Set<String>>();
20 private final Object incomingLock =
new Object();
21 private final Object outgoingLock =
new Object();
23 private void addToOutgoing(
Plugin plugin, String channel) {
24 synchronized (outgoingLock) {
25 Set<Plugin> plugins = outgoingByChannel.get(channel);
26 Set<String> channels = outgoingByPlugin.get(plugin);
28 if (plugins == null) {
29 plugins =
new HashSet<Plugin>();
30 outgoingByChannel.put(channel, plugins);
33 if (channels == null) {
34 channels =
new HashSet<String>();
35 outgoingByPlugin.put(plugin, channels);
39 channels.add(channel);
43 private void removeFromOutgoing(
Plugin plugin, String channel) {
44 synchronized (outgoingLock) {
45 Set<Plugin> plugins = outgoingByChannel.get(channel);
46 Set<String> channels = outgoingByPlugin.get(plugin);
48 if (plugins != null) {
49 plugins.remove(plugin);
51 if (plugins.isEmpty()) {
52 outgoingByChannel.remove(channel);
56 if (channels != null) {
57 channels.remove(channel);
59 if (channels.isEmpty()) {
60 outgoingByChannel.remove(channel);
66 private void removeFromOutgoing(
Plugin plugin) {
67 synchronized (outgoingLock) {
68 Set<String> channels = outgoingByPlugin.get(plugin);
70 if (channels != null) {
71 String[] toRemove = channels.toArray(
new String[0]);
73 outgoingByPlugin.remove(plugin);
75 for (String channel : toRemove) {
76 removeFromOutgoing(plugin, channel);
83 synchronized (incomingLock) {
84 Set<PluginMessageListenerRegistration> registrations = incomingByChannel.get(registration.
getChannel());
86 if (registrations == null) {
87 registrations =
new HashSet<PluginMessageListenerRegistration>();
88 incomingByChannel.put(registration.
getChannel(), registrations);
90 if (registrations.contains(registration)) {
91 throw new IllegalArgumentException(
"This registration already exists");
95 registrations.add(registration);
97 registrations = incomingByPlugin.get(registration.
getPlugin());
99 if (registrations == null) {
100 registrations =
new HashSet<PluginMessageListenerRegistration>();
101 incomingByPlugin.put(registration.
getPlugin(), registrations);
103 if (registrations.contains(registration)) {
104 throw new IllegalArgumentException(
"This registration already exists");
108 registrations.add(registration);
113 synchronized (incomingLock) {
114 Set<PluginMessageListenerRegistration> registrations = incomingByChannel.get(registration.
getChannel());
116 if (registrations != null) {
117 registrations.remove(registration);
119 if (registrations.isEmpty()) {
120 incomingByChannel.remove(registration.
getChannel());
124 registrations = incomingByPlugin.get(registration.
getPlugin());
126 if (registrations != null) {
127 registrations.remove(registration);
129 if (registrations.isEmpty()) {
130 incomingByPlugin.remove(registration.
getPlugin());
136 private void removeFromIncoming(
Plugin plugin, String channel) {
137 synchronized (incomingLock) {
138 Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
140 if (registrations != null) {
144 if (registration.
getChannel().equals(channel)) {
145 removeFromIncoming(registration);
152 private void removeFromIncoming(
Plugin plugin) {
153 synchronized (incomingLock) {
154 Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
156 if (registrations != null) {
159 incomingByPlugin.remove(plugin);
162 removeFromIncoming(registration);
171 return channel.equals(
"REGISTER") || channel.equals(
"UNREGISTER");
175 if (plugin == null) {
176 throw new IllegalArgumentException(
"Plugin cannot be null");
183 addToOutgoing(plugin, channel);
187 if (plugin == null) {
188 throw new IllegalArgumentException(
"Plugin cannot be null");
192 removeFromOutgoing(plugin, channel);
196 if (plugin == null) {
197 throw new IllegalArgumentException(
"Plugin cannot be null");
200 removeFromOutgoing(plugin);
204 if (plugin == null) {
205 throw new IllegalArgumentException(
"Plugin cannot be null");
211 if (listener == null) {
212 throw new IllegalArgumentException(
"Listener cannot be null");
217 addToIncoming(result);
223 if (plugin == null) {
224 throw new IllegalArgumentException(
"Plugin cannot be null");
226 if (listener == null) {
227 throw new IllegalArgumentException(
"Listener cannot be null");
235 if (plugin == null) {
236 throw new IllegalArgumentException(
"Plugin cannot be null");
240 removeFromIncoming(plugin, channel);
244 if (plugin == null) {
245 throw new IllegalArgumentException(
"Plugin cannot be null");
248 removeFromIncoming(plugin);
252 synchronized (outgoingLock) {
253 Set<String> keys = outgoingByChannel.keySet();
254 return ImmutableSet.copyOf(keys);
259 if (plugin == null) {
260 throw new IllegalArgumentException(
"Plugin cannot be null");
263 synchronized (outgoingLock) {
264 Set<String> channels = outgoingByPlugin.get(plugin);
266 if (channels != null) {
267 return ImmutableSet.copyOf(channels);
269 return ImmutableSet.of();
275 synchronized (incomingLock) {
276 Set<String> keys = incomingByChannel.keySet();
277 return ImmutableSet.copyOf(keys);
282 if (plugin == null) {
283 throw new IllegalArgumentException(
"Plugin cannot be null");
286 synchronized (incomingLock) {
287 Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
289 if (registrations != null) {
290 Builder<String> builder = ImmutableSet.builder();
296 return builder.build();
298 return ImmutableSet.of();
304 if (plugin == null) {
305 throw new IllegalArgumentException(
"Plugin cannot be null");
308 synchronized (incomingLock) {
309 Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
311 if (registrations != null) {
312 return ImmutableSet.copyOf(registrations);
314 return ImmutableSet.of();
322 synchronized (incomingLock) {
323 Set<PluginMessageListenerRegistration> registrations = incomingByChannel.get(channel);
325 if (registrations != null) {
326 return ImmutableSet.copyOf(registrations);
328 return ImmutableSet.of();
334 if (plugin == null) {
335 throw new IllegalArgumentException(
"Plugin cannot be null");
339 synchronized (incomingLock) {
340 Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
342 if (registrations != null) {
343 Builder<PluginMessageListenerRegistration> builder = ImmutableSet.builder();
346 if (registration.
getChannel().equals(channel)) {
347 builder.add(registration);
351 return builder.build();
353 return ImmutableSet.of();
359 if (registration == null) {
360 throw new IllegalArgumentException(
"Registration cannot be null");
363 synchronized (incomingLock) {
364 Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(registration.
getPlugin());
366 if (registrations != null) {
367 return registrations.contains(registration);
375 if (plugin == null) {
376 throw new IllegalArgumentException(
"Plugin cannot be null");
380 synchronized (incomingLock) {
381 Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
383 if (registrations != null) {
385 if (registration.
getChannel().equals(channel)) {
396 if (plugin == null) {
397 throw new IllegalArgumentException(
"Plugin cannot be null");
401 synchronized (outgoingLock) {
402 Set<String> channels = outgoingByPlugin.get(plugin);
404 if (channels != null) {
405 return channels.contains(channel);
413 if (source == null) {
414 throw new IllegalArgumentException(
"Player source cannot be null");
416 if (message == null) {
417 throw new IllegalArgumentException(
"Message cannot be null");
434 if (channel == null) {
435 throw new IllegalArgumentException(
"Channel cannot be null");
456 if (messenger == null) {
457 throw new IllegalArgumentException(
"Messenger cannot be null");
459 if (source == null) {
460 throw new IllegalArgumentException(
"Plugin source cannot be null");
463 throw new IllegalArgumentException(
"Plugin must be enabled to send messages");
465 if (message == null) {
466 throw new IllegalArgumentException(
"Message cannot be null");