From 7659884e73cfdfb9b7392236286cc05f71b1b137 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 26 Feb 2016 01:17:07 +1100 Subject: [PATCH] Fixes Fixes #868 Fixes #778 Fixes attributes passing wrong key to persistent meta Fixes blob compatibility with JDBC driver for persistent meta Fix stackoverflow from countEntities under certain circumstances Minor cleanup --- .../bukkit/object/BukkitPlayer.java | 39 ++------ .../bukkit/util/BukkitChunkManager.java | 4 +- .../plotsquared/bukkit/util/SendChunk.java | 4 +- .../com/intellectualcrafters/plot/PS.java | 95 +++++-------------- .../plot/commands/Purge.java | 24 ++--- .../plot/database/SQLManager.java | 41 ++------ .../plot/object/PlotPlayer.java | 20 +++- .../sponge/object/SpongePlayer.java | 28 ------ 8 files changed, 78 insertions(+), 177 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java index 297ebdf7c..14f1b8776 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java @@ -1,16 +1,5 @@ package com.plotsquared.bukkit.object; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.Effect; -import org.bukkit.GameMode; -import org.bukkit.WeatherType; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.permissions.Permission; -import org.bukkit.plugin.PluginManager; - import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotPlayer; @@ -19,6 +8,16 @@ import com.intellectualcrafters.plot.util.PlotGamemode; import com.intellectualcrafters.plot.util.PlotWeather; import com.intellectualcrafters.plot.util.UUIDHandler; import com.plotsquared.bukkit.util.BukkitUtil; +import org.bukkit.Bukkit; +import org.bukkit.Effect; +import org.bukkit.GameMode; +import org.bukkit.WeatherType; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.permissions.Permission; +import org.bukkit.plugin.PluginManager; + +import java.util.UUID; public class BukkitPlayer extends PlotPlayer { @@ -133,24 +132,6 @@ public class BukkitPlayer extends PlotPlayer { return BukkitUtil.getLocationFull(player); } - @Override - public void setAttribute(String key) { - setPersistentMeta("attrib_" + key, new byte[]{(byte) 1}); - } - - @Override - public boolean getAttribute(String key) { - if (!hasPersistentMeta(key)) { - return false; - } - return getPersistentMeta("attrib_" + key)[0] == 1; - } - - @Override - public void removeAttribute(String key) { - removePersistentMeta("attrib_" + key); - } - @Override public void loadData() { if (!player.isOnline()) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java index 03b7b73a1..4e33b537f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java @@ -1075,7 +1075,9 @@ public class BukkitChunkManager extends ChunkManager { final Set chunks = new HashSet<>(); for (int X = bx; X <= tx; X++) { for (int Z = bz; Z <= tz; Z++) { - chunks.add(world.getChunkAt(X, Z)); + if (world.isChunkLoaded(X, Z)) { + chunks.add(world.getChunkAt(X, Z)); + } } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/SendChunk.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/SendChunk.java index 942905d66..89abaf630 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/SendChunk.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/SendChunk.java @@ -129,7 +129,9 @@ public class SendChunk { final World myworld = Bukkit.getWorld(worldname); final ArrayList chunks = new ArrayList<>(); for (final ChunkLoc loc : locs) { - chunks.add(myworld.getChunkAt(loc.x, loc.z)); + if (myworld.isChunkLoaded(loc.x, loc.z)) { + chunks.add(myworld.getChunkAt(loc.x, loc.z)); + } } sendChunk(chunks); } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/PS.java b/Core/src/main/java/com/intellectualcrafters/plot/PS.java index 090228f0d..7f312608d 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/PS.java @@ -1,38 +1,5 @@ package com.intellectualcrafters.plot; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.nio.file.Files; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import java.util.regex.Pattern; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - import com.intellectualcrafters.configuration.ConfigurationSection; import com.intellectualcrafters.configuration.MemorySection; import com.intellectualcrafters.configuration.file.YamlConfiguration; @@ -41,11 +8,7 @@ import com.intellectualcrafters.plot.commands.WE_Anywhere; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Configuration; import com.intellectualcrafters.plot.config.Settings; -import com.intellectualcrafters.plot.database.DBFunc; -import com.intellectualcrafters.plot.database.Database; -import com.intellectualcrafters.plot.database.MySQL; -import com.intellectualcrafters.plot.database.SQLManager; -import com.intellectualcrafters.plot.database.SQLite; +import com.intellectualcrafters.plot.database.*; import com.intellectualcrafters.plot.flag.AbstractFlag; import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.flag.FlagValue; @@ -53,41 +16,26 @@ import com.intellectualcrafters.plot.generator.GeneratorWrapper; import com.intellectualcrafters.plot.generator.HybridPlotWorld; import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; -import com.intellectualcrafters.plot.object.Location; -import com.intellectualcrafters.plot.object.Plot; -import com.intellectualcrafters.plot.object.PlotAnalysis; -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotCluster; -import com.intellectualcrafters.plot.object.PlotFilter; -import com.intellectualcrafters.plot.object.PlotId; -import com.intellectualcrafters.plot.object.PlotManager; -import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.object.RegionWrapper; -import com.intellectualcrafters.plot.object.RunnableVal; -import com.intellectualcrafters.plot.util.AbstractTitle; -import com.intellectualcrafters.plot.util.ChatManager; -import com.intellectualcrafters.plot.util.ChunkManager; -import com.intellectualcrafters.plot.util.CommentManager; -import com.intellectualcrafters.plot.util.EconHandler; -import com.intellectualcrafters.plot.util.EventUtil; -import com.intellectualcrafters.plot.util.ExpireManager; -import com.intellectualcrafters.plot.util.InventoryUtil; -import com.intellectualcrafters.plot.util.MainUtil; -import com.intellectualcrafters.plot.util.MathMan; -import com.intellectualcrafters.plot.util.PlotGamemode; -import com.intellectualcrafters.plot.util.PlotWeather; -import com.intellectualcrafters.plot.util.ReflectionUtils; -import com.intellectualcrafters.plot.util.SchematicHandler; -import com.intellectualcrafters.plot.util.SetQueue; -import com.intellectualcrafters.plot.util.SetupUtils; -import com.intellectualcrafters.plot.util.StringMan; -import com.intellectualcrafters.plot.util.TaskManager; -import com.intellectualcrafters.plot.util.UUIDHandler; -import com.intellectualcrafters.plot.util.WorldUtil; +import com.intellectualcrafters.plot.object.*; +import com.intellectualcrafters.plot.util.*; import com.intellectualcrafters.plot.util.area.QuadMap; import com.plotsquared.listener.WESubscriber; import com.sk89q.worldedit.WorldEdit; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; +import java.nio.file.Files; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.*; +import java.util.Map.Entry; +import java.util.regex.Pattern; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + /** * An implementation of the core, * with a static getter for easy access @@ -1874,7 +1822,14 @@ public class PS { TASK = null; database = null; // Validate that all data in the db is correct - DBFunc.validatePlots(getPlots()); + final HashSet plots = new HashSet<>(); + foreachPlotRaw(new RunnableVal() { + @Override + public void run(Plot value) { + plots.add(value); + } + }); + DBFunc.validatePlots(plots); // Close the connection DBFunc.close(); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Purge.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Purge.java index 2b623401d..8972aa649 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Purge.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Purge.java @@ -113,7 +113,7 @@ public class Purge extends SubCommand { } } } - final HashSet toDelete = new HashSet<>(); + final HashSet toDelete = new HashSet<>(); for (Plot plot : PS.get().getBasePlots()) { if (world != null && !plot.getArea().worldname.equalsIgnoreCase(world)) { continue; @@ -134,10 +134,7 @@ public class Purge extends SubCommand { continue; } for (Plot current : plot.getConnectedPlots()) { - final int DBid = DBFunc.getId(current); - if (DBid != Integer.MAX_VALUE) { - toDelete.add(DBid); - } + toDelete.add(current); } } if (PS.get().plots_tmp != null) { @@ -160,10 +157,7 @@ public class Purge extends SubCommand { if (unknown && UUIDHandler.getName(plot.owner) != null) { continue; } - final int DBid = DBFunc.getId(plot); - if (DBid != Integer.MAX_VALUE) { - toDelete.add(DBid); - } + toDelete.add(plot); } } } @@ -175,8 +169,16 @@ public class Purge extends SubCommand { CmdConfirm.addPending(plr, cmd, new Runnable() { @Override public void run() { - DBFunc.purgeIds(toDelete); - C.PURGE_SUCCESS.send(plr, toDelete.size() + ""); + HashSet ids = new HashSet(); + for (Plot plot : toDelete) { + if (plot.temp != Integer.MAX_VALUE) { + ids.add(plot.temp); + PlotArea area = plot.getArea(); + plot.getArea().removePlot(plot.getId()); + } + } + DBFunc.purgeIds(ids); + C.PURGE_SUCCESS.send(plr, ids.size() + "/" + (toDelete.size())); } }); return true; diff --git a/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index f2fa954b2..c5352e234 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -25,38 +25,15 @@ import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.flag.FlagManager; -import com.intellectualcrafters.plot.object.BlockLoc; -import com.intellectualcrafters.plot.object.Plot; -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotCluster; -import com.intellectualcrafters.plot.object.PlotId; -import com.intellectualcrafters.plot.object.PlotSettings; -import com.intellectualcrafters.plot.object.RunnableVal; +import com.intellectualcrafters.plot.object.*; import com.intellectualcrafters.plot.object.comment.PlotComment; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.StringMan; import com.intellectualcrafters.plot.util.TaskManager; -import java.sql.Blob; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.sql.*; +import java.util.*; import java.util.Map.Entry; -import java.util.Queue; -import java.util.Set; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicInteger; @@ -2423,16 +2400,14 @@ public class SQLManager implements AbstractDB { addPlayerTask(uuid, new UniqueStatement("addPersistentMeta") { @Override public void set(final PreparedStatement stmt) throws SQLException { - Blob blob = connection.createBlob(); - blob.setBytes(1, meta); if (replace) { - stmt.setBlob(1, blob); + stmt.setBytes(1, meta); stmt.setString(2, uuid.toString()); stmt.setString(3, key); } else { stmt.setString(1, uuid.toString()); stmt.setString(2, key); - stmt.setBlob(3, blob); + stmt.setBytes(3, meta); } } @@ -2487,8 +2462,7 @@ public class SQLManager implements AbstractDB { while (resultSet.next()) { String key = resultSet.getString("key"); - Blob rawValue = resultSet.getBlob("value"); - byte[] bytes = rawValue.getBytes(1, (int) rawValue.length()); + byte[] bytes = resultSet.getBytes("value"); metaMap.put(key, bytes); } @@ -3019,9 +2993,12 @@ public class SQLManager implements AbstractDB { } for (final Entry> entry : database.entrySet()) { + String worldname = entry.getKey(); final HashMap map = entry.getValue(); if (!map.isEmpty()) { for (final Entry entry2 : map.entrySet()) { + Plot plot = entry2.getValue(); + System.out.println("Plot: " + plot + " | " + worldname); PS.debug("$1Plot was deleted: " + entry2.getValue() + "// TODO implement this when sure safe"); } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java index 6ef6e4ba7..0519b1eaf 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java @@ -304,20 +304,30 @@ public abstract class PlotPlayer implements CommandCaller { * - For session only data use meta * @param key */ - public abstract void setAttribute(final String key); - + public void setAttribute(final String key) { + setPersistentMeta("attrib_" + key, new byte[]{(byte) 1}); + } + + /** * The attribute will be either true or false * @param key */ - public abstract boolean getAttribute(final String key); + public boolean getAttribute(final String key) { + if (!hasPersistentMeta("attrib_" + key)) { + return false; + } + return getPersistentMeta("attrib_" + key)[0] == 1; + } /** * Remove an attribute from a player * @param key */ - public abstract void removeAttribute(final String key); - + public void removeAttribute(final String key) { + removePersistentMeta("attrib_" + key); + } + /** * Set the player's local weather * @param weather diff --git a/Sponge/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java b/Sponge/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java index af32c40ad..9dc8935c2 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java @@ -5,7 +5,6 @@ import com.intellectualcrafters.plot.commands.RequiredType; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.PlotGamemode; import com.intellectualcrafters.plot.util.PlotWeather; import com.intellectualcrafters.plot.util.UUIDHandler; @@ -147,33 +146,6 @@ public class SpongePlayer extends PlotPlayer { throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); } - @Override - public void setAttribute(String key) { - key = "plotsquared_user_attributes." + key; - if ((EconHandler.manager == null) || player.hasPermission("plotsquared_user_attributes.*")) { - setMeta(key, true); - return; - } - EconHandler.manager.setPermission(getName(), key, true); - } - - @Override - public boolean getAttribute(String key) { - key = "plotsquared_user_attributes." + key; - if ((EconHandler.manager == null) || player.hasPermission("plotsquared_user_attributes.*")) { - final Object v = getMeta(key); - return v == null ? false : (Boolean) v; - } - return player.hasPermission(key); - } - - @Override - public void removeAttribute(String key) { - key = "plotsquared_user_attributes." + key; - EconHandler.manager.setPermission(getName(), key, false); - deleteMeta(key); - } - @Override public void setWeather(final PlotWeather weather) { // TODO Auto-generated method stub