Search in sources :

Example 1 with RegionManager

use of br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager in project RedProtect by FabioZumbi12.

the class RedProtect method startLoad.

private void startLoad() throws Exception {
    redProtectUtil = new RedProtectUtil(this);
    config = new ConfigManager();
    lang = new LangManager();
    blockConfig = new BlockConfig();
    if (config.configRoot().purge.regen.enable_whitelist_regen && Bukkit.getServer().hasWhitelist()) {
        Bukkit.getServer().setWhitelist(false);
        RedProtect.get().logger.success("Whitelist disabled!");
    }
    logger.info("Registering commands...");
    cmdHandler = new CommandHandler(this);
    logger.info("Registering listeners...");
    getServer().getPluginManager().registerEvents(cmdHandler, this);
    getServer().getPluginManager().registerEvents(new GlobalListener(), this);
    getServer().getPluginManager().registerEvents(new BlockListener(), this);
    getServer().getPluginManager().registerEvents(new PlayerListener(), this);
    getServer().getPluginManager().registerEvents(new EntityListener(), this);
    getServer().getPluginManager().registerEvents(new WorldListener(), this);
    // Register hooks
    hooks.registerHooksFirst();
    rm = new RegionManager();
    rm.loadAll();
    redProtectUtil.ReadAllDB(rm.getAllRegions());
    rm.saveAll(false);
    if (!config.configRoot().file_type.equalsIgnoreCase("mysql")) {
        startAutoSave();
    }
    logger.info("There are " + rm.getTotalRegionsNum() + " regions on (" + config.configRoot().file_type + ") database!");
    // Register hooks
    hooks.registerHooksLast();
    // Load Gui lang file
    guiLang = new LangGuiManager();
    // Update Manager
    if (updater != null) {
        Bukkit.getScheduler().cancelTask(updater.getTaskId());
        updater = null;
    }
    if (config.configRoot().update.enable) {
        updater = new SpigetUpdater(this);
        updater.setCurrentJarFile(this.getFile().getName());
        updater.hourlyUpdateCheck(getServer().getConsoleSender(), config.configRoot().update.enable, false);
    }
    bukkitVersion = getBukkitVersion();
    logger.debug(LogLevel.DEFAULT, "Version String: " + bukkitVersion);
    if (bukkitVersion >= 180) {
        getServer().getPluginManager().registerEvents(new Compat18(), this);
    }
    if (bukkitVersion >= 190) {
        getServer().getPluginManager().registerEvents(new Compat19(), this);
    }
    if (bukkitVersion >= 1110) {
        getServer().getPluginManager().registerEvents(new Compat111(), this);
    }
    if (bukkitVersion >= 1140) {
        getServer().getPluginManager().registerEvents(new Compat114(), this);
    }
    if (bukkitVersion <= 189) {
        rpvHelper = (VersionHelper) Class.forName("br.net.fabiozumbi12.RedProtect.Bukkit.helpers.VersionHelper18").newInstance();
    }
    if (bukkitVersion >= 190) {
        rpvHelper = (VersionHelper) Class.forName("br.net.fabiozumbi12.RedProtect.Bukkit.helpers.VersionHelper112").newInstance();
    }
    if (bukkitVersion >= 1130) {
        rpvHelper = (VersionHelper) Class.forName("br.net.fabiozumbi12.RedProtect.Bukkit.helpers.VersionHelper113").newInstance();
    }
    if (bukkitVersion >= 1140) {
        rpvHelper = (VersionHelper) Class.forName("br.net.fabiozumbi12.RedProtect.Bukkit.helpers.VersionHelperLatest").newInstance();
    }
    // Register as listener
    getServer().getPluginManager().registerEvents(rpvHelper, this);
    // Print helper version
    logger.info("Helper version: " + rpvHelper.getVersion());
    // Load gui items for Minecraft version
    config.setGuiRoot(rpvHelper.setGuiItems(config.guiRoot()));
    // Load mob permissions
    modListener = new ModListener(this);
    // Metrics
    try {
        Metrics metrics = new Metrics(this);
        metrics.addCustomChart(new Metrics.SingleLineChart("server_regions", () -> rm.getAllRegions().size()));
        if (metrics.isEnabled())
            logger.info("Metrics enabled! See our stats here: https://bstats.org/plugin/bukkit/RedProtect");
    } catch (Exception ex) {
        logger.info("Metrics not enabled due errors: " + ex.getLocalizedMessage());
    }
}
Also used : LangManager(br.net.fabiozumbi12.RedProtect.Bukkit.config.LangManager) CommandHandler(br.net.fabiozumbi12.RedProtect.Bukkit.commands.CommandHandler) SpigetUpdater(br.net.fabiozumbi12.RedProtect.Bukkit.updater.SpigetUpdater) ConfigManager(br.net.fabiozumbi12.RedProtect.Bukkit.config.ConfigManager) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) RedProtectUtil(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.RedProtectUtil) Metrics(br.net.fabiozumbi12.RedProtect.Bukkit.metrics.Metrics) LangGuiManager(br.net.fabiozumbi12.RedProtect.Bukkit.config.LangGuiManager) RegionManager(br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager) BlockConfig(br.net.fabiozumbi12.RedProtect.Bukkit.config.BlockConfig)

Example 2 with RegionManager

use of br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager in project RedProtect by FabioZumbi12.

the class RedProtect method onEnable.

public void onEnable() {
    try {
        plugin = this;
        ph = new PermissionHandler();
        rm = new RegionManager();
        // Init schematic
        schematic = new RPSchematics();
        // Init config, lang, listeners and flags
        startLoad();
        logger.info("Loading API...");
        this.redProtectAPI = new RedProtectAPI();
        logger.info("API Loaded!");
        logger.clear("&4 _   _  _  &c _   _   _  _ _  _  _ _ _  __");
        logger.clear("&4|_| |_ | \\ &c|_| |_| | |  |  |_ |   |    /");
        logger.clear("&4| \\ |_ |_/ &c|   | \\ |_|  |  |_ |_  |   /");
        logger.clear("&a» " + getDescription().getFullName() + " enabled");
        logger.clear("");
    } catch (Exception e) {
        CoreUtil.printJarVersion();
        e.printStackTrace();
        getServer().setWhitelist(true);
        getServer().getOnlinePlayers().forEach(p -> p.kickPlayer("The server has been whitelisted due to an error while loading plugins!"));
        this.setEnabled(false);
        logger.severe("Due to an error in RedProtect loading, the whitelist has been turned on and every player has been kicked.");
        logger.severe("DO NOT LET ANYONE ENTER before fixing the problem, otherwise you risk losing protected regions.");
    }
}
Also used : PermissionHandler(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.PermissionHandler) HandlerList(org.bukkit.event.HandlerList) java.util(java.util) Player(org.bukkit.entity.Player) Bukkit.listeners(br.net.fabiozumbi12.RedProtect.Bukkit.listeners) CoreUtil(br.net.fabiozumbi12.RedProtect.Core.helpers.CoreUtil) VersionHelper(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.VersionHelper) Location(org.bukkit.Location) Metrics(br.net.fabiozumbi12.RedProtect.Bukkit.metrics.Metrics) RegionManager(br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager) LangManager(br.net.fabiozumbi12.RedProtect.Bukkit.config.LangManager) LogLevel(br.net.fabiozumbi12.RedProtect.Core.helpers.LogLevel) RedProtectUtil(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.RedProtectUtil) LangGuiManager(br.net.fabiozumbi12.RedProtect.Bukkit.config.LangGuiManager) Bukkit(org.bukkit.Bukkit) Economy(net.milkbowl.vault.economy.Economy) SpigetUpdater(br.net.fabiozumbi12.RedProtect.Bukkit.updater.SpigetUpdater) RPSchematics(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.RPSchematics) CommandHandler(br.net.fabiozumbi12.RedProtect.Bukkit.commands.CommandHandler) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) HooksManager(br.net.fabiozumbi12.RedProtect.Bukkit.hooks.HooksManager) RedProtectLogger(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.RedProtectLogger) RedProtectAPI(br.net.fabiozumbi12.RedProtect.Bukkit.API.RedProtectAPI) ConfigManager(br.net.fabiozumbi12.RedProtect.Bukkit.config.ConfigManager) BlockConfig(br.net.fabiozumbi12.RedProtect.Bukkit.config.BlockConfig) Permission(net.milkbowl.vault.permission.Permission) RedProtectAPI(br.net.fabiozumbi12.RedProtect.Bukkit.API.RedProtectAPI) PermissionHandler(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.PermissionHandler) RPSchematics(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.RPSchematics) RegionManager(br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 3 with RegionManager

use of br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager in project RedProtect by FabioZumbi12.

the class WorldFlatFileRegionManager method save.

@Override
public int save(boolean force) {
    int saved = 0;
    try {
        RedProtect.get().logger.debug(LogLevel.DEFAULT, "RegionManager.Save(): File type is " + RedProtect.get().getConfigManager().configRoot().file_type);
        if (!RedProtect.get().getConfigManager().configRoot().file_type.equalsIgnoreCase("mysql")) {
            File datf = new File(RedProtect.get().configDir + File.separator + "data", "data_" + world + ".conf");
            ConfigurationLoader<CommentedConfigurationNode> regionManager = HoconConfigurationLoader.builder().setPath(datf.toPath()).build();
            CommentedConfigurationNode fileDB = regionManager.createEmptyNode();
            Set<CommentedConfigurationNode> dbs = new HashSet<>();
            for (Region r : regions.values()) {
                if (r.getName() == null) {
                    continue;
                }
                if (RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
                    if (!r.toSave() && !force) {
                        continue;
                    }
                    datf = new File(RedProtect.get().configDir + File.separator + "data", world + File.separator + r.getName() + ".conf");
                    regionManager = HoconConfigurationLoader.builder().setPath(datf.toPath()).build();
                    fileDB = regionManager.createEmptyNode();
                }
                RedProtect.get().getUtil().addProps(fileDB, r);
                saved++;
                if (RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
                    dbs.add(fileDB);
                    saveConf(fileDB, regionManager);
                    r.setToSave(false);
                }
            }
            if (!RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
                saveConf(fileDB, regionManager);
            } else {
                // remove deleted regions
                File wfolder = new File(RedProtect.get().configDir + File.separator + "data", world);
                if (wfolder.exists()) {
                    File[] listOfFiles = wfolder.listFiles();
                    if (listOfFiles != null) {
                        for (File region : listOfFiles) {
                            if (region.isFile() && !regions.containsKey(region.getName().replace(".conf", ""))) {
                                region.delete();
                            }
                        }
                    }
                }
            }
            if (force)
                RedProtect.get().logger.info("Saving " + this.world + "'s regions...");
        }
    } catch (Exception e4) {
        e4.printStackTrace();
    }
    return saved;
}
Also used : CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) File(java.io.File) IOException(java.io.IOException)

Example 4 with RegionManager

use of br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager in project RedProtect by FabioZumbi12.

the class RedProtectUtil method SingleToFiles.

public int SingleToFiles() {
    int saved = 0;
    for (World w : Sponge.getServer().getWorlds()) {
        Set<Region> regions = RedProtect.get().getRegionManager().getRegionsByWorld(w.getName());
        for (Region r : regions) {
            File wf = new File(RedProtect.get().configDir + File.separator + "data", w.getName() + File.separator + r.getName() + ".conf");
            ConfigurationLoader<CommentedConfigurationNode> regionManager = HoconConfigurationLoader.builder().setPath(wf.toPath()).build();
            CommentedConfigurationNode fileDB = regionManager.createEmptyNode();
            File f = new File(RedProtect.get().configDir + File.separator + "data", w.getName());
            if (!f.exists()) {
                f.mkdir();
            }
            saved++;
            addProps(fileDB, r);
            saveConf(fileDB, regionManager);
        }
        File oldf = new File(RedProtect.get().configDir + File.separator + "data", "data_" + w.getName() + ".conf");
        if (oldf.exists()) {
            oldf.delete();
        }
    }
    if (!RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
        RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file = true;
    }
    RedProtect.get().getConfigManager().save();
    return saved;
}
Also used : CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) World(org.spongepowered.api.world.World) File(java.io.File)

Example 5 with RegionManager

use of br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager in project RedProtect by FabioZumbi12.

the class RedProtectUtil method mysqlToFile.

public boolean mysqlToFile() {
    HashMap<String, Region> regions = new HashMap<>();
    int saved = 1;
    try {
        Connection dbcon = DriverManager.getConnection("jdbc:mysql://" + RedProtect.get().getConfigManager().configRoot().mysql.host + "/" + RedProtect.get().getConfigManager().configRoot().mysql.db_name + "?autoReconnect=true", RedProtect.get().getConfigManager().configRoot().mysql.user_name, RedProtect.get().getConfigManager().configRoot().mysql.user_pass);
        for (World world : Sponge.getServer().getWorlds()) {
            String tableName = RedProtect.get().getConfigManager().configRoot().mysql.table_prefix + world.getName();
            PreparedStatement st = dbcon.prepareStatement("SELECT * FROM `" + tableName + "` WHERE world=?");
            st.setString(1, world.getName());
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Set<PlayerRegion> leaders = new HashSet<>();
                Set<PlayerRegion> admins = new HashSet<>();
                Set<PlayerRegion> members = new HashSet<>();
                HashMap<String, Object> flags = new HashMap<>();
                int maxMbrX = rs.getInt("maxMbrX");
                int minMbrX = rs.getInt("minMbrX");
                int maxMbrZ = rs.getInt("maxMbrZ");
                int minMbrZ = rs.getInt("minMbrZ");
                int maxY = rs.getInt("maxY");
                int minY = rs.getInt("minY");
                int prior = rs.getInt("prior");
                String rname = rs.getString("name");
                String date = rs.getString("date");
                String wel = rs.getString("wel");
                long value = rs.getLong("value");
                boolean candelete = rs.getBoolean("candelete");
                boolean canPurge = rs.getBoolean("canpurge");
                Location<World> tppoint = null;
                if (rs.getString("tppoint") != null && !rs.getString("tppoint").equalsIgnoreCase("")) {
                    String[] tpstring = rs.getString("tppoint").split(",");
                    tppoint = new Location<>(world, Double.parseDouble(tpstring[0]), Double.parseDouble(tpstring[1]), Double.parseDouble(tpstring[2]));
                }
                for (String member : rs.getString("members").split(", ")) {
                    if (member.length() > 0) {
                        String[] p = member.split("@");
                        members.add(new PlayerRegion(p[0], p.length == 2 ? p[1] : p[0]));
                    }
                }
                for (String admin : rs.getString("admins").split(", ")) {
                    if (admin.length() > 0) {
                        String[] p = admin.split("@");
                        admins.add(new PlayerRegion(p[0], p.length == 2 ? p[1] : p[0]));
                    }
                }
                for (String leader : rs.getString("leaders").split(", ")) {
                    if (leader.length() > 0) {
                        String[] p = leader.split("@");
                        leaders.add(new PlayerRegion(p[0], p.length == 2 ? p[1] : p[0]));
                    }
                }
                for (String flag : rs.getString("flags").split(",")) {
                    String key = flag.split(":")[0];
                    String replace = key + ":";
                    if (replace.length() <= flag.length()) {
                        flags.put(key, parseObject(flag.substring(replace.length())));
                    }
                }
                Region newr = new Region(rname, admins, members, leaders, maxMbrX, minMbrX, maxMbrZ, minMbrZ, minY, maxY, flags, wel, prior, world.getName(), date, value, tppoint, candelete, canPurge);
                regions.put(rname, newr);
            }
            st.close();
            rs.close();
            File datf = new File(RedProtect.get().configDir + File.separator + "data", "data_" + world.getName() + ".conf");
            ConfigurationLoader<CommentedConfigurationNode> regionManager = HoconConfigurationLoader.builder().setPath(datf.toPath()).build();
            CommentedConfigurationNode fileDB = regionManager.createEmptyNode();
            Set<CommentedConfigurationNode> dbs = new HashSet<>();
            for (Region r : regions.values()) {
                if (r.getName() == null) {
                    continue;
                }
                if (RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
                    if (!r.toSave()) {
                        continue;
                    }
                    datf = new File(RedProtect.get().configDir + File.separator + "data", world.getName() + File.separator + r.getName() + ".conf");
                    regionManager = HoconConfigurationLoader.builder().setPath(datf.toPath()).build();
                    fileDB = regionManager.createEmptyNode();
                }
                addProps(fileDB, r);
                saved++;
                if (RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
                    dbs.add(fileDB);
                    saveConf(fileDB, regionManager);
                    r.setToSave(false);
                }
            }
            if (!RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
                saveConf(fileDB, regionManager);
            } else {
                // remove deleted regions
                File wfolder = new File(RedProtect.get().configDir + File.separator + "data", world.getName());
                if (wfolder.exists()) {
                    File[] listOfFiles = wfolder.listFiles();
                    if (listOfFiles != null) {
                        for (File region : listOfFiles) {
                            if (region.isFile() && !regions.containsKey(region.getName().replace(".conf", ""))) {
                                region.delete();
                            }
                        }
                    }
                }
            }
        // try backup
        /*if (!RedProtect.get().getConfigManager().configRoot().flat_file.region_per_file) {
                    backupRegions(Collections.singleton(fileDB), world.getName(), "data_" + world + ".conf");
                } else {
                    backupRegions(dbs, world.getName(), null);
                }*/
        }
        backupRegions();
        dbcon.close();
        if (saved > 0) {
            RedProtect.get().logger.success((saved - 1) + " regions converted to File with success!");
        }
    } catch (SQLException e) {
        printJarVersion();
        e.printStackTrace();
    }
    return true;
}
Also used : World(org.spongepowered.api.world.World) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) File(java.io.File)

Aggregations

PlayerRegion (br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion)6 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)5 File (java.io.File)5 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)5 SpigetUpdater (br.net.fabiozumbi12.RedProtect.Bukkit.updater.SpigetUpdater)3 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)3 CommandHandler (br.net.fabiozumbi12.RedProtect.Bukkit.commands.CommandHandler)2 BlockConfig (br.net.fabiozumbi12.RedProtect.Bukkit.config.BlockConfig)2 ConfigManager (br.net.fabiozumbi12.RedProtect.Bukkit.config.ConfigManager)2 LangGuiManager (br.net.fabiozumbi12.RedProtect.Bukkit.config.LangGuiManager)2 LangManager (br.net.fabiozumbi12.RedProtect.Bukkit.config.LangManager)2 RedProtectUtil (br.net.fabiozumbi12.RedProtect.Bukkit.helpers.RedProtectUtil)2 Metrics (br.net.fabiozumbi12.RedProtect.Bukkit.metrics.Metrics)2 RegionManager (br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionManager)2 CoreUtil (br.net.fabiozumbi12.RedProtect.Core.helpers.CoreUtil)2 RegionManager (br.net.fabiozumbi12.RedProtect.Sponge.region.RegionManager)2 IOException (java.io.IOException)2 java.util (java.util)2 World (org.spongepowered.api.world.World)2 RedProtectAPI (br.net.fabiozumbi12.RedProtect.Bukkit.API.RedProtectAPI)1