2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-10-22 08:08:55 +02:00
package com.intellectualcrafters.plot.database ;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.PlotMain ;
import com.intellectualcrafters.plot.flag.Flag ;
import com.intellectualcrafters.plot.flag.FlagManager ;
import com.intellectualcrafters.plot.object.Plot ;
import com.intellectualcrafters.plot.object.PlotComment ;
import com.intellectualcrafters.plot.object.PlotHomePosition ;
import com.intellectualcrafters.plot.object.PlotId ;
import com.intellectualcrafters.plot.util.Logger ;
import com.intellectualcrafters.plot.util.Logger.LogLevel ;
2014-12-16 05:57:40 +01:00
import com.intellectualcrafters.plot.util.UUIDHandler ;
2014-12-18 03:15:11 +01:00
import org.apache.commons.lang.StringUtils ;
import org.bukkit.Bukkit ;
import org.bukkit.OfflinePlayer ;
import org.bukkit.block.Biome ;
import java.sql.* ;
import java.util.* ;
2014-12-13 12:59:43 +01:00
2014-10-22 08:08:55 +02:00
/ * *
* @author Citymonstret
* /
2014-11-19 18:18:18 +01:00
public class SQLManager implements AbstractDB {
2014-10-22 08:08:55 +02:00
2014-11-16 09:43:30 +01:00
// Public final
2014-12-18 03:15:11 +01:00
public final String SET_OWNER ;
public final String GET_ALL_PLOTS ;
public final String CREATE_PLOTS ;
public final String CREATE_SETTINGS ;
public final String CREATE_HELPERS ;
public final String CREATE_PLOT ;
2014-11-16 09:43:30 +01:00
// Private Final
2014-12-18 03:15:11 +01:00
private Connection connection ;
2014-11-16 09:43:30 +01:00
private final String prefix ;
/ * *
* Constructor
*
2014-12-18 03:15:11 +01:00
* @param c connection
* @param p prefix
2014-11-16 09:43:30 +01:00
* /
2014-12-16 06:03:20 +01:00
public SQLManager ( final Connection c , final String p ) {
2014-11-16 09:43:30 +01:00
// Private final
2014-12-16 06:03:20 +01:00
this . connection = c ;
this . prefix = p ;
2014-12-14 02:12:21 +01:00
// Set timout
2014-12-16 06:03:20 +01:00
// setTimout();
2014-11-16 09:43:30 +01:00
// Public final
2014-12-16 06:03:20 +01:00
this . SET_OWNER = " UPDATE ` " + this . prefix + " plot` SET `owner` = ? WHERE `plot_id_x` = ? AND `plot_id_z` = ? " ;
this . GET_ALL_PLOTS = " SELECT `id`, `plot_id_x`, `plot_id_z`, `world` FROM ` " + this . prefix + " plot` " ;
this . CREATE_PLOTS = " INSERT INTO ` " + this . prefix + " plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) values " ;
this . CREATE_SETTINGS = " INSERT INTO ` " + this . prefix + " plot_settings` (`plot_plot_id`) values " ;
this . CREATE_HELPERS = " INSERT INTO ` " + this . prefix + " plot_helpers` (`plot_plot_id`, `user_uuid`) values " ;
this . CREATE_PLOT = " INSERT INTO ` " + this . prefix + " plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) VALUES(?, ?, ?, ?) " ;
// schedule reconnect
2014-12-15 02:04:13 +01:00
if ( PlotMain . getMySQL ( ) ! = null ) {
2014-12-16 06:03:20 +01:00
Bukkit . getScheduler ( ) . scheduleSyncRepeatingTask ( PlotMain . getMain ( ) , new Runnable ( ) {
@Override
public void run ( ) {
2014-12-15 02:04:13 +01:00
try {
2014-12-16 06:03:20 +01:00
SQLManager . this . connection = PlotMain . getMySQL ( ) . forceConnection ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final Exception e ) {
2014-12-15 02:04:13 +01:00
e . printStackTrace ( ) ;
}
2014-12-14 02:12:21 +01:00
}
2014-12-15 02:04:13 +01:00
} , 11000 , 11000 ) ;
}
2014-12-16 06:03:20 +01:00
2014-12-14 02:12:21 +01:00
}
2014-12-16 06:03:20 +01:00
//
// public void setTimout() {
// runTask(new Runnable() {
// @Override
// public void run() {
// try {
// final PreparedStatement statement =
// connection.prepareStatement("SET GLOBAL wait_timeout =28800;");
// statement.executeQuery();
// statement.close();
// } catch (final SQLException e) {
// e.printStackTrace();
// Logger.add(LogLevel.DANGER, "Could not reset MySQL timout.");
// }
// }
// });
// }
2014-11-05 04:42:08 +01:00
/ * *
* Set Plot owner
*
2014-12-18 03:15:11 +01:00
* @param plot Plot Object
* @param uuid Owner UUID
2014-11-05 04:42:08 +01:00
* /
@Override
public void setOwner ( final Plot plot , final UUID uuid ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( SQLManager . this . SET_OWNER ) ;
2014-11-05 04:42:08 +01:00
statement . setString ( 1 , uuid . toString ( ) ) ;
statement . setInt ( 2 , plot . id . x ) ;
statement . setInt ( 3 , plot . id . y ) ;
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . DANGER , " Could not set owner for plot " + plot . id ) ;
}
}
} ) ;
}
@Override
public void createAllSettingsAndHelpers ( final ArrayList < Plot > plots ) {
// TODO SEVERE [ More than 5000 plots will fail in a single SQLite
// query.
2014-11-15 12:53:26 +01:00
final HashMap < String , HashMap < PlotId , Integer > > stored = new HashMap < > ( ) ;
final HashMap < Integer , ArrayList < UUID > > helpers = new HashMap < > ( ) ;
2014-11-05 04:42:08 +01:00
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement stmt = this . connection . prepareStatement ( this . GET_ALL_PLOTS ) ;
2014-11-05 04:42:08 +01:00
final ResultSet result = stmt . executeQuery ( ) ;
while ( result . next ( ) ) {
final int id = result . getInt ( " id " ) ;
final int idx = result . getInt ( " plot_id_x " ) ;
final int idz = result . getInt ( " plot_id_z " ) ;
final String world = result . getString ( " world " ) ;
if ( ! stored . containsKey ( world ) ) {
stored . put ( world , new HashMap < PlotId , Integer > ( ) ) ;
}
stored . get ( world ) . put ( new PlotId ( idx , idz ) , id ) ;
}
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
}
for ( final Plot plot : plots ) {
final String world = Bukkit . getWorld ( plot . world ) . getName ( ) ;
if ( stored . containsKey ( world ) ) {
final Integer id = stored . get ( world ) . get ( plot . id ) ;
if ( id ! = null ) {
helpers . put ( id , plot . helpers ) ;
}
}
}
if ( helpers . size ( ) = = 0 ) {
return ;
}
// add plot settings
2014-11-16 09:43:30 +01:00
final Integer [ ] ids = helpers . keySet ( ) . toArray ( new Integer [ helpers . keySet ( ) . size ( ) ] ) ;
2014-12-16 06:03:20 +01:00
StringBuilder statement = new StringBuilder ( this . CREATE_SETTINGS ) ;
2014-11-05 04:42:08 +01:00
for ( int i = 0 ; i < ( ids . length - 1 ) ; i + + ) {
statement . append ( " (?), " ) ;
}
statement . append ( " (?) " ) ;
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = this . connection . prepareStatement ( statement . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
for ( int i = 0 ; i < ids . length ; i + + ) {
stmt . setInt ( i + 1 , ids [ i ] ) ;
}
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
}
// add plot helpers
String prefix = " " ;
2014-12-16 06:03:20 +01:00
statement = new StringBuilder ( this . CREATE_HELPERS ) ;
2014-11-05 04:42:08 +01:00
for ( final Integer id : helpers . keySet ( ) ) {
for ( final UUID helper : helpers . get ( id ) ) {
statement . append ( prefix + " (?, ?) " ) ;
prefix = " , " ;
}
}
if ( prefix . equals ( " " ) ) {
return ;
}
try {
2014-12-16 06:03:20 +01:00
stmt = this . connection . prepareStatement ( statement . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
int counter = 0 ;
for ( final Integer id : helpers . keySet ( ) ) {
for ( final UUID helper : helpers . get ( id ) ) {
stmt . setInt ( ( counter * 2 ) + 1 , id ) ;
stmt . setString ( ( counter * 2 ) + 2 , helper . toString ( ) ) ;
counter + + ;
}
}
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to set helper for plots " ) ;
e . printStackTrace ( ) ;
}
}
/ * *
* Create a plot
*
* @param plots
* /
@Override
public void createPlots ( final ArrayList < Plot > plots ) {
// TODO SEVERE [ More than 5000 plots will fail in a single SQLite
// query.
if ( plots . size ( ) = = 0 ) {
return ;
}
2014-12-16 06:03:20 +01:00
final StringBuilder statement = new StringBuilder ( this . CREATE_PLOTS ) ;
2014-11-05 04:42:08 +01:00
for ( int i = 0 ; i < ( plots . size ( ) - 1 ) ; i + + ) {
statement . append ( " (?,?,?,?), " ) ;
}
statement . append ( " (?,?,?,?) " ) ;
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = this . connection . prepareStatement ( statement . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
for ( int i = 0 ; i < plots . size ( ) ; i + + ) {
final Plot plot = plots . get ( i ) ;
stmt . setInt ( ( i * 4 ) + 1 , plot . id . x ) ;
stmt . setInt ( ( i * 4 ) + 2 , plot . id . y ) ;
stmt . setString ( ( i * 4 ) + 3 , plot . owner . toString ( ) ) ;
stmt . setString ( ( i * 4 ) + 4 , plot . world ) ;
}
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . DANGER , " Failed to save plots! " ) ;
}
}
/ * *
* Create a plot
*
* @param plot
* /
@Override
public void createPlot ( final Plot plot ) {
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = this . connection . prepareStatement ( this . CREATE_PLOT ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , plot . id . x ) ;
stmt . setInt ( 2 , plot . id . y ) ;
stmt . setString ( 3 , plot . owner . toString ( ) ) ;
stmt . setString ( 4 , plot . world ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . DANGER , " Failed to save plot " + plot . id ) ;
}
}
/ * *
* Create tables
*
* @throws SQLException
* /
@Override
public void createTables ( final String database , final boolean add_constraint ) throws SQLException {
2014-12-16 06:03:20 +01:00
2014-11-05 04:42:08 +01:00
final boolean mysql = database . equals ( " mysql " ) ;
2014-12-16 06:03:20 +01:00
final Statement stmt = this . connection . createStatement ( ) ;
2014-11-05 04:42:08 +01:00
if ( mysql ) {
2014-12-16 06:03:20 +01:00
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot` ( " + " `id` INT(11) NOT NULL AUTO_INCREMENT, " + " `plot_id_x` INT(11) NOT NULL, " + " `plot_id_z` INT(11) NOT NULL, " + " `owner` VARCHAR(45) NOT NULL, " + " `world` VARCHAR(45) NOT NULL, " + " `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, " + " PRIMARY KEY (`id`) " + " ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_denied` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `user_uuid` VARCHAR(40) NOT NULL " + " ) ENGINE=InnoDB DEFAULT CHARSET=utf8 " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_helpers` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `user_uuid` VARCHAR(40) NOT NULL " + " ) ENGINE=InnoDB DEFAULT CHARSET=utf8 " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_comments` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `comment` VARCHAR(40) NOT NULL, " + " `tier` INT(11) NOT NULL, " + " `sender` VARCHAR(40) NOT NULL " + " ) ENGINE=InnoDB DEFAULT CHARSET=utf8 " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_trusted` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `user_uuid` VARCHAR(40) NOT NULL " + " ) ENGINE=InnoDB DEFAULT CHARSET=utf8 " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_settings` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `biome` VARCHAR(45) DEFAULT 'FOREST', " + " `rain` INT(1) DEFAULT 0, " + " `custom_time` TINYINT(1) DEFAULT '0', " + " `time` INT(11) DEFAULT '8000', " + " `deny_entry` TINYINT(1) DEFAULT '0', " + " `alias` VARCHAR(50) DEFAULT NULL, " + " `flags` VARCHAR(512) DEFAULT NULL, " + " `merged` INT(11) DEFAULT NULL, " + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT', " + " PRIMARY KEY (`plot_plot_id`), " + " UNIQUE KEY `unique_alias` (`alias`) " + " ) ENGINE=InnoDB DEFAULT CHARSET=utf8 " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_ratings` ( `plot_plot_id` INT(11) NOT NULL, `rating` INT(2) NOT NULL, `player` VARCHAR(40) NOT NULL, PRIMARY KEY(`plot_plot_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 " ) ;
2014-11-05 04:42:08 +01:00
if ( add_constraint ) {
2014-12-16 06:03:20 +01:00
stmt . addBatch ( " ALTER TABLE ` " + this . prefix + " plot_settings` ADD CONSTRAINT ` " + this . prefix + " plot_settings_ibfk_1` FOREIGN KEY (`plot_plot_id`) REFERENCES ` " + this . prefix + " plot` (`id`) ON DELETE CASCADE " ) ;
2014-11-05 04:42:08 +01:00
}
2014-12-18 03:15:11 +01:00
} else {
2014-12-16 06:03:20 +01:00
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot` ( " + " `id` INTEGER PRIMARY KEY AUTOINCREMENT, " + " `plot_id_x` INT(11) NOT NULL, " + " `plot_id_z` INT(11) NOT NULL, " + " `owner` VARCHAR(45) NOT NULL, " + " `world` VARCHAR(45) NOT NULL, " + " `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP) " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_denied` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `user_uuid` VARCHAR(40) NOT NULL " + " ) " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_helpers` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `user_uuid` VARCHAR(40) NOT NULL " + " ) " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_trusted` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `user_uuid` VARCHAR(40) NOT NULL " + " ) " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_comments` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `comment` VARCHAR(40) NOT NULL, " + " `tier` INT(11) NOT NULL, " + " `sender` VARCHAR(40) NOT NULL " + " ) " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_settings` ( " + " `plot_plot_id` INT(11) NOT NULL, " + " `biome` VARCHAR(45) DEFAULT 'FOREST', " + " `rain` INT(1) DEFAULT 0, " + " `custom_time` TINYINT(1) DEFAULT '0', " + " `time` INT(11) DEFAULT '8000', " + " `deny_entry` TINYINT(1) DEFAULT '0', " + " `alias` VARCHAR(50) DEFAULT NULL, " + " `flags` VARCHAR(512) DEFAULT NULL, " + " `merged` INT(11) DEFAULT NULL, " + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT', " + " PRIMARY KEY (`plot_plot_id`) " + " ) " ) ;
stmt . addBatch ( " CREATE TABLE IF NOT EXISTS ` " + this . prefix + " plot_ratings` (`plot_plot_id` INT(11) NOT NULL, `rating` INT(2) NOT NULL, `player` VARCHAR(40) NOT NULL, PRIMARY KEY(`plot_plot_id`)) " ) ;
2014-11-05 04:42:08 +01:00
}
stmt . executeBatch ( ) ;
stmt . clearBatch ( ) ;
stmt . close ( ) ;
}
/ * *
* Delete a plot
*
* @param plot
* /
@Override
public void delete ( final String world , final Plot plot ) {
PlotMain . removePlot ( world , plot . id , false ) ;
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
PreparedStatement stmt = null ;
final int id = getId ( world , plot . id ) ;
try {
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_settings` WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , id ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_helpers` WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , id ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_trusted` WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , id ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_comments` WHERE `plot_plot_id` = ? " ) ;
2014-11-06 08:23:21 +01:00
stmt . setInt ( 1 , id ) ;
stmt . executeUpdate ( ) ;
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot` WHERE `id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , id ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . DANGER , " Failed to delete plot " + plot . id ) ;
}
}
} ) ;
}
/ * *
* Create plot settings
*
* @param id
* @param plot
* /
@Override
public void createPlotSettings ( final int id , final Plot plot ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " INSERT INTO ` " + SQLManager . this . prefix + " plot_settings`(`plot_plot_id`) VALUES( " + " ?) " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , id ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
}
}
} ) ;
}
@Override
public int getId ( final String world , final PlotId id2 ) {
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = this . connection . prepareStatement ( " SELECT `id` FROM ` " + this . prefix + " plot` WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND world = ? ORDER BY `timestamp` ASC " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , id2 . x ) ;
stmt . setInt ( 2 , id2 . y ) ;
stmt . setString ( 3 , world ) ;
final ResultSet r = stmt . executeQuery ( ) ;
int id = Integer . MAX_VALUE ;
while ( r . next ( ) ) {
id = r . getInt ( " id " ) ;
}
stmt . close ( ) ;
return id ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
}
return Integer . MAX_VALUE ;
}
/ * *
* @return
* /
@Override
public LinkedHashMap < String , HashMap < PlotId , Plot > > getPlots ( ) {
2014-11-21 23:45:46 +01:00
final LinkedHashMap < String , HashMap < PlotId , Plot > > newplots = new LinkedHashMap < > ( ) ;
2014-11-05 04:42:08 +01:00
try {
2014-12-16 06:03:20 +01:00
final DatabaseMetaData data = this . connection . getMetaData ( ) ;
ResultSet rs = data . getColumns ( null , null , this . prefix + " plot " , " plot_id " ) ;
2014-11-05 04:42:08 +01:00
final boolean execute = rs . next ( ) ;
if ( execute ) {
2014-12-16 06:03:20 +01:00
final Statement statement = this . connection . createStatement ( ) ;
statement . addBatch ( " ALTER IGNORE TABLE ` " + this . prefix + " plot` ADD `plot_id_x` int(11) DEFAULT 0 " ) ;
statement . addBatch ( " ALTER IGNORE TABLE ` " + this . prefix + " plot` ADD `plot_id_z` int(11) DEFAULT 0 " ) ;
statement . addBatch ( " UPDATE ` " + this . prefix + " plot` SET \ n " + " `plot_id_x` = IF( " + " LOCATE(';', `plot_id`) > 0, " + " SUBSTRING(`plot_id`, 1, LOCATE(';', `plot_id`) - 1), " + " `plot_id` " + " ), " + " `plot_id_z` = IF( " + " LOCATE(';', `plot_id`) > 0, " + " SUBSTRING(`plot_id`, LOCATE(';', `plot_id`) + 1), " + " NULL " + " ) " ) ;
statement . addBatch ( " ALTER TABLE ` " + this . prefix + " plot` DROP `plot_id` " ) ;
statement . addBatch ( " ALTER IGNORE TABLE ` " + this . prefix + " plot_settings` ADD `flags` VARCHAR(512) DEFAULT NULL " ) ;
2014-11-05 04:42:08 +01:00
statement . executeBatch ( ) ;
statement . close ( ) ;
}
2014-12-16 06:03:20 +01:00
rs = data . getColumns ( null , null , this . prefix + " plot_settings " , " merged " ) ;
2014-11-05 04:42:08 +01:00
if ( ! rs . next ( ) ) {
2014-12-16 06:03:20 +01:00
final Statement statement = this . connection . createStatement ( ) ;
statement . addBatch ( " ALTER TABLE ` " + this . prefix + " plot_settings` ADD `merged` int(11) DEFAULT NULL " ) ;
2014-11-05 04:42:08 +01:00
statement . executeBatch ( ) ;
statement . close ( ) ;
}
2014-12-18 03:15:11 +01:00
} catch ( final Exception e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
}
2014-11-21 23:45:46 +01:00
final HashMap < Integer , Plot > plots = new HashMap < > ( ) ;
2014-11-05 04:42:08 +01:00
Statement stmt = null ;
try {
2014-11-21 23:45:46 +01:00
Set < String > worlds = new HashSet < > ( ) ;
2014-11-05 04:42:08 +01:00
if ( PlotMain . config . contains ( " worlds " ) ) {
worlds = PlotMain . config . getConfigurationSection ( " worlds " ) . getKeys ( false ) ;
}
final HashMap < String , UUID > uuids = new HashMap < String , UUID > ( ) ;
final HashMap < String , Integer > noExist = new HashMap < String , Integer > ( ) ;
/ *
2014-10-28 08:54:41 +01:00
* Getting plots
* /
2014-12-16 06:03:20 +01:00
stmt = this . connection . createStatement ( ) ;
ResultSet r = stmt . executeQuery ( " SELECT `id`, `plot_id_x`, `plot_id_z`, `owner`, `world` FROM ` " + this . prefix + " plot` " ) ;
2014-11-05 04:42:08 +01:00
PlotId plot_id ;
int id ;
Plot p ;
String o ;
UUID user ;
while ( r . next ( ) ) {
plot_id = new PlotId ( r . getInt ( " plot_id_x " ) , r . getInt ( " plot_id_z " ) ) ;
id = r . getInt ( " id " ) ;
final String worldname = r . getString ( " world " ) ;
if ( ! worlds . contains ( worldname ) ) {
if ( noExist . containsKey ( worldname ) ) {
noExist . put ( worldname , noExist . get ( worldname ) + 1 ) ;
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
noExist . put ( worldname , 1 ) ;
}
}
o = r . getString ( " owner " ) ;
user = uuids . get ( o ) ;
if ( user = = null ) {
user = UUID . fromString ( o ) ;
uuids . put ( o , user ) ;
}
2014-12-18 03:15:11 +01:00
p = new Plot ( plot_id , user , new ArrayList < UUID > ( ) , new ArrayList < UUID > ( ) , new ArrayList < UUID > ( ) , " " , PlotHomePosition . DEFAULT , null , worldname , new boolean [ ] { false , false , false , false } ) ;
2014-11-05 04:42:08 +01:00
plots . put ( id , p ) ;
}
2014-12-16 06:03:20 +01:00
// stmt.close();
2014-11-05 04:42:08 +01:00
/ *
* Getting helpers
* /
2014-12-16 06:03:20 +01:00
// stmt = connection.createStatement();
r = stmt . executeQuery ( " SELECT `user_uuid`, `plot_plot_id` FROM ` " + this . prefix + " plot_helpers` " ) ;
2014-10-28 08:54:41 +01:00
while ( r . next ( ) ) {
id = r . getInt ( " plot_plot_id " ) ;
o = r . getString ( " user_uuid " ) ;
user = uuids . get ( o ) ;
2014-11-05 04:42:08 +01:00
if ( user = = null ) {
2014-10-28 08:54:41 +01:00
user = UUID . fromString ( o ) ;
uuids . put ( o , user ) ;
}
2014-11-05 04:42:08 +01:00
final Plot plot = plots . get ( id ) ;
if ( plot ! = null ) {
2014-10-28 08:54:41 +01:00
plot . addHelper ( user ) ;
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
PlotMain . sendConsoleSenderMessage ( " &cPLOT " + id + " in plot_helpers does not exist. Please create the plot or remove this entry. " ) ;
2014-10-28 08:54:41 +01:00
}
}
2014-12-16 06:03:20 +01:00
// stmt.close();
2014-11-05 04:42:08 +01:00
2014-10-28 08:54:41 +01:00
/ *
* Getting trusted
* /
2014-12-16 06:03:20 +01:00
// stmt = connection.createStatement();
r = stmt . executeQuery ( " SELECT `user_uuid`, `plot_plot_id` FROM ` " + this . prefix + " plot_trusted` " ) ;
2014-10-28 08:54:41 +01:00
while ( r . next ( ) ) {
id = r . getInt ( " plot_plot_id " ) ;
o = r . getString ( " user_uuid " ) ;
user = uuids . get ( o ) ;
2014-11-05 04:42:08 +01:00
if ( user = = null ) {
2014-10-28 08:54:41 +01:00
user = UUID . fromString ( o ) ;
uuids . put ( o , user ) ;
}
2014-11-05 04:42:08 +01:00
final Plot plot = plots . get ( id ) ;
if ( plot ! = null ) {
2014-10-28 08:54:41 +01:00
plot . addTrusted ( user ) ;
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
PlotMain . sendConsoleSenderMessage ( " &cPLOT " + id + " in plot_trusted does not exist. Please create the plot or remove this entry. " ) ;
2014-10-28 08:54:41 +01:00
}
}
2014-12-16 06:03:20 +01:00
// stmt.close();
2014-11-05 04:42:08 +01:00
2014-10-28 08:54:41 +01:00
/ *
* Getting denied
* /
2014-12-16 06:03:20 +01:00
// stmt = connection.createStatement();
r = stmt . executeQuery ( " SELECT `user_uuid`, `plot_plot_id` FROM ` " + this . prefix + " plot_denied` " ) ;
2014-10-28 08:54:41 +01:00
while ( r . next ( ) ) {
id = r . getInt ( " plot_plot_id " ) ;
o = r . getString ( " user_uuid " ) ;
user = uuids . get ( o ) ;
2014-11-05 04:42:08 +01:00
if ( user = = null ) {
2014-10-28 08:54:41 +01:00
user = UUID . fromString ( o ) ;
uuids . put ( o , user ) ;
}
2014-11-05 04:42:08 +01:00
final Plot plot = plots . get ( id ) ;
if ( plot ! = null ) {
2014-10-28 08:54:41 +01:00
plot . addDenied ( user ) ;
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
PlotMain . sendConsoleSenderMessage ( " &cPLOT " + id + " in plot_denied does not exist. Please create the plot or remove this entry. " ) ;
2014-10-28 08:54:41 +01:00
}
}
2014-12-16 06:03:20 +01:00
// stmt.close();
2014-11-05 04:42:08 +01:00
2014-12-16 06:03:20 +01:00
// stmt = connection.createStatement();
r = stmt . executeQuery ( " SELECT * FROM ` " + this . prefix + " plot_settings` " ) ;
2014-10-28 08:54:41 +01:00
while ( r . next ( ) ) {
id = r . getInt ( " plot_plot_id " ) ;
2014-11-05 04:42:08 +01:00
final Plot plot = plots . get ( id ) ;
if ( plot ! = null ) {
final String b = r . getString ( " biome " ) ;
if ( b ! = null ) {
for ( final Biome mybiome : Biome . values ( ) ) {
2014-10-28 08:54:41 +01:00
if ( mybiome . toString ( ) . equalsIgnoreCase ( b ) ) {
break ;
}
}
}
2014-11-05 04:42:08 +01:00
final String alias = r . getString ( " alias " ) ;
if ( alias ! = null ) {
2014-10-28 08:54:41 +01:00
plot . settings . setAlias ( alias ) ;
}
2014-11-05 04:42:08 +01:00
final String pos = r . getString ( " position " ) ;
if ( pos ! = null ) {
for ( final PlotHomePosition plotHomePosition : PlotHomePosition . values ( ) ) {
2014-10-28 08:54:41 +01:00
if ( plotHomePosition . isMatching ( pos ) ) {
2014-11-05 04:42:08 +01:00
if ( plotHomePosition ! = PlotHomePosition . DEFAULT ) {
2014-10-28 08:54:41 +01:00
plot . settings . setPosition ( plotHomePosition ) ;
}
break ;
}
}
}
2014-11-05 04:42:08 +01:00
final Integer m = r . getInt ( " merged " ) ;
if ( m ! = null ) {
final boolean [ ] merged = new boolean [ 4 ] ;
2014-10-28 08:54:41 +01:00
for ( int i = 0 ; i < 4 ; i + + ) {
2014-11-05 04:42:08 +01:00
merged [ 3 - i ] = ( ( m ) & ( 1 < < i ) ) ! = 0 ;
2014-10-28 08:54:41 +01:00
}
plot . settings . setMerged ( merged ) ;
2014-12-18 03:15:11 +01:00
} else {
plot . settings . setMerged ( new boolean [ ] { false , false , false , false } ) ;
2014-10-28 08:54:41 +01:00
}
2014-11-05 04:42:08 +01:00
2014-10-28 08:54:41 +01:00
String [ ] flags_string ;
2014-11-05 04:42:08 +01:00
final String myflags = r . getString ( " flags " ) ;
2014-10-28 08:54:41 +01:00
if ( myflags = = null ) {
2014-12-18 03:15:11 +01:00
flags_string = new String [ ] { } ;
} else {
2014-10-28 08:54:41 +01:00
flags_string = myflags . split ( " , " ) ;
}
2014-11-05 04:42:08 +01:00
final ArrayList < Flag > flags = new ArrayList < Flag > ( ) ;
2014-10-28 08:54:41 +01:00
boolean exception = false ;
2014-11-05 04:42:08 +01:00
for ( final String element : flags_string ) {
if ( element . contains ( " : " ) ) {
final String [ ] split = element . split ( " : " ) ;
2014-10-28 08:54:41 +01:00
try {
2014-11-15 12:05:48 +01:00
flags . add ( new Flag ( FlagManager . getFlag ( split [ 0 ] , true ) , split [ 1 ] . replaceAll ( " \ u00AF " , " : " ) . replaceAll ( " <EFBFBD> " , " , " ) ) ) ;
2014-12-18 03:15:11 +01:00
} catch ( final Exception e ) {
2014-10-28 08:54:41 +01:00
exception = true ;
}
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
flags . add ( new Flag ( FlagManager . getFlag ( element , true ) , " " ) ) ;
2014-10-28 08:54:41 +01:00
}
}
if ( exception ) {
2014-11-05 04:42:08 +01:00
PlotMain . sendConsoleSenderMessage ( " &cPlot " + id + " had an invalid flag. A fix has been attempted. " ) ;
2014-10-28 08:54:41 +01:00
setFlags ( id , flags . toArray ( new Flag [ 0 ] ) ) ;
}
plot . settings . setFlags ( flags . toArray ( new Flag [ 0 ] ) ) ;
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
PlotMain . sendConsoleSenderMessage ( " &cPLOT " + id + " in plot_settings does not exist. Please create the plot or remove this entry. " ) ;
2014-10-28 08:54:41 +01:00
}
}
stmt . close ( ) ;
2014-11-05 04:42:08 +01:00
for ( final Plot plot : plots . values ( ) ) {
final String world = plot . world ;
2014-10-28 08:54:41 +01:00
if ( ! newplots . containsKey ( world ) ) {
newplots . put ( world , new HashMap < PlotId , Plot > ( ) ) ;
}
newplots . get ( world ) . put ( plot . id , plot ) ;
}
2014-11-06 08:23:21 +01:00
boolean invalidPlot = false ;
2014-11-05 04:42:08 +01:00
for ( final String worldname : noExist . keySet ( ) ) {
2014-11-06 08:23:21 +01:00
invalidPlot = true ;
PlotMain . sendConsoleSenderMessage ( " &c[WARNING] Found " + noExist . get ( worldname ) + " plots in DB for non existant world; ' " + worldname + " '. " ) ;
}
if ( invalidPlot ) {
PlotMain . sendConsoleSenderMessage ( " &c[WARNING] - Please create the world/s or remove the plots using the purge command " ) ;
2014-11-05 04:42:08 +01:00
}
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to load plots. " ) ;
e . printStackTrace ( ) ;
}
return newplots ;
}
@Override
public void setMerged ( final String world , final Plot plot , final boolean [ ] merged ) {
plot . settings . setMerged ( merged ) ;
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
int n = 0 ;
for ( int i = 0 ; i < 4 ; + + i ) {
n = ( n < < 1 ) + ( merged [ i ] ? 1 : 0 ) ;
}
2014-12-16 06:03:20 +01:00
final PreparedStatement stmt = SQLManager . this . connection . prepareStatement ( " UPDATE ` " + SQLManager . this . prefix + " plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , n ) ;
stmt . setInt ( 2 , getId ( world , plot . id ) ) ;
stmt . execute ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Could not set merged for plot " + plot . id ) ;
}
}
} ) ;
}
@Override
public void setFlags ( final String world , final Plot plot , final Flag [ ] flags ) {
plot . settings . setFlags ( flags ) ;
final StringBuilder flag_string = new StringBuilder ( ) ;
int i = 0 ;
for ( final Flag flag : flags ) {
if ( i ! = 0 ) {
flag_string . append ( " , " ) ;
}
2014-11-09 23:55:45 +01:00
flag_string . append ( flag . getKey ( ) + " : " + flag . getValue ( ) . replaceAll ( " : " , " \ u00AF " ) . replaceAll ( " , " , " \ u00B4 " ) ) ;
2014-11-05 04:42:08 +01:00
i + + ;
}
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement stmt = SQLManager . this . connection . prepareStatement ( " UPDATE ` " + SQLManager . this . prefix + " plot_settings` SET `flags` = ? WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setString ( 1 , flag_string . toString ( ) ) ;
stmt . setInt ( 2 , getId ( world , plot . id ) ) ;
stmt . execute ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Could not set flag for plot " + plot . id ) ;
}
}
} ) ;
}
2014-10-28 08:54:41 +01:00
public void setFlags ( final int id , final Flag [ ] flags ) {
2014-11-05 04:42:08 +01:00
final ArrayList < Flag > newflags = new ArrayList < Flag > ( ) ;
for ( final Flag flag : flags ) {
if ( ( flag ! = null ) & & ( flag . getKey ( ) ! = null ) & & ! flag . getKey ( ) . equals ( " " ) ) {
2014-10-25 02:16:15 +02:00
newflags . add ( flag ) ;
}
}
2014-11-05 04:42:08 +01:00
final String flag_string = StringUtils . join ( newflags , " , " ) ;
2014-10-25 02:16:15 +02:00
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement stmt = SQLManager . this . connection . prepareStatement ( " UPDATE ` " + SQLManager . this . prefix + " plot_settings` SET `flags` = ? WHERE `plot_plot_id` = ? " ) ;
2014-10-25 02:16:15 +02:00
stmt . setString ( 1 , flag_string ) ;
stmt . setInt ( 2 , id ) ;
stmt . execute ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-10-25 02:16:15 +02:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Could not set flag for plot " + id ) ;
}
}
} ) ;
}
2014-10-22 08:08:55 +02:00
2014-11-05 04:42:08 +01:00
/ * *
* @param plot
* @param alias
* /
@Override
public void setAlias ( final String world , final Plot plot , final String alias ) {
plot . settings . setAlias ( alias ) ;
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " UPDATE ` " + SQLManager . this . prefix + " plot_settings` SET `alias` = ? WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setString ( 1 , alias ) ;
stmt . setInt ( 2 , getId ( world , plot . id ) ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to set alias for plot " + plot . id ) ;
e . printStackTrace ( ) ;
}
}
} ) ;
}
/ * *
* @param r
* /
private void runTask ( final Runnable r ) {
PlotMain . getMain ( ) . getServer ( ) . getScheduler ( ) . runTaskAsynchronously ( PlotMain . getMain ( ) , r ) ;
}
@Override
public void purge ( final String world , final PlotId id ) {
runTask ( new Runnable ( ) {
2014-10-22 08:08:55 +02:00
@Override
public void run ( ) {
2014-11-05 04:42:08 +01:00
final ArrayList < Integer > ids = new ArrayList < Integer > ( ) ;
2014-10-22 08:08:55 +02:00
// Fetching a list of plot IDs for a world
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement stmt = SQLManager . this . connection . prepareStatement ( " SELECT `id` FROM ` " + SQLManager . this . prefix + " plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ? " ) ;
2014-10-22 08:08:55 +02:00
stmt . setString ( 1 , world ) ;
stmt . setInt ( 2 , id . x ) ;
stmt . setInt ( 3 , id . y ) ;
2014-11-05 04:42:08 +01:00
final ResultSet result = stmt . executeQuery ( ) ;
2014-10-22 08:08:55 +02:00
while ( result . next ( ) ) {
2014-11-05 04:42:08 +01:00
final int id = result . getInt ( " id " ) ;
2014-10-22 08:08:55 +02:00
ids . add ( id ) ;
}
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-10-22 08:08:55 +02:00
e . printStackTrace ( ) ;
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " FAILED TO PURGE WORLD ' " + world + " '! " ) ;
2014-10-22 08:08:55 +02:00
return ;
}
if ( ids . size ( ) > 0 ) {
try {
2014-11-05 04:42:08 +01:00
2014-10-22 08:08:55 +02:00
String prefix = " " ;
2014-11-05 04:42:08 +01:00
final StringBuilder idstr = new StringBuilder ( " " ) ;
for ( final Integer id : ids ) {
2014-10-22 08:08:55 +02:00
idstr . append ( prefix + id ) ;
prefix = " OR `plot_plot_id` = " ;
}
2014-11-05 04:42:08 +01:00
2014-12-16 06:03:20 +01:00
PreparedStatement stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_helpers` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-10-22 08:08:55 +02:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-11-05 04:42:08 +01:00
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_denied` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-10-22 08:08:55 +02:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-11-05 04:42:08 +01:00
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_settings` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-10-22 08:08:55 +02:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-11-05 04:42:08 +01:00
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_trusted` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-10-22 08:08:55 +02:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-11-05 04:42:08 +01:00
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-10-22 08:08:55 +02:00
stmt . setString ( 1 , world ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-10-22 08:08:55 +02:00
e . printStackTrace ( ) ;
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . DANGER , " FAILED TO PURGE PLOT FROM DB ' " + world + " ' , ' " + id + " ' ! " ) ;
2014-10-22 08:08:55 +02:00
return ;
}
}
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . GENERAL , " SUCCESSFULLY PURGED PLOT FROM DB ' " + world + " ' , ' " + id + " '! " ) ;
2014-10-22 08:08:55 +02:00
}
2014-11-05 04:42:08 +01:00
} ) ;
}
@Override
public void purge ( final String world ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
final ArrayList < Integer > ids = new ArrayList < Integer > ( ) ;
// Fetching a list of plot IDs for a world
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement stmt = SQLManager . this . connection . prepareStatement ( " SELECT `id` FROM ` " + SQLManager . this . prefix + " plot` WHERE `world` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setString ( 1 , world ) ;
final ResultSet result = stmt . executeQuery ( ) ;
while ( result . next ( ) ) {
final int id = result . getInt ( " id " ) ;
ids . add ( id ) ;
}
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " FAILED TO PURGE WORLD ' " + world + " '! " ) ;
return ;
}
if ( ids . size ( ) > 0 ) {
try {
String prefix = " " ;
final StringBuilder idstr = new StringBuilder ( " " ) ;
for ( final Integer id : ids ) {
idstr . append ( prefix + id ) ;
prefix = " OR `plot_plot_id` = " ;
}
2014-12-16 06:03:20 +01:00
PreparedStatement stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_helpers` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-10-22 08:08:55 +02:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-11-05 04:42:08 +01:00
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_denied` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-11-05 04:42:08 +01:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_settings` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-11-05 04:42:08 +01:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot_trusted` WHERE `plot_plot_id` = " + idstr + " " ) ;
2014-11-05 04:42:08 +01:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + prefix + " plot` WHERE `world` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setString ( 1 , world ) ;
2014-10-22 08:08:55 +02:00
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . DANGER , " FAILED TO PURGE WORLD ' " + world + " '! " ) ;
return ;
}
}
Logger . add ( LogLevel . GENERAL , " SUCCESSFULLY PURGED WORLD ' " + world + " '! " ) ;
}
} ) ;
}
/ * *
* @param plot
* @param position
* /
@Override
public void setPosition ( final String world , final Plot plot , final String position ) {
plot . settings . setPosition ( PlotHomePosition . valueOf ( position ) ) ;
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = SQLManager . this . connection . prepareStatement ( " UPDATE ` " + SQLManager . this . prefix + " plot_settings` SET `position` = ? WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setString ( 1 , position ) ;
stmt . setInt ( 2 , getId ( world , plot . id ) ) ;
stmt . executeUpdate ( ) ;
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to set position for plot " + plot . id ) ;
e . printStackTrace ( ) ;
}
}
} ) ;
}
/ * *
* @param id
2014-12-18 03:15:11 +01:00
*
2014-11-05 04:42:08 +01:00
* @return
* /
@Override
public HashMap < String , Object > getSettings ( final int id ) {
final HashMap < String , Object > h = new HashMap < String , Object > ( ) ;
PreparedStatement stmt = null ;
try {
2014-12-16 06:03:20 +01:00
stmt = this . connection . prepareStatement ( " SELECT * FROM ` " + this . prefix + " plot_settings` WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
stmt . setInt ( 1 , id ) ;
final ResultSet r = stmt . executeQuery ( ) ;
String var ;
Object val ;
while ( r . next ( ) ) {
var = " biome " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
var = " rain " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
var = " custom_time " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
var = " time " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
var = " deny_entry " ;
val = r . getObject ( var ) ;
h . put ( var , ( short ) 0 ) ;
var = " alias " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
var = " position " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
var = " flags " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
var = " merged " ;
val = r . getObject ( var ) ;
h . put ( var , val ) ;
}
stmt . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to load settings for plot: " + id ) ;
e . printStackTrace ( ) ;
}
return h ;
}
@Override
2014-11-02 04:45:52 +01:00
public void removeComment ( final String world , final Plot plot , final PlotComment comment ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_comments` WHERE `plot_plot_id` = ? AND `comment` = ? AND `tier` = ? AND `sender` = ? " ) ;
2014-11-02 04:45:52 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
statement . setString ( 2 , comment . comment ) ;
statement . setInt ( 3 , comment . tier ) ;
statement . setString ( 4 , comment . senderName ) ;
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-02 04:45:52 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Failed to remove helper for plot " + plot . id ) ;
}
}
} ) ;
}
2014-10-22 08:08:55 +02:00
2014-11-02 04:45:52 +01:00
@Override
2014-11-05 04:42:08 +01:00
public ArrayList < PlotComment > getComments ( final String world , final Plot plot , final int tier ) {
final ArrayList < PlotComment > comments = new ArrayList < PlotComment > ( ) ;
2014-11-02 04:45:52 +01:00
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = this . connection . prepareStatement ( " SELECT `*` FROM ` " + this . prefix + " plot_comments` WHERE `plot_plot_id` = ? AND `tier` = ? " ) ;
2014-11-02 04:45:52 +01:00
statement . setInt ( 1 , getId ( plot . getWorld ( ) . getName ( ) , plot . id ) ) ;
statement . setInt ( 2 , tier ) ;
2014-11-05 04:42:08 +01:00
final ResultSet set = statement . executeQuery ( ) ;
2014-11-02 04:45:52 +01:00
PlotComment comment ;
while ( set . next ( ) ) {
2014-11-05 04:42:08 +01:00
final String sender = set . getString ( " sender " ) ;
final String msg = set . getString ( " comment " ) ;
2014-11-02 04:45:52 +01:00
comment = new PlotComment ( msg , sender , tier ) ;
comments . add ( comment ) ;
}
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-02 04:45:52 +01:00
Logger . add ( LogLevel . WARNING , " Failed to fetch rating for plot " + plot . getId ( ) . toString ( ) ) ;
e . printStackTrace ( ) ;
}
return comments ;
}
2014-11-05 04:42:08 +01:00
2014-11-02 04:45:52 +01:00
@Override
public void setComment ( final String world , final Plot plot , final PlotComment comment ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " INSERT INTO ` " + SQLManager . this . prefix + " plot_comments` (`plot_plot_id`, `comment`, `tier`, `sender`) VALUES(?,?,?,?) " ) ;
2014-11-02 04:45:52 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
statement . setString ( 2 , comment . comment ) ;
statement . setInt ( 3 , comment . tier ) ;
statement . setString ( 4 , comment . senderName ) ;
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Failed to remove helper for plot " + plot . id ) ;
}
}
} ) ;
}
/ * *
* @param plot
* @param player
* /
@Override
public void removeHelper ( final String world , final Plot plot , final OfflinePlayer player ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_helpers` WHERE `plot_plot_id` = ? AND `user_uuid` = ? " ) ;
2014-11-05 04:42:08 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
2014-12-13 12:59:43 +01:00
statement . setString ( 2 , UUIDHandler . getUUID ( player ) . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-02 04:45:52 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Failed to remove helper for plot " + plot . id ) ;
}
}
} ) ;
}
2014-11-05 04:42:08 +01:00
/ * *
* @param plot
* @param player
* /
@Override
public void removeTrusted ( final String world , final Plot plot , final OfflinePlayer player ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_trusted` WHERE `plot_plot_id` = ? AND `user_uuid` = ? " ) ;
2014-11-05 04:42:08 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
2014-12-13 12:59:43 +01:00
statement . setString ( 2 , UUIDHandler . getUUID ( player ) . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Failed to remove trusted user for plot " + plot . id ) ;
}
}
} ) ;
}
/ * *
* @param plot
* @param player
* /
@Override
public void setHelper ( final String world , final Plot plot , final OfflinePlayer player ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " INSERT INTO ` " + SQLManager . this . prefix + " plot_helpers` (`plot_plot_id`, `user_uuid`) VALUES(?,?) " ) ;
2014-11-05 04:42:08 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
2014-12-13 12:59:43 +01:00
statement . setString ( 2 , UUIDHandler . getUUID ( player ) . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to set helper for plot " + plot . id ) ;
e . printStackTrace ( ) ;
}
}
} ) ;
}
/ * *
* @param plot
* @param player
* /
@Override
public void setTrusted ( final String world , final Plot plot , final OfflinePlayer player ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " INSERT INTO ` " + SQLManager . this . prefix + " plot_trusted` (`plot_plot_id`, `user_uuid`) VALUES(?,?) " ) ;
2014-11-05 04:42:08 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
2014-12-13 12:59:43 +01:00
statement . setString ( 2 , UUIDHandler . getUUID ( player ) . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to set plot trusted for plot " + plot . id ) ;
e . printStackTrace ( ) ;
}
}
} ) ;
}
/ * *
* @param plot
* @param player
* /
@Override
public void removeDenied ( final String world , final Plot plot , final OfflinePlayer player ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " DELETE FROM ` " + SQLManager . this . prefix + " plot_denied` WHERE `plot_plot_id` = ? AND `user_uuid` = ? " ) ;
2014-11-05 04:42:08 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
2014-12-13 12:59:43 +01:00
statement . setString ( 2 , UUIDHandler . getUUID ( player ) . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
e . printStackTrace ( ) ;
Logger . add ( LogLevel . WARNING , " Failed to remove denied for plot " + plot . id ) ;
}
}
} ) ;
}
/ * *
* @param plot
* @param player
* /
@Override
public void setDenied ( final String world , final Plot plot , final OfflinePlayer player ) {
runTask ( new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = SQLManager . this . connection . prepareStatement ( " INSERT INTO ` " + SQLManager . this . prefix + " plot_denied` (`plot_plot_id`, `user_uuid`) VALUES(?,?) " ) ;
2014-11-05 04:42:08 +01:00
statement . setInt ( 1 , getId ( world , plot . id ) ) ;
2014-12-13 12:59:43 +01:00
statement . setString ( 2 , UUIDHandler . getUUID ( player ) . toString ( ) ) ;
2014-11-05 04:42:08 +01:00
statement . executeUpdate ( ) ;
statement . close ( ) ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to set denied for plot " + plot . id ) ;
e . printStackTrace ( ) ;
}
}
} ) ;
}
@Override
public double getRatings ( final Plot plot ) {
try {
2014-12-16 06:03:20 +01:00
final PreparedStatement statement = this . connection . prepareStatement ( " SELECT AVG(`rating`) AS `rating` FROM ` " + this . prefix + " plot_ratings` WHERE `plot_plot_id` = ? " ) ;
2014-11-05 04:42:08 +01:00
statement . setInt ( 1 , getId ( plot . getWorld ( ) . getName ( ) , plot . id ) ) ;
final ResultSet set = statement . executeQuery ( ) ;
double rating = 0 ;
while ( set . next ( ) ) {
rating = set . getDouble ( " rating " ) ;
}
statement . close ( ) ;
return rating ;
2014-12-18 03:15:11 +01:00
} catch ( final SQLException e ) {
2014-11-05 04:42:08 +01:00
Logger . add ( LogLevel . WARNING , " Failed to fetch rating for plot " + plot . getId ( ) . toString ( ) ) ;
e . printStackTrace ( ) ;
}
return 0 . 0d ;
}
2014-11-02 04:45:52 +01:00
2014-10-22 08:08:55 +02:00
}