diff --git a/MobArena.jar b/MobArena.jar index 0312f7b..fc89d23 100644 Binary files a/MobArena.jar and b/MobArena.jar differ diff --git a/build.xml b/build.xml index 8d555d2..a775729 100644 --- a/build.xml +++ b/build.xml @@ -30,14 +30,11 @@ + - - - - @@ -51,18 +48,22 @@ - + + + + + - - + + + + + + + + + + - + diff --git a/resources/plugin.yml b/resources/plugin.yml index c4ceb5c..1dd6375 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,7 +1,7 @@ name: MobArena author: garbagemule main: com.garbagemule.MobArena.MobArena -version: 0.94.4.49 +version: 0.94.4.51 softdepend: [Spout,MultiVerse,XcraftGate,Towny,Heroes,MagicSpells,Vault] commands: ma: diff --git a/src/com/garbagemule/MobArena/util/FileUtils.java b/src/com/garbagemule/MobArena/util/FileUtils.java index 6a39f86..08e9d6c 100644 --- a/src/com/garbagemule/MobArena/util/FileUtils.java +++ b/src/com/garbagemule/MobArena/util/FileUtils.java @@ -4,7 +4,11 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Enumeration; import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import org.bukkit.configuration.file.YamlConfiguration; @@ -18,14 +22,32 @@ public class FileUtils * Note that even if the resources have different paths, they will all * be extracted to the given directory. * @param dir a directory - * @param resources an array of resources to extract + * @param resources a list of resources to extract * @return a list of all the files that were written */ - public static List extractResources(File dir, String... resources) { + public static List extractResources(File dir, List resources) { + return extractResources(dir, "", resources); + } + + public static List extractResources(File dir, String path, List filenames) { List files = new ArrayList(); - for (String resource : resources) { - File file = extractResource(dir, resource); + // If the path is empty, just forget about it. + if (!path.equals("")) { + // We want no leading slashes + if (path.startsWith("/")) { + path = path.substring(1); + } + + // But we do want trailing slashes + if (!path.endsWith("/")) { + path = path + "/"; + } + } + + // Extract each resource + for (String filename : filenames) { + File file = extractResource(dir, path + filename); if (file != null) { files.add(file); @@ -33,6 +55,18 @@ public class FileUtils } return files; } + + /** + * Extracts all of the given resources to the given directory. + * Convenience method, used if one is too lazy to create a new list for + * the resource names. + * @param dir a directory + * @param resources an array of resources to extract + * @return a list of all the files that were written + */ + public static List extractResources(File dir, String... resources) { + return extractResources(dir, Arrays.asList(resources)); + } /** * Extracts the given resource to the given directory. @@ -77,6 +111,53 @@ public class FileUtils return null; } + /** + * Lists all files in the MobArena jar file that exist on the given path. + * The resulting list contains only filenames (with extensions) + * @param path the path of the jar file to list files from + * @param ext the file extension of the file type, can be null or the empty string + * @return a list of file names + */ + public static List listFilesOnPath(String path, String ext) { + try { + // If the jar can't be found for some odd reason, escape. + File file = new File("plugins" + File.separator + "MobArena.jar"); + if (file == null || !file.exists()) return null; + + // Create a JarFile out of the.. jar file + JarFile jarFile = new JarFile(file); + List result = new ArrayList(); + + // JarEntry names never start with / + if (path.startsWith("/")) { + path = path.substring(1); + } + + // If null, replace with the empty string. + if (ext == null) { + ext = ""; + } + // Require that extensions start with a period + else if (!ext.startsWith(".")) { + ext = "." + ext; + } + + // Loop through all entries, add the ones that match the path and extension + for (Enumeration entries = jarFile.entries(); entries.hasMoreElements(); ) { + String name = entries.nextElement().getName(); + + if (name.startsWith(path) && name.endsWith(ext)) { + result.add(getFilename(name)); + } + } + return result; + } + catch (Exception e) { + e.printStackTrace(); + return new ArrayList(1); + } + } + private static String getFilename(String resource) { int slash = resource.lastIndexOf("/"); return (slash < 0 ? resource : resource.substring(slash + 1)); diff --git a/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java b/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java index 9af7903..4b05e3a 100644 --- a/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java +++ b/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java @@ -41,52 +41,47 @@ public class AbilityManager public static void loadAbilities(File classDir) { abilities = new HashMap(); - // Extract all of the source files. + // Grab the source directory. File javaDir = new File(classDir, "src"); - extractSourceFiles(javaDir); - // The custom abilities to compile will be in the 'src'-dir - compileAbilities(javaDir, classDir); + /* If the source directory exists, we need to verify that the system + * has a java compiler before attempting anything. If not, we need to + * skip the compiling step and just go straight to loading in the + * existing class files. */ + if (javaDir.exists()) { + if (ToolProvider.getSystemJavaCompiler() != null) { + compileAbilities(javaDir, classDir); + } else { + Messenger.warning("Found plugins/MobArena/abilites/src/ folder, but no Java compiler. The source files will not be compiled!"); + } + } + + /* If there is only one file in the directory, make sure it isn't the + * src/ folder, in which case there will be no .class files to load. + * In the case of no .class files, extract the defaults. */ + String[] files = classDir.list(); + if (files.length == 0 || (files.length == 1 && files[0].equals("src"))) { + Messenger.info("No boss abilities found. Extracting defaults..."); + extractDefaultAbilities(classDir); + } // Load all the custom abilities. loadClasses(classDir); } - private static void extractSourceFiles(File javaDir) { - // Only extract the files if the folder doesn't exist. - if (javaDir.exists()) return; + private static void extractDefaultAbilities(File classDir) { + // Grab a list of all the class files. + List resources = FileUtils.listFilesOnPath("res/abilities/", ".class"); - String path = "abilities/"; + // Check that there is stuff to extract. + if (resources == null || resources.isEmpty()) { + Messenger.severe("Couldn't extract the default boss abilities!"); + return; + } - List files = new ArrayList(); - - files.add(path + "ChainLightning.java"); - files.add(path + "LivingBomb.java"); - files.add(path + "ObsidianBomb.java"); - files.add(path + "Flood.java"); - files.add(path + "WarpToPlayer.java"); - files.add(path + "RootTarget.java"); - files.add(path + "ShufflePositions.java"); - - files.add(path + "ShootArrow.java"); - files.add(path + "ShootFireball.java"); - - files.add(path + "FireAura.java"); - files.add(path + "LightningAura.java"); - - files.add(path + "DisorientDistant.java"); - files.add(path + "DisorientNearby.java"); - files.add(path + "DisorientTarget.java"); - - files.add(path + "PullDistant.java"); - files.add(path + "PullNearby.java"); - files.add(path + "PullTarget.java"); - - files.add(path + "ThrowDistant.java"); - files.add(path + "ThrowNearby.java"); - files.add(path + "ThrowTarget.java"); - - FileUtils.extractResources(javaDir, files.toArray(new String[0])); + // Extract everything. + List files = FileUtils.extractResources(classDir, "abilities/", resources); + Messenger.info("Extracted abilities: " + fileListToString(files, "$")); } private static void compileAbilities(File javaDir, File classDir) { @@ -100,27 +95,33 @@ public class AbilityManager return; } - // Get the compiler and the file manager + // Notify the console. + Messenger.info("Compiling abilities: " + fileListToString(toCompile)); + + // Get the compiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); // Generate some JavaFileObjects - Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles(toCompile); - - // Include the MobArena.jar on the classpath, and set the destination folder. - List options = Arrays.asList("-classpath", classpath, "-d", classDir.getPath()); - - // Set up the compilation task. - JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits); - - // Call the task. - task.call(); - - // And close the file manager. try { + Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles(toCompile); + + // Include the MobArena.jar on the classpath, and set the destination folder. + List options = Arrays.asList("-classpath", classpath, "-d", classDir.getPath()); + + // Set up the compilation task. + JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits); + + // Call the task. + task.call(); + + // And close the file manager. fileManager.close(); } - catch (Exception e) {} + catch (Exception e) { + Messenger.severe("Compilation step failed..."); + e.printStackTrace(); + } } private static List getSourceFilesToCompile(File javaDir, File classDir) { @@ -264,16 +265,27 @@ public class AbilityManager return null; } - /** - * Turn an array of aliases into a comma-separated string of aliases. - * @param aliases an array of Strings - * @return a comma-separated String - */ - /*private static String aliasString(String[] aliases) { + private static String fileListToString(List list) { + return fileListToString(list, null); + } + + private static String fileListToString(List list, String exclude) { + if (list.isEmpty()) return ""; + StringBuffer buffy = new StringBuffer(); - for (String a : aliases) { - buffy.append(a + ", "); + + for (File file : list) { + String name = file.getName(); + int dot = name.lastIndexOf("."); + + if (exclude != null && name.contains(exclude)) { + continue; + } + + buffy.append(", " + name.substring(0, dot)); } - return buffy.substring(0, buffy.length() - 2); - }*/ + + // Trim off the first ", ". + return buffy.substring(2); + } }