PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/milkbowl/vault/chat/plugins/Chat_iChat.java

https://gitlab.com/MineYourMind/Vault
Java | 273 lines | 209 code | 48 blank | 16 comment | 29 complexity | dc58d069bc7cc140b41c42df1f869930 MD5 | raw file
  1. /* This file is part of Vault.
  2. Vault is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Lesser General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. Vault is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with Vault. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package net.milkbowl.vault.chat.plugins;
  14. import java.util.logging.Logger;
  15. import net.TheDgtl.iChat.iChat;
  16. import net.TheDgtl.iChat.iChatAPI;
  17. import net.milkbowl.vault.chat.Chat;
  18. import net.milkbowl.vault.permission.Permission;
  19. import org.bukkit.Bukkit;
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.event.EventHandler;
  22. import org.bukkit.event.EventPriority;
  23. import org.bukkit.event.Listener;
  24. import org.bukkit.event.server.PluginDisableEvent;
  25. import org.bukkit.event.server.PluginEnableEvent;
  26. import org.bukkit.plugin.Plugin;
  27. public class Chat_iChat extends Chat {
  28. private static final Logger log = Logger.getLogger("Minecraft");
  29. private final String name = "iChat";
  30. private Plugin plugin = null;
  31. private iChatAPI iChat = null;
  32. public Chat_iChat(Plugin plugin, Permission perms) {
  33. super(perms);
  34. this.plugin = plugin;
  35. Bukkit.getServer().getPluginManager().registerEvents(new PermissionServerListener(this), plugin);
  36. // Load Plugin in case it was loaded before
  37. if (iChat == null) {
  38. Plugin chat = plugin.getServer().getPluginManager().getPlugin("iChat");
  39. if (chat != null) {
  40. iChat = ((iChat) chat).API;
  41. log.info(String.format("[%s][Chat] %s hooked.", plugin.getDescription().getName(), "iChat"));
  42. }
  43. }
  44. }
  45. public class PermissionServerListener implements Listener {
  46. Chat_iChat chat = null;
  47. public PermissionServerListener(Chat_iChat chat) {
  48. this.chat = chat;
  49. }
  50. @EventHandler(priority = EventPriority.MONITOR)
  51. public void onPluginEnable(PluginEnableEvent event) {
  52. if (this.chat.iChat == null) {
  53. Plugin chat = event.getPlugin();
  54. if (chat.getDescription().getName().equals("iChat")) {
  55. this.chat.iChat = ((iChat) chat).API;
  56. log.info(String.format("[%s][Chat] %s hooked.", plugin.getDescription().getName(), "iChat"));
  57. }
  58. }
  59. }
  60. @EventHandler(priority = EventPriority.MONITOR)
  61. public void onPluginDisable(PluginDisableEvent event) {
  62. if (this.chat.iChat != null) {
  63. if (event.getPlugin().getDescription().getName().equals("iChat")) {
  64. this.chat.iChat = null;
  65. log.info(String.format("[%s][Chat] %s un-hooked.", plugin.getDescription().getName(), "iChat"));
  66. }
  67. }
  68. }
  69. }
  70. @Override
  71. public String getName() {
  72. return name;
  73. }
  74. @Override
  75. public boolean isEnabled() {
  76. return iChat != null;
  77. }
  78. @Override
  79. public String getPlayerPrefix(String world, String player) {
  80. Player p = plugin.getServer().getPlayer(player);
  81. if (p == null) {
  82. throw new UnsupportedOperationException("iChat does not support offline player info nodes!");
  83. }
  84. if (!p.getWorld().getName().equals(world)) {
  85. return null;
  86. }
  87. return iChat.getPrefix(p);
  88. }
  89. @Override
  90. public void setPlayerPrefix(String world, String player, String prefix) {
  91. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  92. }
  93. @Override
  94. public String getPlayerSuffix(String world, String player) {
  95. Player p = plugin.getServer().getPlayer(player);
  96. if (p == null) {
  97. throw new UnsupportedOperationException("iChat does not support offline player info nodes!");
  98. }
  99. if (!p.getWorld().getName().equals(world)) {
  100. return null;
  101. }
  102. return iChat.getSuffix(p);
  103. }
  104. @Override
  105. public void setPlayerSuffix(String world, String player, String suffix) {
  106. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  107. }
  108. @Override
  109. public String getGroupPrefix(String world, String group) {
  110. throw new UnsupportedOperationException("iChat does not support group info nodes!");
  111. }
  112. @Override
  113. public void setGroupPrefix(String world, String group, String prefix) {
  114. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  115. }
  116. @Override
  117. public String getGroupSuffix(String world, String group) {
  118. throw new UnsupportedOperationException("iChat does not support group info nodes!");
  119. }
  120. @Override
  121. public void setGroupSuffix(String world, String group, String suffix) {
  122. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  123. }
  124. @Override
  125. public int getPlayerInfoInteger(String world, String player, String node, int defaultValue) {
  126. String val = getPlayerInfoString(world, player, node, null);
  127. if (val == null) {
  128. return defaultValue;
  129. }
  130. Integer i = defaultValue;
  131. try {
  132. i = Integer.valueOf(val);
  133. return i;
  134. } catch (NumberFormatException e) {
  135. return defaultValue;
  136. }
  137. }
  138. @Override
  139. public void setPlayerInfoInteger(String world, String player, String node, int value) {
  140. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  141. }
  142. @Override
  143. public int getGroupInfoInteger(String world, String group, String node, int defaultValue) {
  144. throw new UnsupportedOperationException("iChat does not support group info nodes!");
  145. }
  146. @Override
  147. public void setGroupInfoInteger(String world, String group, String node, int value) {
  148. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  149. }
  150. @Override
  151. public double getPlayerInfoDouble(String world, String player, String node, double defaultValue) {
  152. String val = getPlayerInfoString(world, player, node, null);
  153. if (val == null) {
  154. return defaultValue;
  155. }
  156. double d = defaultValue;
  157. try {
  158. d = Double.valueOf(val);
  159. return d;
  160. } catch (NumberFormatException e) {
  161. return defaultValue;
  162. }
  163. }
  164. @Override
  165. public void setPlayerInfoDouble(String world, String player, String node, double value) {
  166. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  167. }
  168. @Override
  169. public double getGroupInfoDouble(String world, String group, String node, double defaultValue) {
  170. throw new UnsupportedOperationException("iChat does not support group info nodes!");
  171. }
  172. @Override
  173. public void setGroupInfoDouble(String world, String group, String node, double value) {
  174. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  175. }
  176. @Override
  177. public boolean getPlayerInfoBoolean(String world, String player, String node, boolean defaultValue) {
  178. String val = getPlayerInfoString(world, player, node, null);
  179. if (val == null) {
  180. return defaultValue;
  181. } else {
  182. Boolean v = Boolean.valueOf(val);
  183. return v != null ? v : defaultValue;
  184. }
  185. }
  186. @Override
  187. public void setPlayerInfoBoolean(String world, String player, String node, boolean value) {
  188. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  189. }
  190. @Override
  191. public boolean getGroupInfoBoolean(String world, String group, String node, boolean defaultValue) {
  192. throw new UnsupportedOperationException("iChat does not support group info nodes!");
  193. }
  194. @Override
  195. public void setGroupInfoBoolean(String world, String group, String node, boolean value) {
  196. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  197. }
  198. @Override
  199. public String getPlayerInfoString(String world, String player, String node, String defaultValue) {
  200. Player p = plugin.getServer().getPlayer(player);
  201. if (p == null) {
  202. throw new UnsupportedOperationException("iChat does not support offline player info nodes!");
  203. }
  204. if (!p.getWorld().getName().equals(world)) {
  205. return null;
  206. }
  207. String val = iChat.getInfo(p, node);
  208. return val != null ? val : defaultValue;
  209. }
  210. @Override
  211. public void setPlayerInfoString(String world, String player, String node, String value) {
  212. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  213. }
  214. @Override
  215. public String getGroupInfoString(String world, String group, String node, String defaultValue) {
  216. throw new UnsupportedOperationException("iChat does not support group info nodes!");
  217. }
  218. @Override
  219. public void setGroupInfoString(String world, String group, String node, String value) {
  220. throw new UnsupportedOperationException("iChat does not support mutable info nodes!");
  221. }
  222. }