Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.40 KB | None | 0 0
  1. package com.kNoAPP.Clara.aspects;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7.  
  8. import org.apache.commons.io.FileUtils;
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Material;
  12. import org.bukkit.Sound;
  13. import org.bukkit.World;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.enchantments.Enchantment;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.inventory.Inventory;
  18. import org.bukkit.inventory.ItemFlag;
  19. import org.bukkit.inventory.ItemStack;
  20. import org.bukkit.inventory.meta.ItemMeta;
  21. import org.bukkit.scheduler.BukkitRunnable;
  22.  
  23. import com.kNoAPP.Clara.Clara;
  24. import com.kNoAPP.Clara.bungee.BungeeAPI;
  25. import com.kNoAPP.Clara.data.Data;
  26. import com.kNoAPP.Clara.utils.Tools;
  27.  
  28. public class Environment {
  29.  
  30.     public static List<Environment> environments = new ArrayList<Environment>();
  31.    
  32.     public static HashMap<String, Environment> changingName = new HashMap<String, Environment>();
  33.     public static HashMap<String, Object[]> settingWorld = new HashMap<String, Object[]>();
  34.    
  35.     private String name;
  36.     private int id;
  37.     private Material icon;
  38.    
  39.     private List<String> pluginNames;
  40.     private List<EWorld> worlds;
  41.    
  42.     private boolean forceRestart;
  43.     private boolean loadFreshWorld;
  44.     private boolean saveWorld;
  45.    
  46.     public Environment(String name, int id) {
  47.         this.name = name;
  48.         this.id = id;
  49.         icon = Material.PAPER;
  50.        
  51.         pluginNames = new ArrayList<String>();
  52.         worlds = new ArrayList<EWorld>();
  53.        
  54.         forceRestart = false;
  55.         loadFreshWorld = false;
  56.         saveWorld = false;
  57.     }
  58.    
  59.     public Environment(String name, int id, Material icon) {
  60.         this.name = name;
  61.         this.id = id;
  62.         this.icon = icon;
  63.        
  64.         pluginNames = new ArrayList<String>();
  65.         worlds = new ArrayList<EWorld>();
  66.        
  67.         forceRestart = false;
  68.         loadFreshWorld = false;
  69.         saveWorld = false;
  70.     }
  71.    
  72.     public Environment(String name, int id, Material icon, List<String> pluginNames, List<EWorld> worlds,
  73.             boolean forceRestart, boolean loadFreshWorld, boolean saveWorld) {
  74.         this.name = name;
  75.         this.id = id;
  76.         this.icon = icon;
  77.        
  78.         this.pluginNames = pluginNames;
  79.         this.worlds = worlds;
  80.        
  81.         this.forceRestart = forceRestart;
  82.         this.loadFreshWorld = loadFreshWorld;
  83.         this.saveWorld = saveWorld;
  84.     }
  85.    
  86.     public String getName() {
  87.         return name;
  88.     }
  89.    
  90.     public void setName(String name) {
  91.         this.name = name;
  92.     }
  93.    
  94.     public int getID() {
  95.         return id;
  96.     }
  97.    
  98.     public boolean forceRestart() {
  99.         return forceRestart;
  100.     }
  101.    
  102.     public void setForceRestart(boolean forceRestart) {
  103.         this.forceRestart = forceRestart;
  104.     }
  105.    
  106.     public boolean loadFreshWorld() {
  107.         return loadFreshWorld;
  108.     }
  109.    
  110.     public void setLoadFreshWorld(boolean loadFreshWorld) {
  111.         this.loadFreshWorld = loadFreshWorld;
  112.     }
  113.    
  114.     public boolean saveWorld() {
  115.         return saveWorld;
  116.     }
  117.    
  118.     public void setSaveWorld(boolean saveWorld) {
  119.         this.saveWorld = saveWorld;
  120.     }
  121.    
  122.     public Material getIcon() {
  123.         return icon;
  124.     }
  125.    
  126.     public void setIcon(Material icon) {
  127.         this.icon = icon;
  128.     }
  129.    
  130.     public List<String> getPluginNames() {
  131.         return pluginNames;
  132.     }
  133.    
  134.     public List<EWorld> getWorlds() {
  135.         return worlds;
  136.     }
  137.    
  138.     public List<String> serializeWorlds() {
  139.         List<String> sWorlds = new ArrayList<String>();
  140.         for(EWorld ew : worlds) {
  141.             String sWorld = ew.getName() + ";" + ew.getCopiedName();
  142.             sWorlds.add(sWorld);
  143.         }
  144.         return sWorlds;
  145.     }
  146.    
  147.     public void addPlugin(File f) { //Could be String
  148.         pluginNames.add(f.getName());
  149.     }
  150.    
  151.     public void removePlugin(File f) { //Could be String
  152.         pluginNames.remove(f.getName());
  153.     }
  154.    
  155.     public void addWorld(EWorld ew) {
  156.         worlds.add(ew);
  157.     }
  158.    
  159.     public void removeWorld(EWorld ew) {
  160.         worlds.remove(ew);
  161.     }
  162.    
  163.     public boolean isMissingPlugins(boolean local) {
  164.         return Tools.convertBoolean(getMissingPlugins(local).size());
  165.     }
  166.    
  167.     public boolean isMissingWorlds(boolean local) {
  168.         return Tools.convertBoolean(getMissingWorlds(local).size());
  169.     }
  170.    
  171.     public List<String> getMissingPlugins(boolean local) {
  172.         List<String> pluginFiles = new ArrayList<String>();
  173.         File source;
  174.         if(local) source = new File(Bukkit.getWorldContainer(), "plugins");
  175.         else source = new File(Data.ENVIRONMENT.getFileConfig().getString("Database"));
  176.         File[] targets = source.listFiles();
  177.        
  178.         for(String s : pluginNames) {
  179.             boolean m = true;
  180.             for(File target : targets) {
  181.                 if(target.getName().equals(s) && target.isFile()) m = false;
  182.             }
  183.             if(m) pluginFiles.add(s);
  184.         }
  185.        
  186.         return pluginFiles;
  187.     }
  188.    
  189.     public List<EWorld> getMissingWorlds(boolean local) {
  190.         List<EWorld> worldFiles = new ArrayList<EWorld>();
  191.         File source;
  192.         if(local) source = Bukkit.getWorldContainer();
  193.         else source = new File(Data.ENVIRONMENT.getFileConfig().getString("Database"));
  194.         File[] targets = source.listFiles();
  195.        
  196.         for(EWorld ew : worlds) {
  197.             boolean m = true;
  198.             for(File target : targets) {
  199.                 if(local && target.getName().equals(ew.getCopiedName())) {
  200.                     m = false;
  201.                 }
  202.                 if(!local && target.getName().equals(ew.getName())) {
  203.                     m = false;
  204.                 }
  205.             }
  206.             if(m) worldFiles.add(ew);
  207.         }
  208.         return worldFiles;
  209.     }
  210.    
  211.     /**
  212.      * Gets plugins involved with the current Environment only
  213.      * @param local plugin(t) or database folder(f)
  214.      */
  215.     public List<File> getPlugins(boolean local) {
  216.         List<File> pluginFiles = new ArrayList<File>();
  217.         File source;
  218.         if(local) source = new File(Bukkit.getWorldContainer(), "plugins");
  219.         else source = new File(Data.ENVIRONMENT.getFileConfig().getString("Database"));
  220.         File[] targets = source.listFiles();
  221.        
  222.         for(File target : targets) {
  223.             if(pluginNames.contains(target.getName()) && target.isFile()) {
  224.                 pluginFiles.add(target);
  225.             }
  226.         }
  227.         return pluginFiles;
  228.     }
  229.    
  230.     public List<File> getWorlds(boolean local) {
  231.         List<File> worldFiles = new ArrayList<File>();
  232.         File source;
  233.         if(local) source = Bukkit.getWorldContainer();
  234.         else source = new File(Data.ENVIRONMENT.getFileConfig().getString("Database"));
  235.         File[] targets = source.listFiles();
  236.        
  237.         for(File target : targets) {
  238.             for(EWorld ew : worlds) {
  239.                 if(local && target.getName().equals(ew.getCopiedName()) && target.isDirectory()) {
  240.                     worldFiles.add(target);
  241.                 }
  242.                 if(!local && target.getName().equals(ew.getName()) && target.isDirectory()) {
  243.                     worldFiles.add(target);
  244.                 }
  245.             }
  246.         }
  247.         return worldFiles;
  248.     }
  249.    
  250.     public EWorld getEWorld(String n, boolean local) {
  251.         for(EWorld ew : worlds) {
  252.             if(local && n.equals(ew.getCopiedName())) {
  253.                 return ew;
  254.             }
  255.             if(!local && n.equals(ew.getName())) {
  256.                 return ew;
  257.             }
  258.         }
  259.         return null;
  260.     }
  261.    
  262.     public ItemStack getItem() {
  263.         ItemStack is = new ItemStack(icon);
  264.         ItemMeta im = is.getItemMeta();
  265.         im.setDisplayName(ChatColor.GOLD + name);
  266.         List<String> lores = new ArrayList<String>();
  267.         lores.add(ChatColor.GRAY + "ID: " + id);
  268.         if(getThisEnvironment() == this) {
  269.             lores.add(ChatColor.GREEN + "Active!");
  270.             im.addEnchant(Enchantment.ARROW_INFINITE, 1, false);
  271.             im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  272.         }
  273.         im.setLore(lores);
  274.         is.setItemMeta(im);
  275.         return is;
  276.     }
  277.    
  278.     public void add() {
  279.         environments.add(this);
  280.     }
  281.    
  282.     public void remove() {
  283.         environments.remove(this);
  284.     }
  285.    
  286.     public void load() {
  287.         if(getThisEnvironment() != null) {
  288.             Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[" + Clara.getPlugin().getName() + "] Environment [" + getName() + "] has been queued for loading!");
  289.             FileConfiguration fc = Data.ENVIRONMENT.getFileConfig();
  290.             fc.set("Queued", getID());
  291.             getThisEnvironment().unload();
  292.             return;
  293.         }
  294.        
  295.         Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[" + Clara.getPlugin().getName() + "] Loading Environment [" + getName() + "]");
  296.         if(isMissingPlugins(false)) {
  297.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[" + Clara.getPlugin().getName() + "] This setup is missing plugins!");
  298.             for(String s : getMissingPlugins(false)) {
  299.                 Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[" + Clara.getPlugin().getName() + "] " + ChatColor.GOLD + s);
  300.             }
  301.         }
  302.        
  303.         long delay = 0;
  304.         if((getWorlds(false).size() != 0 || forceRestart) && Data.MAIN.getFileConfig().getBoolean("Enable.MySQL_Bungee")) {
  305.             delay = 40L;
  306.             Server transfer = Server.transferServer(Server.getThisServer());
  307.             for(Player pl : Bukkit.getOnlinePlayers()) {
  308.                 if(transfer != null) {
  309.                     pl.sendMessage(Message.WARN.getMessage("This server is changing setups!"));
  310.                     pl.sendMessage(Message.WARN.getMessage("You've been connected to " + transfer.getName() + "!"));
  311.                     BungeeAPI.connect(pl, transfer.getName());
  312.                 } else {
  313.                     pl.kickPlayer(Message.WARN.getMessage("This server is changing setups!"));
  314.                 }
  315.             }
  316.         }
  317.        
  318.         //Removes/Unloads Worlds
  319.         /*
  320.         new BukkitRunnable() {
  321.             public void run() {
  322.                 for(File f : getWorlds(false)) {
  323.                     EWorld ew = getEWorld(f.getName(), false);
  324.                     File d = new File(Bukkit.getWorldContainer(), ew.getCopiedName());
  325.                     if(d.exists()) {
  326.                         World w = Bukkit.getWorld(d.getName());
  327.                         if(w != null) {
  328.                             w.setAutoSave(false);
  329.                             Bukkit.getServer().unloadWorld(w.getName(), true);
  330.                             try {FileUtils.deleteDirectory(d);}
  331.                             catch(Exception ex) {ex.printStackTrace();}
  332.                         }
  333.                     }
  334.                 }
  335.             }
  336.         }.runTaskLater(Clara.getPlugin(), delay);
  337.         */
  338.        
  339.         new BukkitRunnable() {
  340.             public void run() {
  341.                 loadPlugins();
  342.                 loadWorlds();
  343.                
  344.                 FileConfiguration fc = Data.ENVIRONMENT.getFileConfig();
  345.                 fc.set("Active", getID());
  346.                 if(fc.getInt("Queued") == getID()) fc.set("Queued", 0); //Removes Queue
  347.                 Data.ENVIRONMENT.saveDataFile(fc);
  348.                
  349.                 if(getWorlds(false).size() == 0 && !forceRestart) {
  350.                     Bukkit.reload(); //Try this
  351.                 } else {
  352.                     Bukkit.shutdown();
  353.                 }
  354.             }
  355.         }.runTaskLater(Clara.getPlugin(), delay);
  356.     }
  357.    
  358.     public void unload() {
  359.         Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[" + Clara.getPlugin().getName() + "] Unloading Environment [" + getName() + "]");
  360.        
  361.         long delay = 0;
  362.         if((getWorlds(false).size() != 0 || forceRestart) && Data.MAIN.getFileConfig().getBoolean("Enable.MySQL_Bungee")) {
  363.             delay = 40L;
  364.             Server transfer = Server.transferServer(Server.getThisServer());
  365.             for(Player pl : Bukkit.getOnlinePlayers()) {
  366.                 if(transfer != null) {
  367.                     pl.sendMessage(Message.WARN.getMessage("This server is changing setups!"));
  368.                     pl.sendMessage(Message.WARN.getMessage("You've been connected to " + transfer.getName() + "!"));
  369.                     BungeeAPI.connect(pl, transfer.getName());
  370.                 } else {
  371.                     pl.kickPlayer(Message.WARN.getMessage("This server is changing setups!"));
  372.                 }
  373.             }
  374.         }
  375.        
  376.         new BukkitRunnable() {
  377.             public void run() {
  378.                 unloadPlugins();
  379.                 unloadWorlds();
  380.                
  381.                 FileConfiguration fc = Data.ENVIRONMENT.getFileConfig();
  382.                 fc.set("Active", 0);
  383.                 Data.ENVIRONMENT.saveDataFile(fc);
  384.                
  385.                 if(getWorlds(false).size() == 0 && !forceRestart) {
  386.                     Bukkit.reload(); //Try this
  387.                 } else {
  388.                     Bukkit.shutdown();
  389.                 }
  390.             }
  391.         }.runTaskLater(Clara.getPlugin(), delay);
  392.     }
  393.    
  394.     public void loadPlugins() {
  395.         File d = new File(Bukkit.getWorldContainer(), "plugins");
  396.         for(File f : getPlugins(false)) {
  397.             try {FileUtils.copyFileToDirectory(f, d);}
  398.             catch(Exception ex) {ex.printStackTrace();}
  399.         }
  400.     }
  401.    
  402.     public void unloadPlugins() {
  403.         for(File f : getPlugins(true)) {
  404.             try{f.delete();}
  405.             catch(Exception ex) {ex.printStackTrace();}
  406.         }
  407.     }
  408.    
  409.     public void loadWorlds() {
  410.         for(File f : getWorlds(false)) {
  411.             EWorld ew = getEWorld(f.getName(), false);
  412.             File d = new File(Bukkit.getWorldContainer(), ew.getCopiedName());
  413.             if(d.exists()) {
  414.                 World w = Bukkit.getWorld(d.getName());
  415.                 if(w != null) {
  416.                     w.setAutoSave(false);
  417.                     Bukkit.getServer().unloadWorld(w.getName(), true);
  418.                     try {FileUtils.deleteDirectory(d);}
  419.                     catch(Exception ex) {ex.printStackTrace();}
  420.                 }
  421.             }
  422.             try {FileUtils.copyDirectory(f, d);}
  423.             catch(Exception ex) {ex.printStackTrace();}
  424.         }
  425.     }
  426.    
  427.     public void unloadWorlds() {
  428.         for(File f : getWorlds(true)) {
  429.             if(saveWorld) {
  430.                 Bukkit.getWorld(f.getName()).save();
  431.                 EWorld ew = getEWorld(f.getName(), true);
  432.                 File d = new File(Data.ENVIRONMENT.getFileConfig().getString("Database"), ew.getName());
  433.                
  434.                 try{FileUtils.deleteDirectory(d);}
  435.                 catch(Exception ex) {Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[" + Clara.getPlugin().getName() + "] Failed to delete a world from the Database!");}
  436.                
  437.                 try{FileUtils.copyDirectory(f, d);}
  438.                 catch(Exception ex) {Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[" + Clara.getPlugin().getName() + "] Failed to save a world to the Database!");}
  439.             }
  440.            
  441.             try{FileUtils.deleteDirectory(f);}
  442.             catch(Exception ex) {ex.printStackTrace();}
  443.         }
  444.     }
  445.    
  446.     public Inventory getSubInventory() {
  447.         Inventory inv = Bukkit.createInventory(null, 54, name);
  448.         inv.setItem(0, SpecialItem.BACK.getItem());
  449.         inv.setItem(8, SpecialItem.SETTINGS.getItem());
  450.         inv.setItem(4, getItem());
  451.         inv.setItem(22, SpecialItem.MANAGE_PLUGINS.getItem());
  452.         inv.setItem(25, SpecialItem.MANAGE_WORLDS.getItem());
  453.         if(getThisEnvironment() == null) inv.setItem(19, SpecialItem.START_SERVER.getItem());
  454.         else if(getThisEnvironment() == this) inv.setItem(19, SpecialItem.STOP_SERVER.getItem());
  455.         else inv.setItem(19, SpecialItem.QUEUE_SERVER.getItem());
  456.         inv.setItem(37, SpecialItem.CHANGE_NAME.getItem());
  457.         inv.setItem(40, SpecialItem.CHANGE_ICON.getItem());
  458.         inv.setItem(43, SpecialItem.DELETE_ENVIRONMENT.getItem());
  459.         return inv;
  460.     }
  461.    
  462.     public void openSubInventory(Player p) {
  463.         p.playSound(p.getLocation(), Sound.BLOCK_WOOD_BUTTON_CLICK_ON, 1F, 1F);
  464.         p.openInventory(getSubInventory());
  465.     }
  466.    
  467.     public Inventory getSettingsInventory() {
  468.         Inventory inv = Bukkit.createInventory(null, 9, name + " - Settings");
  469.         if(forceRestart) inv.setItem(0, SpecialItem.FORCE_RESTART_TRUE.getItem());
  470.         else inv.setItem(0, SpecialItem.FORCE_RESTART_FALSE.getItem());
  471.         if(saveWorld) inv.setItem(1, SpecialItem.SAVE_WORLD_TRUE.getItem());
  472.         else inv.setItem(1, SpecialItem.SAVE_WORLD_FALSE.getItem());
  473.         if(loadFreshWorld) inv.setItem(2, SpecialItem.LOAD_WORLD_TRUE.getItem());
  474.         else inv.setItem(2, SpecialItem.LOAD_WORLD_FALSE.getItem());
  475.         inv.setItem(8, SpecialItem.BACK.getItem());
  476.         return inv;
  477.     }
  478.    
  479.     public void openSettingsInventory(Player p) {
  480.         p.playSound(p.getLocation(), Sound.BLOCK_WOOD_BUTTON_CLICK_ON, 1F, 1F);
  481.         p.openInventory(getSettingsInventory());
  482.     }
  483.    
  484.     public Inventory getMPInventory(int page) {
  485.         Inventory inv = Bukkit.createInventory(null, 54, name + " - Plugins");
  486.         List<File> files = getAllFiles(false, false);
  487.         inv.setItem(49, SpecialItem.BACK.getItem());
  488.         if(files.size() >= page*45) inv.setItem(53, SpecialItem.NEXT_ICON.setLores(new String[]{ChatColor.GRAY + "Turn to page " + (page+1)}).getItem());
  489.         if(page > 1) inv.setItem(45, SpecialItem.PREVIOUS_ICON.setLores(new String[]{ChatColor.GRAY + "Turn to page " + (page-1)}).getItem());
  490.        
  491.         for(int i=0; i<45; i++) {
  492.             if(i+((page-1)*45) < files.size()) {
  493.                 File f = files.get(i+((page-1)*45));
  494.                 inv.setItem(i, getMPItem(f));
  495.             } else break;
  496.         }
  497.        
  498.         return inv;
  499.     }
  500.    
  501.     public ItemStack getMPItem(File f) {
  502.         ItemStack is = new ItemStack(Material.PAPER, 1);
  503.         ItemMeta im = is.getItemMeta();
  504.        
  505.         im.setDisplayName(ChatColor.YELLOW + f.getName());
  506.         List<String> lores = new ArrayList<String>();
  507.         if(pluginNames.contains(f.getName())) {
  508.             im.addEnchant(Enchantment.ARROW_INFINITE, 1, false);
  509.             im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  510.             lores.add(ChatColor.GREEN + "Selected!");
  511.         }
  512.         lores.add(ChatColor.GRAY + f.getParentFile().getPath());
  513.         im.setLore(lores);
  514.         is.setItemMeta(im);
  515.         return is;
  516.     }
  517.    
  518.     public void openMPInventory(Player p, int page) {
  519.         p.playSound(p.getLocation(), Sound.BLOCK_WOOD_BUTTON_CLICK_ON, 1F, 1F);
  520.         p.openInventory(getMPInventory(page));
  521.     }
  522.    
  523.     public Inventory getMWInventory(int page) {
  524.         Inventory inv = Bukkit.createInventory(null, 54, name + " - Worlds");
  525.         List<File> files = getAllFiles(false, true);
  526.         inv.setItem(49, SpecialItem.BACK.getItem());
  527.         if(files.size() >= page*45) inv.setItem(53, SpecialItem.NEXT_ICON.setLores(new String[]{ChatColor.GRAY + "Turn to page " + (page+1)}).getItem());
  528.         if(page > 1) inv.setItem(45, SpecialItem.PREVIOUS_ICON.setLores(new String[]{ChatColor.GRAY + "Turn to page " + (page-1)}).getItem());
  529.        
  530.         for(int i=0; i<45; i++) {
  531.             if(i+((page-1)*45) < files.size()) {
  532.                 File f = files.get(i+((page-1)*45));
  533.                 inv.setItem(i, getMWItem(f));
  534.             } else break;
  535.         }
  536.         return inv;
  537.     }
  538.    
  539.     public ItemStack getMWItem(File f) {
  540.         ItemStack is = new ItemStack(Material.PAPER, 1);
  541.         ItemMeta im = is.getItemMeta();
  542.        
  543.         im.setDisplayName(ChatColor.YELLOW + f.getName());
  544.         List<String> lores = new ArrayList<String>();
  545.         for(EWorld ew : worlds) {
  546.             if(ew.getName().equals(f.getName())) {
  547.                 im.addEnchant(Enchantment.ARROW_INFINITE, 1, false);
  548.                 im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  549.                 lores.add(ChatColor.GREEN + "Selected!");
  550.                 lores.add(ChatColor.GOLD + ew.getCopiedName());
  551.             }
  552.         }
  553.         lores.add(ChatColor.GRAY + f.getParentFile().getPath());
  554.         im.setLore(lores);
  555.         is.setItemMeta(im);
  556.         return is;
  557.     }
  558.    
  559.     public void openMWInventory(Player p, int page) {
  560.         p.playSound(p.getLocation(), Sound.BLOCK_WOOD_BUTTON_CLICK_ON, 1F, 1F);
  561.         p.openInventory(getMWInventory(page));
  562.     }
  563.    
  564.     public Inventory getIconInventory() {
  565.         Inventory inv = Bukkit.createInventory(null, 9, name + " - Change Icon");
  566.         for(int a=0; a<9; a++) {
  567.             if(a != 4) {
  568.                 inv.setItem(a, SpecialItem.PLACE_HOLDER.getItem());
  569.             }
  570.         }
  571.         return inv;
  572.     }
  573.    
  574.     public void openIconInventory(Player p) {
  575.         p.sendMessage(Message.INFO.getMessage("Place your icon in the inventory!"));
  576.         p.playSound(p.getLocation(), Sound.BLOCK_WOOD_BUTTON_CLICK_ON, 1F, 1F);
  577.         p.openInventory(getIconInventory());
  578.     }
  579.    
  580.     public static Environment createBasicSetup() {
  581.         int aID = nextOpenID();
  582.         Environment e = new Environment("Setup-" + aID, aID);
  583.         e.add();
  584.         return e;
  585.     }
  586.    
  587.     private static int nextOpenID() {
  588.         boolean found = false;
  589.         int id = 0;
  590.         while(!found) {
  591.             id++;
  592.             found = true;
  593.             for(Environment e : environments) {
  594.                 if(e.getID() == id) {
  595.                     found = false;
  596.                 }
  597.             }
  598.         }
  599.         return id;
  600.     }
  601.    
  602.     /**
  603.      * Gets all files in the local or database folder
  604.      * @param local plugin(t) or database folder(f)
  605.      */
  606.     public static File[] getAllFiles(boolean local) {
  607.         File source;
  608.         if(local) source = new File(Bukkit.getWorldContainer(), "plugins");
  609.         else source = new File(Data.ENVIRONMENT.getFileConfig().getString("Database"));
  610.         return source.listFiles();
  611.     }
  612.    
  613.     /**
  614.      * Gets all files in the local or database folder
  615.      * @param local plugin(t) or database folder(f)
  616.      * @param directory is directory?
  617.      */
  618.     public static List<File> getAllFiles(boolean local, boolean directory) {
  619.         File source;
  620.         if(local) source = new File(Bukkit.getWorldContainer(), "plugins");
  621.         else source = new File(Data.ENVIRONMENT.getFileConfig().getString("Database"));
  622.        
  623.         List<File> files = new ArrayList<File>();
  624.         for(File f : source.listFiles()) if(f.isDirectory() == directory) files.add(f);
  625.         return files;
  626.     }
  627.    
  628.     public static void importEnvironments() {
  629.         FileConfiguration fc = Data.ENVIRONMENT.getFileConfig();
  630.         if(fc.getConfigurationSection("Environment") != null) { //New plugins will trigger this check.
  631.             for(String name : fc.getConfigurationSection("Environment").getKeys(false)) {
  632.                 int id = fc.getInt("Environment." + name + ".id");
  633.                 Material icon = Material.getMaterial(fc.getString("Environment." + name + ".icon"));
  634.                 List<String> pluginNames = fc.getStringList("Environment." + name + ".plugins");
  635.                 List<EWorld> worlds = new ArrayList<EWorld>();
  636.                 if(fc.getStringList("Environment." + name + ".worlds") != null) {
  637.                     for(String s : fc.getStringList("Environment." + name + ".worlds")) {
  638.                         worlds.add(EWorld.deserialize(s));
  639.                     }
  640.                 }
  641.                
  642.                 boolean forceRestart = fc.getBoolean("Environment." + name + ".settings.FR");
  643.                 boolean loadFreshWorld = fc.getBoolean("Environment." + name + ".settings.LFW");
  644.                 boolean saveWorld = fc.getBoolean("Environment." + name + ".settings.SW");
  645.                
  646.                 new Environment(name, id, icon, pluginNames, worlds,
  647.                         forceRestart, loadFreshWorld, saveWorld).add();
  648.             }
  649.         }
  650.     }
  651.    
  652.     public static void exportEnvironments() {
  653.         FileConfiguration fc = Data.ENVIRONMENT.getFileConfig();
  654.         fc.set("Environment", null);
  655.         for(Environment e : environments) {
  656.             fc.set("Environment." + e.getName() + ".id", e.getID());
  657.             fc.set("Environment." + e.getName() + ".icon", e.getIcon().toString());
  658.             fc.set("Environment." + e.getName() + ".plugins", e.getPluginNames());
  659.             fc.set("Environment." + e.getName() + ".worlds", e.serializeWorlds());
  660.            
  661.             fc.set("Environment." + e.getName() + ".settings.FR", e.forceRestart());
  662.             fc.set("Environment." + e.getName() + ".settings.LFW", e.loadFreshWorld());
  663.             fc.set("Environment." + e.getName() + ".settings.SW", e.saveWorld());
  664.         }
  665.         Data.ENVIRONMENT.saveDataFile(fc);
  666.     }
  667.    
  668.     public static Environment getEnvironment(String name) {
  669.         for(Environment e : environments) {
  670.             if(e.getName().equalsIgnoreCase(name)) {
  671.                 return e;
  672.             }
  673.         }
  674.         return null;
  675.     }
  676.    
  677.     public static Environment getEnvironment(int id) {
  678.         for(Environment e : environments) {
  679.             if(e.getID() == id) {
  680.                 return e;
  681.             }
  682.         }
  683.         return null;
  684.     }
  685.    
  686.     public static Environment getThisEnvironment() {
  687.         return getEnvironment(Data.ENVIRONMENT.getFileConfig().getInt("Active"));
  688.     }
  689.    
  690.     public static Environment getQueuedEnvironment() {
  691.         return getEnvironment(Data.ENVIRONMENT.getFileConfig().getInt("Queued"));
  692.     }
  693.    
  694.     public static Inventory getMainInventory(int page) {
  695.         Inventory inv = Bukkit.createInventory(null, 54, "Clara Setups");
  696.         inv.setItem(4, SpecialItem.CLARA_SETUPS.getItem());
  697.         if(environments.size() >= page*45) inv.setItem(8, SpecialItem.NEXT_ICON.setLores(new String[]{ChatColor.GRAY + "Turn to page " + (page+1)}).getItem());
  698.         if(page > 1) inv.setItem(0, SpecialItem.PREVIOUS_ICON.setLores(new String[]{ChatColor.GRAY + "Turn to page " + (page-1)}).getItem());
  699.        
  700.         for(int i=0; i<45; i++) {
  701.             if(i+((page-1)*45) < environments.size()) {
  702.                 Environment e = environments.get(i+((page-1)*45));
  703.                 inv.setItem(i+9, e.getItem());
  704.             } else inv.setItem(i+9, SpecialItem.NEW_SETUP.getItem());
  705.         }
  706.  
  707.         return inv;
  708.     }
  709.    
  710.     public static void openMainInventory(Player p, int page) {
  711.         p.playSound(p.getLocation(), Sound.BLOCK_WOOD_BUTTON_CLICK_ON, 1F, 1F);
  712.         p.openInventory(getMainInventory(page));
  713.     }
  714. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement