PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/MineYourMind/Vault
Java | 284 lines | 228 code | 40 blank | 16 comment | 43 complexity | 9501b5a8ae31ee5b24ef46abe4e19736 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 com.miraclem4n.mchat.api.Reader;
  15. import com.miraclem4n.mchat.api.Writer;
  16. import com.miraclem4n.mchat.types.InfoType;
  17. import in.mDev.MiracleM4n.mChatSuite.mChatSuite;
  18. import java.util.logging.Logger;
  19. import net.milkbowl.vault.chat.Chat;
  20. import net.milkbowl.vault.permission.Permission;
  21. import org.bukkit.Bukkit;
  22. import org.bukkit.event.EventHandler;
  23. import org.bukkit.event.EventPriority;
  24. import org.bukkit.event.Listener;
  25. import org.bukkit.event.server.PluginDisableEvent;
  26. import org.bukkit.event.server.PluginEnableEvent;
  27. import org.bukkit.plugin.Plugin;
  28. public class Chat_mChatSuite extends Chat {
  29. private static final Logger log = Logger.getLogger("Minecraft");
  30. private final String name = "mChatSuite";
  31. private Plugin plugin = null;
  32. private mChatSuite mChat = null;
  33. public Chat_mChatSuite(Plugin plugin, Permission perms) {
  34. super(perms);
  35. this.plugin = plugin;
  36. Bukkit.getServer().getPluginManager().registerEvents(new PermissionServerListener(), plugin);
  37. // Load Plugin in case it was loaded before
  38. if (mChat == null) {
  39. Plugin chat = plugin.getServer().getPluginManager().getPlugin("mChatSuite");
  40. if (chat != null && chat.isEnabled()) {
  41. mChat = (mChatSuite) chat;
  42. log.info(String.format("[%s][Chat] %s hooked.", plugin.getDescription().getName(), "mChatSuite"));
  43. }
  44. }
  45. }
  46. public class PermissionServerListener implements Listener {
  47. @EventHandler(priority = EventPriority.MONITOR)
  48. public void onPluginEnable(PluginEnableEvent event) {
  49. if (mChat == null) {
  50. Plugin chat = event.getPlugin();
  51. if (chat.getDescription().getName().equals("mChatSuite")) {
  52. mChat = (mChatSuite) chat;
  53. log.info(String.format("[%s][Chat] %s hooked.", plugin.getDescription().getName(), "mChatSuite"));
  54. }
  55. }
  56. }
  57. @EventHandler(priority = EventPriority.MONITOR)
  58. public void onPluginDisable(PluginDisableEvent event) {
  59. if (mChat != null) {
  60. if (event.getPlugin().getDescription().getName().equals("mChatSuite")) {
  61. mChat = null;
  62. log.info(String.format("[%s][Chat] %s un-hooked.", plugin.getDescription().getName(), "mChatSuite"));
  63. }
  64. }
  65. }
  66. }
  67. @Override
  68. public String getName() {
  69. return name;
  70. }
  71. @Override
  72. public boolean isEnabled() {
  73. return mChat != null && mChat.isEnabled();
  74. }
  75. @Override
  76. public String getPlayerPrefix(String world, String player) {
  77. return Reader.getPrefix(player, InfoType.USER, world);
  78. }
  79. @Override
  80. public void setPlayerPrefix(String world, String player, String prefix) {
  81. setPlayerInfoValue(world, player, "prefix", prefix);
  82. }
  83. @Override
  84. public String getPlayerSuffix(String world, String player) {
  85. return Reader.getSuffix(player, InfoType.USER, world);
  86. }
  87. @Override
  88. public void setPlayerSuffix(String world, String player, String suffix) {
  89. setPlayerInfoValue(world, player, "suffix", suffix);
  90. }
  91. @Override
  92. public String getGroupPrefix(String world, String group) {
  93. return Reader.getPrefix(group, InfoType.GROUP, world);
  94. }
  95. @Override
  96. public void setGroupPrefix(String world, String group, String prefix) {
  97. setGroupInfoValue(world, group, "prefix", prefix);
  98. }
  99. @Override
  100. public String getGroupSuffix(String world, String group) {
  101. return Reader.getSuffix(group, InfoType.GROUP, world);
  102. }
  103. @Override
  104. public void setGroupSuffix(String world, String group, String suffix) {
  105. setGroupInfoValue(world, group, "suffix", suffix);
  106. }
  107. @Override
  108. public int getPlayerInfoInteger(String world, String player, String node, int defaultValue) {
  109. String val = getPlayerInfoValue(world, player, node);
  110. if (val == null || val.equals("")) {
  111. return defaultValue;
  112. }
  113. try {
  114. return Integer.parseInt(val);
  115. } catch (NumberFormatException e) {
  116. return defaultValue;
  117. }
  118. }
  119. @Override
  120. public void setPlayerInfoInteger(String world, String player, String node, int value) {
  121. setPlayerInfoValue(world, player, node, value);
  122. }
  123. @Override
  124. public int getGroupInfoInteger(String world, String group, String node, int defaultValue) {
  125. String val = getGroupInfoValue(world, group, node);
  126. if (val == null || val.equals("")) {
  127. return defaultValue;
  128. }
  129. try {
  130. return Integer.parseInt(val);
  131. } catch (NumberFormatException e) {
  132. return defaultValue;
  133. }
  134. }
  135. @Override
  136. public void setGroupInfoInteger(String world, String group, String node, int value) {
  137. setGroupInfoValue(world, group, node, value);
  138. }
  139. @Override
  140. public double getPlayerInfoDouble(String world, String player, String node, double defaultValue) {
  141. String val = getPlayerInfoValue(world, player, node);
  142. if (val == null || val.equals("")) {
  143. return defaultValue;
  144. }
  145. try {
  146. return Double.parseDouble(val);
  147. } catch (NumberFormatException e) {
  148. return defaultValue;
  149. }
  150. }
  151. @Override
  152. public void setPlayerInfoDouble(String world, String player, String node, double value) {
  153. setPlayerInfoValue(world, player, node, value);
  154. }
  155. @Override
  156. public double getGroupInfoDouble(String world, String group, String node,double defaultValue) {
  157. String val = getGroupInfoValue(world, group, node);
  158. if (val == null || val.equals("")) {
  159. return defaultValue;
  160. }
  161. try {
  162. return Double.parseDouble(val);
  163. } catch (NumberFormatException e) {
  164. return defaultValue;
  165. }
  166. }
  167. @Override
  168. public void setGroupInfoDouble(String world, String group, String node, double value) {
  169. setGroupInfoValue(world, group, node, value);
  170. }
  171. @Override
  172. public boolean getPlayerInfoBoolean(String world, String player, String node, boolean defaultValue) {
  173. String val = getPlayerInfoValue(world, player, node);
  174. if (val == null || val.equals("")) {
  175. return defaultValue;
  176. }
  177. return Boolean.parseBoolean(val);
  178. }
  179. @Override
  180. public void setPlayerInfoBoolean(String world, String player, String node, boolean value) {
  181. setPlayerInfoValue(world, player, node, value);
  182. }
  183. @Override
  184. public boolean getGroupInfoBoolean(String world, String group, String node, boolean defaultValue) {
  185. String val = getGroupInfoValue(world, group, node);
  186. if (val == null || val.equals("")) {
  187. return defaultValue;
  188. }
  189. return Boolean.valueOf(val);
  190. }
  191. @Override
  192. public void setGroupInfoBoolean(String world, String group, String node, boolean value) {
  193. setGroupInfoValue(world, group, node, value);
  194. }
  195. @Override
  196. public String getPlayerInfoString(String world, String player, String node, String defaultValue) {
  197. String val = getPlayerInfoValue(world, player, node);
  198. if (val == null) {
  199. return defaultValue;
  200. } else {
  201. return val;
  202. }
  203. }
  204. @Override
  205. public void setPlayerInfoString(String world, String player, String node, String value) {
  206. setPlayerInfoValue(world, player, node, value);
  207. }
  208. @Override
  209. public String getGroupInfoString(String world, String group, String node, String defaultValue) {
  210. String val = getGroupInfoValue(world, group, node);
  211. if (val == null) {
  212. return defaultValue;
  213. } else {
  214. return val;
  215. }
  216. }
  217. @Override
  218. public void setGroupInfoString(String world, String group, String node, String value) {
  219. setGroupInfoValue(world, group, node, value);
  220. }
  221. private void setPlayerInfoValue(String world, String player, String node, Object value) {
  222. if (world != null) {
  223. Writer.setWorldVar(player, InfoType.USER, world, node, value.toString());
  224. } else {
  225. Writer.setInfoVar(player, InfoType.USER, node, value.toString());
  226. }
  227. }
  228. private void setGroupInfoValue(String world, String group, String node, Object value) {
  229. if (world != null) {
  230. Writer.setWorldVar(group, InfoType.GROUP, world, node, value);
  231. } else {
  232. Writer.setInfoVar(group, InfoType.GROUP, node, value);
  233. }
  234. }
  235. private String getPlayerInfoValue(String world, String player, String node) {
  236. return Reader.getInfo(player, InfoType.USER, world, node);
  237. }
  238. private String getGroupInfoValue(String world, String group, String node) {
  239. return Reader.getInfo(group, InfoType.GROUP, world, node);
  240. }
  241. }