/EssentialsXMPP/src/net/ess3/xmpp/EssentialsXMPP.java

https://github.com/mbax/Essentials · Java · 151 lines · 129 code · 21 blank · 1 comment · 7 complexity · 6913024a3dee39a64387739189607a30 MD5 · raw file

  1. package net.ess3.xmpp;
  2. import static net.ess3.I18n._;
  3. import net.ess3.api.ICommandHandler;
  4. import net.ess3.api.IEssentials;
  5. import net.ess3.api.IUser;
  6. import net.ess3.commands.EssentialsCommandHandler;
  7. import java.util.List;
  8. import java.util.Locale;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import net.ess3.api.IPlugin;
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.plugin.PluginManager;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16. public class EssentialsXMPP extends JavaPlugin implements IEssentialsXMPP
  17. {
  18. private static final Logger LOGGER = Logger.getLogger("Minecraft");
  19. private static EssentialsXMPP instance = null;
  20. private transient UserManager users;
  21. private transient XMPPManager xmpp;
  22. private transient IEssentials ess;
  23. private transient ICommandHandler commandHandler;
  24. public static IEssentialsXMPP getInstance()
  25. {
  26. return instance;
  27. }
  28. @Override
  29. public void onEnable()
  30. {
  31. instance = this;
  32. final PluginManager pluginManager = getServer().getPluginManager();
  33. final IPlugin plugin = (IPlugin)pluginManager.getPlugin("Essentials-3");
  34. ess = plugin.getEssentials();
  35. if (!this.getDescription().getVersion().equals(plugin.getDescription().getVersion()))
  36. {
  37. LOGGER.log(Level.WARNING, _("versionMismatchAll"));
  38. }
  39. if (!plugin.isEnabled())
  40. {
  41. this.setEnabled(false);
  42. return;
  43. }
  44. final EssentialsXMPPPlayerListener playerListener = new EssentialsXMPPPlayerListener(ess);
  45. pluginManager.registerEvents(playerListener, this);
  46. users = new UserManager(this.getDataFolder());
  47. xmpp = new XMPPManager(this);
  48. ess.addReloadListener(users);
  49. ess.addReloadListener(xmpp);
  50. commandHandler = new EssentialsCommandHandler(EssentialsXMPP.class.getClassLoader(), "net.ess3.xmpp.Command", "essentials.", ess);
  51. }
  52. @Override
  53. public void onDisable()
  54. {
  55. if (xmpp != null)
  56. {
  57. xmpp.disconnect();
  58. }
  59. instance = null;
  60. }
  61. @Override
  62. public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args)
  63. {
  64. return commandHandler.handleCommand(sender, command, commandLabel, args);
  65. }
  66. @Override
  67. public void setAddress(final CommandSender user, final String address)
  68. {
  69. final String username = user.getName().toLowerCase(Locale.ENGLISH);
  70. instance.users.setAddress(username, address);
  71. }
  72. @Override
  73. public String getAddress(final String name)
  74. {
  75. return instance.users.getAddress(name);
  76. }
  77. @Override
  78. public IUser getUserByAddress(final String address)
  79. {
  80. String username = instance.users.getUserByAddress(address);
  81. return username == null ? null : ess.getUserMap().getUser(username);
  82. }
  83. @Override
  84. public boolean toggleSpy(final CommandSender user)
  85. {
  86. final String username = user.getName().toLowerCase(Locale.ENGLISH);
  87. final boolean spy = !instance.users.isSpy(username);
  88. instance.users.setSpy(username, spy);
  89. return spy;
  90. }
  91. @Override
  92. public String getAddress(final CommandSender user)
  93. {
  94. return instance.users.getAddress(user.getName());
  95. }
  96. @Override
  97. public boolean sendMessage(final CommandSender user, final String message)
  98. {
  99. return instance.xmpp.sendMessage(instance.users.getAddress(user.getName()), message);
  100. }
  101. @Override
  102. public boolean sendMessage(final String address, final String message)
  103. {
  104. return instance.xmpp.sendMessage(address, message);
  105. }
  106. @Override
  107. public List<String> getSpyUsers()
  108. {
  109. return instance.users.getSpyUsers();
  110. }
  111. @Override
  112. public void broadcastMessage(final IUser sender, final String message, final String xmppAddress)
  113. {
  114. ess.broadcastMessage(sender, message);
  115. try
  116. {
  117. for (String address : getSpyUsers())
  118. {
  119. if (!address.equalsIgnoreCase(xmppAddress))
  120. {
  121. sendMessage(address, message);
  122. }
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. // Ignore exceptions
  128. }
  129. }
  130. }