From 0daf83c232159bf99d1062b504d2f56099d046f0 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Fri, 30 Jan 2015 09:16:31 +1100 Subject: [PATCH] Fixed PlotMe converter + Added more debug statements --- PlotSquared/pom.xml | 2 +- .../intellectualcrafters/plot/PlotMain.java | 9 +- .../plot/database/PlotMeConverter.java | 11 +- .../plot/database/SQLManager.java | 273 +++++++++--------- .../plot/listeners/PlayerEvents.java | 11 +- .../plot/util/UUIDHandler.java | 6 +- .../plot/uuid/DefaultUUIDWrapper.java | 5 + .../plot/uuid/OfflineUUIDWrapper.java | 5 + .../plot/uuid/UUIDWrapper.java | 2 + 9 files changed, 178 insertions(+), 146 deletions(-) diff --git a/PlotSquared/pom.xml b/PlotSquared/pom.xml index a61df6cea..8faac2127 100644 --- a/PlotSquared/pom.xml +++ b/PlotSquared/pom.xml @@ -8,7 +8,7 @@ UTF-8 PlotSquared - 2.5.12 + 2.6.0 PlotSquared jar diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index 7a8959090..5ed067e87 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -900,7 +900,14 @@ public class PlotMain extends JavaPlugin implements Listener { public static void worldLoad(WorldLoadEvent event) { if (!UUIDHandler.CACHED) { UUIDHandler.cacheAll(); - if (Settings.CONVERT_PLOTME && Bukkit.getPluginManager().getPlugin("PlotMe") != null) { + if (Settings.CONVERT_PLOTME) { + if (Bukkit.getPluginManager().getPlugin("PlotMe") != null) { + sendConsoleSenderMessage("&c[IMPORTANT] THIS MESSAGE MAY BE EXTREMELY HELPFUL IF YOU WISH TO CONVERT A PLOTME DATABASE"); + sendConsoleSenderMessage("&c[IMPORTANT] - Please delete your PlotMe.jar before starting your server!"); + sendConsoleSenderMessage("&c[IMPORTANT] - This is required as the database may be locked if PlotMe is using it!"); + sendConsoleSenderMessage("&c[IMPORTANT] - After the conversion is finished, please set 'plotme-convert.enabled' to false in the 'settings.yml'"); + return; + } try { new PlotMeConverter(PlotMain.getMain()).runAsync(); } catch (final Exception e) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java index 99f74dbb6..e62f5695d 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java @@ -83,6 +83,9 @@ public class PlotMeConverter { final ArrayList createdPlots = new ArrayList<>(); final String dataFolder = new File(".").getAbsolutePath() + File.separator + "plugins" + File.separator + "PlotMe" + File.separator; final File plotMeFile = new File(dataFolder + "config.yml"); + if (!plotMeFile.exists()) { + return; + } final FileConfiguration plotConfig = YamlConfiguration.loadConfiguration(plotMeFile); int count = 0; @@ -96,6 +99,7 @@ public class PlotMeConverter { connection = new SQLite(PlotMain.getMain(), dataFolder + File.separator + "plots.db").openConnection(); } sendMessage("Collecting plot data"); + sendMessage(" - plotmePlots"); ResultSet r; Statement stmt; final HashMap plotSize = new HashMap<>(); @@ -114,7 +118,6 @@ public class PlotMeConverter { plotSize.put(world, size); plots.put(world, new HashMap()); } - UUID owner = UUIDHandler.getUUID(name); if (owner == null) { if (name.equals("*")) { @@ -128,7 +131,7 @@ public class PlotMeConverter { final Plot plot = new Plot(id, owner, new ArrayList(), new ArrayList(), world); plots.get(world).put(id, plot); } - + sendMessage(" - plotmeAllowed"); r = stmt.executeQuery("SELECT * FROM `plotmeAllowed`"); while (r.next()) { final PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ")); @@ -147,7 +150,7 @@ public class PlotMeConverter { plots.get(world).get(id).helpers.add(helper); } } - + sendMessage(" - plotmeDenied"); r = stmt.executeQuery("SELECT * FROM `plotmeDenied`"); while (r.next()) { final PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ")); @@ -301,7 +304,7 @@ public class PlotMeConverter { PlotMain.setAllPlotsRaw(DBFunc.getPlots()); sendMessage("Conversion has finished"); - PlotMain.sendConsoleSenderMessage("&cAlthough the server may be functional in it's current state, it is recommended that you restart the server and remove PlotMe to finalize the installation. Please make careful note of any warning messages that may have showed up during conversion."); + PlotMain.sendConsoleSenderMessage("&cPlease disable 'plotme-convert.enabled' in the settings.yml to indicate that you conversion is no longer required."); } catch (final Exception e) { e.printStackTrace(); } 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 dcfa3dbe4..6f9060443 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -31,11 +31,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.List; import java.util.Set; import java.util.UUID; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.block.Biome; import com.intellectualcrafters.plot.PlotMain; @@ -151,100 +153,105 @@ public class SQLManager implements AbstractDB { } @Override - public void createAllSettingsAndHelpers(final ArrayList plots) { - - // TODO SEVERE [ More than 5000 plots will fail in a single SQLite - // query. - - final HashMap> stored = new HashMap<>(); - final HashMap> helpers = new HashMap<>(); - try { - final PreparedStatement stmt = this.connection.prepareStatement(this.GET_ALL_PLOTS); - 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()); - } - stored.get(world).put(new PlotId(idx, idz), id); - } - } catch (final SQLException e) { - e.printStackTrace(); + public void createAllSettingsAndHelpers(final ArrayList mylist) { + int size = mylist.size(); + int packet; + if (PlotMain.getMySQL() != null) { + packet = Math.min(size, 50000); } - - 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); - } - } + else { + packet = Math.min(size, 5000); } - - if (helpers.size() == 0) { - return; - } - // add plot settings - final Integer[] ids = helpers.keySet().toArray(new Integer[helpers.keySet().size()]); - StringBuilder statement = new StringBuilder(this.CREATE_SETTINGS); - for (int i = 0; i < (ids.length - 1); i++) { - statement.append("(?),"); - } - statement.append("(?)"); - PreparedStatement stmt = null; - try { - stmt = this.connection.prepareStatement(statement.toString()); - for (int i = 0; i < ids.length; i++) { - stmt.setInt(i + 1, ids[i]); - } - stmt.executeUpdate(); - stmt.close(); - } catch (final SQLException e) { - for (int i = 0; i < ids.length; i++) { - createPlotSettings(ids[i], null); - } - } - - // add plot helpers - String prefix = ""; - statement = new StringBuilder(this.CREATE_HELPERS); - for (final Integer id : helpers.keySet()) { - for (final UUID helper : helpers.get(id)) { - statement.append(prefix + "(?, ?)"); - prefix = ","; - } - } - if (prefix.equals("")) { - return; - } - try { - stmt = this.connection.prepareStatement(statement.toString()); - 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(); - } catch (final SQLException e) { + int amount = size/packet; + for (int j = 0; j <= amount;j++) { + List plots = mylist.subList(j * packet, Math.min(size, (j + 1) * packet)); + final HashMap> stored = new HashMap<>(); + final HashMap> helpers = new HashMap<>(); try { - for (final Integer id : helpers.keySet()) { - for (final UUID helper : helpers.get(id)) { - setHelper(id, helper); + final PreparedStatement stmt = this.connection.prepareStatement(this.GET_ALL_PLOTS); + 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()); + } + stored.get(world).put(new PlotId(idx, idz), id); + } + } catch (final SQLException e) { + e.printStackTrace(); + } + for (final Plot plot : plots) { + final String world = plot.world; + if (stored.containsKey(world)) { + final Integer id = stored.get(world).get(plot.id); + if (id != null) { + helpers.put(id, plot.helpers); } } } - catch (Exception e2) {} - PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Failed to set all helpers for plots"); + if (helpers.size() == 0) { + return; + } + // add plot settings + final Integer[] ids = helpers.keySet().toArray(new Integer[helpers.keySet().size()]); + StringBuilder statement = new StringBuilder(this.CREATE_SETTINGS); + for (int i = 0; i < (ids.length - 1); i++) { + statement.append("(?),"); + } + statement.append("(?)"); + PreparedStatement stmt = null; + try { + stmt = this.connection.prepareStatement(statement.toString()); + for (int i = 0; i < ids.length; i++) { + stmt.setInt(i + 1, ids[i]); + } + stmt.executeUpdate(); + stmt.close(); + } catch (final SQLException e) { + for (int i = 0; i < ids.length; i++) { + createPlotSettings(ids[i], null); + } + } + // add plot helpers + String prefix = ""; + statement = new StringBuilder(this.CREATE_HELPERS); + for (final Integer id : helpers.keySet()) { + for (final UUID helper : helpers.get(id)) { + statement.append(prefix + "(?, ?)"); + prefix = ","; + } + } + if (prefix.equals("")) { + return; + } + try { + stmt = this.connection.prepareStatement(statement.toString()); + 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(); + } catch (final SQLException e) { + try { + for (final Integer id : helpers.keySet()) { + for (final UUID helper : helpers.get(id)) { + setHelper(id, helper); + } + } + } + catch (Exception e2) {} + PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Failed to set all helpers for plots"); + } } } @@ -254,53 +261,62 @@ public class SQLManager implements AbstractDB { * @param plots */ @Override - public void createPlots(final ArrayList plots) { - - // TODO SEVERE [ More than 5000 plots will fail in a single SQLite - // query. - - if (plots.size() == 0) { - return; + public void createPlots(final ArrayList mylist) { + int size = mylist.size(); + int packet; + if (PlotMain.getMySQL() != null) { + packet = Math.min(size, 50000); } - final StringBuilder statement = new StringBuilder(this.CREATE_PLOTS); - - for (int i = 0; i < (plots.size() - 1); i++) { - statement.append("(?,?,?,?),"); + else { + packet = Math.min(size, 5000); } - statement.append("(?,?,?,?)"); - - PreparedStatement stmt = null; - try { - stmt = this.connection.prepareStatement(statement.toString()); - 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); - try { - stmt.setString((i * 4) + 3, plot.owner.toString()); - } - catch (Exception e) { - stmt.setString((i * 4) + 3, DBFunc.everyone.toString()); - } - stmt.setString((i * 4) + 4, plot.world); + int amount = size/packet; + for (int j = 0; j <= amount;j++) { + List plots = mylist.subList(j * packet, Math.min(size, (j + 1) * packet)); + if (plots.size() == 0) { + return; } - stmt.executeUpdate(); - stmt.close(); - } catch (final Exception e) { - PlotMain.sendConsoleSenderMessage("&6[WARN] "+"Could not bulk save. Conversion may be slower..."); + final StringBuilder statement = new StringBuilder(this.CREATE_PLOTS); + + for (int i = 0; i < (plots.size() - 1); i++) { + statement.append("(?,?,?,?),"); + } + statement.append("(?,?,?,?)"); + + PreparedStatement stmt = null; try { - for (Plot plot : plots) { + stmt = this.connection.prepareStatement(statement.toString()); + 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); try { - createPlot(plot); + stmt.setString((i * 4) + 3, plot.owner.toString()); } - catch (Exception e3) { - PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"Failed to save plot: "+plot.id); + catch (Exception e) { + stmt.setString((i * 4) + 3, DBFunc.everyone.toString()); + } + stmt.setString((i * 4) + 4, plot.world); + } + stmt.executeUpdate(); + stmt.close(); + } catch (final Exception e) { + e.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&6[WARN] "+"Could not bulk save. Conversion may be slower..."); + try { + for (Plot plot : plots) { + try { + createPlot(plot); + } + catch (Exception e3) { + PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"Failed to save plot: "+plot.id); + } } } - } - catch (Exception e2) { - e2.printStackTrace(); - PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"Failed to save plots!"); + catch (Exception e2) { + e2.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"Failed to save plots!"); + } } } } @@ -1393,9 +1409,6 @@ public class SQLManager implements AbstractDB { if (alias != null) { cluster.settings.setAlias(alias); } - else { - System.out.print("ALIAS IS NULL!!!"); - } final String pos = r.getString("position"); 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 98ed9f812..2661d2278 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -38,7 +38,6 @@ import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; import org.bukkit.entity.Animals; import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; import org.bukkit.entity.ItemFrame; import org.bukkit.entity.Monster; import org.bukkit.entity.Player; @@ -106,12 +105,12 @@ import com.intellectualcrafters.plot.util.UUIDHandler; */ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotListener implements Listener { - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public static void onWorldLoad(final WorldLoadEvent event) { PlotMain.loadWorld(event.getWorld()); } - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public static void onChunkLoad(final ChunkLoadEvent event) { String worldname = event.getWorld().getName(); Chunk chunk = event.getChunk(); @@ -125,7 +124,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi } } - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public static void onJoin(final PlayerJoinEvent event) { Player player = event.getPlayer(); if (!player.hasPlayedBefore()) { @@ -275,7 +274,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi event.setCancelled(true); } - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public static void onBigBoom(final EntityExplodeEvent event) { final World world = event.getLocation().getWorld(); if (!isPlotWorld(world)) { @@ -482,7 +481,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi } } - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public static void onInteract(final PlayerInteractEvent event) { Block block = event.getClickedBlock(); if (block == null) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java index ec6177039..31b030933 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java @@ -188,7 +188,7 @@ public class UUIDHandler { } public static UUID getUUID(final String name) { - if (name == null) { + if (name == null || name.length() == 0) { return null; } // check online @@ -205,11 +205,9 @@ public class UUIDHandler { if (uuid != null) { return uuid; } - // Read from disk OR convert directly to offline UUID if (Settings.UUID_FROM_DISK || uuidWrapper instanceof OfflineUUIDWrapper) { - OfflinePlayer op = Bukkit.getOfflinePlayer(name); - uuid = UUIDHandler.uuidWrapper.getUUID(op); + uuid = UUIDHandler.uuidWrapper.getUUID(name); add(new StringWrapper(name), uuid); return uuid; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/DefaultUUIDWrapper.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/DefaultUUIDWrapper.java index 85d7be275..169c9bcde 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/DefaultUUIDWrapper.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/DefaultUUIDWrapper.java @@ -28,4 +28,9 @@ public class DefaultUUIDWrapper extends UUIDWrapper { return Bukkit.getPlayer(uuid); } + @Override + public UUID getUUID(String name) { + return Bukkit.getOfflinePlayer(name).getUniqueId(); + } + } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java index 77a76513d..d6a6ca107 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java @@ -56,4 +56,9 @@ public class OfflineUUIDWrapper extends UUIDWrapper { return null; } + @Override + public UUID getUUID(String name) { + return UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)); + } + } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/UUIDWrapper.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/UUIDWrapper.java index 8e37da72f..516309400 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/UUIDWrapper.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/UUIDWrapper.java @@ -9,6 +9,8 @@ public abstract class UUIDWrapper { public abstract UUID getUUID(Player player); public abstract UUID getUUID(OfflinePlayer player); + + public abstract UUID getUUID(String name); public abstract OfflinePlayer getOfflinePlayer(UUID uuid);