Advertisement
Olivki

ModHandler

Jun 28th, 2013
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.95 KB | None | 0 0
  1. package se.proxus.mods;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Comparator;
  7. import java.util.HashMap;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.logging.Level;
  11.  
  12. import se.proxus.Gallium;
  13. import se.proxus.mods.list.combat.AntiVelocity;
  14. import se.proxus.mods.list.combat.AutoAttack;
  15. import se.proxus.mods.list.combat.AutoSoup;
  16. import se.proxus.mods.list.combat.Criticals;
  17. import se.proxus.mods.list.gui.Chat;
  18. import se.proxus.mods.list.gui.Gui;
  19. import se.proxus.mods.list.gui.Notifications;
  20. import se.proxus.mods.list.misc.ChatDate;
  21. import se.proxus.mods.list.movement.AutoSwim;
  22. import se.proxus.mods.list.movement.Flight;
  23. import se.proxus.mods.list.movement.Freecam;
  24. import se.proxus.mods.list.movement.NoFall;
  25. import se.proxus.mods.list.movement.Sprint;
  26. import se.proxus.mods.list.none.Friends;
  27. import se.proxus.mods.list.none.IRC;
  28. import se.proxus.mods.list.none.Panels;
  29. import se.proxus.mods.list.render.ChestESP;
  30. import se.proxus.mods.list.render.ESP;
  31. import se.proxus.mods.list.render.LineESP;
  32. import se.proxus.mods.list.render.Nametags;
  33. import se.proxus.mods.list.render.Tracer;
  34. import se.proxus.mods.list.render.Waypoints;
  35. import se.proxus.mods.list.server.AutoEquip;
  36. import se.proxus.mods.list.server.AutoFish;
  37. import se.proxus.mods.list.server.AutoRespawn;
  38. import se.proxus.mods.list.server.Derp;
  39. import se.proxus.mods.list.world.Brightness;
  40. import se.proxus.mods.list.world.Miner;
  41.  
  42. public class ModHandler {
  43.  
  44.     private File directory;
  45.     private Gallium client;
  46.     private static final List<Mod> LOADED_MODS = new LinkedList<Mod>();
  47.     private static final HashMap<String, Mod> LOADED_MODS_HASH = new HashMap<String, Mod>();
  48.  
  49.     public ModHandler(final Gallium client) {
  50.     setClient(client);
  51.     }
  52.  
  53.     /**
  54.      * @author Oliver The method where you should add all your mods in.
  55.      *         <i>(Example: addMod(new Flight())</i>
  56.      */
  57.     public void init() {
  58.     directory = new File(getClient().getDirectory(), File.separator
  59.         + "mods" + File.separator);
  60.     getDirectory().mkdirs();
  61.     getLoadedMods().clear();
  62.     getLoadedModsHash().clear();
  63.     addMod(new Gui(getClient()));
  64.     addMod(new NoFall(getClient()));
  65.     addMod(new AutoAttack(getClient()));
  66.     addMod(new AntiVelocity(getClient()));
  67.     addMod(new ChatDate(getClient()));
  68.     addMod(new Criticals(getClient()));
  69.     addMod(new Brightness(getClient()));
  70.     addMod(new Miner(getClient()));
  71.     addMod(new Sprint(getClient()));
  72.     addMod(new Derp(getClient()));
  73.     addMod(new AutoSoup(getClient()));
  74.     addMod(new Flight(getClient()));
  75.     addMod(new AutoRespawn(getClient()));
  76.     addMod(new AutoEquip(getClient()));
  77.     addMod(new AutoSwim(getClient()));
  78.     addMod(new IRC(getClient()));
  79.     addMod(new Tracer(getClient()));
  80.     addMod(new ESP(getClient()));
  81.     addMod(new Friends(getClient()));
  82.     addMod(new AutoFish(getClient()));
  83.     addMod(new Freecam(getClient()));
  84.     addMod(new LineESP(getClient()));
  85.     addMod(new Chat(getClient()));
  86.     addMod(new Panels(getClient()));
  87.     addMod(new ChestESP(getClient()));
  88.     addMod(new Waypoints(getClient()));
  89.     addMod(new Nametags(getClient()));
  90.     addMod(new Notifications(getClient()));
  91.     /** Resort them after the alphabet. **/
  92.     getClient().getLogger().info(
  93.         "Reorganizing the Mod list after the alphabet.");
  94.     List<Mod> tempList = new LinkedList<Mod>();
  95.     tempList.addAll(getLoadedMods());
  96.     Mod[] tempArray = tempList.toArray(new Mod[tempList.size()]);
  97.     sortMods(tempArray);
  98.     getLoadedMods().clear();
  99.     for (Mod mod : tempArray)
  100.         addMod(mod);
  101.     tempArray = null;
  102.     tempList.clear();
  103.     tempList = null;
  104.     System.gc();
  105.     }
  106.  
  107.     /**
  108.      * @author Oliver
  109.      * @param key
  110.      *            The name of the key that got pressed, you can toggle your mods
  111.      *            in here.
  112.      */
  113.     public void onKeyPressed(final String key) {
  114.     for (Mod mod : getLoadedMods())
  115.         if (key.equalsIgnoreCase(mod.getKey())
  116.             && !key.equalsIgnoreCase("NONE"))
  117.         mod.toggle();
  118.     }
  119.  
  120.     /**
  121.      * @author Oliver
  122.      * @param mod
  123.      *            The Mod that you want to add.
  124.      */
  125.     public void addMod(final Mod mod) {
  126.     if (!getLoadedModsHash().containsKey(mod.getName())) {
  127.         getLoadedMods().add(mod);
  128.         getLoadedModsHash().put(
  129.             mod.getName().replace(" ", "").toLowerCase(), mod);
  130.         getClient().getLogger().log(Level.INFO,
  131.             "Added the mod: " + mod.getName());
  132.     } else
  133.         throw new IllegalArgumentException("The mod is already added!");
  134.     }
  135.  
  136.     /**
  137.      * @author Oliver
  138.      * @param name
  139.      *            The Mod that you want to remove.
  140.      */
  141.     public void removeMod(final Mod mod) {
  142.     getLoadedMods().remove(getLoadedMods().indexOf(mod));
  143.     getLoadedModsHash().remove(mod.getName().toLowerCase());
  144.     }
  145.  
  146.     /**
  147.      * @author Oliver
  148.      * @param name
  149.      *            The name of the Mod that you wan to get.
  150.      * @return Returns the Mod with the same name as the one entered.
  151.      */
  152.     public Mod getMod(final String name) {
  153.     return getLoadedModsHash().get(name.replace(" ", "").toLowerCase());
  154.     }
  155.  
  156.     /**
  157.      * @author Oliver
  158.      * @param text
  159.      *            The text typen in.
  160.      * @return The mods that contains any part of the text.
  161.      */
  162.     public ArrayList<Mod> getMods(final String text) {
  163.     ArrayList<Mod> toAdd = new ArrayList<Mod>();
  164.     if (text.isEmpty())
  165.         toAdd.clear();
  166.     else
  167.         for (Mod mod : getLoadedMods())
  168.         if (mod.getName().toLowerCase().startsWith(text.toLowerCase()))
  169.             toAdd.add(mod);
  170.     return toAdd;
  171.     }
  172.  
  173.     public Mod[] sortMods(final Mod[] mods) {
  174.     Arrays.sort(mods, new Comparator<Mod>() {
  175.         @Override
  176.         public int compare(final Mod mod1, final Mod mod2) {
  177.         String modName1 = mod1.getName();
  178.         String modName2 = mod2.getName();
  179.         return modName1.compareTo(modName2);
  180.         }
  181.     });
  182.     return mods;
  183.     }
  184.  
  185.     /**
  186.      * @author Oliver
  187.      * @return Returns the loaded mods.
  188.      */
  189.     public List<Mod> getLoadedMods() {
  190.     return LOADED_MODS;
  191.     }
  192.  
  193.     /**
  194.      * @author Oliver
  195.      * @return Returns the loaded mods.
  196.      */
  197.     public HashMap<String, Mod> getLoadedModsHash() {
  198.     return LOADED_MODS_HASH;
  199.     }
  200.  
  201.     /**
  202.      * A method for getting all the current active mods. <i>(Ignores all the
  203.      * hidden ones.)</i>
  204.      *
  205.      * @return Returns a ArrayList with all the active mods.
  206.      */
  207.     public ArrayList<Mod> getActiveMods() {
  208.     ArrayList<Mod> activeMods = new ArrayList<Mod>();
  209.     for (Mod mod : getLoadedMods())
  210.         if (mod.getState() && !mod.isHidden())
  211.         if (!activeMods.contains(mod))
  212.             activeMods.add(mod);
  213.     return activeMods;
  214.     }
  215.  
  216.     public Mod[] getRegisteredMods() {
  217.     return getLoadedMods().toArray(new Mod[getLoadedMods().size()]);
  218.     }
  219.  
  220.     /**
  221.      * @author Oliver
  222.      * @return Returns the mods directory.
  223.      */
  224.     public File getDirectory() {
  225.     return directory;
  226.     }
  227.  
  228.     public Gallium getClient() {
  229.     return client;
  230.     }
  231.  
  232.     public void setClient(final Gallium client) {
  233.     this.client = client;
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement