use of com.plotsquared.core.configuration.ConfigurationSection in project PlotSquared by IntellectualSites.
the class SQLManager method getClusters.
@Override
public HashMap<String, Set<PlotCluster>> getClusters() {
LinkedHashMap<String, Set<PlotCluster>> newClusters = new LinkedHashMap<>();
HashMap<Integer, PlotCluster> clusters = new HashMap<>();
try {
HashSet<String> areas = new HashSet<>();
if (this.worldConfiguration.contains("worlds")) {
ConfigurationSection worldSection = this.worldConfiguration.getConfigurationSection("worlds");
if (worldSection != null) {
for (String worldKey : worldSection.getKeys(false)) {
areas.add(worldKey);
ConfigurationSection areaSection = worldSection.getConfigurationSection(worldKey + ".areas");
if (areaSection != null) {
for (String areaKey : areaSection.getKeys(false)) {
String[] split = areaKey.split("(?<![;])-");
if (split.length == 3) {
areas.add(worldKey + ';' + split[0]);
}
}
}
}
}
}
HashMap<String, UUID> uuids = new HashMap<>();
HashMap<String, Integer> noExist = new HashMap<>();
/*
* Getting clusters
*/
try (Statement stmt = this.connection.createStatement()) {
ResultSet resultSet = stmt.executeQuery("SELECT * FROM `" + this.prefix + "cluster`");
PlotCluster cluster;
String owner;
UUID user;
int id;
while (resultSet.next()) {
PlotId pos1 = PlotId.of(resultSet.getInt("pos1_x"), resultSet.getInt("pos1_z"));
PlotId pos2 = PlotId.of(resultSet.getInt("pos2_x"), resultSet.getInt("pos2_z"));
id = resultSet.getInt("id");
String areaid = resultSet.getString("world");
if (!areas.contains(areaid)) {
noExist.merge(areaid, 1, Integer::sum);
}
owner = resultSet.getString("owner");
user = uuids.get(owner);
if (user == null) {
user = UUID.fromString(owner);
uuids.put(owner, user);
}
cluster = new PlotCluster(null, pos1, pos2, user, id);
clusters.put(id, cluster);
Set<PlotCluster> set = newClusters.computeIfAbsent(areaid, k -> new HashSet<>());
set.add(cluster);
}
// Getting helpers
resultSet = stmt.executeQuery("SELECT `user_uuid`, `cluster_id` FROM `" + this.prefix + "cluster_helpers`");
while (resultSet.next()) {
id = resultSet.getInt("cluster_id");
owner = resultSet.getString("user_uuid");
user = uuids.get(owner);
if (user == null) {
user = UUID.fromString(owner);
uuids.put(owner, user);
}
cluster = clusters.get(id);
if (cluster != null) {
cluster.helpers.add(user);
} else {
LOGGER.warn("Cluster #{}({}) in cluster_helpers does not exist." + " Please create the cluster or remove this entry", id, cluster);
}
}
// Getting invited
resultSet = stmt.executeQuery("SELECT `user_uuid`, `cluster_id` FROM `" + this.prefix + "cluster_invited`");
while (resultSet.next()) {
id = resultSet.getInt("cluster_id");
owner = resultSet.getString("user_uuid");
user = uuids.get(owner);
if (user == null) {
user = UUID.fromString(owner);
uuids.put(owner, user);
}
cluster = clusters.get(id);
if (cluster != null) {
cluster.invited.add(user);
} else {
LOGGER.warn("Cluster #{}({}) in cluster_helpers does not exist." + " Please create the cluster or remove this entry", id, cluster);
}
}
resultSet = stmt.executeQuery("SELECT * FROM `" + this.prefix + "cluster_settings`");
while (resultSet.next()) {
id = resultSet.getInt("cluster_id");
cluster = clusters.get(id);
if (cluster != null) {
String alias = resultSet.getString("alias");
if (alias != null) {
cluster.settings.setAlias(alias);
}
String pos = resultSet.getString("position");
switch(pos.toLowerCase()) {
case "":
case "default":
case "0,0,0":
case "center":
case "centre":
break;
default:
try {
BlockLoc loc = BlockLoc.fromString(pos);
cluster.settings.setPosition(loc);
} catch (Exception ignored) {
}
}
int m = resultSet.getInt("merged");
boolean[] merged = new boolean[4];
for (int i = 0; i < 4; i++) {
merged[3 - i] = (m & 1 << i) != 0;
}
cluster.settings.setMerged(merged);
} else {
LOGGER.warn("Cluster #{}({}) in cluster_helpers does not exist." + " Please create the cluster or remove this entry", id, cluster);
}
}
resultSet.close();
}
boolean invalidPlot = false;
for (Entry<String, Integer> entry : noExist.entrySet()) {
String a = entry.getKey();
invalidPlot = true;
LOGGER.warn("Warning! Found {} clusters in DB for non existent area; '{}'", noExist.get(a), a);
}
if (invalidPlot) {
LOGGER.warn("Warning! Please create the world(s) or remove the clusters using the purge command");
}
} catch (SQLException e) {
LOGGER.error("Failed to load clusters", e);
}
return newClusters;
}
use of com.plotsquared.core.configuration.ConfigurationSection in project PlotSquared by IntellectualSites.
the class SQLManager method getPlots.
/**
* Load all plots, helpers, denied, trusted, and every setting from DB into a {@link HashMap}.
*/
@Override
public HashMap<String, HashMap<PlotId, Plot>> getPlots() {
HashMap<String, HashMap<PlotId, Plot>> newPlots = new HashMap<>();
HashMap<Integer, Plot> plots = new HashMap<>();
try {
HashSet<String> areas = new HashSet<>();
if (this.worldConfiguration.contains("worlds")) {
ConfigurationSection worldSection = this.worldConfiguration.getConfigurationSection("worlds");
if (worldSection != null) {
for (String worldKey : worldSection.getKeys(false)) {
areas.add(worldKey);
ConfigurationSection areaSection = worldSection.getConfigurationSection(worldKey + ".areas");
if (areaSection != null) {
for (String areaKey : areaSection.getKeys(false)) {
String[] split = areaKey.split("(?<![;])-");
if (split.length == 3) {
areas.add(worldKey + ';' + split[0]);
}
}
}
}
}
}
HashMap<String, UUID> uuids = new HashMap<>();
HashMap<String, AtomicInteger> noExist = new HashMap<>();
/*
* Getting plots
*/
try (Statement statement = this.connection.createStatement()) {
int id;
String o;
UUID user;
try (ResultSet resultSet = statement.executeQuery("SELECT `id`, `plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp` FROM `" + this.prefix + "plot`")) {
ArrayList<Integer> toDelete = new ArrayList<>();
while (resultSet.next()) {
PlotId plot_id = PlotId.of(resultSet.getInt("plot_id_x"), resultSet.getInt("plot_id_z"));
id = resultSet.getInt("id");
String areaID = resultSet.getString("world");
if (!areas.contains(areaID)) {
if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id);
continue;
} else {
AtomicInteger value = noExist.get(areaID);
if (value != null) {
value.incrementAndGet();
} else {
noExist.put(areaID, new AtomicInteger(1));
}
}
}
o = resultSet.getString("owner");
user = uuids.get(o);
if (user == null) {
try {
user = UUID.fromString(o);
} catch (IllegalArgumentException e) {
if (Settings.UUID.FORCE_LOWERCASE) {
user = UUID.nameUUIDFromBytes(("OfflinePlayer:" + o.toLowerCase()).getBytes(Charsets.UTF_8));
} else {
user = UUID.nameUUIDFromBytes(("OfflinePlayer:" + o).getBytes(Charsets.UTF_8));
}
}
uuids.put(o, user);
}
long time;
try {
Timestamp timestamp = resultSet.getTimestamp("timestamp");
time = timestamp.getTime();
} catch (SQLException exception) {
String parsable = resultSet.getString("timestamp");
try {
time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(parsable).getTime();
} catch (ParseException e) {
LOGGER.error("Could not parse date for plot: #{}({};{}) ({})", id, areaID, plot_id, parsable);
time = System.currentTimeMillis() + id;
}
}
Plot p = new Plot(plot_id, user, new HashSet<>(), new HashSet<>(), new HashSet<>(), "", null, null, null, new boolean[] { false, false, false, false }, time, id);
HashMap<PlotId, Plot> map = newPlots.get(areaID);
if (map != null) {
Plot last = map.put(p.getId(), p);
if (last != null) {
if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(last.temp);
} else {
LOGGER.info("Plot #{}({}) in `{}plot` is a duplicate." + " Delete this plot or set `database-purger: true` in the settings.yml", id, last, this.prefix);
}
}
} else {
map = new HashMap<>();
newPlots.put(areaID, map);
map.put(p.getId(), p);
}
plots.put(id, p);
}
deleteRows(toDelete, this.prefix + "plot", "id");
}
if (Settings.Enabled_Components.RATING_CACHE) {
try (ResultSet r = statement.executeQuery("SELECT `plot_plot_id`, `player`, `rating` FROM `" + this.prefix + "plot_rating`")) {
ArrayList<Integer> toDelete = new ArrayList<>();
while (r.next()) {
id = r.getInt("plot_plot_id");
o = r.getString("player");
user = uuids.get(o);
if (user == null) {
user = UUID.fromString(o);
uuids.put(o, user);
}
Plot plot = plots.get(id);
if (plot != null) {
plot.getSettings().getRatings().put(user, r.getInt("rating"));
} else if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id);
} else {
LOGGER.warn("Entry #{}({}) in `plot_rating` does not exist." + " Create this plot or set `database-purger: true` in settings.yml", id, plot);
}
}
deleteRows(toDelete, this.prefix + "plot_rating", "plot_plot_id");
}
}
/*
* Getting helpers
*/
try (ResultSet r = statement.executeQuery("SELECT `user_uuid`, `plot_plot_id` FROM `" + this.prefix + "plot_helpers`")) {
ArrayList<Integer> toDelete = new ArrayList<>();
while (r.next()) {
id = r.getInt("plot_plot_id");
o = r.getString("user_uuid");
user = uuids.get(o);
if (user == null) {
user = UUID.fromString(o);
uuids.put(o, user);
}
Plot plot = plots.get(id);
if (plot != null) {
plot.getTrusted().add(user);
} else if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id);
} else {
LOGGER.warn("Entry #{}({}) in `plot_helpers` does not exist." + " Create this plot or set `database-purger: true` in settings.yml", id, plot);
}
}
deleteRows(toDelete, this.prefix + "plot_helpers", "plot_plot_id");
}
/*
* Getting trusted
*/
try (ResultSet r = statement.executeQuery("SELECT `user_uuid`, `plot_plot_id` FROM `" + this.prefix + "plot_trusted`")) {
ArrayList<Integer> toDelete = new ArrayList<>();
while (r.next()) {
id = r.getInt("plot_plot_id");
o = r.getString("user_uuid");
user = uuids.get(o);
if (user == null) {
user = UUID.fromString(o);
uuids.put(o, user);
}
Plot plot = plots.get(id);
if (plot != null) {
plot.getMembers().add(user);
} else if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id);
} else {
LOGGER.warn("Entry #{}({}) in `plot_trusted` does not exist." + " Create this plot or set `database-purger: true` in settings.yml", id, plot);
}
}
deleteRows(toDelete, this.prefix + "plot_trusted", "plot_plot_id");
}
/*
* Getting denied
*/
try (ResultSet r = statement.executeQuery("SELECT `user_uuid`, `plot_plot_id` FROM `" + this.prefix + "plot_denied`")) {
ArrayList<Integer> toDelete = new ArrayList<>();
while (r.next()) {
id = r.getInt("plot_plot_id");
o = r.getString("user_uuid");
user = uuids.get(o);
if (user == null) {
user = UUID.fromString(o);
uuids.put(o, user);
}
Plot plot = plots.get(id);
if (plot != null) {
plot.getDenied().add(user);
} else if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id);
} else {
LOGGER.warn("Entry #{}({}) in `plot_denied` does not exist." + " Create this plot or set `database-purger: true` in settings.yml", id, plot);
}
}
deleteRows(toDelete, this.prefix + "plot_denied", "plot_plot_id");
}
try (final ResultSet resultSet = statement.executeQuery("SELECT * FROM `" + this.prefix + "plot_flags`")) {
BlockTypeListFlag.skipCategoryVerification = // allow invalid tags, as initialized lazily
true;
final ArrayList<Integer> toDelete = new ArrayList<>();
final Map<Plot, Collection<PlotFlag<?, ?>>> invalidFlags = new HashMap<>();
while (resultSet.next()) {
id = resultSet.getInt("plot_id");
final String flag = resultSet.getString("flag");
String value = resultSet.getString("value");
final Plot plot = plots.get(id);
if (plot != null) {
final PlotFlag<?, ?> plotFlag = GlobalFlagContainer.getInstance().getFlagFromString(flag);
if (plotFlag == null) {
plot.getFlagContainer().addUnknownFlag(flag, value);
} else {
value = CaptionUtility.stripClickEvents(plotFlag, value);
try {
plot.getFlagContainer().addFlag(plotFlag.parse(value));
} catch (final FlagParseException e) {
e.printStackTrace();
LOGGER.error("Plot with ID {} has an invalid value:", id);
LOGGER.error("Failed to parse flag '{}', value '{}': {}", plotFlag.getName(), e.getValue(), e.getErrorMessage());
if (!invalidFlags.containsKey(plot)) {
invalidFlags.put(plot, new ArrayList<>());
}
invalidFlags.get(plot).add(plotFlag);
}
}
} else if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id);
} else {
LOGGER.warn("Entry #{}({}) in `plot_flags` does not exist." + " Create this plot or set `database-purger: true` in settings.yml", id, plot);
}
}
BlockTypeListFlag.skipCategoryVerification = // don't allow invalid tags anymore
false;
if (Settings.Enabled_Components.DATABASE_PURGER) {
for (final Map.Entry<Plot, Collection<PlotFlag<?, ?>>> plotFlagEntry : invalidFlags.entrySet()) {
for (final PlotFlag<?, ?> flag : plotFlagEntry.getValue()) {
LOGGER.info("Plot {} has an invalid flag ({}). A fix has been attempted", plotFlagEntry.getKey(), flag.getName());
removeFlag(plotFlagEntry.getKey(), flag);
}
}
}
deleteRows(toDelete, this.prefix + "plot_flags", "plot_id");
}
try (ResultSet resultSet = statement.executeQuery("SELECT * FROM `" + this.prefix + "plot_settings`")) {
ArrayList<Integer> toDelete = new ArrayList<>();
while (resultSet.next()) {
id = resultSet.getInt("plot_plot_id");
Plot plot = plots.get(id);
if (plot != null) {
plots.remove(id);
String alias = resultSet.getString("alias");
if (alias != null) {
plot.getSettings().setAlias(alias);
}
String pos = resultSet.getString("position");
switch(pos.toLowerCase()) {
case "":
case "default":
case "0,0,0":
case "center":
case "centre":
break;
default:
try {
plot.getSettings().setPosition(BlockLoc.fromString(pos));
} catch (Exception ignored) {
}
}
int m = resultSet.getInt("merged");
boolean[] merged = new boolean[4];
for (int i = 0; i < 4; i++) {
merged[3 - i] = (m & 1 << i) != 0;
}
plot.getSettings().setMerged(merged);
} else if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id);
} else {
LOGGER.warn("Entry #{}({}) in `plot_settings` does not exist." + " Create this plot or set `database-purger: true` in settings.yml", id, plot);
}
}
deleteRows(toDelete, this.prefix + "plot_settings", "plot_plot_id");
}
}
if (!plots.entrySet().isEmpty()) {
createEmptySettings(new ArrayList<>(plots.keySet()), null);
for (Entry<Integer, Plot> entry : plots.entrySet()) {
entry.getValue().getSettings();
}
}
boolean invalidPlot = false;
for (Entry<String, AtomicInteger> entry : noExist.entrySet()) {
String worldName = entry.getKey();
invalidPlot = true;
if (Settings.DEBUG) {
LOGGER.info("Warning! Found {} plots in DB for non existent world: '{}'", entry.getValue().intValue(), worldName);
}
}
if (invalidPlot && Settings.DEBUG) {
LOGGER.info("Warning! Please create the world(s) or remove the plots using the purge command");
}
} catch (SQLException e) {
LOGGER.error("Failed to load plots", e);
}
return newPlots;
}
use of com.plotsquared.core.configuration.ConfigurationSection in project PlotSquared by IntellectualSites.
the class Reload method onCommand.
@Override
public boolean onCommand(PlotPlayer<?> player, String[] args) {
try {
// The following won't affect world generation, as that has to be
// loaded during startup unfortunately.
PlotSquared.get().setupConfigs();
this.worldConfiguration = PlotSquared.get().getWorldConfiguration();
this.worldFile = PlotSquared.get().getWorldsFile();
PlotSquared.get().loadCaptionMap();
this.plotAreaManager.forEachPlotArea(area -> {
ConfigurationSection worldSection = this.worldConfiguration.getConfigurationSection("worlds." + area.getWorldName());
if (worldSection == null) {
return;
}
if (area.getType() != PlotAreaType.PARTIAL || !worldSection.contains("areas")) {
area.saveConfiguration(worldSection);
area.loadDefaultConfiguration(worldSection);
} else {
ConfigurationSection areaSection = worldSection.getConfigurationSection("areas." + area.getId() + "-" + area.getMin() + "-" + area.getMax());
YamlConfiguration clone = new YamlConfiguration();
for (String key : areaSection.getKeys(true)) {
if (areaSection.get(key) instanceof MemorySection) {
continue;
}
if (!clone.contains(key)) {
clone.set(key, areaSection.get(key));
}
}
for (String key : worldSection.getKeys(true)) {
if (worldSection.get(key) instanceof MemorySection) {
continue;
}
if (!key.startsWith("areas") && !clone.contains(key)) {
clone.set(key, worldSection.get(key));
}
}
area.saveConfiguration(clone);
// netSections is the combination of
for (String key : clone.getKeys(true)) {
if (clone.get(key) instanceof MemorySection) {
continue;
}
if (!worldSection.contains(key)) {
worldSection.set(key, clone.get(key));
} else {
Object value = worldSection.get(key);
if (Objects.equals(value, clone.get(key))) {
areaSection.set(key, clone.get(key));
}
}
}
area.loadDefaultConfiguration(clone);
}
});
this.worldConfiguration.save(this.worldFile);
player.sendMessage(TranslatableCaption.of("reload.reloaded_configs"));
} catch (Exception e) {
e.printStackTrace();
player.sendMessage(TranslatableCaption.of("reload.reload_failed"));
}
return true;
}
Aggregations