SpoutUtils.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package com.gmail.nossr50.util.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.util.ArrayList;
  9. import java.util.jar.JarFile;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Material;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.plugin.PluginManager;
  14. import org.getspout.spoutapi.SpoutManager;
  15. import org.getspout.spoutapi.event.spout.SpoutCraftEnableEvent;
  16. import org.getspout.spoutapi.player.SpoutPlayer;
  17. import com.gmail.nossr50.mcMMO;
  18. import com.gmail.nossr50.config.spout.SpoutConfig;
  19. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  20. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  21. import com.gmail.nossr50.datatypes.skills.SkillType;
  22. import com.gmail.nossr50.locale.LocaleLoader;
  23. import com.gmail.nossr50.skills.repair.Repair;
  24. import com.gmail.nossr50.util.StringUtils;
  25. import com.gmail.nossr50.util.player.UserManager;
  26. import com.gmail.nossr50.util.skills.SkillUtils;
  27. public class SpoutUtils {
  28. // The order of the values is extremely important, a few methods depend on it to work properly
  29. protected enum Tier {
  30. FOUR(4) {
  31. @Override public int getLevel() { return SpoutConfig.getInstance().getNotificationTier4(); }
  32. @Override protected Material getAcrobaticsNotificationItem() { return Material.DIAMOND_BOOTS; }
  33. @Override protected Material getArcheryNotificationItem() { return Material.BOW; }
  34. @Override protected Material getAxesNotificationItem() { return Material.DIAMOND_AXE; }
  35. @Override protected Material getExcavationNotificationItem() { return Material.CLAY; }
  36. @Override protected Material getFishingNotificationItem() { return Material.FISHING_ROD; }
  37. @Override protected Material getHerbalismNotificationItem() { return Material.WATER_LILY; }
  38. @Override protected Material getMiningNotificationItem() { return Material.EMERALD_ORE; }
  39. @Override protected Material getSwordsNotificationItem() { return Material.DIAMOND_SWORD; }
  40. @Override protected Material getTamingNotificationItem() { return Material.BONE; }
  41. @Override protected Material getUnarmedNotificationItem() { return Material.DIAMOND_HELMET; }
  42. @Override protected Material getWoodcuttingNotificationItem() { return Material.LOG; }},
  43. THREE(3) {
  44. @Override public int getLevel() { return SpoutConfig.getInstance().getNotificationTier3(); }
  45. @Override protected Material getAcrobaticsNotificationItem() { return Material.GOLD_BOOTS; }
  46. @Override protected Material getArcheryNotificationItem() { return Material.ARROW; }
  47. @Override protected Material getAxesNotificationItem() { return Material.GOLD_AXE; }
  48. @Override protected Material getExcavationNotificationItem() { return Material.SAND; }
  49. @Override protected Material getFishingNotificationItem() { return Material.COOKED_FISH; }
  50. @Override protected Material getHerbalismNotificationItem() { return Material.RED_ROSE; }
  51. @Override protected Material getMiningNotificationItem() { return Material.DIAMOND_ORE; }
  52. @Override protected Material getSwordsNotificationItem() { return Material.GOLD_SWORD; }
  53. @Override protected Material getTamingNotificationItem() { return Material.GRILLED_PORK; }
  54. @Override protected Material getUnarmedNotificationItem() { return Material.GOLD_HELMET; }
  55. @Override protected Material getWoodcuttingNotificationItem() { return Material.WOOD; }},
  56. TWO(2) {
  57. @Override public int getLevel() { return SpoutConfig.getInstance().getNotificationTier2(); }
  58. @Override protected Material getAcrobaticsNotificationItem() { return Material.IRON_BOOTS; }
  59. @Override protected Material getArcheryNotificationItem() { return Material.ARROW; }
  60. @Override protected Material getAxesNotificationItem() { return Material.IRON_AXE; }
  61. @Override protected Material getExcavationNotificationItem() { return Material.GRAVEL; }
  62. @Override protected Material getFishingNotificationItem() { return Material.COOKED_FISH; }
  63. @Override protected Material getHerbalismNotificationItem() { return Material.YELLOW_FLOWER; }
  64. @Override protected Material getMiningNotificationItem() { return Material.GOLD_ORE; }
  65. @Override protected Material getSwordsNotificationItem() { return Material.IRON_SWORD; }
  66. @Override protected Material getTamingNotificationItem() { return Material.GRILLED_PORK; }
  67. @Override protected Material getUnarmedNotificationItem() { return Material.IRON_HELMET; }
  68. @Override protected Material getWoodcuttingNotificationItem() { return Material.LEAVES; }},
  69. ONE(1) {
  70. @Override public int getLevel() { return SpoutConfig.getInstance().getNotificationTier1(); }
  71. @Override protected Material getAcrobaticsNotificationItem() { return Material.CHAINMAIL_BOOTS; }
  72. @Override protected Material getArcheryNotificationItem() { return Material.FLINT; }
  73. @Override protected Material getAxesNotificationItem() { return Material.STONE_AXE; }
  74. @Override protected Material getExcavationNotificationItem() { return Material.GRASS; }
  75. @Override protected Material getFishingNotificationItem() { return Material.RAW_FISH; }
  76. @Override protected Material getHerbalismNotificationItem() { return Material.CACTUS; }
  77. @Override protected Material getMiningNotificationItem() { return Material.IRON_ORE; }
  78. @Override protected Material getSwordsNotificationItem() { return Material.STONE_SWORD; }
  79. @Override protected Material getTamingNotificationItem() { return Material.PORK; }
  80. @Override protected Material getUnarmedNotificationItem() { return Material.CHAINMAIL_HELMET; }
  81. @Override protected Material getWoodcuttingNotificationItem() { return Material.SAPLING; }};
  82. int numerical;
  83. private Tier(int numerical) {
  84. this.numerical = numerical;
  85. }
  86. public int toNumerical() {
  87. return numerical;
  88. }
  89. abstract protected int getLevel();
  90. abstract protected Material getAcrobaticsNotificationItem();
  91. abstract protected Material getArcheryNotificationItem();
  92. abstract protected Material getAxesNotificationItem();
  93. abstract protected Material getExcavationNotificationItem();
  94. abstract protected Material getFishingNotificationItem();
  95. abstract protected Material getHerbalismNotificationItem();
  96. abstract protected Material getMiningNotificationItem();
  97. abstract protected Material getSwordsNotificationItem();
  98. abstract protected Material getTamingNotificationItem();
  99. abstract protected Material getUnarmedNotificationItem();
  100. abstract protected Material getWoodcuttingNotificationItem();
  101. }
  102. private final static String spoutDirectory = mcMMO.getMainDirectory() + "Resources" + File.separator;
  103. private final static String hudDirectory = spoutDirectory + "HUD" + File.separator;
  104. private final static String hudStandardDirectory = hudDirectory + "Standard" + File.separator;
  105. private final static String hudRetroDirectory = hudDirectory + "Retro" + File.separator;
  106. private final static String soundDirectory = spoutDirectory + "Sound" + File.separator;
  107. /**
  108. * Write file to disk.
  109. *
  110. * @param fileName The name of the file
  111. * @param filePath The name of the file path
  112. */
  113. private static File writeFile(String fileName, String filePath) {
  114. File currentFile = new File(filePath + fileName);
  115. BufferedOutputStream os = null;
  116. JarFile jar = null;
  117. // No point in writing the file again if it already exists.
  118. if (currentFile.exists()) {
  119. return currentFile;
  120. }
  121. try {
  122. jar = new JarFile(mcMMO.mcmmo);
  123. @SuppressWarnings("resource")
  124. InputStream is = jar.getInputStream(jar.getJarEntry("resources/" + fileName));
  125. byte[] buf = new byte[2048];
  126. int nbRead;
  127. os = new BufferedOutputStream(new FileOutputStream(currentFile));
  128. while ((nbRead = is.read(buf)) != -1) {
  129. os.write(buf, 0, nbRead);
  130. }
  131. }
  132. catch (FileNotFoundException e) {
  133. e.printStackTrace();
  134. }
  135. catch (IOException e) {
  136. e.printStackTrace();
  137. }
  138. finally {
  139. if (jar != null) {
  140. try {
  141. jar.close();
  142. }
  143. catch (IOException ex) {
  144. ex.printStackTrace();
  145. }
  146. }
  147. if (os != null) {
  148. try {
  149. os.close();
  150. }
  151. catch (IOException ex) {
  152. ex.printStackTrace();
  153. }
  154. }
  155. }
  156. return currentFile;
  157. }
  158. /**
  159. * Extract Spout files to the Resources directory.
  160. */
  161. public static ArrayList<File> extractFiles() {
  162. ArrayList<File> files = new ArrayList<File>();
  163. // Setup directories
  164. new File(spoutDirectory).mkdir();
  165. new File(hudDirectory).mkdir();
  166. new File(hudStandardDirectory).mkdir();
  167. new File(hudRetroDirectory).mkdir();
  168. new File(soundDirectory).mkdir();
  169. // XP Bar images
  170. for (int x = 0; x < 255; x++) {
  171. String fileName;
  172. if (x < 10) {
  173. fileName = "xpbar_inc00" + x + ".png";
  174. }
  175. else if (x < 100) {
  176. fileName = "xpbar_inc0" + x + ".png";
  177. }
  178. else {
  179. fileName = "xpbar_inc" + x + ".png";
  180. }
  181. files.add(writeFile(fileName, hudStandardDirectory));
  182. }
  183. // Standard XP Icons
  184. for (SkillType skillType : SkillType.values()) {
  185. if (skillType.isChildSkill()) {
  186. continue;
  187. }
  188. String skillName = StringUtils.getCapitalized(skillType.toString());
  189. files.add(writeFile(skillName + ".png", hudStandardDirectory));
  190. files.add(writeFile(skillName + "_r.png", hudRetroDirectory));
  191. }
  192. // Blank icons
  193. files.add(writeFile("Icon.png", hudStandardDirectory));
  194. files.add(writeFile("Icon_r.png", hudRetroDirectory));
  195. // Sound FX
  196. files.add(writeFile("level.wav", soundDirectory));
  197. return files;
  198. }
  199. /**
  200. * Handle level-up notifications through Spout.
  201. *
  202. * @param skillType The skill that leveled up
  203. * @param spoutPlayer The player that leveled up
  204. */
  205. public static void levelUpNotification(SkillType skillType, SpoutPlayer spoutPlayer) {
  206. PlayerProfile profile = UserManager.getPlayer(spoutPlayer).getProfile();
  207. int skillLevel = profile.getSkillLevel(skillType);
  208. Material notificationItem;
  209. switch (skillType) {
  210. case ACROBATICS:
  211. notificationItem = getAcrobaticsNotificationItem(skillLevel);
  212. break;
  213. case ARCHERY:
  214. notificationItem = getArcheryNotificationItem(skillLevel);
  215. break;
  216. case AXES:
  217. notificationItem = getAxesNotificationItem(skillLevel);
  218. break;
  219. case EXCAVATION:
  220. notificationItem = getExcavationNotificationItem(skillLevel);
  221. break;
  222. case FISHING:
  223. notificationItem = getFishingNotificationItem(skillLevel);
  224. break;
  225. case HERBALISM:
  226. notificationItem = getHerbalismNotificationItem(skillLevel);
  227. break;
  228. case MINING:
  229. notificationItem = getMiningNotificationItem(skillLevel);
  230. break;
  231. case REPAIR:
  232. notificationItem = Material.ANVIL;
  233. break;
  234. case SWORDS:
  235. notificationItem = getSwordsNotificationItem(skillLevel);
  236. break;
  237. case TAMING:
  238. notificationItem = getTamingNotificationItem(skillLevel);
  239. break;
  240. case UNARMED:
  241. notificationItem = getUnarmedNotificationItem(skillLevel);
  242. break;
  243. case WOODCUTTING:
  244. notificationItem = getWoodcuttingNotificationItem(skillLevel);
  245. break;
  246. default:
  247. notificationItem = Material.MAP;
  248. break;
  249. }
  250. spoutPlayer.sendNotification(LocaleLoader.getString("Spout.LevelUp.1"), LocaleLoader.getString("Spout.LevelUp.2", SkillUtils.getSkillName(skillType), skillLevel), notificationItem);
  251. SpoutManager.getSoundManager().playCustomSoundEffect(mcMMO.p, spoutPlayer, "level.wav", false);
  252. }
  253. private static Material getAcrobaticsNotificationItem(int skillLevel) {
  254. for (Tier tier : Tier.values()) {
  255. if (skillLevel >= tier.getLevel()) {
  256. return tier.getAcrobaticsNotificationItem();
  257. }
  258. }
  259. return Material.LEATHER_BOOTS;
  260. }
  261. private static Material getArcheryNotificationItem(int skillLevel) {
  262. for (Tier tier : Tier.values()) {
  263. if (skillLevel >= tier.getLevel()) {
  264. return tier.getArcheryNotificationItem();
  265. }
  266. }
  267. return Material.FEATHER;
  268. }
  269. private static Material getAxesNotificationItem(int skillLevel) {
  270. for (Tier tier : Tier.values()) {
  271. if (skillLevel >= tier.getLevel()) {
  272. return tier.getAxesNotificationItem();
  273. }
  274. }
  275. return Material.WOOD_AXE;
  276. }
  277. private static Material getExcavationNotificationItem(int skillLevel) {
  278. for (Tier tier : Tier.values()) {
  279. if (skillLevel >= tier.getLevel()) {
  280. return tier.getExcavationNotificationItem();
  281. }
  282. }
  283. return Material.DIRT;
  284. }
  285. private static Material getFishingNotificationItem(int skillLevel) {
  286. for (Tier tier : Tier.values()) {
  287. if (skillLevel >= tier.getLevel()) {
  288. return tier.getFishingNotificationItem();
  289. }
  290. }
  291. return Material.RAW_FISH;
  292. }
  293. private static Material getHerbalismNotificationItem(int skillLevel) {
  294. for (Tier tier : Tier.values()) {
  295. if (skillLevel >= tier.getLevel()) {
  296. return tier.getHerbalismNotificationItem();
  297. }
  298. }
  299. return Material.VINE;
  300. }
  301. private static Material getMiningNotificationItem(int skillLevel) {
  302. for (Tier tier : Tier.values()) {
  303. if (skillLevel >= tier.getLevel()) {
  304. return tier.getMiningNotificationItem();
  305. }
  306. }
  307. return Material.COAL_ORE;
  308. }
  309. private static Material getSwordsNotificationItem(int skillLevel) {
  310. for (Tier tier : Tier.values()) {
  311. if (skillLevel >= tier.getLevel()) {
  312. return tier.getSwordsNotificationItem();
  313. }
  314. }
  315. return Material.WOOD_SWORD;
  316. }
  317. private static Material getTamingNotificationItem(int skillLevel) {
  318. for (Tier tier : Tier.values()) {
  319. if (skillLevel >= tier.getLevel()) {
  320. return tier.getTamingNotificationItem();
  321. }
  322. }
  323. return Material.PORK;
  324. }
  325. private static Material getUnarmedNotificationItem(int skillLevel) {
  326. for (Tier tier : Tier.values()) {
  327. if (skillLevel >= tier.getLevel()) {
  328. return tier.getUnarmedNotificationItem();
  329. }
  330. }
  331. return Material.LEATHER_HELMET;
  332. }
  333. private static Material getWoodcuttingNotificationItem(int skillLevel) {
  334. for (Tier tier : Tier.values()) {
  335. if (skillLevel >= tier.getLevel()) {
  336. return tier.getWoodcuttingNotificationItem();
  337. }
  338. }
  339. return Material.STICK;
  340. }
  341. /**
  342. * Re-enable SpoutCraft for players after a /reload
  343. */
  344. public static void reloadSpoutPlayers() {
  345. PluginManager pluginManager = mcMMO.p.getServer().getPluginManager();
  346. for (SpoutPlayer spoutPlayer : SpoutManager.getPlayerChunkMap().getOnlinePlayers()) {
  347. pluginManager.callEvent(new SpoutCraftEnableEvent(spoutPlayer));
  348. }
  349. }
  350. public static void reloadSpoutPlayer(Player player) {
  351. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  352. if (spoutPlayer != null) {
  353. mcMMO.p.getServer().getPluginManager().callEvent(new SpoutCraftEnableEvent(spoutPlayer));
  354. }
  355. }
  356. public static void preCacheFiles() {
  357. SpoutManager.getFileManager().addToPreLoginCache(mcMMO.p, extractFiles());
  358. }
  359. public static void processLevelup(McMMOPlayer mcMMOPlayer, SkillType skillType, int levelsGained) {
  360. Player player = mcMMOPlayer.getPlayer();
  361. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  362. if (spoutPlayer.isSpoutCraftEnabled()) {
  363. levelUpNotification(skillType, spoutPlayer);
  364. /* Update custom titles */
  365. if (SpoutConfig.getInstance().getShowPowerLevel()) {
  366. spoutPlayer.setTitle(LocaleLoader.getString("Spout.Title", spoutPlayer.getName(), mcMMOPlayer.getPowerLevel()));
  367. }
  368. }
  369. else {
  370. player.sendMessage(LocaleLoader.getString(StringUtils.getCapitalized(skillType.toString()) + ".Skillup", levelsGained, mcMMOPlayer.getProfile().getSkillLevel(skillType)));
  371. }
  372. }
  373. public static void processXpGain(Player player, PlayerProfile profile) {
  374. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  375. if (spoutPlayer.isSpoutCraftEnabled() && SpoutConfig.getInstance().getXPBarEnabled()) {
  376. profile.getSpoutHud().updateXpBar();
  377. }
  378. }
  379. public static void sendRepairNotifications(Player player, int anvilId) {
  380. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  381. if (spoutPlayer.isSpoutCraftEnabled()) {
  382. String[] spoutMessages = Repair.getSpoutAnvilMessages(anvilId);
  383. spoutPlayer.sendNotification(spoutMessages[0], spoutMessages[1], Material.getMaterial(anvilId));
  384. }
  385. else {
  386. player.sendMessage(Repair.getAnvilMessage(anvilId));
  387. }
  388. }
  389. public static void sendDonationNotification(Player player) {
  390. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  391. if (spoutPlayer.isSpoutCraftEnabled()) {
  392. spoutPlayer.sendNotification(LocaleLoader.getString("Spout.Donate"), ChatColor.GREEN + "gjmcferrin@gmail.com", Material.DIAMOND);
  393. }
  394. else {
  395. player.sendMessage(LocaleLoader.getString("MOTD.Donate"));
  396. player.sendMessage(ChatColor.GOLD + " - " + ChatColor.GREEN + "gjmcferrin@gmail.com" + ChatColor.GOLD + " Paypal");
  397. }
  398. }
  399. }