Advertisement
porezes

Untitled

Jan 11th, 2021
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.71 KB | None | 0 0
  1. package org.bukkit.plugin.messaging;
  2.  
  3. import com.google.common.collect.ImmutableSet;
  4. import com.google.common.collect.ImmutableSet.Builder;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.plugin.Plugin;
  7. import org.github.paperspigot.event.ServerExceptionEvent;
  8. import org.github.paperspigot.exception.ServerPluginMessageException;
  9.  
  10. import java.util.HashMap;
  11. import java.util.HashSet;
  12. import java.util.Map;
  13. import java.util.Set;
  14.  
  15. /**
  16. * Standard implementation to {@link Messenger}
  17. */
  18. public class StandardMessenger implements Messenger {
  19. private final Map<String, Set<PluginMessageListenerRegistration>> incomingByChannel = new HashMap<String, Set<PluginMessageListenerRegistration>>();
  20. private final Map<Plugin, Set<PluginMessageListenerRegistration>> incomingByPlugin = new HashMap<Plugin, Set<PluginMessageListenerRegistration>>();
  21. private final Map<String, Set<Plugin>> outgoingByChannel = new HashMap<String, Set<Plugin>>();
  22. private final Map<Plugin, Set<String>> outgoingByPlugin = new HashMap<Plugin, Set<String>>();
  23. private final Object incomingLock = new Object();
  24. private final Object outgoingLock = new Object();
  25.  
  26. private void addToOutgoing(Plugin plugin, String channel) {
  27. synchronized (outgoingLock) {
  28. Set<Plugin> plugins = outgoingByChannel.get(channel);
  29. Set<String> channels = outgoingByPlugin.get(plugin);
  30.  
  31. if (plugins == null) {
  32. plugins = new HashSet<Plugin>();
  33. outgoingByChannel.put(channel, plugins);
  34. }
  35.  
  36. if (channels == null) {
  37. channels = new HashSet<String>();
  38. outgoingByPlugin.put(plugin, channels);
  39. }
  40.  
  41. plugins.add(plugin);
  42. channels.add(channel);
  43. }
  44. }
  45.  
  46. private void removeFromOutgoing(Plugin plugin, String channel) {
  47. synchronized (outgoingLock) {
  48. Set<Plugin> plugins = outgoingByChannel.get(channel);
  49. Set<String> channels = outgoingByPlugin.get(plugin);
  50.  
  51. if (plugins != null) {
  52. plugins.remove(plugin);
  53.  
  54. if (plugins.isEmpty()) {
  55. outgoingByChannel.remove(channel);
  56. }
  57. }
  58.  
  59. if (channels != null) {
  60. channels.remove(channel);
  61.  
  62. if (channels.isEmpty()) {
  63. outgoingByChannel.remove(channel);
  64. }
  65. }
  66. }
  67. }
  68.  
  69. private void removeFromOutgoing(Plugin plugin) {
  70. synchronized (outgoingLock) {
  71. Set<String> channels = outgoingByPlugin.get(plugin);
  72.  
  73. if (channels != null) {
  74. String[] toRemove = channels.toArray(new String[0]);
  75.  
  76. outgoingByPlugin.remove(plugin);
  77.  
  78. for (String channel : toRemove) {
  79. removeFromOutgoing(plugin, channel);
  80. }
  81. }
  82. }
  83. }
  84.  
  85. private void addToIncoming(PluginMessageListenerRegistration registration) {
  86. synchronized (incomingLock) {
  87. Set<PluginMessageListenerRegistration> registrations = incomingByChannel.get(registration.getChannel());
  88.  
  89. if (registrations == null) {
  90. registrations = new HashSet<PluginMessageListenerRegistration>();
  91. incomingByChannel.put(registration.getChannel(), registrations);
  92. } else {
  93. if (registrations.contains(registration)) {
  94. throw new IllegalArgumentException("This registration already exists");
  95. }
  96. }
  97.  
  98. registrations.add(registration);
  99.  
  100. registrations = incomingByPlugin.get(registration.getPlugin());
  101.  
  102. if (registrations == null) {
  103. registrations = new HashSet<PluginMessageListenerRegistration>();
  104. incomingByPlugin.put(registration.getPlugin(), registrations);
  105. } else {
  106. if (registrations.contains(registration)) {
  107. throw new IllegalArgumentException("This registration already exists");
  108. }
  109. }
  110.  
  111. registrations.add(registration);
  112. }
  113. }
  114.  
  115. private void removeFromIncoming(PluginMessageListenerRegistration registration) {
  116. synchronized (incomingLock) {
  117. Set<PluginMessageListenerRegistration> registrations = incomingByChannel.get(registration.getChannel());
  118.  
  119. if (registrations != null) {
  120. registrations.remove(registration);
  121.  
  122. if (registrations.isEmpty()) {
  123. incomingByChannel.remove(registration.getChannel());
  124. }
  125. }
  126.  
  127. registrations = incomingByPlugin.get(registration.getPlugin());
  128.  
  129. if (registrations != null) {
  130. registrations.remove(registration);
  131.  
  132. if (registrations.isEmpty()) {
  133. incomingByPlugin.remove(registration.getPlugin());
  134. }
  135. }
  136. }
  137. }
  138.  
  139. private void removeFromIncoming(Plugin plugin, String channel) {
  140. synchronized (incomingLock) {
  141. Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
  142.  
  143. if (registrations != null) {
  144. PluginMessageListenerRegistration[] toRemove = registrations.toArray(new PluginMessageListenerRegistration[0]);
  145.  
  146. for (PluginMessageListenerRegistration registration : toRemove) {
  147. if (registration.getChannel().equals(channel)) {
  148. removeFromIncoming(registration);
  149. }
  150. }
  151. }
  152. }
  153. }
  154.  
  155. private void removeFromIncoming(Plugin plugin) {
  156. synchronized (incomingLock) {
  157. Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
  158.  
  159. if (registrations != null) {
  160. PluginMessageListenerRegistration[] toRemove = registrations.toArray(new PluginMessageListenerRegistration[0]);
  161.  
  162. incomingByPlugin.remove(plugin);
  163.  
  164. for (PluginMessageListenerRegistration registration : toRemove) {
  165. removeFromIncoming(registration);
  166. }
  167. }
  168. }
  169. }
  170.  
  171. public boolean isReservedChannel(String channel) {
  172. validateChannel(channel);
  173.  
  174. return channel.equals("REGISTER") || channel.equals("UNREGISTER");
  175. }
  176.  
  177. public void registerOutgoingPluginChannel(Plugin plugin, String channel) {
  178. if (plugin == null) {
  179. throw new IllegalArgumentException("Plugin cannot be null");
  180. }
  181. validateChannel(channel);
  182. if (isReservedChannel(channel)) {
  183. throw new ReservedChannelException(channel);
  184. }
  185.  
  186. addToOutgoing(plugin, channel);
  187. }
  188.  
  189. public void unregisterOutgoingPluginChannel(Plugin plugin, String channel) {
  190. if (plugin == null) {
  191. throw new IllegalArgumentException("Plugin cannot be null");
  192. }
  193. validateChannel(channel);
  194.  
  195. removeFromOutgoing(plugin, channel);
  196. }
  197.  
  198. public void unregisterOutgoingPluginChannel(Plugin plugin) {
  199. if (plugin == null) {
  200. throw new IllegalArgumentException("Plugin cannot be null");
  201. }
  202.  
  203. removeFromOutgoing(plugin);
  204. }
  205.  
  206. public PluginMessageListenerRegistration registerIncomingPluginChannel(Plugin plugin, String channel, PluginMessageListener listener) {
  207. if (plugin == null) {
  208. throw new IllegalArgumentException("Plugin cannot be null");
  209. }
  210. validateChannel(channel);
  211. if (isReservedChannel(channel)) {
  212. throw new ReservedChannelException(channel);
  213. }
  214. if (listener == null) {
  215. throw new IllegalArgumentException("Listener cannot be null");
  216. }
  217.  
  218. PluginMessageListenerRegistration result = new PluginMessageListenerRegistration(this, plugin, channel, listener);
  219.  
  220. addToIncoming(result);
  221.  
  222. return result;
  223. }
  224.  
  225. public void unregisterIncomingPluginChannel(Plugin plugin, String channel, PluginMessageListener listener) {
  226. if (plugin == null) {
  227. throw new IllegalArgumentException("Plugin cannot be null");
  228. }
  229. if (listener == null) {
  230. throw new IllegalArgumentException("Listener cannot be null");
  231. }
  232. validateChannel(channel);
  233.  
  234. removeFromIncoming(new PluginMessageListenerRegistration(this, plugin, channel, listener));
  235. }
  236.  
  237. public void unregisterIncomingPluginChannel(Plugin plugin, String channel) {
  238. if (plugin == null) {
  239. throw new IllegalArgumentException("Plugin cannot be null");
  240. }
  241. validateChannel(channel);
  242.  
  243. removeFromIncoming(plugin, channel);
  244. }
  245.  
  246. public void unregisterIncomingPluginChannel(Plugin plugin) {
  247. if (plugin == null) {
  248. throw new IllegalArgumentException("Plugin cannot be null");
  249. }
  250.  
  251. removeFromIncoming(plugin);
  252. }
  253.  
  254. public Set<String> getOutgoingChannels() {
  255. synchronized (outgoingLock) {
  256. Set<String> keys = outgoingByChannel.keySet();
  257. return ImmutableSet.copyOf(keys);
  258. }
  259. }
  260.  
  261. public Set<String> getOutgoingChannels(Plugin plugin) {
  262. if (plugin == null) {
  263. throw new IllegalArgumentException("Plugin cannot be null");
  264. }
  265.  
  266. synchronized (outgoingLock) {
  267. Set<String> channels = outgoingByPlugin.get(plugin);
  268.  
  269. if (channels != null) {
  270. return ImmutableSet.copyOf(channels);
  271. } else {
  272. return ImmutableSet.of();
  273. }
  274. }
  275. }
  276.  
  277. public Set<String> getIncomingChannels() {
  278. synchronized (incomingLock) {
  279. Set<String> keys = incomingByChannel.keySet();
  280. return ImmutableSet.copyOf(keys);
  281. }
  282. }
  283.  
  284. public Set<String> getIncomingChannels(Plugin plugin) {
  285. if (plugin == null) {
  286. throw new IllegalArgumentException("Plugin cannot be null");
  287. }
  288.  
  289. synchronized (incomingLock) {
  290. Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
  291.  
  292. if (registrations != null) {
  293. Builder<String> builder = ImmutableSet.builder();
  294.  
  295. for (PluginMessageListenerRegistration registration : registrations) {
  296. builder.add(registration.getChannel());
  297. }
  298.  
  299. return builder.build();
  300. } else {
  301. return ImmutableSet.of();
  302. }
  303. }
  304. }
  305.  
  306. public Set<PluginMessageListenerRegistration> getIncomingChannelRegistrations(Plugin plugin) {
  307. if (plugin == null) {
  308. throw new IllegalArgumentException("Plugin cannot be null");
  309. }
  310.  
  311. synchronized (incomingLock) {
  312. Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
  313.  
  314. if (registrations != null) {
  315. return ImmutableSet.copyOf(registrations);
  316. } else {
  317. return ImmutableSet.of();
  318. }
  319. }
  320. }
  321.  
  322. public Set<PluginMessageListenerRegistration> getIncomingChannelRegistrations(String channel) {
  323. validateChannel(channel);
  324.  
  325. synchronized (incomingLock) {
  326. Set<PluginMessageListenerRegistration> registrations = incomingByChannel.get(channel);
  327.  
  328. if (registrations != null) {
  329. return ImmutableSet.copyOf(registrations);
  330. } else {
  331. return ImmutableSet.of();
  332. }
  333. }
  334. }
  335.  
  336. public Set<PluginMessageListenerRegistration> getIncomingChannelRegistrations(Plugin plugin, String channel) {
  337. if (plugin == null) {
  338. throw new IllegalArgumentException("Plugin cannot be null");
  339. }
  340. validateChannel(channel);
  341.  
  342. synchronized (incomingLock) {
  343. Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
  344.  
  345. if (registrations != null) {
  346. Builder<PluginMessageListenerRegistration> builder = ImmutableSet.builder();
  347.  
  348. for (PluginMessageListenerRegistration registration : registrations) {
  349. if (registration.getChannel().equals(channel)) {
  350. builder.add(registration);
  351. }
  352. }
  353.  
  354. return builder.build();
  355. } else {
  356. return ImmutableSet.of();
  357. }
  358. }
  359. }
  360.  
  361. public boolean isRegistrationValid(PluginMessageListenerRegistration registration) {
  362. if (registration == null) {
  363. throw new IllegalArgumentException("Registration cannot be null");
  364. }
  365.  
  366. synchronized (incomingLock) {
  367. Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(registration.getPlugin());
  368.  
  369. if (registrations != null) {
  370. return registrations.contains(registration);
  371. }
  372.  
  373. return false;
  374. }
  375. }
  376.  
  377. public boolean isIncomingChannelRegistered(Plugin plugin, String channel) {
  378. if (plugin == null) {
  379. throw new IllegalArgumentException("Plugin cannot be null");
  380. }
  381. validateChannel(channel);
  382.  
  383. synchronized (incomingLock) {
  384. Set<PluginMessageListenerRegistration> registrations = incomingByPlugin.get(plugin);
  385.  
  386. if (registrations != null) {
  387. for (PluginMessageListenerRegistration registration : registrations) {
  388. if (registration.getChannel().equals(channel)) {
  389. return true;
  390. }
  391. }
  392. }
  393.  
  394. return false;
  395. }
  396. }
  397.  
  398. public boolean isOutgoingChannelRegistered(Plugin plugin, String channel) {
  399. if (plugin == null) {
  400. throw new IllegalArgumentException("Plugin cannot be null");
  401. }
  402. validateChannel(channel);
  403.  
  404. synchronized (outgoingLock) {
  405. Set<String> channels = outgoingByPlugin.get(plugin);
  406.  
  407. if (channels != null) {
  408. return channels.contains(channel);
  409. }
  410.  
  411. return false;
  412. }
  413. }
  414.  
  415. public void dispatchIncomingMessage(Player source, String channel, byte[] message) {
  416. if (source == null) {
  417. throw new IllegalArgumentException("Player source cannot be null");
  418. }
  419. if (message == null) {
  420. throw new IllegalArgumentException("Message cannot be null");
  421. }
  422. validateChannel(channel);
  423.  
  424. Set<PluginMessageListenerRegistration> registrations = getIncomingChannelRegistrations(channel);
  425.  
  426. for (PluginMessageListenerRegistration registration : registrations) {
  427. // Spigot Start
  428. try
  429. {
  430. registration.getListener().onPluginMessageReceived( channel, source, message );
  431. } catch ( Throwable t )
  432. {
  433. // Paper start
  434. String msg = "Could not pass incoming plugin message to " + registration.getPlugin();
  435. org.bukkit.Bukkit.getLogger().log( java.util.logging.Level.WARNING, msg, t );
  436. source.getServer().getPluginManager().callEvent(new ServerExceptionEvent(
  437. new ServerPluginMessageException(msg, t, registration.getPlugin(), source, channel, message)
  438. ));
  439. // Paper end
  440. }
  441. // Spigot End
  442. }
  443. }
  444.  
  445. /**
  446. * Validates a Plugin Channel name.
  447. *
  448. * @param channel Channel name to validate.
  449. */
  450. public static void validateChannel(String channel) {
  451. if (channel == null) {
  452. throw new IllegalArgumentException("Channel cannot be null");
  453. }
  454. if (channel.length() > Messenger.MAX_CHANNEL_SIZE) {
  455. throw new ChannelNameTooLongException(channel);
  456. }
  457. }
  458.  
  459. /**
  460. * Validates the input of a Plugin Message, ensuring the arguments are all
  461. * valid.
  462. *
  463. * @param messenger Messenger to use for validation.
  464. * @param source Source plugin of the Message.
  465. * @param channel Plugin Channel to send the message by.
  466. * @param message Raw message payload to send.
  467. * @throws IllegalArgumentException Thrown if the source plugin is
  468. * disabled.
  469. * @throws IllegalArgumentException Thrown if source, channel or message
  470. * is null.
  471. * @throws MessageTooLargeException Thrown if the message is too big.
  472. * @throws ChannelNameTooLongException Thrown if the channel name is too
  473. * long.
  474. * @throws ChannelNotRegisteredException Thrown if the channel is not
  475. * registered for this plugin.
  476. */
  477. public static void validatePluginMessage(Messenger messenger, Plugin source, String channel, byte[] message) {
  478. if (messenger == null) {
  479. throw new IllegalArgumentException("Messenger cannot be null");
  480. }
  481. if (source == null) {
  482. throw new IllegalArgumentException("Plugin source cannot be null");
  483. }
  484. if (!source.isEnabled()) {
  485. throw new IllegalArgumentException("Plugin must be enabled to send messages");
  486. }
  487. if (message == null) {
  488. throw new IllegalArgumentException("Message cannot be null");
  489. }
  490. if (!messenger.isOutgoingChannelRegistered(source, channel)) {
  491. throw new ChannelNotRegisteredException(channel);
  492. }
  493. if (message.length > Messenger.MAX_MESSAGE_SIZE) {
  494. throw new MessageTooLargeException(message);
  495. }
  496. validateChannel(channel);
  497. }
  498. }
  499.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement