use of com.plotsquared.core.plot.PlotId in project PlotSquared by IntellectualSites.
the class SearchPlotProvider method getPlotsBySearch.
/**
* Fuzzy plot search with spaces separating terms.
* - Terms: type, alias, world, owner, trusted, member
*
* @param search Search string
* @return Search results
*/
@NonNull
private static List<Plot> getPlotsBySearch(@NonNull final String search) {
String[] split = search.split(" ");
int size = split.length * 2;
List<UUID> uuids = new ArrayList<>();
PlotId id = null;
for (String term : split) {
try {
UUID uuid = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(term, Settings.UUID.BLOCKING_TIMEOUT);
if (uuid == null) {
uuid = UUID.fromString(term);
}
uuids.add(uuid);
} catch (Exception ignored) {
id = PlotId.fromString(term);
}
}
ArrayList<ArrayList<Plot>> plotList = IntStream.range(0, size).mapToObj(i -> new ArrayList<Plot>()).collect(Collectors.toCollection(() -> new ArrayList<>(size)));
PlotArea area = null;
String alias = null;
for (Plot plot : PlotQuery.newQuery().allPlots()) {
int count = 0;
if (!uuids.isEmpty()) {
for (UUID uuid : uuids) {
if (plot.isOwner(uuid)) {
count += 2;
} else if (plot.isAdded(uuid)) {
count++;
}
}
}
if (id != null) {
if (plot.getId().equals(id)) {
count++;
}
}
if (area != null && plot.getArea().equals(area)) {
count++;
}
if (alias != null && alias.equals(plot.getAlias())) {
count += 2;
}
if (count != 0) {
plotList.get(count - 1).add(plot);
}
}
List<Plot> plots = new ArrayList<>();
for (int i = plotList.size() - 1; i >= 0; i--) {
if (!plotList.get(i).isEmpty()) {
plots.addAll(plotList.get(i));
}
}
return plots;
}
use of com.plotsquared.core.plot.PlotId in project PlotSquared by IntellectualSites.
the class Save method onCommand.
@Override
public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
final String world = player.getLocation().getWorldName();
if (!this.plotAreaManager.hasPlotArea(world)) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
return false;
}
final Plot plot = player.getCurrentPlot();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
}
if (!plot.hasOwner()) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return false;
}
if (plot.getVolume() > Integer.MAX_VALUE) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
return false;
}
if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_SAVE)) {
player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
return false;
}
if (plot.getRunning() > 0) {
player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
return false;
}
plot.addRunning();
this.schematicHandler.getCompoundTag(plot).whenComplete((compoundTag, throwable) -> {
TaskManager.runTaskAsync(() -> {
String time = (System.currentTimeMillis() / 1000) + "";
Location[] corners = plot.getCorners();
corners[0] = corners[0].withY(plot.getArea().getMinBuildHeight());
corners[1] = corners[1].withY(plot.getArea().getMaxBuildHeight());
int size = (corners[1].getX() - corners[0].getX()) + 1;
PlotId id = plot.getId();
String world1 = plot.getArea().toString().replaceAll(";", "-").replaceAll("[^A-Za-z0-9]", "");
final String file = time + '_' + world1 + '_' + id.getX() + '_' + id.getY() + '_' + size;
UUID uuid = player.getUUID();
schematicHandler.upload(compoundTag, uuid, file, new RunnableVal<>() {
@Override
public void run(URL url) {
plot.removeRunning();
if (url == null) {
player.sendMessage(TranslatableCaption.of("backups.backup_save_failed"));
return;
}
player.sendMessage(TranslatableCaption.of("web.save_success"));
player.sendMessage(TranslatableCaption.of("errors.deprecated_commands"), Template.of("replacement", "/plot download"));
try (final MetaDataAccess<List<String>> schematicAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SCHEMATICS)) {
schematicAccess.get().ifPresent(schematics -> schematics.add(file + ".schem"));
}
}
});
});
});
return true;
}
use of com.plotsquared.core.plot.PlotId in project PlotSquared by IntellectualSites.
the class SQLManager method createPlotsAndData.
@Override
public void createPlotsAndData(final List<Plot> myList, final Runnable whenDone) {
addGlobalTask(() -> {
try {
// Create the plots
createPlots(myList, () -> {
final Map<PlotId, Integer> idMap = new HashMap<>();
try {
// Creating datastructures
HashMap<PlotId, Plot> plotMap = new HashMap<>();
for (Plot plot : myList) {
plotMap.put(plot.getId(), plot);
}
ArrayList<LegacySettings> settings = new ArrayList<>();
final ArrayList<UUIDPair> helpers = new ArrayList<>();
final ArrayList<UUIDPair> trusted = new ArrayList<>();
final ArrayList<UUIDPair> denied = new ArrayList<>();
// Populating structures
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement(SQLManager.this.GET_ALL_PLOTS);
ResultSet result = stmt.executeQuery()) {
while (result.next()) {
int id = result.getInt("id");
int x = result.getInt("plot_id_x");
int y = result.getInt("plot_id_z");
PlotId plotId = PlotId.of(x, y);
Plot plot = plotMap.get(plotId);
idMap.put(plotId, id);
if (plot != null) {
settings.add(new LegacySettings(id, plot.getSettings()));
for (UUID uuid : plot.getDenied()) {
denied.add(new UUIDPair(id, uuid));
}
for (UUID uuid : plot.getMembers()) {
trusted.add(new UUIDPair(id, uuid));
}
for (UUID uuid : plot.getTrusted()) {
helpers.add(new UUIDPair(id, uuid));
}
}
}
}
createFlags(idMap, myList, () -> createSettings(settings, () -> createTiers(helpers, "helpers", () -> createTiers(trusted, "trusted", () -> createTiers(denied, "denied", () -> {
try {
SQLManager.this.connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
if (whenDone != null) {
whenDone.run();
}
})))));
} catch (SQLException e) {
LOGGER.warn("Failed to set all flags and member tiers for plots", e);
try {
SQLManager.this.connection.commit();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
} catch (Exception e) {
LOGGER.warn("Warning! Failed to set all helper for plots", e);
try {
SQLManager.this.connection.commit();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
}
use of com.plotsquared.core.plot.PlotId 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.plot.PlotId in project PlotSquared by IntellectualSites.
the class SQLManager method swapPlots.
@Override
public CompletableFuture<Boolean> swapPlots(Plot plot1, Plot plot2) {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
TaskManager.runTaskAsync(() -> {
final int id1 = getId(plot1);
final int id2 = getId(plot2);
final PlotId pos1 = plot1.getId();
final PlotId pos2 = plot2.getId();
try (final PreparedStatement preparedStatement = this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot` SET `plot_id_x` = ?, `plot_id_z` = ? WHERE `id` = ?")) {
preparedStatement.setInt(1, pos1.getX());
preparedStatement.setInt(2, pos1.getY());
preparedStatement.setInt(3, id1);
preparedStatement.execute();
preparedStatement.setInt(1, pos2.getX());
preparedStatement.setInt(2, pos2.getY());
preparedStatement.setInt(3, id2);
preparedStatement.execute();
} catch (final Exception e) {
LOGGER.error("Failed to persist wap of {} and {}", plot1, plot2);
e.printStackTrace();
future.complete(false);
return;
}
future.complete(true);
});
return future;
}
Aggregations