AthionPlots -> PlotSquared converter

This commit is contained in:
boy0001 2015-05-14 19:45:14 +10:00
parent 710b62fbf5
commit f3777bd8c5
6 changed files with 217 additions and 77 deletions

View File

@ -78,7 +78,7 @@ import com.intellectualcrafters.plot.commands.plugin;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.plotme.ClassicPlotMeConnector;
import com.intellectualcrafters.plot.database.plotme.PlotMeConverter;
import com.intellectualcrafters.plot.database.plotme.LikePlotMeConverter;
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.generator.BukkitHybridUtils;
import com.intellectualcrafters.plot.generator.HybridGen;
@ -429,10 +429,12 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
TaskManager.runTaskLaterAsync(new Runnable() {
@Override
public void run() {
new PlotMeConverter().run(new ClassicPlotMeConnector());
if (!(new LikePlotMeConverter("PlotMe").run(new ClassicPlotMeConnector()))) {
new LikePlotMeConverter("AthionPlots").run(new ClassicPlotMeConnector());
}
}
}, 20);
if (Bukkit.getPluginManager().getPlugin("PlotMe") != null) {
if (Bukkit.getPluginManager().getPlugin("PlotMe") != null || Bukkit.getPluginManager().getPlugin("AthionPlots") != null) {
return true;
}
return false;

View File

@ -209,8 +209,7 @@ public class SQLManager implements AbstractDB {
createTiers(helpers, "helpers");
createTiers(trusted, "trusted");
createTiers(denied, "denied");
TaskManager.runTaskLater(whenDone, 20);
TaskManager.runTaskLater(whenDone, 60);
}
catch (SQLException e) {
e.printStackTrace();
@ -364,7 +363,7 @@ public class SQLManager implements AbstractDB {
e2.printStackTrace();
PlotSquared.log("&6[WARN] " + "Could not bulk save!");
try {
for (final T obj : subList) {
for (final T obj : subList) {
try {
stmt = connection.prepareStatement(mod.getCreateSQL());
mod.setSQL(stmt, obj);
@ -392,19 +391,17 @@ public class SQLManager implements AbstractDB {
@Override
public String getCreateSQLite(int size) {
return getCreateSQLite(size, "INSERT INTO `" + prefix + "plot_settings` SELECT ? AS `plot_plot_id`, ? AS `biome`, ? AS `rain`, ? AS `custom_time`, ? AS `time`, ? AS `deny_entry`, ? AS `alias`, ? AS `flags`, ? AS `merged`, ? AS `position` ", 10);
return getCreateSQLite(size, "INSERT INTO `" + prefix + "plot_settings` SELECT ? AS `plot_plot_id`, ? AS `biome`, ? AS `rain`, ? AS `custom_time`, ? AS `time`, ? AS `deny_entry`, ? AS `alias`, ? AS `flags`, ? AS `merged`, ? AS `position`", 10);
}
@Override
public String getCreateSQL() {
return "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)";
// return "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`,`biome`,`rain`,`custom_time`,`time`,`deny_entry`,`alias`,`flags`,`merged`,`position`) VALUES(?,?,?,?,?,?,?,?,?,?)";
}
@Override
public void setMySQL(PreparedStatement stmt, int i, SettingsPair pair) throws SQLException {
// stmt.setInt((i * 1) + 1, id.id);
// `plot_plot_id`, ? AS `biome`, ? AS `rain`, ? AS `custom_time`, ? AS `time`, ? AS `deny_entry`, ? AS `alias`, ? AS `flags`, ? AS `merged`, ? AS `position`
stmt.setInt((i * 10) + 1, pair.id ); // id
stmt.setNull((i * 10) + 2, 4); // biome
stmt.setNull((i * 10) + 3, 4); // rain
@ -451,16 +448,48 @@ public class SQLManager implements AbstractDB {
@Override
public void setSQLite(PreparedStatement stmt, int i, SettingsPair pair) throws SQLException {
stmt.setInt((i * 10) + 1, pair.id );
stmt.setNull((i * 10) + 2, 4);
stmt.setNull((i * 10) + 3, 4);
stmt.setNull((i * 10) + 4, 4);
stmt.setNull((i * 10) + 5, 4);
stmt.setNull((i * 10) + 6, 4);
stmt.setNull((i * 10) + 7, 4);
stmt.setNull((i * 10) + 8, 4);
stmt.setNull((i * 10) + 9, 4);
stmt.setString((i * 10) + 10, "DEFAULT");
stmt.setInt((i * 10) + 1, pair.id ); // id
stmt.setNull((i * 10) + 2, 4); // biome
stmt.setNull((i * 10) + 3, 4); // rain
stmt.setNull((i * 10) + 4, 4); // custom_time
stmt.setNull((i * 10) + 5, 4); // time
stmt.setNull((i * 10) + 6, 4); // deny_entry
if (pair.settings.getAlias().equals("")) {
stmt.setNull((i * 10) + 7, 4);
}
else {
stmt.setString((i * 10) + 7, pair.settings.getAlias());
}
if (pair.settings.flags == null) {
stmt.setNull((i * 10) + 8, 4);
}
else {
final StringBuilder flag_string = new StringBuilder();
int k = 0;
for (final Flag flag : pair.settings.flags) {
if (k != 0) {
flag_string.append(",");
}
flag_string.append(flag.getKey() + ":" + flag.getValueString().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4"));
k++;
}
stmt.setString((i * 10) + 8, flag_string.toString());
}
boolean[] merged = pair.settings.getMerged();
int n = 0;
for (int j = 0; j < 4; ++j) {
n = (n << 1) + (merged[j] ? 1 : 0);
}
stmt.setInt((i * 10) + 9, n);
BlockLoc loc = pair.settings.getPosition();
String position;
if (loc.y == 0) {
position = "DEFAULT";
}
else {
position = loc.x + "," + loc.y + "," + loc.z;
}
stmt.setString((i * 10) + 10, position);
}
@Override

View File

@ -10,7 +10,7 @@ import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
public abstract class APlotMeConnector {
public abstract Connection getPlotMeConnection(FileConfiguration plotConfig, String dataFolder);
public abstract Connection getPlotMeConnection(String plugin, FileConfiguration plotConfig, String dataFolder);
public abstract HashMap<String, HashMap<PlotId, Plot>> getPlotMePlots(Connection connection) throws SQLException;
}

View File

@ -9,6 +9,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.configuration.file.FileConfiguration;
@ -17,16 +18,22 @@ import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.database.MySQL;
import com.intellectualcrafters.plot.database.SQLite;
import com.intellectualcrafters.plot.generator.SquarePlotWorld;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
public class ClassicPlotMeConnector extends APlotMeConnector {
private String plugin;
@Override
public Connection getPlotMeConnection(FileConfiguration plotConfig, String dataFolder) {
public Connection getPlotMeConnection(String plugin, FileConfiguration plotConfig, String dataFolder) {
this.plugin = plugin.toLowerCase();
try {
if (plotConfig.getBoolean("usemySQL")) {
final String user = plotConfig.getString("mySQLuname");
@ -41,25 +48,99 @@ public class ClassicPlotMeConnector extends APlotMeConnector {
catch (SQLException | ClassNotFoundException e) {}
return null;
}
public void setMerged(HashMap<String, HashMap<PlotId, boolean[]>> merges, String world, PlotId id, int direction) {
HashMap<PlotId, boolean[]> plots = merges.get(world);
PlotId id2;
switch (direction) {
case 0: {
id2 = new PlotId(id.x, id.y);
break;
}
case 1: {
id2 = new PlotId(id.x, id.y);
break;
}
case 2: {
id2 = new PlotId(id.x, id.y);
break;
}
case 3: {
id2 = new PlotId(id.x, id.y);
break;
}
default: {
return;
}
}
boolean[] merge1;
boolean[] merge2;
if (plots.containsKey(id)) {
merge1 = plots.get(id);
}
else {
merge1 = new boolean[]{false, false, false, false};
}
if (plots.containsKey(id2)) {
merge2 = plots.get(id2);
}
else {
merge2 = new boolean[]{false, false, false, false};
}
merge1[direction] = true;
merge2[(direction + 2) % 4] = true;
plots.put(id, merge1);
plots.put(id2, merge1);
}
@Override
public HashMap<String, HashMap<PlotId, Plot>> getPlotMePlots(Connection connection) throws SQLException {
ResultSet r;
PreparedStatement stmt;
final HashMap<String, Integer> plotSize = new HashMap<>();
final HashMap<String, Integer> plotWidth = new HashMap<>();
final HashMap<String, Integer> roadWidth = new HashMap<>();
final HashMap<String, HashMap<PlotId, Plot>> plots = new HashMap<>();
stmt = connection.prepareStatement("SELECT * FROM `plotmePlots`");
final HashMap<String, HashMap<PlotId, boolean[]>> merges = new HashMap<>();
stmt = connection.prepareStatement("SELECT * FROM `" + plugin + "Plots`");
r = stmt.executeQuery();
boolean checkUUID = DBFunc.hasColumn(r, "ownerid");
boolean merge = !plugin.equals("plotme");
while (r.next()) {
final PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ"));
final String name = r.getString("owner");
final String world = PlotMeConverter.getWorld(r.getString("world"));
if (!plotSize.containsKey(world)) {
final int size = r.getInt("topZ") - r.getInt("bottomZ");
plotSize.put(world, size);
final String world = LikePlotMeConverter.getWorld(r.getString("world"));
if (!plots.containsKey(world)) {
int plot = PlotSquared.config.getInt("worlds." + world + ".plot.size");
int path = PlotSquared.config.getInt("worlds." + world + ".road.width");
plotWidth.put(world, plot);
roadWidth.put(world, path);
plots.put(world, new HashMap<PlotId, Plot>());
if (merge) {
merges.put(world, new HashMap<PlotId,boolean[]>());
}
}
if (merge) {
int tx = r.getInt("topX");
int tz = r.getInt("topZ");
int bx = r.getInt("bottomX");
int bz = r.getInt("bottomZ");
int path = roadWidth.get(world);
int plot = plotWidth.get(world);
Location top = getPlotTopLocAbs(path, plot, id);
Location bot = getPlotBottomLocAbs(path, plot, id);
if (tx > top.getX()) {
setMerged(merges, world, id, 1);
}
if (tz > top.getZ()) {
setMerged(merges, world, id, 2);
}
if (bx < bot.getX()) {
setMerged(merges, world, id, 3);
}
if (bz > bot.getZ()) {
setMerged(merges, world, id, 0);
}
}
UUID owner = UUIDHandler.getUUID(name);
if (owner == null) {
@ -94,19 +175,30 @@ public class ClassicPlotMeConnector extends APlotMeConnector {
plots.get(world).put(id, plot);
}
for (Entry<String, HashMap<PlotId, boolean[]>> entry : merges.entrySet()) {
String world = entry.getKey();
for (Entry<PlotId, boolean[]> entry2 : entry.getValue().entrySet()) {
HashMap<PlotId, Plot> newplots = plots.get(world);
Plot plot = newplots.get(entry2.getKey());
if (plot != null) {
plot.settings.setMerged(entry2.getValue());
}
}
}
r.close();
stmt.close();
try {
MainUtil.sendConsoleMessage(" - plotmeDenied");
stmt = connection.prepareStatement("SELECT * FROM `plotmeDenied`");
MainUtil.sendConsoleMessage(" - " + plugin + "Denied");
stmt = connection.prepareStatement("SELECT * FROM `" + plugin + "Denied`");
r = stmt.executeQuery();
while (r.next()) {
final PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ"));
final String name = r.getString("player");
final String world = PlotMeConverter.getWorld(r.getString("world"));
final String world = LikePlotMeConverter.getWorld(r.getString("world"));
UUID denied = UUIDHandler.getUUID(name);
if (denied == null) {
if (name.equals("*")) {
@ -121,13 +213,13 @@ public class ClassicPlotMeConnector extends APlotMeConnector {
}
}
stmt = connection.prepareStatement("SELECT * FROM `plotmeAllowed`");
stmt = connection.prepareStatement("SELECT * FROM `" + plugin + "Allowed`");
r = stmt.executeQuery();
while (r.next()) {
final PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ"));
final String name = r.getString("player");
final String world = PlotMeConverter.getWorld(r.getString("world"));
final String world = LikePlotMeConverter.getWorld(r.getString("world"));
UUID helper = UUIDHandler.getUUID(name);
if (helper == null) {
if (name.equals("*")) {
@ -146,11 +238,24 @@ public class ClassicPlotMeConnector extends APlotMeConnector {
stmt.close();
}
catch (Exception e) {
}
catch (Exception e) {}
return plots;
}
public Location getPlotTopLocAbs(int path, int plot, final PlotId plotid) {
final int px = plotid.x;
final int pz = plotid.y;
final int x = (px * (path + plot)) - ((int) Math.floor(path / 2)) - 1;
final int z = (pz * (path + plot)) - ((int) Math.floor(path / 2)) - 1;
return new Location(null, x, 256, z);
}
public Location getPlotBottomLocAbs(int path, int plot, final PlotId plotid) {
final int px = plotid.x;
final int pz = plotid.y;
final int x = (px * (path + plot)) - plot - ((int) Math.floor(path / 2)) - 1;
final int z = (pz * (path + plot)) - plot - ((int) Math.floor(path / 2)) - 1;
return new Location(null, x, 1, z);
}
}

View File

@ -23,16 +23,11 @@ package com.intellectualcrafters.plot.database.plotme;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
@ -43,13 +38,10 @@ import org.bukkit.configuration.file.YamlConfiguration;
import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.database.SQLite;
import com.intellectualcrafters.plot.generator.HybridGen;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
/**
* Created 2014-08-17 for PlotSquared
@ -57,18 +49,28 @@ import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
* @author Citymonstret
* @author Empire92
*/
public class PlotMeConverter {
public class LikePlotMeConverter {
private String plugin;
/**
* Constructor
*
* @param plugin Plugin Used to run the converter
*/
public LikePlotMeConverter(String plugin) {
this.plugin = plugin;
}
private void sendMessage(final String message) {
PlotSquared.log("&3PlotMe&8->&3PlotSquared&8: &7" + message);
}
public String getPlotMePath() {
return new File(".").getAbsolutePath() + File.separator + "plugins" + File.separator + "PlotMe" + File.separator;
return new File(".").getAbsolutePath() + File.separator + "plugins" + File.separator + plugin + File.separator;
}
public String getAthionPlotsPath() {
return new File(".").getAbsolutePath() + File.separator + "plugins" + File.separator + plugin + File.separator;
}
public FileConfiguration getPlotMeConfig(String dataFolder) {
@ -83,43 +85,35 @@ public class PlotMeConverter {
return plotConfig.getConfigurationSection("worlds").getKeys(false);
}
public void run(final APlotMeConnector connector) {
public boolean run(final APlotMeConnector connector) {
try {
String dataFolder = getPlotMePath();
FileConfiguration plotConfig = getPlotMeConfig(dataFolder);
if (plotConfig == null) {
return;
return false;
}
Connection connection = connector.getPlotMeConnection(plotConfig, dataFolder);
Connection connection = connector.getPlotMeConnection(plugin, plotConfig, dataFolder);
if (connection == null) {
sendMessage("Cannot connect to PlotMe DB. Conversion process will not continue");
return;
return false;
}
sendMessage("PlotMe conversion has started. To disable this, please set 'plotme-convert.enabled' in the 'settings.yml'");
sendMessage("Connecting to PlotMe DB");
sendMessage(plugin + " conversion has started. To disable this, please set 'plotme-convert.enabled' in the 'settings.yml'");
sendMessage("Connecting to " + plugin + " DB");
int plotCount = 0;
final ArrayList<Plot> createdPlots = new ArrayList<>();
sendMessage("Collecting plot data");
sendMessage(" - plotmePlots");
String dbPrefix = plugin.toLowerCase();
sendMessage(" - " + dbPrefix + "Plots");
final Set<String> worlds = getPlotMeWorlds(plotConfig);
HashMap<String, HashMap<PlotId, Plot>> plots = connector.getPlotMePlots(connection);
for (Entry<String, HashMap<PlotId, Plot>> entry : plots.entrySet()) {
plotCount += entry.getValue().size();
}
if (!Settings.CONVERT_PLOTME) {
return;
}
sendMessage(" - plotmeAllowed");
sendMessage("Collected " + plotCount + " plots from PlotMe");
for (final String world : plots.keySet()) {
for (final String world : plotConfig.getConfigurationSection("worlds").getKeys(false)) {
sendMessage("Copying config for: " + world);
try {
final String plotMeWorldName = world.toLowerCase();
@ -145,6 +139,18 @@ public class PlotMeConverter {
sendMessage("&c-- &lFailed to save configuration for world '" + world + "'\nThis will need to be done using the setup command, or manually");
}
}
HashMap<String, HashMap<PlotId, Plot>> plots = connector.getPlotMePlots(connection);
for (Entry<String, HashMap<PlotId, Plot>> entry : plots.entrySet()) {
plotCount += entry.getValue().size();
}
if (!Settings.CONVERT_PLOTME) {
return false;
}
sendMessage(" - " + dbPrefix + "Allowed");
sendMessage("Collected " + plotCount + " plots from PlotMe");
final File PLOTME_DG_FILE = new File(dataFolder + File.separator + "PlotMe-DefaultGenerator" + File.separator + "config.yml");
if (PLOTME_DG_FILE.exists()) {
final YamlConfiguration PLOTME_DG_YML = YamlConfiguration.loadConfiguration(PLOTME_DG_FILE);
@ -215,6 +221,7 @@ public class PlotMeConverter {
@Override
public void run() {
sendMessage("&aDatabase conversion is now complete!");
PlotSquared.setAllPlotsRaw(DBFunc.getPlots());
}
});
sendMessage("Saving configuration...");
@ -270,7 +277,6 @@ public class PlotMeConverter {
myworld.save();
}
}
PlotSquared.setAllPlotsRaw(DBFunc.getPlots());
} catch (final Exception e) {
e.printStackTrace();
}
@ -284,6 +290,7 @@ public class PlotMeConverter {
} catch (final Exception e) {
PlotSquared.log("&/end/");
}
return true;
}
public static String getWorld(final String world) {

View File

@ -232,17 +232,14 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
@EventHandler
public void PlayerCommand(final PlayerCommandPreprocessEvent event) {
final String message = event.getMessage();
if (message.toLowerCase().startsWith("/plotme")) {
final Plugin plotme = Bukkit.getPluginManager().getPlugin("PlotMe");
if (plotme == null) {
final Player player = event.getPlayer();
if (Settings.USE_PLOTME_ALIAS) {
player.performCommand(message.replace("/plotme", "plots"));
} else {
MainUtil.sendMessage(BukkitUtil.getPlayer(player), C.NOT_USING_PLOTME);
}
event.setCancelled(true);
if (message.toLowerCase().startsWith("/plotme") || message.toLowerCase().startsWith("/ap")) {
final Player player = event.getPlayer();
if (Settings.USE_PLOTME_ALIAS) {
player.performCommand(message.replace("/plotme", "plots"));
} else {
MainUtil.sendMessage(BukkitUtil.getPlayer(player), C.NOT_USING_PLOTME);
}
event.setCancelled(true);
}
}