SpoutTools.java 16 KB

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