SpoutStuff.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. package com.gmail.nossr50.spout;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.ArrayList;
  10. import java.util.jar.JarEntry;
  11. import java.util.jar.JarFile;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.Material;
  14. import org.bukkit.entity.Player;
  15. import org.getspout.spoutapi.SpoutManager;
  16. import org.getspout.spoutapi.event.spout.SpoutCraftEnableEvent;
  17. import org.getspout.spoutapi.keyboard.Keyboard;
  18. import org.getspout.spoutapi.player.SpoutPlayer;
  19. import com.gmail.nossr50.mcMMO;
  20. import com.gmail.nossr50.config.SpoutConfig;
  21. import com.gmail.nossr50.datatypes.PlayerProfile;
  22. import com.gmail.nossr50.datatypes.SkillType;
  23. import com.gmail.nossr50.listeners.SpoutListener;
  24. import com.gmail.nossr50.util.Misc;
  25. import com.gmail.nossr50.util.Users;
  26. public class SpoutStuff {
  27. private static mcMMO plugin = mcMMO.p;
  28. public final static String spoutDirectory = mcMMO.mainDirectory + "Resources" + File.separator;
  29. public final static String hudDirectory = spoutDirectory + "HUD" + File.separator;
  30. public final static String hudStandardDirectory = hudDirectory + "Standard" + File.separator;
  31. public final static String hudRetroDirectory = hudDirectory + "Retro" + File.separator;
  32. public final static String soundDirectory = spoutDirectory + "Sound" + File.separator;
  33. private final static SpoutListener spoutListener = new SpoutListener();
  34. public static Keyboard keypress;
  35. /**
  36. * Write file to disk.
  37. *
  38. * @param theFileName The name of the file
  39. * @param theFilePath The name of the file path
  40. */
  41. private static void writeFile(String theFileName, String theFilePath) {
  42. try {
  43. File currentFile = new File(theFilePath + theFileName);
  44. JarFile jar = new JarFile(mcMMO.mcmmo);
  45. JarEntry entry = jar.getJarEntry("resources/" + theFileName);
  46. InputStream is = jar.getInputStream(entry);
  47. byte[] buf = new byte[2048];
  48. int nbRead;
  49. OutputStream os = new BufferedOutputStream(new FileOutputStream(currentFile));
  50. while ((nbRead = is.read(buf)) != -1) {
  51. os.write(buf, 0, nbRead);
  52. }
  53. os.flush();
  54. os.close();
  55. jar.close();
  56. }
  57. catch (FileNotFoundException e) {
  58. e.printStackTrace();
  59. }
  60. catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. /**
  65. * Extract Spout files to the Resources directory.
  66. */
  67. public static void extractFiles() {
  68. //Setup directories
  69. new File(spoutDirectory).mkdir();
  70. new File(hudDirectory).mkdir();
  71. new File(hudStandardDirectory).mkdir();
  72. new File(hudRetroDirectory).mkdir();
  73. new File(soundDirectory).mkdir();
  74. //XP Bar images
  75. for (int x = 0; x < 255; x++) {
  76. String theFileName;
  77. if (x < 10) {
  78. theFileName = "xpbar_inc00" + x + ".png";
  79. }
  80. else if (x < 100) {
  81. theFileName = "xpbar_inc0" + x + ".png";
  82. }
  83. else {
  84. theFileName = "xpbar_inc" + x + ".png";
  85. }
  86. writeFile(theFileName, hudStandardDirectory);
  87. }
  88. //Standard XP Icons
  89. for (SkillType y : SkillType.values()) {
  90. if (y.equals(SkillType.ALL)) {
  91. continue;
  92. }
  93. String standardFileName = Misc.getCapitalized(y.toString())+".png";
  94. String retroFileName = Misc.getCapitalized(y.toString())+"_r.png";
  95. writeFile(standardFileName, hudStandardDirectory);
  96. writeFile(retroFileName, hudRetroDirectory);
  97. }
  98. //Blank icons
  99. writeFile("Icon.png", hudStandardDirectory);
  100. writeFile("Icon_r.png", hudRetroDirectory);
  101. //Sound FX
  102. writeFile("repair.wav", soundDirectory);
  103. writeFile("level.wav", soundDirectory);
  104. }
  105. /**
  106. * Setup Spout config options
  107. */
  108. public static void setupSpoutConfigs() {
  109. String temp = SpoutConfig.getInstance().getMenuKey();
  110. for (Keyboard x : Keyboard.values()) {
  111. if (x.toString().equalsIgnoreCase(temp)) {
  112. keypress = x;
  113. }
  114. }
  115. if (keypress == null) {
  116. System.out.println("Invalid KEY for Menu.Key, using KEY_M");
  117. keypress = Keyboard.KEY_M;
  118. }
  119. }
  120. /**
  121. * Get all the Spout files in the Resources folder.
  122. *
  123. * @return a list of all files is the Resources folder
  124. */
  125. public static ArrayList<File> getFiles() {
  126. ArrayList<File> files = new ArrayList<File>();
  127. /* XP BAR */
  128. for (int x = 0; x < 255; x++) {
  129. if (x < 10) {
  130. files.add(new File(hudStandardDirectory + "xpbar_inc00" + x + ".png"));
  131. }
  132. else if (x < 100) {
  133. files.add(new File(hudStandardDirectory + "xpbar_inc0" + x + ".png"));
  134. }
  135. else {
  136. files.add(new File(hudStandardDirectory + "xpbar_inc" + x + ".png"));
  137. }
  138. }
  139. /* Standard XP Icons */
  140. for (SkillType y : SkillType.values()) {
  141. if (y.equals(SkillType.ALL)) {
  142. continue;
  143. }
  144. files.add(new File(hudStandardDirectory + Misc.getCapitalized(y.toString()) + ".png"));
  145. files.add(new File(hudRetroDirectory + Misc.getCapitalized(y.toString()) + "_r.png"));
  146. }
  147. /* Blank icons */
  148. files.add(new File(hudStandardDirectory + "Icon.png"));
  149. files.add(new File(hudRetroDirectory + "Icon_r.png"));
  150. //Repair SFX
  151. files.add(new File(soundDirectory + "repair.wav"));
  152. //Level SFX
  153. files.add(new File(soundDirectory + "level.wav"));
  154. return files;
  155. }
  156. /**
  157. * Register custom Spout events.
  158. */
  159. public static void registerCustomEvent() {
  160. plugin.getServer().getPluginManager().registerEvents(spoutListener, plugin);
  161. }
  162. /**
  163. * Handle level-up notifications through Spout.
  164. *
  165. * @param skillType The skill that leveled up
  166. * @param spoutPlayer The player that leveled up
  167. */
  168. public static void levelUpNotification(SkillType skillType, SpoutPlayer spoutPlayer) {
  169. PlayerProfile profile = Users.getProfile(spoutPlayer);
  170. int notificationTier = getNotificationTier(profile.getSkillLevel(skillType));
  171. Material mat = null;
  172. switch (skillType) {
  173. case TAMING:
  174. switch (notificationTier) {
  175. case 1:
  176. case 2:
  177. mat = Material.PORK;
  178. break;
  179. case 3:
  180. case 4:
  181. mat = Material.GRILLED_PORK;
  182. break;
  183. case 5:
  184. mat = Material.BONE;
  185. break;
  186. default:
  187. break;
  188. }
  189. break;
  190. case MINING:
  191. switch (notificationTier) {
  192. case 1:
  193. mat = Material.COAL_ORE;
  194. break;
  195. case 2:
  196. mat = Material.IRON_ORE;
  197. break;
  198. case 3:
  199. mat = Material.GOLD_ORE;
  200. break;
  201. case 4:
  202. mat = Material.LAPIS_ORE;
  203. break;
  204. case 5:
  205. mat = Material.DIAMOND_ORE;
  206. break;
  207. default:
  208. break;
  209. }
  210. break;
  211. case WOODCUTTING:
  212. switch (notificationTier) {
  213. case 1:
  214. case 2:
  215. case 3:
  216. mat = Material.WOOD;
  217. break;
  218. case 4:
  219. case 5:
  220. mat = Material.LOG;
  221. break;
  222. default:
  223. break;
  224. }
  225. break;
  226. case REPAIR:
  227. switch (notificationTier) {
  228. case 1:
  229. mat = Material.COBBLESTONE;
  230. break;
  231. case 2:
  232. mat = Material.IRON_BLOCK;
  233. break;
  234. case 3:
  235. mat = Material.GOLD_BLOCK;
  236. break;
  237. case 4:
  238. mat = Material.LAPIS_BLOCK;
  239. break;
  240. case 5:
  241. mat = Material.DIAMOND_BLOCK;
  242. break;
  243. default:
  244. break;
  245. }
  246. break;
  247. case HERBALISM:
  248. switch (notificationTier) {
  249. case 1:
  250. mat = Material.YELLOW_FLOWER;
  251. break;
  252. case 2:
  253. mat = Material.RED_ROSE;
  254. break;
  255. case 3:
  256. mat = Material.BROWN_MUSHROOM;
  257. break;
  258. case 4:
  259. mat = Material.RED_MUSHROOM;
  260. break;
  261. case 5:
  262. mat = Material.PUMPKIN;
  263. break;
  264. default:
  265. break;
  266. }
  267. break;
  268. case ACROBATICS:
  269. switch (notificationTier) {
  270. case 1:
  271. mat = Material.LEATHER_BOOTS;
  272. break;
  273. case 2:
  274. mat = Material.CHAINMAIL_BOOTS;
  275. break;
  276. case 3:
  277. mat = Material.IRON_BOOTS;
  278. break;
  279. case 4:
  280. mat = Material.GOLD_BOOTS;
  281. break;
  282. case 5:
  283. mat = Material.DIAMOND_BOOTS;
  284. break;
  285. default:
  286. break;
  287. }
  288. break;
  289. case SWORDS:
  290. switch (notificationTier) {
  291. case 1:
  292. mat = Material.WOOD_SWORD;
  293. break;
  294. case 2:
  295. mat = Material.STONE_SWORD;
  296. break;
  297. case 3:
  298. mat = Material.IRON_SWORD;
  299. break;
  300. case 4:
  301. mat = Material.GOLD_SWORD;
  302. break;
  303. case 5:
  304. mat = Material.DIAMOND_SWORD;
  305. break;
  306. default:
  307. break;
  308. }
  309. break;
  310. case ARCHERY:
  311. switch (notificationTier) {
  312. case 1:
  313. case 2:
  314. case 3:
  315. mat = Material.ARROW;
  316. break;
  317. case 4:
  318. case 5:
  319. mat = Material.BOW;
  320. break;
  321. default:
  322. break;
  323. }
  324. break;
  325. case UNARMED:
  326. switch (notificationTier) {
  327. case 1:
  328. mat = Material.LEATHER_HELMET;
  329. break;
  330. case 2:
  331. mat = Material.CHAINMAIL_HELMET;
  332. break;
  333. case 3:
  334. mat = Material.IRON_HELMET;
  335. break;
  336. case 4:
  337. mat = Material.GOLD_HELMET;
  338. break;
  339. case 5:
  340. mat = Material.DIAMOND_HELMET;
  341. break;
  342. default:
  343. break;
  344. }
  345. break;
  346. case EXCAVATION:
  347. switch (notificationTier) {
  348. case 1:
  349. mat = Material.WOOD_SPADE;
  350. break;
  351. case 2:
  352. mat = Material.STONE_SPADE;
  353. break;
  354. case 3:
  355. mat = Material.IRON_SPADE;
  356. break;
  357. case 4:
  358. mat = Material.GOLD_SPADE;
  359. break;
  360. case 5:
  361. mat = Material.DIAMOND_SPADE;
  362. break;
  363. default:
  364. break;
  365. }
  366. break;
  367. case AXES:
  368. switch (notificationTier) {
  369. case 1:
  370. mat = Material.WOOD_AXE;
  371. break;
  372. case 2:
  373. mat = Material.STONE_AXE;
  374. break;
  375. case 3:
  376. mat = Material.IRON_AXE;
  377. break;
  378. case 4:
  379. mat = Material.GOLD_AXE;
  380. break;
  381. case 5:
  382. mat = Material.DIAMOND_AXE;
  383. break;
  384. default:
  385. break;
  386. }
  387. break;
  388. case FISHING:
  389. switch (notificationTier) {
  390. case 1:
  391. case 2:
  392. mat = Material.RAW_FISH;
  393. break;
  394. case 3:
  395. case 4:
  396. mat = Material.COOKED_FISH;
  397. break;
  398. case 5:
  399. mat = Material.FISHING_ROD;
  400. break;
  401. default:
  402. break;
  403. }
  404. break;
  405. default:
  406. mat = Material.WATCH;
  407. break;
  408. }
  409. //TODO: Use Locale
  410. spoutPlayer.sendNotification(ChatColor.GREEN + "Level Up!", ChatColor.YELLOW + Misc.getCapitalized(skillType.toString()) + ChatColor.DARK_AQUA + " (" + ChatColor.GREEN + profile.getSkillLevel(skillType) + ChatColor.DARK_AQUA + ")", mat);
  411. SpoutSounds.playLevelUpNoise(spoutPlayer, plugin);
  412. }
  413. /**
  414. * Gets the notification tier of a skill.
  415. *
  416. * @param level The level of the skill
  417. * @return the notification tier of the skill
  418. */
  419. private static Integer getNotificationTier(Integer level) {
  420. if (level >= 800) {
  421. return 5;
  422. }
  423. else if (level >= 600) {
  424. return 4;
  425. }
  426. else if (level >= 400) {
  427. return 3;
  428. }
  429. else if (level >= 200) {
  430. return 2;
  431. }
  432. else {
  433. return 1;
  434. }
  435. }
  436. /**
  437. * Re-enable SpoutCraft for players after a /reload
  438. */
  439. public static void reloadSpoutPlayers() {
  440. for (SpoutPlayer spoutPlayer : SpoutManager.getPlayerManager().getOnlinePlayers()) {
  441. SpoutCraftEnableEvent spoutCraftEnableEvent = new SpoutCraftEnableEvent(spoutPlayer);
  442. mcMMO.p.getServer().getPluginManager().callEvent(spoutCraftEnableEvent);
  443. }
  444. }
  445. public static void reloadSpoutPlayer(Player player) {
  446. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  447. if (spoutPlayer != null) {
  448. SpoutCraftEnableEvent spoutCraftEnableEvent = new SpoutCraftEnableEvent(spoutPlayer);
  449. mcMMO.p.getServer().getPluginManager().callEvent(spoutCraftEnableEvent);
  450. }
  451. }
  452. }