123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- package com.gmail.nossr50.spout;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- import org.bukkit.Material;
- import org.bukkit.entity.Player;
- import org.getspout.spoutapi.SpoutManager;
- import org.getspout.spoutapi.event.spout.SpoutCraftEnableEvent;
- import org.getspout.spoutapi.keyboard.Keyboard;
- import org.getspout.spoutapi.player.FileManager;
- import org.getspout.spoutapi.player.SpoutPlayer;
- import com.gmail.nossr50.mcMMO;
- import com.gmail.nossr50.config.AdvancedConfig;
- import com.gmail.nossr50.datatypes.PlayerProfile;
- import com.gmail.nossr50.locale.LocaleLoader;
- import com.gmail.nossr50.skills.utilities.SkillTools;
- import com.gmail.nossr50.skills.utilities.SkillType;
- import com.gmail.nossr50.util.StringUtils;
- import com.gmail.nossr50.util.Users;
- public class SpoutTools {
- private static mcMMO plugin = mcMMO.p;
- static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
- public final static String spoutDirectory = mcMMO.getMainDirectory() + "Resources" + File.separator;
- public final static String hudDirectory = spoutDirectory + "HUD" + File.separator;
- public final static String hudStandardDirectory = hudDirectory + "Standard" + File.separator;
- public final static String hudRetroDirectory = hudDirectory + "Retro" + File.separator;
- public final static String soundDirectory = spoutDirectory + "Sound" + File.separator;
- public static boolean showPowerLevel;
- private final static SpoutListener spoutListener = new SpoutListener();
- public static Keyboard menuKey;
- /**
- * Write file to disk.
- *
- * @param theFileName The name of the file
- * @param theFilePath The name of the file path
- */
- private static void writeFile(String theFileName, String theFilePath) {
- InputStream is = null;
- OutputStream os = null;
- JarFile jar = null;
- try {
- File currentFile = new File(theFilePath + theFileName);
- // No point in writing the file again if it already exists.
- if (currentFile.exists()) {
- return;
- }
- jar = new JarFile(mcMMO.mcmmo);
- JarEntry entry = jar.getJarEntry("resources/" + theFileName);
- is = jar.getInputStream(entry);
- byte[] buf = new byte[2048];
- int nbRead;
- os = new BufferedOutputStream(new FileOutputStream(currentFile));
- while ((nbRead = is.read(buf)) != -1) {
- os.write(buf, 0, nbRead);
- }
- os.flush();
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- if (is != null) {
- try {
- is.close();
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- if (os != null) {
- try {
- os.close();
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- if (jar != null) {
- try {
- jar.close();
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
- /**
- * Extract Spout files to the Resources directory.
- */
- public static void extractFiles() {
- // Setup directories
- new File(spoutDirectory).mkdir();
- new File(hudDirectory).mkdir();
- new File(hudStandardDirectory).mkdir();
- new File(hudRetroDirectory).mkdir();
- new File(soundDirectory).mkdir();
- // XP Bar images
- for (int x = 0; x < 255; x++) {
- String theFileName;
- if (x < 10) {
- theFileName = "xpbar_inc00" + x + ".png";
- }
- else if (x < 100) {
- theFileName = "xpbar_inc0" + x + ".png";
- }
- else {
- theFileName = "xpbar_inc" + x + ".png";
- }
- writeFile(theFileName, hudStandardDirectory);
- }
- // Standard XP Icons
- for (SkillType skillType : SkillType.values()) {
- if (skillType.isChildSkill()) {
- continue;
- }
- String skillTypeString = skillType.toString();
- String standardFileName = StringUtils.getCapitalized(skillTypeString)+".png";
- String retroFileName = StringUtils.getCapitalized(skillTypeString)+"_r.png";
- writeFile(standardFileName, hudStandardDirectory);
- writeFile(retroFileName, hudRetroDirectory);
- }
- // Blank icons
- writeFile("Icon.png", hudStandardDirectory);
- writeFile("Icon_r.png", hudRetroDirectory);
- // Sound FX
- writeFile("level.wav", soundDirectory);
- }
- /**
- * Setup Spout config options
- */
- public static void setupSpoutConfigs() {
- showPowerLevel = SpoutConfig.getInstance().getShowPowerLevel();
- String temp = SpoutConfig.getInstance().getMenuKey();
- for (Keyboard x : Keyboard.values()) {
- if (x.toString().equalsIgnoreCase(temp)) {
- menuKey = x;
- }
- }
- if (menuKey == null) {
- mcMMO.p.getLogger().warning("Invalid KEY for Menu.Key, using KEY_M");
- menuKey = Keyboard.KEY_M;
- }
- }
- /**
- * Get all the Spout files in the Resources folder.
- *
- * @return a list of all files is the Resources folder
- */
- public static ArrayList<File> getFiles() {
- ArrayList<File> files = new ArrayList<File>();
- // XP BAR
- for (int x = 0; x < 255; x++) {
- if (x < 10) {
- files.add(new File(hudStandardDirectory + "xpbar_inc00" + x + ".png"));
- }
- else if (x < 100) {
- files.add(new File(hudStandardDirectory + "xpbar_inc0" + x + ".png"));
- }
- else {
- files.add(new File(hudStandardDirectory + "xpbar_inc" + x + ".png"));
- }
- }
- // Standard XP Icons
- for (SkillType skillType : SkillType.values()) {
- if (skillType.isChildSkill()) {
- continue;
- }
- String skillTypeString = skillType.toString();
- files.add(new File(hudStandardDirectory + StringUtils.getCapitalized(skillTypeString) + ".png"));
- files.add(new File(hudRetroDirectory + StringUtils.getCapitalized(skillTypeString) + "_r.png"));
- }
- // Blank icons
- files.add(new File(hudStandardDirectory + "Icon.png"));
- files.add(new File(hudRetroDirectory + "Icon_r.png"));
- // Level SFX
- files.add(new File(soundDirectory + "level.wav"));
- return files;
- }
- /**
- * Register custom Spout events.
- */
- public static void registerCustomEvent() {
- plugin.getServer().getPluginManager().registerEvents(spoutListener, plugin);
- }
- /**
- * Handle level-up notifications through Spout.
- *
- * @param skillType The skill that leveled up
- * @param spoutPlayer The player that leveled up
- */
- public static void levelUpNotification(SkillType skillType, SpoutPlayer spoutPlayer) {
- PlayerProfile profile = Users.getPlayer(spoutPlayer).getProfile();
- int notificationTier = getNotificationTier(profile.getSkillLevel(skillType));
- Material mat = null;
- switch (skillType) {
- case TAMING:
- switch (notificationTier) {
- case 1:
- case 2:
- mat = Material.PORK;
- break;
- case 3:
- case 4:
- mat = Material.GRILLED_PORK;
- break;
- case 5:
- mat = Material.BONE;
- break;
- default:
- break;
- }
- break;
- case MINING:
- switch (notificationTier) {
- case 1:
- mat = Material.COAL_ORE;
- break;
- case 2:
- mat = Material.IRON_ORE;
- break;
- case 3:
- mat = Material.GOLD_ORE;
- break;
- case 4:
- mat = Material.LAPIS_ORE;
- break;
- case 5:
- mat = Material.DIAMOND_ORE;
- break;
- default:
- break;
- }
- break;
- case WOODCUTTING:
- switch (notificationTier) {
- case 1:
- case 2:
- case 3:
- mat = Material.WOOD;
- break;
- case 4:
- case 5:
- mat = Material.LOG;
- break;
- default:
- break;
- }
- break;
- case REPAIR:
- switch (notificationTier) {
- case 1:
- mat = Material.COBBLESTONE;
- break;
- case 2:
- mat = Material.IRON_BLOCK;
- break;
- case 3:
- mat = Material.GOLD_BLOCK;
- break;
- case 4:
- mat = Material.LAPIS_BLOCK;
- break;
- case 5:
- mat = Material.DIAMOND_BLOCK;
- break;
- default:
- break;
- }
- break;
- case HERBALISM:
- switch (notificationTier) {
- case 1:
- mat = Material.YELLOW_FLOWER;
- break;
- case 2:
- mat = Material.RED_ROSE;
- break;
- case 3:
- mat = Material.BROWN_MUSHROOM;
- break;
- case 4:
- mat = Material.RED_MUSHROOM;
- break;
- case 5:
- mat = Material.PUMPKIN;
- break;
- default:
- break;
- }
- break;
- case ACROBATICS:
- switch (notificationTier) {
- case 1:
- mat = Material.LEATHER_BOOTS;
- break;
- case 2:
- mat = Material.CHAINMAIL_BOOTS;
- break;
- case 3:
- mat = Material.IRON_BOOTS;
- break;
- case 4:
- mat = Material.GOLD_BOOTS;
- break;
- case 5:
- mat = Material.DIAMOND_BOOTS;
- break;
- default:
- break;
- }
- break;
- case SWORDS:
- switch (notificationTier) {
- case 1:
- mat = Material.WOOD_SWORD;
- break;
- case 2:
- mat = Material.STONE_SWORD;
- break;
- case 3:
- mat = Material.IRON_SWORD;
- break;
- case 4:
- mat = Material.GOLD_SWORD;
- break;
- case 5:
- mat = Material.DIAMOND_SWORD;
- break;
- default:
- break;
- }
- break;
- case ARCHERY:
- switch (notificationTier) {
- case 1:
- case 2:
- case 3:
- mat = Material.ARROW;
- break;
- case 4:
- case 5:
- mat = Material.BOW;
- break;
- default:
- break;
- }
- break;
- case UNARMED:
- switch (notificationTier) {
- case 1:
- mat = Material.LEATHER_HELMET;
- break;
- case 2:
- mat = Material.CHAINMAIL_HELMET;
- break;
- case 3:
- mat = Material.IRON_HELMET;
- break;
- case 4:
- mat = Material.GOLD_HELMET;
- break;
- case 5:
- mat = Material.DIAMOND_HELMET;
- break;
- default:
- break;
- }
- break;
- case EXCAVATION:
- switch (notificationTier) {
- case 1:
- mat = Material.WOOD_SPADE;
- break;
- case 2:
- mat = Material.STONE_SPADE;
- break;
- case 3:
- mat = Material.IRON_SPADE;
- break;
- case 4:
- mat = Material.GOLD_SPADE;
- break;
- case 5:
- mat = Material.DIAMOND_SPADE;
- break;
- default:
- break;
- }
- break;
- case AXES:
- switch (notificationTier) {
- case 1:
- mat = Material.WOOD_AXE;
- break;
- case 2:
- mat = Material.STONE_AXE;
- break;
- case 3:
- mat = Material.IRON_AXE;
- break;
- case 4:
- mat = Material.GOLD_AXE;
- break;
- case 5:
- mat = Material.DIAMOND_AXE;
- break;
- default:
- break;
- }
- break;
- case FISHING:
- switch (notificationTier) {
- case 1:
- case 2:
- mat = Material.RAW_FISH;
- break;
- case 3:
- case 4:
- mat = Material.COOKED_FISH;
- break;
- case 5:
- mat = Material.FISHING_ROD;
- break;
- default:
- break;
- }
- break;
- default:
- mat = Material.WATCH;
- break;
- }
- spoutPlayer.sendNotification(LocaleLoader.getString("Spout.LevelUp.1"), LocaleLoader.getString("Spout.LevelUp.2", SkillTools.localizeSkillName(skillType), profile.getSkillLevel(skillType)), mat);
- SpoutSounds.playLevelUpNoise(spoutPlayer, plugin);
- }
- /**
- * Gets the notification tier of a skill.
- *
- * @param level The level of the skill
- * @return the notification tier of the skill
- */
- private static Integer getNotificationTier(Integer level) {
- if (level >= advancedConfig.getSpoutNotificationTier4()) {
- return 5;
- }
- else if (level >= advancedConfig.getSpoutNotificationTier3()) {
- return 4;
- }
- else if (level >= advancedConfig.getSpoutNotificationTier2()) {
- return 3;
- }
- else if (level >= advancedConfig.getSpoutNotificationTier1()) {
- return 2;
- }
- else {
- return 1;
- }
- }
- /**
- * Re-enable SpoutCraft for players after a /reload
- */
- public static void reloadSpoutPlayers() {
- for (SpoutPlayer spoutPlayer : SpoutManager.getPlayerChunkMap().getOnlinePlayers()) {
- SpoutCraftEnableEvent spoutCraftEnableEvent = new SpoutCraftEnableEvent(spoutPlayer);
- mcMMO.p.getServer().getPluginManager().callEvent(spoutCraftEnableEvent);
- }
- }
- public static void reloadSpoutPlayer(Player player) {
- SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
- if (spoutPlayer != null) {
- SpoutCraftEnableEvent spoutCraftEnableEvent = new SpoutCraftEnableEvent(spoutPlayer);
- mcMMO.p.getServer().getPluginManager().callEvent(spoutCraftEnableEvent);
- }
- }
- public static void preCacheFiles() {
- extractFiles(); //Extract source materials
- FileManager FM = SpoutManager.getFileManager();
- FM.addToPreLoginCache(plugin, getFiles());
- }
- }
|