TerrificTable55

Untitled

Oct 22nd, 2022
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.99 KB | None | 0 0
  1. package intent.extensions;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.PrintWriter;
  8. import java.util.HashSet;
  9. import java.util.Iterator;
  10. import java.util.Map.Entry;
  11.  
  12. import com.google.common.collect.Sets;
  13. import com.google.gson.JsonElement;
  14. import com.google.gson.JsonObject;
  15.  
  16. import intent.Frickin;
  17. import intent.modules.Module;
  18. import intent.modules.Module.Category;
  19. import intent.modules.render.ClickGUI;
  20. import intent.modules.render.TabGUI;
  21. import intent.settings.Setting;
  22. import intent.ui.MainMenu;
  23. import intent.util.JsonUtils;
  24. import net.minecraft.client.Minecraft;
  25. import net.minecraft.client.gui.FontRenderer;
  26. import net.minecraft.client.gui.Gui;
  27. import net.minecraft.client.gui.GuiScreen;
  28. import net.minecraft.client.renderer.GlStateManager;
  29. import javax.swing.JOptionPane;
  30.  
  31. public class FileManager {
  32.  
  33.     public static File ROOT_DIR = new File(Frickin.name);
  34.     public static File MODULE_BASE_DIR = new File(ROOT_DIR, "modules");
  35.     public static File clickgui = new File(ROOT_DIR, "clickgui.json");
  36.  
  37.     public FileManager() {
  38.         super();
  39.  
  40.     }
  41.  
  42.  
  43.     public void init() {
  44.         // Root Dir
  45.         if(!ROOT_DIR.exists()) ROOT_DIR.mkdirs();
  46.  
  47.         // Modules saving
  48.         if(!MODULE_BASE_DIR.exists()){
  49.             saveMods();
  50.         }
  51.  
  52.         //ClickGUI
  53.         if(!clickgui.exists()){
  54.             saveClickGui();
  55.         }
  56.  
  57.         if(MODULE_BASE_DIR.exists() && clickgui.exists()){
  58.             loadAll();
  59.         }
  60.     }
  61.  
  62.     // Blacklisted Modules
  63.     private HashSet<String> modulesBlacklist = Sets.newHashSet(
  64.             ClickGUI.class.getName(),
  65.             TabGUI.class.getName()
  66.         );
  67.  
  68.     public boolean isModuleBlacklisted(Module m){
  69.         return modulesBlacklist.contains(m.getClass().getName());
  70.     }
  71.  
  72.     public static void WARNINGBOX(String infoMessage, String titleBar)
  73.     {
  74.         JOptionPane.showMessageDialog(null, infoMessage, "["+Frickin.name+"]: " + titleBar, JOptionPane.WARNING_MESSAGE);
  75.     }
  76.  
  77.  
  78.     private void saveClickGui() {
  79.         try{
  80.             JsonObject json = new JsonObject();
  81.  
  82.             for (Category c : Module.Category.values()){
  83.                 JsonObject jsonCategory = new JsonObject();
  84.                 try{
  85.                     if (c.x != 0){
  86.                         jsonCategory.addProperty("x", c.x);
  87.                         jsonCategory.addProperty("y", c.y);
  88.                         jsonCategory.addProperty("x2", c.x2);
  89.                         jsonCategory.addProperty("y2", c.y2);
  90.                         jsonCategory.addProperty("extended", c.extended);
  91.                     }
  92.                 }catch(Exception e) { }
  93.                 json.add(c.name(), jsonCategory);
  94.             }
  95.  
  96.             if(!ROOT_DIR.exists()) ROOT_DIR.mkdirs();
  97.             PrintWriter save = new PrintWriter(new FileWriter(clickgui));
  98.             save.println(JsonUtils.prettyGson.toJson(json));
  99.             save.close();
  100.         } catch(Exception e) {
  101.             WARNINGBOX("Failed to save data for ClickGUI:\n"+e.toString(), "File Manager");
  102.             e.printStackTrace();
  103.         }
  104.  
  105.     }
  106.  
  107.     public void saveMods() {
  108.         try{
  109.             JsonObject json = new JsonObject();
  110.             for(Module m : Frickin.modules) {
  111.                 File module = new File(MODULE_BASE_DIR, m.getName() + ".json");
  112.  
  113.                 JsonObject jsonMod = new JsonObject();
  114.  
  115.                 jsonMod.addProperty("enabled", m.isEnabled());
  116.                 jsonMod.addProperty("keybind", m.keyCode.getKeyCode());
  117.  
  118.                 if (Frickin.setmgr.getSettingsByMod(m) != null){
  119.                     for (de.Hero.settings.Setting s : Frickin.setmgr.getSettingsByMod(m)){
  120.                         try{
  121.                             boolean isCombo = s.isCombo();
  122.                             boolean isCheck = s.isCheck();
  123.                             boolean isSlider = s.isSlider();
  124.  
  125.                             if (isCombo){
  126.                                 jsonMod.addProperty(s.getName(), s.getValString());
  127.                             }
  128.                             if (isCheck){
  129.                                 jsonMod.addProperty(s.getName(), s.getValBoolean());
  130.                             }
  131.                             if (isSlider) {
  132.                                 jsonMod.addProperty(s.getName(), s.getValDouble());
  133.                             }
  134.                         }catch(Exception e) { }
  135.                     }
  136.                 }
  137.  
  138.                 json.add(m.getName(), jsonMod);
  139.  
  140.                 if(!ROOT_DIR.exists()) ROOT_DIR.mkdirs();
  141.                 PrintWriter save = new PrintWriter(new FileWriter(module));
  142.                 save.println(JsonUtils.prettyGson.toJson(json));
  143.                 save.close();
  144.             }
  145.  
  146.         } catch(Exception e) {
  147.             WARNINGBOX("Failed to save data for modules:\n"+e.toString(), "File Manager");
  148.             e.printStackTrace();
  149.         }
  150.         saveClickGui();
  151.     }
  152.  
  153.     private void loadAll() {
  154.         try{
  155.  
  156.             BufferedReader loadCat = new BufferedReader(new FileReader(clickgui));
  157.             JsonObject jsonCat = (JsonObject)JsonUtils.jsonParser.parse(loadCat);
  158.             loadCat.close();
  159.             Iterator<Entry<String, JsonElement>> itrCat = jsonCat.entrySet().iterator();
  160.  
  161.             while(itrCat.hasNext()) {
  162.                 Entry<String, JsonElement> entryCat = itrCat.next();
  163.                 JsonObject jsonCategory = (JsonObject)entryCat.getValue();
  164.  
  165.                 if (Category.isInEnum(entryCat.getKey())){
  166.                     Category c = Category.valueOf(entryCat.getKey());
  167.  
  168.                     try{
  169.                         c.x = jsonCategory.get("x").getAsDouble();
  170.                         c.y = jsonCategory.get("y").getAsDouble();
  171.                         c.x2 = jsonCategory.get("x2").getAsDouble();
  172.                         c.y2 = jsonCategory.get("y2").getAsDouble();
  173.                         c.extended = jsonCategory.get("extended").getAsBoolean();
  174.                     }catch(Exception e){ e.printStackTrace(); }
  175.                 }
  176.             }
  177.  
  178.             for (Module m : modules) {
  179.  
  180.                 BufferedReader load = new BufferedReader(new FileReader(new File(MODULE_BASE_DIR, m.getName() + ".json")));
  181.                 JsonObject json = (JsonObject)JsonUtils.jsonParser.parse(load);
  182.                 load.close();
  183.                 Iterator<Entry<String, JsonElement>> itr = json.entrySet().iterator();
  184.  
  185.                 if (m != null && !isModuleBlacklisted(m)) {
  186.                     JsonObject jsonModule = (JsonObject)entry.getValue();
  187.                     boolean enabled = jsonModule.get("enabled").getAsBoolean();
  188.                     int keybind = jsonModule.get("keybind").getAsInt();
  189.  
  190.                     if (Frickin.setmgr.getSettingsByMod(m) != null){
  191.                         for (de.Hero.settings.Setting s : Frickin.setmgr.getSettingsByMod(m)){
  192.                             try{
  193.                                 boolean isCombo = s.isCombo();
  194.                                 boolean isCheck = s.isCheck();
  195.                                 boolean isSlider = s.isSlider();
  196.  
  197.                                 if (isCombo){
  198.                                     Frickin.setmgr.getSettingByName(s.getName()).setValString( jsonModule.get(s.getName()).getAsString() );
  199.                                 }
  200.                                 if (isCheck){
  201.                                     Frickin.setmgr.getSettingByName(s.getName()).setValBoolean( jsonModule.get(s.getName()).getAsBoolean() );
  202.                                 }
  203.                                 if (isSlider) {
  204.                                     if (s.onlyInt())
  205.                                         Frickin.setmgr.getSettingByName(s.getName()).setValDouble( jsonModule.get(s.getName()).getAsInt() );
  206.                                     else Frickin.setmgr.getSettingByName(s.getName()).setValDouble( jsonModule.get(s.getName()).getAsDouble() );
  207.                                 }
  208.                             }catch(Exception ignored) { }
  209.                         }
  210.                     }
  211.  
  212.                     if(enabled) {
  213.                         m.toggle();
  214.                     }
  215.                     m.keyCode.setKeyCode(keybind);
  216.                 }
  217.             }
  218.         } catch(Exception e) {
  219.             WARNINGBOX("Failed to load saved data for modules & ClickGUI, please try relaunching the client or report this issue to the developers:\n"+e.toString(), "File Manager");
  220.             e.printStackTrace();
  221.         }
  222.     }
  223.  
  224. }
  225.  
Advertisement
Add Comment
Please, Sign In to add comment