Added database command

This commit is contained in:
Sauilitired 2014-11-15 12:05:48 +01:00
parent 0017b73bdb
commit 8f8fa01389
8 changed files with 233 additions and 56 deletions

View File

@ -22,7 +22,6 @@
package com.intellectualcrafters.plot;
import com.intellectualcrafters.plot.database.DBFunc;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Biome;
@ -114,8 +113,6 @@ public class Plot implements Cloneable {
* @param plotBiome
* @param helpers
* @param denied
* @param changeTime
* @param time
* @param merged
*/
public Plot(final PlotId id, final UUID owner, final Biome plotBiome, final ArrayList<UUID> helpers, final ArrayList<UUID> trusted, final ArrayList<UUID> denied, final String alias, final PlotHomePosition position, final Flag[] flags, final String world, final boolean[] merged) {
@ -286,7 +283,7 @@ public class Plot implements Cloneable {
public void clear(final Player plr) {
PlotHelper.clear(plr, this);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {

View File

@ -1252,15 +1252,15 @@ public class PlotMain extends JavaPlugin {
return plots;
}
public static void setAllPlotsRaw(final LinkedHashMap<String, HashMap<PlotId, Plot>> plots) {
PlotMain.plots = plots;
}
public static void setAllPlotsRaw(final HashMap<String, HashMap<PlotId, Plot>> plots) {
PlotMain.plots = new LinkedHashMap<>(plots);
// PlotMain.plots.putAll(plots);
}
public static void setAllPlotsRaw(final LinkedHashMap<String, HashMap<PlotId, Plot>> plots) {
PlotMain.plots = plots;
}
/**
* !!WorldGeneration!!
*/
@ -1345,7 +1345,7 @@ public class PlotMain extends JavaPlugin {
if (Settings.DB.USE_MYSQL) {
// TODO: Remake SQLManager
if (DBFunc.dbManager == null) {
DBFunc.dbManager = new SQLManager();
DBFunc.dbManager = new SQLManager(connection, Settings.DB.PREFIX);
}
try {
mySQL = new MySQL(this, Settings.DB.HOST_NAME, Settings.DB.PORT, Settings.DB.DATABASE, Settings.DB.USER, Settings.DB.PASSWORD);
@ -1385,7 +1385,7 @@ public class PlotMain extends JavaPlugin {
// DBFunc.dbManager = new MongoManager();
sendConsoleSenderMessage(C.PREFIX.s() + "MongoDB is not yet implemented");
} else if (Settings.DB.USE_SQLITE) {
DBFunc.dbManager = new SQLManager();
DBFunc.dbManager = new SQLManager(connection, Settings.DB.PREFIX);
try {
connection = new SQLite(this, Settings.DB.SQLITE_DB + ".db").openConnection();
{

View File

@ -131,6 +131,7 @@ public enum Command {
DEOP("deop", "deadmin"),
BAN("ban", "block"),
UNBAN("unban", "unblock"),
DATABASE("database", "convert"),
TP("tp", "tp");
/**
*

View File

@ -0,0 +1,163 @@
package com.intellectualcrafters.plot.commands;
import com.intellectualcrafters.plot.PlayerFunctions;
import com.intellectualcrafters.plot.Plot;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.StringComparsion;
import com.intellectualcrafters.plot.database.MySQL;
import com.intellectualcrafters.plot.database.SQLManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;
/**
* Created 2014-11-15 for PlotSquared
*
* @author Citymonstret
*/
public class Database extends SubCommand {
final String[] tables = new String[]{
"plot_trusted", "plot_ratings", "plot_comments"
};
public Database() {
super(Command.DATABASE, "Convert/Backup Storage", "database [type] [...details]", CommandCategory.DEBUG, false);
}
private static boolean sendMessageU(UUID uuid, String msg) {
if (uuid == null) {
PlotMain.sendConsoleSenderMessage(msg);
} else {
Player p = Bukkit.getPlayer(uuid);
if (p != null && p.isOnline())
return PlayerFunctions.sendMessage(p, msg);
else
return sendMessageU(null, msg);
}
return true;
}
public static void insertPlots(final SQLManager manager, final UUID requester, final Connection c) {
Plugin p = PlotMain.getPlugin(PlotMain.class);
final java.util.Set<Plot> plots = PlotMain.getPlots();
p.getServer().getScheduler().runTaskAsynchronously(p, new Runnable() {
@Override
public void run() {
try {
ArrayList<Plot> ps = new ArrayList<>();
for (Plot p : plots)
ps.add(p);
manager.createPlots(ps);
manager.createAllSettingsAndHelpers(ps);
sendMessageU(requester, "&6Database conversion finished");
} catch (Exception e) {
sendMessageU(requester, "Failed to insert plot objects, see stacktrace for info");
e.printStackTrace();
}
try {
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean execute(Player plr, String... args) {
if (args.length < 1) {
return sendMessage(plr, "/plot database [sqlite/mysql]");
}
String type = new StringComparsion(args[0], new String[]{"mysql", "sqlite"}).getBestMatch().toLowerCase();
switch (type) {
case "MYSQL":
if (args.length < 6) {
return sendMessage(plr, "/plot database mysql [host] [port] [username] [password] [database] {prefix}");
}
String host =
args[1];
String port =
args[2];
String username =
args[3];
String password =
args[4];
String database =
args[5];
String prefix =
"";
if (args.length > 6) {
prefix = args[6];
}
Connection n;
try {
n = new MySQL(
PlotMain.getPlugin(PlotMain.class),
host,
port,
database,
username,
password
).openConnection();
// Connection
if (n.isClosed()) {
return sendMessage(plr, "Failed to open connection");
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return sendMessage(plr, "Failed to open connection, read stacktrace for info");
}
SQLManager manager = new SQLManager(n, prefix);
try {
final DatabaseMetaData meta = n.getMetaData();
ResultSet set = meta.getTables(null, null, prefix + "plot", null);
if (!set.next()) {
manager.createTables("mysql", true);
} else {
for (String s : tables) {
set = meta.getTables(null, null, prefix + s, null);
if (!set.next()) {
manager.createTables("mysql", false);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return sendMessage(plr, "Could not create the required tables and/or load the database") &&
sendMessage(plr, "Please see the stacktrace for more information");
}
UUID requester = null;
if (plr != null) {
requester = plr.getUniqueId();
}
insertPlots(manager, requester, n);
break;
case "SQLITE":
if (args.length < 2) {
return sendMessage(plr, "/plot database sqlite [file name]");
}
sendMessage(plr, "This is not supported yet");
break;
default:
return sendMessage(plr, "Unknown database type");
}
return false;
}
private boolean sendMessage(Player player, String msg) {
if (player == null) {
PlotMain.sendConsoleSenderMessage(msg);
} else {
PlayerFunctions.sendMessage(player, msg);
}
return true;
}
}

View File

@ -46,7 +46,27 @@ public class MainCommand implements CommandExecutor, TabCompleter {
public static final String
MAIN_PERMISSION = "plots.use";
private static SubCommand[] _subCommands = new SubCommand[]{new Ban(), new Unban(), new OP(), new DEOP(), new Claim(), new Paste(), new Copy(), new Clipboard(), new Auto(), new Home(), new Visit(), new TP(), new Set(), new Clear(), new Delete(), new SetOwner(), new Denied(), new Helpers(), new Trusted(), new Info(), new list(), new Help(), new Debug(), new Schematic(), new plugin(), new Inventory(), new Purge(), new Reload(), new Merge(), new Unlink(), new Kick(), new Setup(), new DebugClaimTest(), new Inbox(), new Comment(), new Swap(), new MusicSubcommand()};
private static SubCommand[] _subCommands =
new SubCommand[]{
new Ban(), new Unban(),
new OP(), new DEOP(),
new Claim(), new Paste(),
new Copy(), new Clipboard(),
new Auto(), new Home(),
new Visit(), new TP(),
new Set(), new Clear(),
new Delete(), new SetOwner(),
new Denied(), new Helpers(),
new Trusted(), new Info(),
new list(), new Help(),
new Debug(), new Schematic(),
new plugin(), new Inventory(),
new Purge(), new Reload(),
new Merge(), new Unlink(),
new Kick(), new Setup(),
new DebugClaimTest(), new Inbox(),
new Comment(), new Database(),
new Swap(), new MusicSubcommand()};
public static ArrayList<SubCommand> subCommands = new ArrayList<SubCommand>() {
{

View File

@ -24,6 +24,7 @@ package com.intellectualcrafters.plot.commands;
import com.intellectualcrafters.plot.C;
import com.intellectualcrafters.plot.PlayerFunctions;
import com.intellectualcrafters.plot.Plot;
import com.intellectualcrafters.plot.database.DBFunc;
import org.bukkit.entity.Player;
@SuppressWarnings({"unused", "deprecated", "javadoc"})
@ -74,12 +75,17 @@ public class Rate extends SubCommand {
return true;
}
// TODO implement check for already rated
final boolean rated = false;
boolean rated = true;
try {
DBFunc.getRatings(plot);
} catch (Exception e) {
rated = false;
}
if (rated) {
sendMessage(plr, C.RATING_ALREADY_EXISTS, plot.getId().toString());
}
// TODO actually do something...
final boolean success = false;
boolean success = true;
if (success) {
sendMessage(plr, C.RATING_APPLIED, plot.getId().toString());
} else {

View File

@ -40,6 +40,10 @@ public class DBFunc {
public static AbstractDB dbManager;
// TODO MongoDB @Brandon
/**
*
*/
public static UUID everyone = UUID.fromString("1-1-3-3-7");
/**
* Set Plot owner
@ -101,10 +105,6 @@ public class DBFunc {
dbManager.createPlotSettings(id, plot);
}
public static int getId(final String world, final PlotId id2) {
return dbManager.getId(world, id2);
}
/**
* Get a plot id
*
@ -121,6 +121,9 @@ public class DBFunc {
* catch(SQLException e) { e.printStackTrace(); } return Integer.MAX_VALUE;
* }
*/
public static int getId(final String world, final PlotId id2) {
return dbManager.getId(world, id2);
}
/**
* @return
@ -169,11 +172,6 @@ public class DBFunc {
return dbManager.getSettings(id);
}
/**
*
*/
public static UUID everyone = UUID.fromString("1-1-3-3-7");
/**
* @param plot
* @param comment
@ -192,7 +190,6 @@ public class DBFunc {
/**
* @param plot
* @param comment
*/
public static ArrayList<PlotComment> getCommenst(final String world, final Plot plot, final int tier) {
return dbManager.getComments(world, plot, tier);

View File

@ -21,49 +21,42 @@
package com.intellectualcrafters.plot.database;
import static com.intellectualcrafters.plot.PlotMain.connection;
import static com.intellectualcrafters.plot.Settings.DB.PREFIX;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.UUID;
import com.intellectualcrafters.plot.*;
import com.intellectualcrafters.plot.Logger.LogLevel;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Biome;
import com.intellectualcrafters.plot.Flag;
import com.intellectualcrafters.plot.FlagManager;
import com.intellectualcrafters.plot.Logger;
import com.intellectualcrafters.plot.Logger.LogLevel;
import com.intellectualcrafters.plot.Plot;
import com.intellectualcrafters.plot.PlotComment;
import com.intellectualcrafters.plot.PlotHomePosition;
import com.intellectualcrafters.plot.PlotId;
import com.intellectualcrafters.plot.PlotMain;
import java.sql.*;
import java.util.*;
/**
* @author Citymonstret
*/
public class SQLManager extends AbstractDB {
// TODO MongoDB @Brandon
private static Connection connection;
private static String PREFIX;
public static final String SET_OWNER =
"UPDATE `" + PREFIX + "plot` SET `owner` = ? WHERE `plot_id_x` = ? AND `plot_id_z` = ?";
public static final String SET_OWNER = "UPDATE `" + PREFIX + "plot` SET `owner` = ? WHERE `plot_id_x` = ? AND `plot_id_z` = ?";
public static final String GET_ALL_PLOTS = "SELECT `id`, `plot_id_x`, `plot_id_z`, `world` FROM `" + PREFIX + "plot`";
public static final String CREATE_PLOTS = "INSERT INTO `" + PREFIX + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) values ";
public static final String CREATE_SETTINGS = "INSERT INTO `" + PREFIX + "plot_settings` (`plot_plot_id`) values ";
public static final String CREATE_HELPERS = "INSERT INTO `" + PREFIX + "plot_helpers` (`plot_plot_id`, `user_uuid`) values ";
public static final String CREATE_PLOT = "INSERT INTO `" + PREFIX + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) VALUES(?, ?, ?, ?)";
// TODO MongoDB @Brandon
public static final String GET_ALL_PLOTS =
"SELECT `id`, `plot_id_x`, `plot_id_z`, `world` FROM `" + PREFIX + "plot`";
public static final String CREATE_PLOTS =
"INSERT INTO `" + PREFIX + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) values ";
public static final String CREATE_SETTINGS =
"INSERT INTO `" + PREFIX + "plot_settings` (`plot_plot_id`) values ";
public static final String CREATE_HELPERS =
"INSERT INTO `" + PREFIX + "plot_helpers` (`plot_plot_id`, `user_uuid`) values ";
public static final String CREATE_PLOT =
"INSERT INTO `" + PREFIX + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) VALUES(?, ?, ?, ?)";
public SQLManager(Connection c, String p) {
connection = c;
PREFIX = p;
}
/**
* Set Plot owner
@ -567,7 +560,7 @@ public class SQLManager extends AbstractDB {
if (element.contains(":")) {
final String[] split = element.split(":");
try {
flags.add(new Flag(FlagManager.getFlag(split[0], true), split[1].replaceAll("\u00AF", ":").replaceAll("´", ",")));
flags.add(new Flag(FlagManager.getFlag(split[0], true), split[1].replaceAll("\u00AF", ":").replaceAll("<EFBFBD>", ",")));
} catch (final Exception e) {
exception = true;
}