SpoutStuff.java 15 KB

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