From 8074d041b8c4e3d3c76ff7edef6838f3a08f8c22 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sat, 19 Mar 2016 17:32:05 +1100 Subject: [PATCH] Fix "Cleaner reflection" breaking plugin. Also add method to sort plots by modification date. --- .../plotsquared/bukkit/chat/Reflection.java | 4 +- .../com/intellectualcrafters/plot/PS.java | 216 +++++------------- .../plot/util/ExpireManager.java | 5 + 3 files changed, 67 insertions(+), 158 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java b/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java index eb716f75a..eed7c47f4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java @@ -54,7 +54,7 @@ public final class Reflection { return _loadedNMSClasses.get(className); } - final String fullName = "net.minecraft.server." + getVersion() + className; + final String fullName = "net.minecraft.server." + getVersion() + "." + className; Class clazz; try { clazz = Class.forName(fullName); @@ -80,7 +80,7 @@ public final class Reflection { return _loadedOBCClasses.get(className); } - final String fullName = "org.bukkit.craftbukkit." + getVersion() + className; + final String fullName = "org.bukkit.craftbukkit." + getVersion() + "." + className; Class clazz; try { clazz = Class.forName(fullName); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/PS.java b/Core/src/main/java/com/intellectualcrafters/plot/PS.java index 7a8db0ba3..c3c49d344 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/PS.java @@ -923,6 +923,55 @@ public class PS { return result; } + @Deprecated + public ArrayList sortPlotsByTimestamp(final Collection plots) { + List unknown = new ArrayList<>(); + int hardmax = 256000; + int max = 0; + int overflowSize = 0; + for (final Plot plot : plots) { + final int hash = MathMan.getPositiveId(plot.hashCode()); + if (hash > max) { + if (hash >= hardmax) { + overflowSize++; + } else { + max = hash; + } + } + } + hardmax = Math.min(hardmax, max); + final Plot[] cache = new Plot[hardmax + 1]; + final List overflow = new ArrayList<>(overflowSize); + final ArrayList extra = new ArrayList<>(); + for (final Plot plot : plots) { + final int hash = MathMan.getPositiveId(plot.hashCode()); + if (hash < hardmax) { + if (hash >= 0) { + cache[hash] = plot; + } else { + extra.add(plot); + } + } else if ((Math.abs(plot.getId().x) > 15446) || (Math.abs(plot.getId().y) > 15446)) { + extra.add(plot); + } else { + overflow.add(plot); + } + } + final Plot[] overflowArray = overflow.toArray(new Plot[overflow.size()]); + sortPlotsByHash(overflowArray); + final ArrayList result = new ArrayList<>(cache.length + overflowArray.length); + for (final Plot plot : cache) { + if (plot != null) { + result.add(plot); + } + } + Collections.addAll(result, overflowArray); + for (final Plot plot : extra) { + result.add(plot); + } + return result; + } + /** * Sort plots by creation timestamp * @param input @@ -930,167 +979,20 @@ public class PS { * @return */ @Deprecated - public ArrayList sortPlotsByTimestamp(final Collection input) { + public List sortPlotsByModified(final Collection input) { List list; - if (input instanceof ArrayList) { + if (input instanceof List) { list = (List) input; } else { list = new ArrayList<>(input); } - long min = Integer.MAX_VALUE; - long max = 0; - final int size = list.size(); - final int limit = Math.min(1048576, size * 2); - for (final Plot plot : list) { - final long time = plot.getTimestamp(); - if (time < min) { - min = time; + Collections.sort(list, new Comparator() { + @Override + public int compare(Plot a, Plot b) { + return (int) Math.signum(ExpireManager.IMP.getTimestamp(a.owner) - ExpireManager.IMP.getTimestamp(b.owner)); } - if (time > max) { - max = time; - } - } - final long range = max - min; - try { - final ArrayList overflow = new ArrayList<>(); - Plot[] plots; - if ((range > limit) && (size > 1024)) { - plots = new Plot[limit]; - final int factor = (int) ((range / limit)); - for (Plot plot : list) { - int index = (int) (plot.getTimestamp() - min) / factor; - if (index < 0) { - index = 0; - } - if (index >= plots.length) { - overflow.add(plot); - continue; - } - Plot current = plots[index]; - while (true) { - if (current == null) { - plots[index] = plot; - break; - } - if (current.getTimestamp() > plot.getTimestamp()) { - plots[index] = plot; - plot = current; - } - index++; - if (index >= plots.length) { - overflow.add(plot); - break; - } - current = plots[index]; - } - } - } else if ((range < size) || (size < 1024)) { - final ArrayList result = new ArrayList<>(list); - Collections.sort(result, new Comparator() { - @Override - public int compare(final Plot a, final Plot b) { - if (a.getTimestamp() > b.getTimestamp()) { - return -1; - } else if (b.getTimestamp() > a.getTimestamp()) { - return 1; - } - return 0; - } - }); - return result; - } else if (min != 0) { - plots = new Plot[(int) range]; - for (Plot plot : list) { - int index = (int) (plot.getTimestamp() - min); - if (index >= plots.length) { - overflow.add(plot); - continue; - } - Plot current = plots[index]; - while (true) { - if (current == null) { - plots[index] = plot; - break; - } - if (current.getTimestamp() > plot.getTimestamp()) { - plots[index] = plot; - plot = current; - } - index++; - if (index >= plots.length) { - overflow.add(plot); - break; - } - current = plots[index]; - } - } - } else { - plots = new Plot[(int) range]; - for (Plot plot : list) { - int index = (int) (plot.getTimestamp()); - if (index >= plots.length) { - overflow.add(plot); - continue; - } - Plot current = plots[index]; - // Move everything along until a free spot is found - while (true) { - if (current == null) { - plots[index] = plot; - break; - } - if (current.getTimestamp() > plot.getTimestamp()) { - plots[index] = plot; - plot = current; - } - index++; - if (index >= plots.length) { - overflow.add(plot); - break; - } - current = plots[index]; - } - } - } - final ArrayList result = new ArrayList<>(size); - if (!overflow.isEmpty()) { - Collections.sort(overflow, new Comparator() { - @Override - public int compare(final Plot a, final Plot b) { - if (a.getTimestamp() > b.getTimestamp()) { - return -1; - } else if (b.getTimestamp() > a.getTimestamp()) { - return 1; - } - return 0; - } - }); - for (final Plot plot : overflow) { - result.add(plot); - } - } - for (int i = plots.length - 1; i >= 0; i--) { - if (plots[i] != null) { - result.add(plots[i]); - } - } - return result; - } catch (final Exception e) { - e.printStackTrace(); - final ArrayList result = new ArrayList<>(list); - Collections.sort(result, new Comparator() { - @Override - public int compare(final Plot a, final Plot b) { - if (a.getTimestamp() > b.getTimestamp()) { - return -1; - } else if (b.getTimestamp() > a.getTimestamp()) { - return 1; - } - return 0; - } - }); - return result; - } + }); + return list; } /** @@ -1179,6 +1081,8 @@ public class PS { case DISTANCE_FROM_ORIGIN: toReturn.addAll(sortPlotsByHash(map.get(area))); break; + case LAST_MODIFIED: + toReturn.addAll(sortPlotsByModified(map.get(area))); default: break; } @@ -2562,6 +2466,6 @@ public class PS { } public enum SortType { - CREATION_DATE, CREATION_DATE_TIMESTAMP, DISTANCE_FROM_ORIGIN + CREATION_DATE, CREATION_DATE_TIMESTAMP, LAST_MODIFIED, DISTANCE_FROM_ORIGIN } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java b/Core/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java index 2ae2213a8..af78b4302 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java @@ -46,6 +46,11 @@ public class ExpireManager { } } + public long getTimestamp(UUID uuid) { + Long value = dates_cache.get(uuid); + return value == null ? 0 : value; + } + public void confirmExpiry(final PlotPlayer pp) { if (Settings.AUTO_CLEAR_CONFIRMATION && plotsToDelete != null && !plotsToDelete.isEmpty() && pp.hasPermission("plots.admin.command.autoclear")) { final int num = plotsToDelete.size();