Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 2.75 KB | Hits: 8 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. package com.pie.jotta.util.command;
  2.  
  3. import java.io.File;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7.  
  8. import com.pie.jotta.Constants;
  9. import com.pie.jotta.Main;
  10. import com.pie.jotta.util.CustomClassLoader;
  11. import com.pie.jotta.util.logger.Logger;
  12.  
  13. /*
  14.  *  This file is part of Jotta.
  15.  *
  16.  *  Jotta is free software: you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation, either version 3 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  Jotta is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with Jotta.  If not, see <http://www.gnu.org/licenses/>.
  28.  */
  29.  
  30. public class CommandLoader {
  31.  
  32.         private static HashMap<String, Class<?>> list = new HashMap<String, Class<?>>();
  33.         private static CustomClassLoader c = new CustomClassLoader(Main.class.getClassLoader());
  34.        
  35.         public static HashMap<String, Class<?>> list() {
  36.                 return list;
  37.         }
  38.        
  39.         public static Class<?> command(String key) {
  40.                 if(list.containsKey(key)) {
  41.                         return list.get(key);
  42.                 } else {
  43.                         return null;
  44.                 }
  45.         }
  46.        
  47.         public static void reloadClass(String name) {
  48.                 try {
  49.                         if(list.containsKey(Constants.CMD_PREFIX+name)) {
  50.                                 list.remove(Constants.CMD_PREFIX+name);
  51.                                 list.put(Constants.CMD_PREFIX+name, c.loadClass("com.pie.jotta.util.command."+name));
  52.                         }
  53.                 } catch(Exception e) {
  54.                         e.printStackTrace();
  55.                 }
  56.         }
  57.        
  58.         public static int loadClasses() {
  59.                 int i = 0;
  60.                 for(Object s : iterateFilesAsList()) {
  61.                         try {
  62.                                 System.out.println(s);
  63.                                 list.put("$"+((String)s).toLowerCase(), c.loadClass("com.pie.jotta.util.command."+(String)s));
  64.                         } catch(Exception e) {
  65.                                 e.printStackTrace();
  66.                         }
  67.                         i++;
  68.                 }
  69.                 Logger.getInstance().log("Loaded "+ i +" plugins.");
  70.                 return i;
  71.         }
  72.        
  73.         @SuppressWarnings("deprecation")
  74.         private static Object[] iterateFilesAsList() {
  75.                 try {
  76.                         File dir = new File(Constants.PLUGIN_DIR);
  77.                         String[] fnames = dir.list();
  78.                         ArrayList<String> names = new ArrayList<String>();
  79.                         for(int i=0;i<fnames.length;i++) {
  80.                                 URL u = new File(dir.getAbsolutePath()+"\\"+ fnames[i]).toURL();
  81.                                 String f = u.getFile();
  82.                                 if(f.endsWith(".class") && !f.contains("Command")) {
  83.                                         String[] split = f.split("/");
  84.                                         String[] split2 = split[split.length-1].split("\\\\");
  85.                                         names.add(split2[split2.length-1].replace(".class", ""));
  86.                                 }
  87.                         }
  88.                         return names.toArray();
  89.                 } catch(Exception e) {
  90.                         e.printStackTrace();
  91.                 }
  92.                 return null;
  93.         }
  94.        
  95. }