Search in sources :

Example 6 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.

the class SQLDriver method migrate.

/**
 * Attempt to migrate the tables to the latest version.
 *
 * @throws StorageException thrown if migration fails
 * @throws SQLException thrown on SQL error
 */
private void migrate() throws SQLException, StorageException {
    Closer closer = Closer.create();
    Connection conn = closer.register(getConnection());
    try {
        // Check some tables
        boolean tablesExist;
        boolean isRecent;
        boolean isBeforeMigrations;
        boolean hasMigrations;
        try {
            tablesExist = tryQuery(conn, "SELECT * FROM " + config.getTablePrefix() + "region_cuboid LIMIT 1");
            isRecent = tryQuery(conn, "SELECT world_id FROM " + config.getTablePrefix() + "region_cuboid LIMIT 1");
            isBeforeMigrations = !tryQuery(conn, "SELECT uuid FROM " + config.getTablePrefix() + "user LIMIT 1");
            hasMigrations = tryQuery(conn, "SELECT * FROM " + config.getTablePrefix() + "migrations LIMIT 1");
        } finally {
            closer.closeQuietly();
        }
        // We don't bother with migrating really old tables
        if (tablesExist && !isRecent) {
            throw new StorageException("Sorry, your tables are too old for the region SQL auto-migration system. " + "Please run region_manual_update_20110325.sql on your database, which comes " + "with WorldGuard or can be found in http://github.com/sk89q/worldguard");
        }
        // Our placeholders
        Map<String, String> placeHolders = new HashMap<>();
        placeHolders.put("tablePrefix", config.getTablePrefix());
        Flyway flyway = new Flyway();
        // checks and issue messages appropriately
        if (!hasMigrations) {
            flyway.setInitOnMigrate(true);
            if (tablesExist) {
                // Detect if this is before migrations
                if (isBeforeMigrations) {
                    flyway.setInitVersion(MigrationVersion.fromVersion("1"));
                }
                log.log(Level.INFO, "The SQL region tables exist but the migrations table seems to not exist yet. Creating the migrations table...");
            } else {
                // By default, if Flyway sees any tables at all in the schema, it
                // will assume that we are up to date, so we have to manually
                // check ourselves and then ask Flyway to start from the beginning
                // if our test table doesn't exist
                flyway.setInitVersion(MigrationVersion.fromVersion("0"));
                log.log(Level.INFO, "SQL region tables do not exist: creating...");
            }
        }
        flyway.setClassLoader(getClass().getClassLoader());
        flyway.setLocations("migrations/region/" + getMigrationFolderName());
        flyway.setDataSource(config.getDsn(), config.getUsername(), config.getPassword());
        flyway.setTable(config.getTablePrefix() + "migrations");
        flyway.setPlaceholders(placeHolders);
        flyway.setValidateOnMigrate(false);
        flyway.migrate();
    } catch (FlywayException e) {
        throw new StorageException("Failed to migrate tables", e);
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) Flyway(org.flywaydb.core.Flyway) FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) Connection(java.sql.Connection) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 7 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.

the class SQLDriver method getAll.

@Override
public List<RegionDatabase> getAll() throws StorageException {
    Closer closer = Closer.create();
    try {
        List<RegionDatabase> stores = new ArrayList<>();
        Connection connection = closer.register(getConnection());
        Statement stmt = connection.createStatement();
        ResultSet rs = closer.register(stmt.executeQuery("SELECT name FROM " + config.getTablePrefix() + "world"));
        while (rs.next()) {
            stores.add(get(rs.getString(1)));
        }
        return stores;
    } catch (SQLException e) {
        throw new StorageException("Failed to fetch list of worlds", e);
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) RegionDatabase(com.sk89q.worldguard.protection.managers.storage.RegionDatabase) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 8 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.

the class SQLRegionDatabase method saveAll.

@Override
public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
    checkNotNull(regions);
    initialize();
    Closer closer = Closer.create();
    DataUpdater updater;
    try {
        try {
            updater = new DataUpdater(this, closer.register(getConnection()));
        } catch (SQLException e) {
            throw new StorageException("Failed to get a connection to the database", e);
        }
        try {
            updater.saveAll(regions);
        } catch (SQLException e) {
            throw new StorageException("Failed to save the region data to the database", e);
        }
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) SQLException(java.sql.SQLException) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 9 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project WorldGuard by EngineHub.

the class SQLRegionDatabase method initialize.

/**
 * Initialize the database if it hasn't been yet initialized.
 *
 * @throws StorageException thrown if initialization fails
 */
private synchronized void initialize() throws StorageException {
    if (!initialized) {
        driver.initialize();
        try {
            worldId = chooseWorldId(worldName);
        } catch (SQLException e) {
            throw new StorageException("Failed to choose the ID for this world", e);
        }
        initialized = true;
    }
}
Also used : SQLException(java.sql.SQLException) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 10 with StorageException

use of com.sk89q.worldguard.protection.managers.storage.StorageException in project TARDIS by eccentricdevotion.

the class TARDISWorldGuardUtils method addRechargerProtection.

/**
 * Adds a WorldGuard protected region to recharger location. A 3x3 block area is protected.
 *
 * @param p    the player to assign as the owner of the region
 * @param name the name of the recharger
 * @param one  a start location of a cuboid region
 * @param two  an end location of a cuboid region
 */
public void addRechargerProtection(Player p, String name, Location one, Location two) {
    RegionManager rm = wg.getRegionContainer().get(new BukkitWorld(one.getWorld()));
    BlockVector3 b1;
    BlockVector3 b2;
    b1 = makeBlockVector(one);
    b2 = makeBlockVector(two);
    ProtectedCuboidRegion region = new ProtectedCuboidRegion("tardis_recharger_" + name, b1, b2);
    DefaultDomain dd = new DefaultDomain();
    dd.addPlayer(p.getUniqueId());
    region.setOwners(dd);
    HashMap<Flag<?>, Object> flags = new HashMap<>();
    flags.put(Flags.TNT, State.DENY);
    flags.put(Flags.CREEPER_EXPLOSION, State.DENY);
    flags.put(Flags.FIRE_SPREAD, State.DENY);
    flags.put(Flags.LAVA_FIRE, State.DENY);
    flags.put(Flags.LAVA_FLOW, State.DENY);
    flags.put(Flags.LIGHTER, State.DENY);
    region.setFlags(flags);
    rm.addRegion(region);
    try {
        rm.save();
    } catch (StorageException e) {
        plugin.getLogger().log(Level.INFO, "Could not create WorldGuard Protection for recharger! " + e.getMessage());
    }
}
Also used : RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) BlockVector3(com.sk89q.worldedit.math.BlockVector3) DefaultDomain(com.sk89q.worldguard.domains.DefaultDomain) StateFlag(com.sk89q.worldguard.protection.flags.StateFlag) Flag(com.sk89q.worldguard.protection.flags.Flag) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Aggregations

StorageException (com.sk89q.worldguard.protection.managers.storage.StorageException)31 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)19 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)16 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)15 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)9 DefaultDomain (com.sk89q.worldguard.domains.DefaultDomain)9 ProtectedCuboidRegion (com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion)9 Flag (com.sk89q.worldguard.protection.flags.Flag)6 StateFlag (com.sk89q.worldguard.protection.flags.StateFlag)6 Closer (com.sk89q.worldguard.util.io.Closer)5 SQLException (java.sql.SQLException)5 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)3 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)3 HashMap (java.util.HashMap)3 World (org.bukkit.World)3 YAMLNode (com.sk89q.util.yaml.YAMLNode)2 YAMLProcessor (com.sk89q.util.yaml.YAMLProcessor)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 ApplicableRegionSet (com.sk89q.worldguard.protection.ApplicableRegionSet)2 RegionDatabase (com.sk89q.worldguard.protection.managers.storage.RegionDatabase)2