SpoutStuff.java 15 KB

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