From f3777bd8c584e8546c0504f412ad748390083ee8 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 14 May 2015 19:45:14 +1000 Subject: [PATCH] AthionPlots -> PlotSquared converter --- .../intellectualcrafters/plot/BukkitMain.java | 8 +- .../plot/database/SQLManager.java | 63 +++++--- .../database/plotme/APlotMeConnector.java | 2 +- .../plotme/ClassicPlotMeConnector.java | 139 +++++++++++++++--- ...onverter.java => LikePlotMeConverter.java} | 65 ++++---- .../plot/listeners/PlayerEvents.java | 17 +-- 6 files changed, 217 insertions(+), 77 deletions(-) rename PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/{PlotMeConverter.java => LikePlotMeConverter.java} (93%) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index af0fa85e2..60915d509 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -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; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index 83193fa9b..634e8ab6e 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -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 diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/APlotMeConnector.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/APlotMeConnector.java index 6341e47a2..2b87ecd2e 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/APlotMeConnector.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/APlotMeConnector.java @@ -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> getPlotMePlots(Connection connection) throws SQLException; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/ClassicPlotMeConnector.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/ClassicPlotMeConnector.java index 768f3d839..3278c560b 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/ClassicPlotMeConnector.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/ClassicPlotMeConnector.java @@ -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> merges, String world, PlotId id, int direction) { + HashMap 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> getPlotMePlots(Connection connection) throws SQLException { ResultSet r; PreparedStatement stmt; - final HashMap plotSize = new HashMap<>(); + final HashMap plotWidth = new HashMap<>(); + final HashMap roadWidth = new HashMap<>(); final HashMap> plots = new HashMap<>(); - stmt = connection.prepareStatement("SELECT * FROM `plotmePlots`"); + final HashMap> 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()); + if (merge) { + merges.put(world, new HashMap()); + } + } + + 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> entry : merges.entrySet()) { + String world = entry.getKey(); + for (Entry entry2 : entry.getValue().entrySet()) { + HashMap 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); + } + } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/PlotMeConverter.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/LikePlotMeConverter.java similarity index 93% rename from PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/PlotMeConverter.java rename to PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/LikePlotMeConverter.java index 2e21e1937..0e117ef59 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/PlotMeConverter.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/plotme/LikePlotMeConverter.java @@ -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 createdPlots = new ArrayList<>(); sendMessage("Collecting plot data"); - sendMessage(" - plotmePlots"); + + String dbPrefix = plugin.toLowerCase(); + sendMessage(" - " + dbPrefix + "Plots"); final Set worlds = getPlotMeWorlds(plotConfig); - HashMap> plots = connector.getPlotMePlots(connection); - for (Entry> 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> plots = connector.getPlotMePlots(connection); + for (Entry> 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) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 8e8fdf2ae..7ef3693e3 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -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); } }