SpoutStuff.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50.spout;
  15. import java.io.BufferedOutputStream;
  16. import java.io.File;
  17. import java.io.FileNotFoundException;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.jar.JarEntry;
  25. import java.util.jar.JarFile;
  26. import org.bukkit.Bukkit;
  27. import org.bukkit.ChatColor;
  28. import org.bukkit.Location;
  29. import org.bukkit.Material;
  30. import org.bukkit.entity.Player;
  31. import org.bukkit.event.Event;
  32. import org.bukkit.event.Event.Priority;
  33. import org.getspout.spoutapi.SpoutManager;
  34. import org.getspout.spoutapi.gui.Color;
  35. import org.getspout.spoutapi.keyboard.Keyboard;
  36. import org.getspout.spoutapi.player.SpoutPlayer;
  37. import org.getspout.spoutapi.sound.SoundEffect;
  38. import org.getspout.spoutapi.sound.SoundManager;
  39. import com.gmail.nossr50.Users;
  40. import com.gmail.nossr50.m;
  41. import com.gmail.nossr50.mcMMO;
  42. import com.gmail.nossr50.config.LoadProperties;
  43. import com.gmail.nossr50.datatypes.HUDType;
  44. import com.gmail.nossr50.datatypes.HUDmmo;
  45. import com.gmail.nossr50.datatypes.PlayerProfile;
  46. import com.gmail.nossr50.datatypes.popups.PopupMMO;
  47. import com.gmail.nossr50.datatypes.SkillType;
  48. import com.gmail.nossr50.listeners.mcSpoutInputListener;
  49. import com.gmail.nossr50.listeners.mcSpoutListener;
  50. import com.gmail.nossr50.listeners.mcSpoutScreenListener;
  51. public class SpoutStuff
  52. {
  53. static mcMMO plugin = (mcMMO) Bukkit.getServer().getPluginManager().getPlugin("mcMMO");
  54. private final static mcSpoutListener spoutListener = new mcSpoutListener(plugin);
  55. private final static mcSpoutInputListener spoutInputListener = new mcSpoutInputListener(plugin);
  56. private final static mcSpoutScreenListener spoutScreenListener = new mcSpoutScreenListener(plugin);
  57. public static HashMap<Player, HUDmmo> playerHUDs = new HashMap<Player, HUDmmo>();
  58. public static HashMap<SpoutPlayer, PopupMMO> playerScreens = new HashMap<SpoutPlayer, PopupMMO>();
  59. public static Keyboard keypress;
  60. public static void writeFile(String theFileName, String theFilePath)
  61. {
  62. try {
  63. File currentFile = new File("plugins/mcMMO/Resources/"+theFilePath+theFileName);
  64. @SuppressWarnings("static-access")
  65. JarFile jar = new JarFile(plugin.mcmmo);
  66. JarEntry entry = jar.getJarEntry("resources/"+theFileName);
  67. InputStream is = jar.getInputStream(entry);
  68. byte[] buf = new byte[2048];
  69. int nbRead;
  70. OutputStream os = new BufferedOutputStream(new FileOutputStream(currentFile));
  71. while((nbRead = is.read(buf)) != -1) {
  72. os.write(buf, 0, nbRead);
  73. }
  74. os.flush();
  75. os.close();
  76. } catch (FileNotFoundException e) {
  77. e.printStackTrace();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. public static void extractFiles()
  83. {
  84. //Setup directories
  85. new File("plugins/mcMMO/Resources/").mkdir();
  86. new File("plugins/mcMMO/Resources/HUD/").mkdir();
  87. new File("plugins/mcMMO/Resources/HUD/Standard/").mkdir();
  88. new File("plugins/mcMMO/Resources/HUD/Retro/").mkdir();
  89. new File("plugins/mcMMO/Resources/Sound/").mkdir();
  90. //Xp Bar images
  91. for(int x =0; x < 255; x++)
  92. {
  93. //String s = File.separator;
  94. String theFilePath = "HUD/Standard/";
  95. if(x < 10)
  96. {
  97. String theFileName = "xpbar_inc00"+x+".png";
  98. writeFile(theFileName, theFilePath);
  99. } else if (x < 100)
  100. {
  101. String theFileName = "xpbar_inc0"+x+".png";
  102. writeFile(theFileName, theFilePath);
  103. } else
  104. {
  105. String theFileName = "xpbar_inc"+x+".png";
  106. writeFile(theFileName, theFilePath);
  107. }
  108. }
  109. //Standard XP Icons
  110. String theFilePathA = "HUD/Standard/";
  111. String theFilePathB = "HUD/Retro/";
  112. for(SkillType y : SkillType.values())
  113. {
  114. if(y == SkillType.ALL || y == SkillType.ENCHANTING || y == SkillType.ALCHEMY)
  115. continue;
  116. String theFileNameA = m.getCapitalized(y.toString())+".png";
  117. String theFileNameB = m.getCapitalized(y.toString())+"_r.png";
  118. writeFile(theFileNameA, theFilePathA);
  119. writeFile(theFileNameB, theFilePathB);
  120. }
  121. //Blank icons
  122. writeFile("Icon.png", theFilePathA);
  123. writeFile("Icon_r.png", theFilePathB);
  124. String theSoundFilePath = "Sound/";
  125. //Repair SFX
  126. writeFile("repair.wav", theSoundFilePath);
  127. writeFile("level.wav", theSoundFilePath);
  128. }
  129. public static void setupSpoutConfigs()
  130. {
  131. String temp = LoadProperties.readString("Spout.Menu.Key", "KEY_M");
  132. for(Keyboard x : Keyboard.values())
  133. {
  134. if(x.toString().equalsIgnoreCase(temp))
  135. {
  136. keypress = x;
  137. }
  138. }
  139. if(keypress == null)
  140. {
  141. System.out.println("Invalid KEY for Spout.Menu.Key, using KEY_M");
  142. keypress = Keyboard.KEY_M;
  143. }
  144. }
  145. public static ArrayList<File> getFiles()
  146. {
  147. ArrayList<File> files = new ArrayList<File>();
  148. String dir = "plugins/mcMMO/Resources/";
  149. int x = 0;
  150. //XP BAR
  151. while(x < 255)
  152. {
  153. if(x < 10)
  154. {
  155. files.add(new File(dir+"HUD/Standard/xpbar_inc00"+x+".png"));
  156. } else if (x < 100)
  157. {
  158. files.add(new File(dir+"HUD/Standard/xpbar_inc0"+x+".png"));
  159. } else
  160. {
  161. files.add(new File(dir+"HUD/Standard/xpbar_inc"+x+".png"));
  162. }
  163. x++;
  164. }
  165. //Standard XP Icons
  166. for(SkillType y : SkillType.values())
  167. {
  168. if(y == SkillType.ALL || y == SkillType.ENCHANTING || y == SkillType.ALCHEMY)
  169. continue;
  170. files.add(new File(dir+"HUD/Standard/"+m.getCapitalized(y.toString())+".png"));
  171. files.add(new File(dir+"HUD/Retro/"+m.getCapitalized(y.toString())+"_r.png"));
  172. }
  173. //Blank icons
  174. files.add(new File(dir+"HUD/Standard/Icon.png"));
  175. files.add(new File(dir+"HUD/Retro/Icon_r.png"));
  176. //Repair SFX
  177. files.add(new File(dir+"Sound/repair.wav"));
  178. //Level SFX
  179. files.add(new File(dir+"Sound/level.wav"));
  180. return files;
  181. }
  182. public static void registerCustomEvent()
  183. {
  184. Bukkit.getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT, spoutListener, Priority.Normal, plugin);
  185. Bukkit.getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT, spoutInputListener, Priority.Normal, plugin);
  186. Bukkit.getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT, spoutScreenListener, Priority.Normal, plugin);
  187. }
  188. public static Color getRetroColor(SkillType type)
  189. {
  190. switch(type)
  191. {
  192. case ACROBATICS:
  193. return new Color((float) LoadProperties.acrobatics_r, (float) LoadProperties.acrobatics_g, (float) LoadProperties.acrobatics_b, 1);
  194. case ARCHERY:
  195. return new Color((float) LoadProperties.archery_r, (float)LoadProperties.archery_g, (float)LoadProperties.archery_b, 1f);
  196. case AXES:
  197. return new Color((float) LoadProperties.axes_r, (float)LoadProperties.axes_g, (float)LoadProperties.axes_b, 1f);
  198. case EXCAVATION:
  199. return new Color((float)LoadProperties.excavation_r, (float)LoadProperties.excavation_g, (float)LoadProperties.excavation_b, 1f);
  200. case HERBALISM:
  201. return new Color((float)LoadProperties.herbalism_r, (float)LoadProperties.herbalism_g, (float)LoadProperties.herbalism_b, 1f);
  202. case MINING:
  203. return new Color((float)LoadProperties.mining_r, (float)LoadProperties.mining_g, (float)LoadProperties.mining_b, 1f);
  204. case REPAIR:
  205. return new Color((float)LoadProperties.repair_r, (float)LoadProperties.repair_g, (float)LoadProperties.repair_b, 1f);
  206. case SWORDS:
  207. return new Color((float)LoadProperties.swords_r, (float)LoadProperties.swords_g, (float)LoadProperties.swords_b, 1f);
  208. case TAMING:
  209. return new Color((float)LoadProperties.taming_r, (float)LoadProperties.taming_g, (float)LoadProperties.taming_b, 1f);
  210. case UNARMED:
  211. return new Color((float)LoadProperties.unarmed_r, (float)LoadProperties.unarmed_g, (float)LoadProperties.unarmed_b, 1f);
  212. case WOODCUTTING:
  213. return new Color((float)LoadProperties.woodcutting_r, (float)LoadProperties.woodcutting_g, (float)LoadProperties.woodcutting_b, 1f);
  214. case FISHING:
  215. return new Color((float)LoadProperties.fishing_r, (float)LoadProperties.fishing_g, (float)LoadProperties.fishing_b, 1f);
  216. default:
  217. return new Color(0.3f, 0.3f, 0.75f, 1f);
  218. }
  219. }
  220. public static SpoutPlayer getSpoutPlayer(String playerName)
  221. {
  222. for(Player x : Bukkit.getServer().getOnlinePlayers())
  223. {
  224. if(x.getName().equalsIgnoreCase(playerName))
  225. {
  226. return SpoutManager.getPlayer(x);
  227. }
  228. }
  229. return null;
  230. }
  231. public static void playSoundForPlayer(SoundEffect effect, Player player, Location location)
  232. {
  233. //Contrib stuff
  234. SoundManager SM = SpoutManager.getSoundManager();
  235. SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
  236. SM.playSoundEffect(sPlayer, effect, location);
  237. }
  238. public static void playRepairNoise(Player player)
  239. {
  240. SoundManager SM = SpoutManager.getSoundManager();
  241. SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
  242. SM.playCustomSoundEffect(Bukkit.getServer().getPluginManager().getPlugin("mcMMO"), sPlayer, "http://mcmmo.rycochet.net/mcmmo/Sound/repair.wav", false);
  243. }
  244. public static void playLevelUpNoise(Player player)
  245. {
  246. SoundManager SM = SpoutManager.getSoundManager();
  247. SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
  248. SM.playCustomSoundEffect(Bukkit.getServer().getPluginManager().getPlugin("mcMMO"), sPlayer, "http://mcmmo.rycochet.net/mcmmo/Sound/level.wav", false);
  249. }
  250. public static void levelUpNotification(SkillType skillType, SpoutPlayer sPlayer)
  251. {
  252. PlayerProfile PP = Users.getProfile(sPlayer);
  253. Material mat = null;
  254. switch(skillType)
  255. {
  256. case TAMING:
  257. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  258. {
  259. case 1:
  260. mat = Material.PORK;
  261. break;
  262. case 2:
  263. mat = Material.PORK;
  264. break;
  265. case 3:
  266. mat = Material.GRILLED_PORK;
  267. break;
  268. case 4:
  269. mat = Material.GRILLED_PORK;
  270. break;
  271. case 5:
  272. mat = Material.BONE;
  273. break;
  274. }
  275. break;
  276. case MINING:
  277. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  278. {
  279. case 1:
  280. mat = Material.COAL_ORE;
  281. break;
  282. case 2:
  283. mat = Material.IRON_ORE;
  284. break;
  285. case 3:
  286. mat = Material.GOLD_ORE;
  287. break;
  288. case 4:
  289. mat = Material.LAPIS_ORE;
  290. break;
  291. case 5:
  292. mat = Material.DIAMOND_ORE;
  293. break;
  294. }
  295. break;
  296. case WOODCUTTING:
  297. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  298. {
  299. case 1:
  300. mat = Material.WOOD;
  301. break;
  302. case 2:
  303. mat = Material.WOOD;
  304. break;
  305. case 3:
  306. mat = Material.WOOD;
  307. break;
  308. case 4:
  309. mat = Material.LOG;
  310. break;
  311. case 5:
  312. mat = Material.LOG;
  313. break;
  314. }
  315. break;
  316. case REPAIR:
  317. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  318. {
  319. case 1:
  320. mat = Material.COBBLESTONE;
  321. break;
  322. case 2:
  323. mat = Material.IRON_BLOCK;
  324. break;
  325. case 3:
  326. mat = Material.GOLD_BLOCK;
  327. break;
  328. case 4:
  329. mat = Material.LAPIS_BLOCK;
  330. break;
  331. case 5:
  332. mat = Material.DIAMOND_BLOCK;
  333. break;
  334. }
  335. break;
  336. case HERBALISM:
  337. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  338. {
  339. case 1:
  340. mat = Material.YELLOW_FLOWER;
  341. break;
  342. case 2:
  343. mat = Material.RED_ROSE;
  344. break;
  345. case 3:
  346. mat = Material.BROWN_MUSHROOM;
  347. break;
  348. case 4:
  349. mat = Material.RED_MUSHROOM;
  350. break;
  351. case 5:
  352. mat = Material.PUMPKIN;
  353. break;
  354. }
  355. break;
  356. case ACROBATICS:
  357. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  358. {
  359. case 1:
  360. mat = Material.LEATHER_BOOTS;
  361. break;
  362. case 2:
  363. mat = Material.CHAINMAIL_BOOTS;
  364. break;
  365. case 3:
  366. mat = Material.IRON_BOOTS;
  367. break;
  368. case 4:
  369. mat = Material.GOLD_BOOTS;
  370. break;
  371. case 5:
  372. mat = Material.DIAMOND_BOOTS;
  373. break;
  374. }
  375. break;
  376. case SWORDS:
  377. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  378. {
  379. case 1:
  380. mat = Material.WOOD_SWORD;
  381. break;
  382. case 2:
  383. mat = Material.STONE_SWORD;
  384. break;
  385. case 3:
  386. mat = Material.IRON_SWORD;
  387. break;
  388. case 4:
  389. mat = Material.GOLD_SWORD;
  390. break;
  391. case 5:
  392. mat = Material.DIAMOND_SWORD;
  393. break;
  394. }
  395. break;
  396. case ARCHERY:
  397. mat = Material.ARROW;
  398. break;
  399. case UNARMED:
  400. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  401. {
  402. case 1:
  403. mat = Material.LEATHER_HELMET;
  404. break;
  405. case 2:
  406. mat = Material.CHAINMAIL_HELMET;
  407. break;
  408. case 3:
  409. mat = Material.IRON_HELMET;
  410. break;
  411. case 4:
  412. mat = Material.GOLD_HELMET;
  413. break;
  414. case 5:
  415. mat = Material.DIAMOND_HELMET;
  416. break;
  417. }
  418. break;
  419. case EXCAVATION:
  420. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  421. {
  422. case 1:
  423. mat = Material.WOOD_SPADE;
  424. break;
  425. case 2:
  426. mat = Material.STONE_SPADE;
  427. break;
  428. case 3:
  429. mat = Material.IRON_SPADE;
  430. break;
  431. case 4:
  432. mat = Material.GOLD_SPADE;
  433. break;
  434. case 5:
  435. mat = Material.DIAMOND_SPADE;
  436. break;
  437. }
  438. break;
  439. case AXES:
  440. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  441. {
  442. case 1:
  443. mat = Material.WOOD_AXE;
  444. break;
  445. case 2:
  446. mat = Material.STONE_AXE;
  447. break;
  448. case 3:
  449. mat = Material.IRON_AXE;
  450. break;
  451. case 4:
  452. mat = Material.GOLD_AXE;
  453. break;
  454. case 5:
  455. mat = Material.DIAMOND_AXE;
  456. break;
  457. }
  458. break;
  459. case FISHING:
  460. switch(getNotificationTier(PP.getSkillLevel(skillType)))
  461. {
  462. case 1:
  463. mat = Material.RAW_FISH;
  464. break;
  465. case 2:
  466. mat = Material.RAW_FISH;
  467. break;
  468. case 3:
  469. mat = Material.COOKED_FISH;
  470. break;
  471. case 4:
  472. mat = Material.COOKED_FISH;
  473. break;
  474. case 5:
  475. mat = Material.FISHING_ROD;
  476. break;
  477. }
  478. default:
  479. mat = Material.WATCH;
  480. break;
  481. }
  482. sPlayer.sendNotification(ChatColor.GREEN+"Level Up!", ChatColor.YELLOW+m.getCapitalized(skillType.toString())+ChatColor.DARK_AQUA+" ("+ChatColor.GREEN+PP.getSkillLevel(skillType)+ChatColor.DARK_AQUA+")", mat);
  483. playLevelUpNoise(sPlayer);
  484. }
  485. public static Integer getNotificationTier(Integer level)
  486. {
  487. if(level < 200)
  488. return 1;
  489. else if (level >= 200 && level < 400)
  490. return 2;
  491. else if (level >= 400 && level < 600)
  492. return 3;
  493. else if (level >= 600 && level < 800)
  494. return 4;
  495. else
  496. return 5;
  497. }
  498. public static Integer getXpInc(int skillxp, int xptolevel, HUDType hud)
  499. {
  500. if(hud == HUDType.STANDARD)
  501. {
  502. double percentage = (double) skillxp/xptolevel;
  503. double inc = 0.0039370078740157;
  504. return (int) (percentage/inc);
  505. } else if (hud == HUDType.RETRO)
  506. {
  507. double percentage = (double) skillxp/xptolevel;
  508. double inc = 0.0079365079365079;
  509. return (int) (percentage/inc);
  510. } else {
  511. return 1;
  512. }
  513. }
  514. public static void updateXpBar(Player player)
  515. {
  516. playerHUDs.get(player).updateXpBarDisplay(Users.getProfile(player).getHUDType(), player);
  517. }
  518. public static String getUrlBar(Integer number)
  519. {
  520. if(number.toString().toCharArray().length == 1)
  521. {
  522. return "xpbar_inc00"+number+".png";
  523. } else if (number.toString().toCharArray().length == 2)
  524. {
  525. return "xpbar_inc0"+number+".png";
  526. } else {
  527. return "xpbar_inc"+number+".png";
  528. }
  529. }
  530. public static String getUrlIcon(SkillType skillType)
  531. {
  532. return m.getCapitalized(skillType.toString())+".png";
  533. }
  534. public static boolean shouldBeFilled(PlayerProfile PP)
  535. {
  536. return PP.getXpBarInc() < getXpInc(PP.getSkillXpLevel(PP.getLastGained()), PP.getXpToLevel(PP.getLastGained()), HUDType.STANDARD);
  537. }
  538. }