PageRenderTime 121ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/bukkitcontrib/BukkitContrib.java

https://github.com/Nightgunner5/BukkitContrib
Java | 270 lines | 213 code | 23 blank | 34 comment | 19 complexity | 19386897cfbc72cd55be6eb6058cf0ee MD5 | raw file
  1. package org.bukkitcontrib;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.nio.channels.Channels;
  9. import java.nio.channels.ReadableByteChannel;
  10. import java.util.logging.Logger;
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.Event.Priority;
  15. import org.bukkit.event.Event.Type;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17. import org.bukkit.util.FileUtil;
  18. import org.bukkitcontrib.block.ContribCraftChunk;
  19. import org.bukkitcontrib.config.ConfigReader;
  20. import org.bukkitcontrib.inventory.ItemManager;
  21. import org.bukkitcontrib.inventory.SimpleItemManager;
  22. import org.bukkitcontrib.keyboard.KeyboardManager;
  23. import org.bukkitcontrib.keyboard.SimpleKeyboardManager;
  24. import org.bukkitcontrib.packet.CustomPacket;
  25. import org.bukkitcontrib.packet.PacketPluginReload;
  26. import org.bukkitcontrib.packet.PacketRenderDistance;
  27. import org.bukkitcontrib.player.AppearanceManager;
  28. import org.bukkitcontrib.player.ContribCraftPlayer;
  29. import org.bukkitcontrib.player.ContribPlayer;
  30. import org.bukkitcontrib.player.SimpleAppearanceManager;
  31. import org.bukkitcontrib.player.SimpleSkyManager;
  32. import org.bukkitcontrib.player.SkyManager;
  33. import org.bukkitcontrib.sound.SimpleSoundManager;
  34. import org.bukkitcontrib.sound.SoundManager;
  35. public class BukkitContrib extends JavaPlugin{
  36. public static final ContribPlayerListener playerListener = new ContribPlayerListener();
  37. private static final ContribChunkListener chunkListener = new ContribChunkListener();
  38. private static final PluginListener pluginListener = new PluginListener();
  39. private static final SimpleKeyboardManager keyManager = new SimpleKeyboardManager();
  40. private static final SimpleAppearanceManager appearanceManager = new SimpleAppearanceManager();
  41. private static final SimpleSoundManager soundManager = new SimpleSoundManager();
  42. private static final SimpleItemManager itemManager = new SimpleItemManager();
  43. private static final SimpleSkyManager skyManager = new SimpleSkyManager();
  44. private static BukkitContrib instance;
  45. @Override
  46. public void onDisable() {
  47. //order matters
  48. appearanceManager.onPluginDisable();
  49. itemManager.reset();
  50. skyManager.reset();
  51. Player[] online = getServer().getOnlinePlayers();
  52. for (Player player : online) {
  53. try {
  54. ContribCraftPlayer ccp = (ContribCraftPlayer) ContribCraftPlayer.getContribPlayer(player);
  55. if (ccp.getVersion() > 5)
  56. ccp.sendPacket(new PacketRenderDistance(true, true));
  57. if (ccp.getVersion() > 4)
  58. ccp.sendPacket(new PacketPluginReload((ContribCraftPlayer)player));
  59. }
  60. catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. for (Player player : online) {
  65. ContribCraftPlayer.removeBukkitEntity(player);
  66. ContribCraftPlayer.resetNetServerHandler(player);
  67. }
  68. ContribCraftChunk.resetAllBukkitChunks();
  69. getServer().getScheduler().cancelTasks(this);
  70. //Attempt to auto update if file is available
  71. try {
  72. File directory = new File(Bukkit.getServer().getUpdateFolder());
  73. if (directory.exists()) {
  74. File plugin = new File(directory.getPath(), "BukkitContrib.jar");
  75. if (plugin.exists()) {
  76. FileUtil.copy(plugin, this.getFile());
  77. plugin.delete();
  78. }
  79. }
  80. }
  81. catch (Exception e) {}
  82. }
  83. @Override
  84. public void onEnable() {
  85. BukkitContrib.instance = this;
  86. (new ConfigReader()).read();
  87. (new Thread() {
  88. public void run() {
  89. update();
  90. }
  91. }).start();
  92. getServer().getPluginManager().registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Lowest, this);
  93. getServer().getPluginManager().registerEvent(Type.PLAYER_TELEPORT, playerListener, Priority.Monitor, this);
  94. getServer().getPluginManager().registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Monitor, this);
  95. getServer().getPluginManager().registerEvent(Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Monitor, this);
  96. getServer().getPluginManager().registerEvent(Type.CHUNK_LOAD, chunkListener, Priority.Lowest, this);
  97. getServer().getPluginManager().registerEvent(Type.PLUGIN_DISABLE, pluginListener, Priority.Normal, this);
  98. Player[] online = getServer().getOnlinePlayers();
  99. for (Player player : online) {
  100. ContribCraftPlayer.removeBukkitEntity(player);
  101. ContribCraftPlayer.resetNetServerHandler(player);
  102. ContribCraftPlayer.updateNetServerHandler(player);
  103. ContribCraftPlayer.updateBukkitEntity(player);
  104. sendBukkitContribVersionChat(player);
  105. playerListener.manager.onPlayerJoin(player);
  106. }
  107. ContribCraftChunk.replaceAllBukkitChunks();
  108. appearanceManager.onPluginEnable();
  109. //Start counting ticks
  110. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new ServerTickTask(), 0, 1);
  111. //Remove mappings from previous loads
  112. //Can not remove them on disable because the packets will still be in the send queue
  113. CustomPacket.removeClassMapping();
  114. CustomPacket.addClassMapping();
  115. Logger.getLogger("Minecraft").info("BukkitContrib " + this.getDescription().getVersion() + " has been initialized");
  116. }
  117. /**
  118. * Gets the singleton instance of the bukkitcontrib plugin
  119. * @return bukkitcontrib plugin
  120. */
  121. public static BukkitContrib getInstance() {
  122. return instance;
  123. }
  124. /**
  125. * Gets the keyboard manager
  126. * @return keyboard manager
  127. */
  128. public static KeyboardManager getKeyboardManager() {
  129. return keyManager;
  130. }
  131. /**
  132. * Gets the appearance manager
  133. * @return appearance manager
  134. */
  135. public static AppearanceManager getAppearanceManager() {
  136. return appearanceManager;
  137. }
  138. /**
  139. * Gets the sound manager
  140. * @return sound manager
  141. */
  142. public static SoundManager getSoundManager() {
  143. return soundManager;
  144. }
  145. /**
  146. * Gets the item manager
  147. * @return item manager
  148. */
  149. public static ItemManager getItemManager() {
  150. return itemManager;
  151. }
  152. /**
  153. * Gets the sky manager
  154. * @return sky manager
  155. */
  156. public static SkyManager getSkyManager() {
  157. return skyManager;
  158. }
  159. /**
  160. * Gets the contrib player from the given player id, or null if no such player exists
  161. * @param entityId of the player
  162. * @return contrib player
  163. */
  164. public static ContribPlayer getPlayerFromId(int entityId) {
  165. Player[] online = Bukkit.getServer().getOnlinePlayers();
  166. for (Player player : online) {
  167. if (player.getEntityId() == entityId) {
  168. return (ContribPlayer)ContribCraftPlayer.getContribPlayer(player);
  169. }
  170. }
  171. return null;
  172. }
  173. private static String versionToString(String version) {
  174. String split[] = version.split("\\.");
  175. return ChatColor.getByCode(Integer.parseInt(split[0])).toString() + ChatColor.WHITE.toString() +
  176. ChatColor.getByCode(Integer.parseInt(split[1])) + ChatColor.WHITE.toString() +
  177. ChatColor.getByCode(Integer.parseInt(split[2]));
  178. }
  179. @SuppressWarnings("unused")
  180. private static String colorToString(String color) {
  181. String s = "";
  182. String split[] = color.split(ChatColor.WHITE.toString());
  183. for (int i = 0; i < split.length; i++) {
  184. int code = 0;
  185. for (int j = 0; j < split[i].length(); j++) {
  186. code += (int)(split[i].charAt(j));
  187. }
  188. s += (char)(code - ChatColor.BLACK.toString().charAt(0));
  189. }
  190. return s;
  191. }
  192. protected static void sendBukkitContribVersionChat(Player player) {
  193. player.sendRawMessage(versionToString(BukkitContrib.getInstance().getDescription().getVersion()));
  194. }
  195. protected int getVersion() {
  196. try {
  197. String[] split = this.getDescription().getVersion().split("\\.");
  198. return Integer.parseInt(split[0]) * 100 + Integer.parseInt(split[1]) * 10 + Integer.parseInt(split[2]);
  199. }
  200. catch (Exception e) {}
  201. return -1;
  202. }
  203. protected boolean isUpdateAvailable() {
  204. if (!ConfigReader.isAutoUpdate()) {
  205. return false;
  206. }
  207. try {
  208. URL url = new URL("http://dl.dropbox.com/u/49805/BukkitContribVersion.txt");
  209. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  210. String str;
  211. while ((str = in.readLine()) != null) {
  212. String[] split = str.split("\\.");
  213. int version = Integer.parseInt(split[0]) * 100 + Integer.parseInt(split[1]) * 10 + Integer.parseInt(split[2]);
  214. if (version > getVersion()){
  215. in.close();
  216. return true;
  217. }
  218. }
  219. in.close();
  220. }
  221. catch (Exception e) {}
  222. return false;
  223. }
  224. protected void update() {
  225. if (!isUpdateAvailable()) {
  226. return;
  227. }
  228. try {
  229. File directory = new File(Bukkit.getServer().getUpdateFolder());
  230. if (!directory.exists()) {
  231. directory.mkdir();
  232. }
  233. File plugin = new File(directory.getPath(), "BukkitContrib.jar");
  234. if (!plugin.exists()) {
  235. URL bukkitContrib = new URL("http://bit.ly/autoupdateBukkitContrib");
  236. HttpURLConnection con = (HttpURLConnection)(bukkitContrib.openConnection());
  237. System.setProperty("http.agent", ""); //Spoofing the user agent is required to track stats
  238. con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30");
  239. ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
  240. FileOutputStream fos = new FileOutputStream(plugin);
  241. fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  242. fos.close();
  243. }
  244. }
  245. catch (Exception e) {}
  246. }
  247. }