SpoutStuff.java 16 KB

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