SpoutStuff.java 16 KB

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