Advertisement
Guest User

Untitled

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