Search in sources :

Example 1 with BlockLoc

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;
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) LinkedHashMap(java.util.LinkedHashMap) PlotId(com.plotsquared.core.plot.PlotId) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) HashSet(java.util.HashSet) PlotCluster(com.plotsquared.core.plot.PlotCluster) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) BlockLoc(com.plotsquared.core.location.BlockLoc) ParseException(java.text.ParseException) FlagParseException(com.plotsquared.core.plot.flag.FlagParseException) SQLException(java.sql.SQLException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigurationSection(com.plotsquared.core.configuration.ConfigurationSection)

Example 2 with BlockLoc

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);
            }
        });
    }
}
Also used : ClassicPlotWorld(com.plotsquared.core.generator.ClassicPlotWorld) BlockLoc(com.plotsquared.core.location.BlockLoc) Location(com.plotsquared.core.location.Location)

Example 3 with BlockLoc

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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) BlockLoc(com.plotsquared.core.location.BlockLoc) ParseException(java.text.ParseException) FlagParseException(com.plotsquared.core.plot.flag.FlagParseException) SQLException(java.sql.SQLException)

Example 4 with BlockLoc

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();
}
Also used : SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockLoc(com.plotsquared.core.location.BlockLoc) Location(com.plotsquared.core.location.Location)

Example 5 with BlockLoc

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);
}
Also used : SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) ClassicPlotWorld(com.plotsquared.core.generator.ClassicPlotWorld) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockLoc(com.plotsquared.core.location.BlockLoc) Location(com.plotsquared.core.location.Location)

Aggregations

BlockLoc (com.plotsquared.core.location.BlockLoc)8 Location (com.plotsquared.core.location.Location)6 ClassicPlotWorld (com.plotsquared.core.generator.ClassicPlotWorld)3 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)3 HashSet (java.util.HashSet)3 UUID (java.util.UUID)3 PlotSquared (com.plotsquared.core.PlotSquared)2 DBFunc (com.plotsquared.core.database.DBFunc)2 PlotCluster (com.plotsquared.core.plot.PlotCluster)2 PlotId (com.plotsquared.core.plot.PlotId)2 FlagParseException (com.plotsquared.core.plot.flag.FlagParseException)2 SinglePlotArea (com.plotsquared.core.plot.world.SinglePlotArea)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2 Set (java.util.Set)2 ConfigurationSection (com.plotsquared.core.configuration.ConfigurationSection)1 Settings (com.plotsquared.core.configuration.Settings)1 Caption (com.plotsquared.core.configuration.caption.Caption)1 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)1