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());
}
}
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.");
}
}
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;
}
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;
}
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;
}
Aggregations