use of com.plotsquared.core.location.BlockLoc 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.location.BlockLoc in project PlotSquared by IntellectualSites.
the class Plot method getHome.
/**
* Return the home location for the plot
*
* @param result consumer to pass location to when found
*/
public void getHome(final Consumer<Location> result) {
BlockLoc home = this.getPosition();
if (home == null || home.getX() == 0 && home.getZ() == 0) {
this.getDefaultHome(result);
} else {
if (!isLoaded()) {
result.accept(Location.at("", 0, this.getArea() instanceof ClassicPlotWorld ? ((ClassicPlotWorld) this.getArea()).PLOT_HEIGHT + 1 : 4, 0));
return;
}
Location bottom = this.getBottomAbs();
Location location = toHomeLocation(bottom, home);
this.worldUtil.getBlock(location, block -> {
if (!block.getBlockType().getMaterial().isAir()) {
this.worldUtil.getHighestBlock(this.getWorldName(), location.getX(), location.getZ(), y -> result.accept(location.withY(Math.max(1 + y, bottom.getY()))));
} else {
result.accept(location);
}
});
}
}
use of com.plotsquared.core.location.BlockLoc in project PlotSquared by IntellectualSites.
the class SQLManager method createSettings.
public void createSettings(final ArrayList<LegacySettings> myList, final Runnable whenDone) {
try (final PreparedStatement preparedStatement = this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_settings`" + "(`plot_plot_id`,`biome`,`rain`,`custom_time`,`time`,`deny_entry`,`alias`,`merged`,`position`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
int packet;
if (this.mySQL) {
packet = Math.min(myList.size(), 5000);
} else {
packet = Math.min(myList.size(), 50);
}
int totalUpdated = 0;
int updated = 0;
for (final LegacySettings legacySettings : myList) {
preparedStatement.setInt(1, legacySettings.id);
preparedStatement.setNull(2, 4);
preparedStatement.setNull(3, 4);
preparedStatement.setNull(4, 4);
preparedStatement.setNull(5, 4);
preparedStatement.setNull(6, 4);
if (legacySettings.settings.getAlias().isEmpty()) {
preparedStatement.setNull(7, 4);
} else {
preparedStatement.setString(7, legacySettings.settings.getAlias());
}
boolean[] merged = legacySettings.settings.getMerged();
int hash = HashUtil.hash(merged);
preparedStatement.setInt(8, hash);
BlockLoc loc = legacySettings.settings.getPosition();
String position;
if (loc.getY() == 0) {
position = "DEFAULT";
} else {
position = loc.getX() + "," + loc.getY() + ',' + loc.getZ();
}
preparedStatement.setString(9, position);
preparedStatement.addBatch();
if (++updated >= packet) {
try {
preparedStatement.executeBatch();
} catch (final Exception e) {
LOGGER.error("Failed to store settings for plot with entry ID: {}", legacySettings.id);
e.printStackTrace();
continue;
}
}
totalUpdated += 1;
}
if (totalUpdated < myList.size()) {
try {
preparedStatement.executeBatch();
} catch (final Exception e) {
LOGGER.error("Failed to store settings", e);
}
}
} catch (final Exception e) {
LOGGER.error("Failed to store settings", e);
}
LOGGER.info("Finished converting settings ({} plots processed)", myList.size());
whenDone.run();
}
use of com.plotsquared.core.location.BlockLoc in project PlotSquared by IntellectualSites.
the class Plot method getDefaultHomeSynchronous.
/**
* @param member if to get the home for plot members
* @return location of home for members or visitors
* @deprecated May cause synchronous chunk loads
*/
@Deprecated
public Location getDefaultHomeSynchronous(final boolean member) {
Plot plot = this.getBasePlot(false);
BlockLoc loc = member ? area.defaultHome() : area.nonmemberHome();
if (loc != null) {
int x;
int z;
if (loc.getX() == Integer.MAX_VALUE && loc.getZ() == Integer.MAX_VALUE) {
// center
if (getArea() instanceof SinglePlotArea) {
int y = loc.getY() == Integer.MIN_VALUE ? (isLoaded() ? this.worldUtil.getHighestBlockSynchronous(plot.getWorldName(), 0, 0) + 1 : 63) : loc.getY();
return Location.at(plot.getWorldName(), 0, y, 0, 0, 0);
}
CuboidRegion largest = plot.getLargestRegion();
x = (largest.getMaximumPoint().getX() >> 1) - (largest.getMinimumPoint().getX() >> 1) + largest.getMinimumPoint().getX();
z = (largest.getMaximumPoint().getZ() >> 1) - (largest.getMinimumPoint().getZ() >> 1) + largest.getMinimumPoint().getZ();
} else {
// specific
Location bot = plot.getBottomAbs();
x = bot.getX() + loc.getX();
z = bot.getZ() + loc.getZ();
}
int y = loc.getY() == Integer.MIN_VALUE ? (isLoaded() ? this.worldUtil.getHighestBlockSynchronous(plot.getWorldName(), x, z) + 1 : 63) : loc.getY();
return Location.at(plot.getWorldName(), x, y, z, loc.getYaw(), loc.getPitch());
}
if (getArea() instanceof SinglePlotArea) {
int y = isLoaded() ? this.worldUtil.getHighestBlockSynchronous(plot.getWorldName(), 0, 0) + 1 : 63;
return Location.at(plot.getWorldName(), 0, y, 0, 0, 0);
}
// Side
return plot.getSideSynchronous();
}
use of com.plotsquared.core.location.BlockLoc in project PlotSquared by IntellectualSites.
the class Plot method getDefaultHome.
public void getDefaultHome(boolean member, Consumer<Location> result) {
Plot plot = this.getBasePlot(false);
if (!isLoaded()) {
result.accept(Location.at("", 0, this.getArea() instanceof ClassicPlotWorld ? ((ClassicPlotWorld) this.getArea()).PLOT_HEIGHT + 1 : 4, 0));
return;
}
BlockLoc loc = member ? area.defaultHome() : area.nonmemberHome();
if (loc != null) {
int x;
int z;
if (loc.getX() == Integer.MAX_VALUE && loc.getZ() == Integer.MAX_VALUE) {
// center
if (getArea() instanceof SinglePlotArea) {
x = 0;
z = 0;
} else {
CuboidRegion largest = plot.getLargestRegion();
x = (largest.getMaximumPoint().getX() >> 1) - (largest.getMinimumPoint().getX() >> 1) + largest.getMinimumPoint().getX();
z = (largest.getMaximumPoint().getZ() >> 1) - (largest.getMinimumPoint().getZ() >> 1) + largest.getMinimumPoint().getZ();
}
} else {
// specific
Location bot = plot.getBottomAbs();
x = bot.getX() + loc.getX();
z = bot.getZ() + loc.getZ();
}
if (loc.getY() == Integer.MIN_VALUE) {
if (isLoaded()) {
this.worldUtil.getHighestBlock(plot.getWorldName(), x, z, y -> result.accept(Location.at(plot.getWorldName(), x, y + 1, z)));
} else {
int y = this.getArea() instanceof ClassicPlotWorld ? ((ClassicPlotWorld) this.getArea()).PLOT_HEIGHT + 1 : 63;
result.accept(Location.at(plot.getWorldName(), x, y, z, loc.getYaw(), loc.getPitch()));
}
} else {
result.accept(Location.at(plot.getWorldName(), x, loc.getY(), z, loc.getYaw(), loc.getPitch()));
}
return;
}
// Side
if (getArea() instanceof SinglePlotArea) {
int y = isLoaded() ? this.worldUtil.getHighestBlockSynchronous(plot.getWorldName(), 0, 0) + 1 : 63;
result.accept(Location.at(plot.getWorldName(), 0, y, 0, 0, 0));
}
plot.getSide(result);
}
Aggregations