Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.54 KB | None | 0 0
  1. package de.bananaco.permissions;
  2.  
  3. import java.io.File;
  4. import java.util.*;
  5.  
  6. import com.evilmidget38.UUIDFetcher;
  7. import org.bukkit.World;
  8. import org.bukkit.configuration.ConfigurationSection;
  9. import org.bukkit.configuration.file.YamlConfiguration;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13.  
  14. import de.bananaco.bpermissions.api.Group;
  15. import de.bananaco.bpermissions.api.Permission;
  16. import de.bananaco.bpermissions.api.User;
  17. import de.bananaco.bpermissions.api.WorldManager;
  18. import de.bananaco.bpermissions.imp.Debugger;
  19. import de.bananaco.bpermissions.imp.loadmanager.MainThread;
  20. import de.bananaco.bpermissions.imp.loadmanager.TaskRunnable;
  21.  
  22. public class ImportManager {
  23.  
  24.     private WorldManager wm = WorldManager.getInstance();
  25.     private final JavaPlugin plugin;
  26.  
  27.     public ImportManager(JavaPlugin plugin) {
  28.         this.plugin = plugin;
  29.     }
  30.  
  31.     public boolean pexImport() {
  32.         if (MainThread.getInstance() == null) {
  33.             Debugger.log("MainThread cancelled");
  34.             return false;
  35.         }
  36.         try {
  37.             // load async
  38.             MainThread.getInstance().schedule(new TaskRunnable() {
  39.                 public void run() {
  40.                     try {
  41.                         importPEX();
  42.                     } catch (Exception e) {
  43.                         e.printStackTrace();
  44.                     }
  45.                 }
  46.  
  47.                 public TaskRunnable.TaskType getType() {
  48.                     return TaskRunnable.TaskType.LOAD;
  49.                 }
  50.             });
  51.             // If it loaded correctly cancel the error
  52.         } catch (Exception e) {
  53.             Bukkit.getServer().broadcastMessage(ChatColor.RED + "PEX import failed! Check the server log!");
  54.             e.printStackTrace();
  55.         }
  56.         return true;
  57.     }
  58.  
  59.     protected synchronized void importPEX() throws Exception {
  60.         File file = new File("plugins/PermissionsEx/permissions.yml");
  61.         // No point doing anything if the file doesn't exist
  62.         if (!file.exists()) {
  63.             System.err.println("File not exist");
  64.             return;
  65.         }
  66.         YamlConfiguration perm = new YamlConfiguration();
  67.         perm.load(file);
  68.  
  69.         World world = plugin.getServer().getWorlds().get(0);
  70.         de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName());
  71.  
  72.         ConfigurationSection users = perm.getConfigurationSection("users");
  73.         ConfigurationSection groups = perm.getConfigurationSection("groups");
  74.  
  75.         if (users.getKeys(false) != null && users.getKeys(false).size() > 0) {
  76.             System.out.println("Importing users....");
  77.             for (String user : users.getKeys(false)) {
  78.                 List<String> g = users.getStringList(user + ".group");
  79.                 List<String> p = users.getStringList(user + ".permissions");
  80.                 User u = wd.getUser(user);
  81.                 // Remove the existing groups
  82.                 u.getGroupsAsString().clear();
  83.                 // Add all the groups
  84.                 if (g != null && g.size() > 0) {
  85.                     for (String gr : g) {
  86.                         u.addGroup(gr);
  87.                     }
  88.                 }
  89.                 if (p != null && p.size() > 0) {
  90.                     for (String pr : p) {
  91.                         if (pr.startsWith("-")) {
  92.                             u.addPermission(pr.replace("-", ""), false);
  93.                         } else {
  94.                             u.addPermission(pr, true);
  95.                         }
  96.                     }
  97.                 }
  98.                 String prefix = users.getString(user + ".prefix");
  99.                 if (prefix != null) {
  100.                     u.setValue("prefix", prefix);
  101.                 }
  102.                 String suffix = users.getString(user + ".suffix");
  103.                 if (suffix != null) {
  104.                     u.setValue("suffix", suffix);
  105.                 }
  106.             }
  107.             System.out.println("Done!");
  108.         }
  109.  
  110.         if (groups.getKeys(false) != null && groups.getKeys(false).size() > 0) {
  111.             System.out.println("Importing groups....");
  112.             for (String group : groups.getKeys(false)) {
  113.                 if (groups.getBoolean(group + ".default")) {
  114.                     wd.setDefaultGroup(group);
  115.                     System.out.println("DEFAULT GROUP DETECTED: " + group);
  116.                 }
  117.                 List<String> g = groups.getStringList(group + ".inheritance");
  118.                 List<String> p = groups.getStringList(group + ".permissions");
  119.                 Group u = wd.getGroup(group);
  120.                 // Remove the existing groups
  121.                 u.getGroupsAsString().clear();
  122.                 // Add all the groups
  123.                 if (g != null && g.size() > 0) {
  124.                     for (String gr : g) {
  125.                         u.addGroup(gr);
  126.                     }
  127.                 }
  128.                 if (p != null && p.size() > 0) {
  129.                     for (String pr : p) {
  130.                         if (pr.startsWith("-")) {
  131.                             u.addPermission(pr.replace("-", ""), false);
  132.                         } else {
  133.                             u.addPermission(pr, true);
  134.                         }
  135.                     }
  136.                 }
  137.                 String prefix = groups.getString(group + ".prefix");
  138.                 if (prefix != null) {
  139.                     u.setValue("prefix", prefix);
  140.                 }
  141.                 String suffix = groups.getString(group + ".suffix");
  142.                 if (suffix != null) {
  143.                     u.setValue("suffix", suffix);
  144.                 }
  145.                 String priority = groups.getString(group + ".options.rank");
  146.                 if (priority != null) {
  147.                     u.setValue("priority", priority);
  148.                 }
  149.             }
  150.             System.out.println("Done!");
  151.             wm.saveAll();
  152.         }
  153.     }
  154.  
  155.     public void importYML() throws Exception {
  156.         for (World world : plugin.getServer().getWorlds()) {
  157.             de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName());
  158.             File perms = new File("plugins/bPermissions/worlds/"
  159.                     + world.getName() + ".yml");
  160.             if (perms.exists()) {
  161.                 System.out.println("Importing world: " + world.getName());
  162.                 YamlConfiguration pConfig = new YamlConfiguration();//new Configuration(perms);
  163.                 pConfig.load(perms);
  164.                 // Here we grab the different bits and bobs
  165.                 ConfigurationSection users = pConfig.getConfigurationSection("players");
  166.                 ConfigurationSection groups = pConfig.getConfigurationSection("groups");
  167.  
  168.                 // Load users
  169.                 if (users != null && users.getKeys(false) != null && users.getKeys(false).size() > 0) {
  170.                     Set<String> u = users.getKeys(false);
  171.                     for (String usr : u) {
  172.                         System.out.println("Importing user: " + usr);
  173.                         List<String> g = users.getStringList(usr);
  174.                         // Clear the groups in their list firstly
  175.                         wd.getUser(usr).getGroupsAsString().clear();
  176.                         // Another NPE Fix
  177.                         if (g != null && g.size() > 0) {
  178.                             for (String group : g) {
  179.                                 wd.getUser(usr).addGroup(group);
  180.                             }
  181.                         }
  182.                     }
  183.                 }
  184.                 // Load groups
  185.                 if (groups != null && groups.getKeys(false) != null && groups.getKeys(false).size() > 0) {
  186.                     Set<String> g = groups.getKeys(false);
  187.                     for (String grp : g) {
  188.                         System.out.println("Importing group: " + grp);
  189.                         List<String> p = groups.getStringList(grp);
  190.                         if (p != null && p.size() > 0) {
  191.                             for (String perm : p) {
  192.                                 wd.getGroup(grp).getPermissions().add(Permission.loadFromString(perm));
  193.                             }
  194.                         }
  195.                     }
  196.                 }
  197.             }
  198.             // Forgot to save after importing!
  199.             wd.save();
  200.         }
  201.         wm.cleanup();
  202.     }
  203.  
  204.     public void importGroupManager() throws Exception {
  205.         for (World world : plugin.getServer().getWorlds()) {
  206.             de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName());
  207.  
  208.             File users = new File("plugins/GroupManager/worlds/" + world.getName()
  209.                     + "/users.yml");
  210.             File groups = new File("plugins/GroupManager/worlds/" + world.getName()
  211.                     + "/groups.yml");
  212.  
  213.             if (users.exists() && groups.exists()) {
  214.                 System.out.println("Importing world: " + world.getName());
  215.  
  216.                 YamlConfiguration uConfig = new YamlConfiguration();
  217.                 YamlConfiguration gConfig = new YamlConfiguration();
  218.                 try {
  219.                     uConfig.load(users);
  220.                     gConfig.load(groups);
  221.                 } catch (Exception e) {
  222.                     e.printStackTrace();
  223.                 }
  224.                 ConfigurationSection usConfig = uConfig.getConfigurationSection("users");
  225.                 ConfigurationSection grConfig = gConfig.getConfigurationSection("groups");
  226.  
  227.                 Set<String> usersList = null;
  228.                 if (usConfig != null) {
  229.                     usersList = usConfig.getKeys(false);
  230.                 }
  231.                 Set<String> groupsList = null;
  232.                 if (grConfig != null) {
  233.                     groupsList = grConfig.getKeys(false);
  234.                 }
  235.  
  236.                 if (usersList != null) {
  237.                     for (String player : usersList) {
  238.                         System.out.println("Importing user: " + player);
  239.                         User user = wd.getUser(player);
  240.                         try {
  241.                             List<String> p = uConfig.getStringList("users." + player + ".permissions");
  242.                             List<String> i = uConfig.getStringList("users." + player + ".subgroups");
  243.                             i.add(uConfig.getString("users." + player + ".group"));
  244.  
  245.                             String prefix = uConfig.getString("users." + player + ".info." + "prefix");
  246.                             String suffix = uConfig.getString("users." + player + ".info." + "suffix");
  247.  
  248.                             if (p != null) {
  249.                                 user.getPermissions().addAll(Permission.loadFromString(p));
  250.                             }
  251.                             if (i != null) {
  252.                                 user.getGroupsAsString().clear();
  253.                                 user.getGroupsAsString().addAll(i);
  254.                             }
  255.                             if (prefix != null) {
  256.                                 user.setValue("prefix", prefix);
  257.                             }
  258.                             if (suffix != null) {
  259.                                 user.setValue("suffix", suffix);
  260.                             }
  261.                         } catch (Exception e) {
  262.                             System.err.println("Error importing user: " + player);
  263.                         }
  264.                     }
  265.                 }
  266.  
  267.                 if (groupsList != null) {
  268.                     for (String group : groupsList) {
  269.                         System.out.println("Importing group: " + group);
  270.                         Group gr = wd.getGroup(group);
  271.                         try {
  272.                             List<String> p = gConfig.getStringList("groups." + group + ".permissions");
  273.                             List<String> i = gConfig.getStringList("groups." + group + ".inheritance");
  274.  
  275.                             String prefix = gConfig.getString("groups." + group + ".info." + "prefix");
  276.                             String suffix = gConfig.getString("groups." + group + ".info." + "suffix");
  277.  
  278.                             if (gConfig.getBoolean("groups." + group + ".default")) {
  279.                                 wd.setDefaultGroup(group);
  280.                                 System.out.println("DEFAULT GROUP DETECTED: " + group);
  281.                             }
  282.                             if (p != null) {
  283.                                 gr.getPermissions().addAll(Permission.loadFromString(p));
  284.                             }
  285.                             if (i != null) {
  286.                                 List<String> fp = new ArrayList<String>();
  287.                                 for (int j = 0; j < i.size(); j++) {
  288.                                     String fpp = i.get(j);
  289.                                     if (fpp.startsWith("g:")) {
  290.                                         // do nothing
  291.                                     } else {
  292.                                         fp.add(fpp);
  293.                                     }
  294.                                 }
  295.                                 i.clear();
  296.                                 i.addAll(fp);
  297.                                 gr.getGroupsAsString().addAll(i);
  298.                             }
  299.                             if (prefix != null) {
  300.                                 gr.setValue("prefix", prefix);
  301.                             }
  302.                             if (suffix != null) {
  303.                                 gr.setValue("suffix", suffix);
  304.                             }
  305.                         } catch (Exception e) {
  306.                             System.err.println("Error importing group: " + group);
  307.                         }
  308.                     }
  309.                 }
  310.                 wd.save();
  311.             }
  312.         }
  313.         wm.cleanup();
  314.     }
  315.  
  316.     public void importPermissions3() throws Exception {
  317.  
  318.     }
  319.  
  320.     public void importUuid() throws Exception {
  321.         String GROUPS = "groups";
  322.         String PERMISSIONS = "permissions";
  323.         String META = "meta";
  324.         String USERS = "users";
  325.         List<World> worlds = plugin.getServer().getWorlds();
  326.  
  327.         for (int i = 0; i < worlds.size(); i++) {
  328.             Bukkit.getLogger().info("------ Processing world " + (i + 1) + " of " + worlds.size() + " ------");
  329.             World world = worlds.get(i);
  330.             de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName());
  331.  
  332.             Bukkit.getLogger().info("Processed world " + (i + 1) + " of " + worlds.size() + ". Loading and converting.");
  333.  
  334.             File ufile = new File("plugins/bPermissions/" + world.getName().toLowerCase() + "/users.yml");
  335.             if (!ufile.exists()) {
  336.                 Bukkit.getLogger().info("Users yml not found for world " + world.getName());
  337.                 return;
  338.             }
  339.  
  340.             YamlConfiguration uconfig = new de.bananaco.bpermissions.imp.YamlConfiguration();
  341.  
  342.             long t = System.currentTimeMillis();
  343.             uconfig.load(ufile);
  344.             long f = System.currentTimeMillis();
  345.             Debugger.log("Loading files took " + (f - t) + "ms");
  346.  
  347.             /*
  348.              * Load the users
  349.              */
  350.             ConfigurationSection usersConfig = uconfig.getConfigurationSection(USERS);
  351.             if (usersConfig != null) {
  352.                 Bukkit.getLogger().info("Converting world: " + world.getName());
  353.                 Set<String> names = usersConfig.getKeys(false);
  354.                 List<String> usersList = new ArrayList<>();
  355.                 usersList.addAll(names);
  356.                 UUIDFetcher fetcher = new UUIDFetcher(usersList);
  357.  
  358.                 Map<String, UUID> result = null;
  359.  
  360.                 try {
  361.                     result = fetcher.call();
  362.                 } catch (Exception e) {
  363.                     plugin.getLogger().warning("Exception while running UUIDFetcher");
  364.                     e.printStackTrace();
  365.                     return;
  366.                 }
  367.  
  368.                 Map<String, UUID> players = new HashMap<>();
  369.  
  370.                 // convert names to lower case...
  371.                 for (String username : result.keySet()) {
  372.                     players.put(username.toLowerCase(), result.get(username));
  373.                 }
  374.  
  375.                 int size = names.size();
  376.                 int total = 1;
  377.                 for (String name : names) {
  378.                     UUID uniqueId = players.get(name);
  379.                     plugin.getLogger().info("Converting user " + uniqueId + " - " + total + " of " + size);
  380.                     if (uniqueId == null) {
  381.                         plugin.getLogger().warning("UUID for " + name + " not found! Using alternative method to fetch UUID");
  382.                         plugin.getLogger().warning("Did he already change his username?");
  383.                         uniqueId = Bukkit.getServer().getOfflinePlayer(name).getUniqueId();
  384.                     }
  385.                     List<String> nPerm = usersConfig.getStringList(name + "."
  386.                             + PERMISSIONS);
  387.                     List<String> nGroup = usersConfig.getStringList(name + "."
  388.                             + GROUPS);
  389.                     Set<Permission> perms = Permission.loadFromString(nPerm);
  390.                     // remove the old user
  391.                     User user = new User(name, nGroup, perms, world.getName(), wd);
  392.                     wd.remove(user);
  393.  
  394.                     // Create the new user!
  395.                     User uuidUser = new User(uniqueId.toString(), nGroup, perms, world.getName(), wd);
  396.                     // MetaData
  397.                     ConfigurationSection meta = usersConfig
  398.                             .getConfigurationSection(name + "." + META);
  399.                     if (meta != null) {
  400.                         Set<String> keys = meta.getKeys(false);
  401.                         if (keys != null && keys.size() > 0) {
  402.                             for (String key : keys) {
  403.                                 uuidUser.setValue(key, meta.get(key).toString());
  404.                             }
  405.                         }
  406.                     }
  407.                     wd.add(uuidUser);
  408.                     total++;
  409.                 }
  410.  
  411.                 Bukkit.getLogger().info("Converted world: " + world.getName());
  412.             } else {
  413.                 Bukkit.getLogger().info("Users yml of world is empty: " + world.getName());
  414.                 Debugger.log("Empty ConfigurationSection:" + USERS + ":" + ufile.getPath());
  415.             }
  416.  
  417.             wd.save();
  418.             wm.cleanup();
  419.  
  420.             Bukkit.getLogger().info("------ World " + (i + 1) + " of " + worlds.size() + " complete ------");
  421.  
  422.         }
  423.  
  424.         Bukkit.getLogger().info("Conversion complete!");
  425.     }
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement