diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/uuid/SQLUUIDHandler.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/uuid/SQLUUIDHandler.java index 1c9a30bba..fc366c305 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/uuid/SQLUUIDHandler.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/uuid/SQLUUIDHandler.java @@ -71,96 +71,85 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation { if (!super.startCaching(whenDone)) { return false; } - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - try { - HashBiMap toAdd = - HashBiMap.create(new HashMap()); - try (PreparedStatement statement = getConnection() - .prepareStatement("SELECT `uuid`, `username` FROM `usercache`"); - ResultSet resultSet = statement.executeQuery()) { - while (resultSet.next()) { - StringWrapper username = - new StringWrapper(resultSet.getString("username")); - UUID uuid = UUID.fromString(resultSet.getString("uuid")); - toAdd.put(new StringWrapper(username.value), uuid); - } + TaskManager.runTaskAsync(() -> { + try { + HashBiMap toAdd = + HashBiMap.create(new HashMap<>()); + try (PreparedStatement statement = getConnection() + .prepareStatement("SELECT `uuid`, `username` FROM `usercache`"); + ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { + StringWrapper username = + new StringWrapper(resultSet.getString("username")); + UUID uuid = UUID.fromString(resultSet.getString("uuid")); + toAdd.put(new StringWrapper(username.value), uuid); } - add(toAdd); - // This should be called as long as there are some unknown plots - final ArrayDeque toFetch = new ArrayDeque<>(); - for (UUID u : UUIDHandler.getAllUUIDS()) { - if (!uuidExists(u)) { - toFetch.add(u); - } + } + add(toAdd); + // This should be called as long as there are some unknown plots + final ArrayDeque toFetch = new ArrayDeque<>(); + for (UUID u : UUIDHandler.getAllUUIDS()) { + if (!uuidExists(u)) { + toFetch.add(u); } - if (toFetch.isEmpty()) { + } + if (toFetch.isEmpty()) { + if (whenDone != null) { + whenDone.run(); + } + return; + } + FileUUIDHandler fileHandler = + new FileUUIDHandler(SQLUUIDHandler.this.uuidWrapper); + fileHandler.startCaching(() -> { + // If the file based UUID handler didn't cache it, then we can't cache offline mode + // Also, trying to cache based on files again, is useless as that's what the file based uuid cacher does + if (Settings.UUID.OFFLINE) { if (whenDone != null) { whenDone.run(); } return; } - FileUUIDHandler fileHandler = - new FileUUIDHandler(SQLUUIDHandler.this.uuidWrapper); - fileHandler.startCaching(new Runnable() { - @Override public void run() { - // If the file based UUID handler didn't cache it, then we can't cache offline mode - // Also, trying to cache based on files again, is useless as that's what the file based uuid cacher does - if (Settings.UUID.OFFLINE) { - if (whenDone != null) { - whenDone.run(); - } - return; - } - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - while (!toFetch.isEmpty()) { - try { - for (int i = 0; - i < Math.min(500, toFetch.size()); i++) { - UUID uuid = toFetch.pop(); - HttpURLConnection connection = - (HttpURLConnection) new URL( - SQLUUIDHandler.this.PROFILE_URL + uuid - .toString().replace("-", "")) - .openConnection(); - try (InputStream con = connection - .getInputStream()) { - InputStreamReader reader = - new InputStreamReader(con); - JSONObject response = - (JSONObject) SQLUUIDHandler.this.jsonParser - .parse(reader); - String name = (String) response.get("name"); - if (name != null) { - add(new StringWrapper(name), uuid); - } - } - connection.disconnect(); - } - } catch (IOException | ParseException e) { - PlotSquared.debug( - "Invalid response from Mojang: Some UUIDs will be cached later. (`unknown` until then or player joins)"); - } - try { - Thread.sleep(INTERVAL * 50); - } catch (InterruptedException e) { - e.printStackTrace(); - break; + TaskManager.runTaskAsync(() -> { + while (!toFetch.isEmpty()) { + try { + for (int i = 0; i < Math.min(500, toFetch.size()); i++) { + UUID uuid = toFetch.pop(); + HttpURLConnection connection = (HttpURLConnection) new URL( + SQLUUIDHandler.this.PROFILE_URL + uuid.toString() + .replace("-", "")).openConnection(); + try (InputStream con = connection.getInputStream()) { + InputStreamReader reader = new InputStreamReader(con); + JSONObject response = + (JSONObject) SQLUUIDHandler.this.jsonParser + .parse(reader); + String name = (String) response.get("name"); + if (name != null) { + add(new StringWrapper(name), uuid); } } - if (whenDone != null) { - whenDone.run(); - } - return; + connection.disconnect(); } - }); + } catch (IOException | ParseException e) { + PlotSquared.debug( + "Invalid response from Mojang: Some UUIDs will be cached later. (`unknown` until then or player joins)"); + } + try { + Thread.sleep(INTERVAL * 50); + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } } + if (whenDone != null) { + whenDone.run(); + } + return; }); - } catch (SQLException e) { - throw new SQLUUIDHandlerException("Couldn't select :s", e); - } + }); + } catch (SQLException e) { + throw new SQLUUIDHandlerException("Couldn't select :s", e); } }); return true; @@ -172,34 +161,32 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation { if (ifFetch == null) { return; } - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - try { - URL url = new URL(SQLUUIDHandler.this.PROFILE_URL); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setUseCaches(false); - connection.setDoInput(true); - connection.setDoOutput(true); - String body = JSONArray.toJSONString(Collections.singletonList(name)); - OutputStream stream = connection.getOutputStream(); - stream.write(body.getBytes()); - stream.flush(); - stream.close(); - JSONArray array = (JSONArray) SQLUUIDHandler.this.jsonParser - .parse(new InputStreamReader(connection.getInputStream())); - JSONObject jsonProfile = (JSONObject) array.get(0); - String id = (String) jsonProfile.get("id"); - String name = (String) jsonProfile.get("name"); - ifFetch.value = UUID.fromString( - id.substring(0, 8) + '-' + id.substring(8, 12) + '-' + id.substring(12, 16) - + '-' + id.substring(16, 20) + '-' + id.substring(20, 32)); - } catch (IOException | ParseException e) { - e.printStackTrace(); - } - TaskManager.runTask(ifFetch); + TaskManager.runTaskAsync(() -> { + try { + URL url = new URL(SQLUUIDHandler.this.PROFILE_URL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setUseCaches(false); + connection.setDoInput(true); + connection.setDoOutput(true); + String body = JSONArray.toJSONString(Collections.singletonList(name)); + OutputStream stream = connection.getOutputStream(); + stream.write(body.getBytes()); + stream.flush(); + stream.close(); + JSONArray array = (JSONArray) SQLUUIDHandler.this.jsonParser + .parse(new InputStreamReader(connection.getInputStream())); + JSONObject jsonProfile = (JSONObject) array.get(0); + String id = (String) jsonProfile.get("id"); + String name1 = (String) jsonProfile.get("name"); + ifFetch.value = UUID.fromString( + id.substring(0, 8) + '-' + id.substring(8, 12) + '-' + id.substring(12, 16) + + '-' + id.substring(16, 20) + '-' + id.substring(20, 32)); + } catch (IOException | ParseException e) { + e.printStackTrace(); } + TaskManager.runTask(ifFetch); }); } @@ -215,18 +202,16 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation { @Override public boolean add(final StringWrapper name, final UUID uuid) { // Ignoring duplicates if (super.add(name, uuid)) { - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - try (PreparedStatement statement = getConnection().prepareStatement( - "REPLACE INTO usercache (`uuid`, `username`) VALUES(?, ?)")) { - statement.setString(1, uuid.toString()); - statement.setString(2, name.toString()); - statement.execute(); - PlotSquared - .debug(C.PREFIX + "&cAdded '&6" + uuid + "&c' - '&6" + name + "&c'"); - } catch (SQLException e) { - e.printStackTrace(); - } + TaskManager.runTaskAsync(() -> { + try (PreparedStatement statement = getConnection().prepareStatement( + "REPLACE INTO usercache (`uuid`, `username`) VALUES(?, ?)")) { + statement.setString(1, uuid.toString()); + statement.setString(2, name.toString()); + statement.execute(); + PlotSquared + .debug(C.PREFIX + "&cAdded '&6" + uuid + "&c' - '&6" + name + "&c'"); + } catch (SQLException e) { + e.printStackTrace(); } }); return true; @@ -239,18 +224,16 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation { */ @Override public void rename(final UUID uuid, final StringWrapper name) { super.rename(uuid, name); - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - try (PreparedStatement statement = getConnection() - .prepareStatement("UPDATE usercache SET `username`=? WHERE `uuid`=?")) { - statement.setString(1, name.value); - statement.setString(2, uuid.toString()); - statement.execute(); - PlotSquared.debug( - C.PREFIX + "Name change for '" + uuid + "' to '" + name.value + '\''); - } catch (SQLException e) { - e.printStackTrace(); - } + TaskManager.runTaskAsync(() -> { + try (PreparedStatement statement = getConnection() + .prepareStatement("UPDATE usercache SET `username`=? WHERE `uuid`=?")) { + statement.setString(1, name.value); + statement.setString(2, uuid.toString()); + statement.execute(); + PlotSquared.debug( + C.PREFIX + "Name change for '" + uuid + "' to '" + name.value + '\''); + } catch (SQLException e) { + e.printStackTrace(); } }); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java index 1db55bf28..b73fd9426 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java @@ -214,11 +214,8 @@ import java.util.zip.ZipInputStream; } // Economy if (Settings.Enabled_Components.ECONOMY) { - TaskManager.runTask(new Runnable() { - @Override public void run() { - EconHandler.manager = PlotSquared.this.IMP.getEconomyHandler(); - } - }); + TaskManager.runTask( + () -> EconHandler.manager = PlotSquared.this.IMP.getEconomyHandler()); } /* // Check for updates @@ -247,21 +244,19 @@ import java.util.zip.ZipInputStream; this.IMP.setGenerator(world); } } - TaskManager.runTaskLater(new Runnable() { - @Override public void run() { - for (String world : section.getKeys(false)) { - if (world.equals("CheckingPlotSquaredGenerator")) { - continue; - } - if (!WorldUtil.IMP.isWorld(world) && !world.equals("*")) { - debug("&c`" + world + "` was not properly loaded - " + IMP - .getPluginName() + " will now try to load it properly: "); - debug( - "&8 - &7Are you trying to delete this world? Remember to remove it from the settings.yml, bukkit.yml and multiverse worlds.yml"); - debug( - "&8 - &7Your world management plugin may be faulty (or non existent)"); - PlotSquared.this.IMP.setGenerator(world); - } + TaskManager.runTaskLater(() -> { + for (String world : section.getKeys(false)) { + if (world.equals("CheckingPlotSquaredGenerator")) { + continue; + } + if (!WorldUtil.IMP.isWorld(world) && !world.equals("*")) { + debug("&c`" + world + "` was not properly loaded - " + IMP + .getPluginName() + " will now try to load it properly: "); + debug( + "&8 - &7Are you trying to delete this world? Remember to remove it from the settings.yml, bukkit.yml and multiverse worlds.yml"); + debug( + "&8 - &7Your world management plugin may be faulty (or non existent)"); + PlotSquared.this.IMP.setGenerator(world); } } }, 1); @@ -342,25 +337,21 @@ import java.util.zip.ZipInputStream; } private void startUuidCatching() { - TaskManager.runTaskLater(new Runnable() { - @Override public void run() { - debug("Starting UUID caching"); - UUIDHandler.startCaching(new Runnable() { - @Override public void run() { - UUIDHandler.add(new StringWrapper("*"), DBFunc.EVERYONE); - foreachPlotRaw(new RunnableVal() { - @Override public void run(Plot plot) { - if (plot.hasOwner() && plot.temp != -1) { - if (UUIDHandler.getName(plot.owner) == null) { - UUIDHandler.implementation.unknown.add(plot.owner); - } - } + TaskManager.runTaskLater(() -> { + debug("Starting UUID caching"); + UUIDHandler.startCaching(() -> { + UUIDHandler.add(new StringWrapper("*"), DBFunc.EVERYONE); + foreachPlotRaw(new RunnableVal() { + @Override public void run(Plot plot) { + if (plot.hasOwner() && plot.temp != -1) { + if (UUIDHandler.getName(plot.owner) == null) { + UUIDHandler.implementation.unknown.add(plot.owner); } - }); - startExpiryTasks(); + } } }); - } + startExpiryTasks(); + }); }, 20); } @@ -495,11 +486,8 @@ import java.util.zip.ZipInputStream; if (this.plots_tmp == null) { this.plots_tmp = new HashMap<>(); } - HashMap map = this.plots_tmp.get(area.toString()); - if (map == null) { - map = new HashMap<>(); - this.plots_tmp.put(area.toString(), map); - } + HashMap map = + this.plots_tmp.computeIfAbsent(area.toString(), k -> new HashMap<>()); for (Plot plot : area.getPlots()) { map.put(plot.getId(), plot); } @@ -567,11 +555,7 @@ import java.util.zip.ZipInputStream; result.add(plot); } } - Collections.sort(overflow, new Comparator() { - @Override public int compare(Plot a, Plot b) { - return a.hashCode() - b.hashCode(); - } - }); + overflow.sort(Comparator.comparingInt(Plot::hashCode)); result.addAll(overflow); return result; } @@ -581,12 +565,8 @@ import java.util.zip.ZipInputStream; * * @param plots the collection of plots to sort * @return the sorted collection - * @deprecated Unchecked, please use - * {@link #sortPlots(Collection, SortType, PlotArea)} which has - * additional checks before calling this */ - // TODO: Re-evaluate deprecation of this, as it's being used internally - @Deprecated public ArrayList sortPlotsByHash(Collection plots) { + private ArrayList sortPlotsByHash(Collection plots) { int hardmax = 256000; int max = 0; int overflowSize = 0; @@ -618,7 +598,7 @@ import java.util.zip.ZipInputStream; overflow.add(plot); } } - Plot[] overflowArray = overflow.toArray(new Plot[overflow.size()]); + Plot[] overflowArray = overflow.toArray(new Plot[0]); sortPlotsByHash(overflowArray); ArrayList result = new ArrayList<>(cache.length + overflowArray.length); for (Plot plot : cache) { @@ -627,9 +607,7 @@ import java.util.zip.ZipInputStream; } } Collections.addAll(result, overflowArray); - for (Plot plot : extra) { - result.add(plot); - } + result.addAll(extra); return result; } @@ -638,8 +616,7 @@ import java.util.zip.ZipInputStream; * * @param input an array of plots to sort */ - // TODO: Re-evaluate deprecation of this, as it's being used internally - @Deprecated public void sortPlotsByHash(Plot[] input) { + private void sortPlotsByHash(Plot[] input) { List[] bucket = new ArrayList[32]; for (int i = 0; i < bucket.length; i++) { bucket[i] = new ArrayList<>(); @@ -648,26 +625,25 @@ import java.util.zip.ZipInputStream; int placement = 1; while (!maxLength) { maxLength = true; - for (Plot i : input) { - int tmp = MathMan.getPositiveId(i.hashCode()) / placement; - bucket[tmp & 31].add(i); + for (Plot plot : input) { + int tmp = MathMan.getPositiveId(plot.hashCode()) / placement; + bucket[tmp & 31].add(plot); if (maxLength && tmp > 0) { maxLength = false; } } int a = 0; - for (int b = 0; b < 32; b++) { - for (Plot i : bucket[b]) { - input[a++] = i; + for (int i = 0; i < 32; i++) { + for (Plot plot : bucket[i]) { + input[a++] = plot; } - bucket[b].clear(); + bucket[i].clear(); } placement *= 32; } } - // TODO: Re-evaluate deprecation of this, as it's being used internally - @Deprecated public ArrayList sortPlotsByTimestamp(Collection plots) { + private ArrayList sortPlotsByTimestamp(Collection plots) { int hardMax = 256000; int max = 0; int overflowSize = 0; @@ -699,7 +675,7 @@ import java.util.zip.ZipInputStream; overflow.add(plot); } } - Plot[] overflowArray = overflow.toArray(new Plot[overflow.size()]); + Plot[] overflowArray = overflow.toArray(new Plot[0]); sortPlotsByHash(overflowArray); ArrayList result = new ArrayList<>(cache.length + overflowArray.length); for (Plot plot : cache) { @@ -708,9 +684,7 @@ import java.util.zip.ZipInputStream; } } Collections.addAll(result, overflowArray); - for (Plot plot : extra) { - result.add(plot); - } + result.addAll(extra); return result; } @@ -719,29 +693,18 @@ import java.util.zip.ZipInputStream; * * @param input * @return - * @deprecated Unchecked, use {@link #sortPlots(Collection, SortType, PlotArea)} instead which will call this after checks */ - // TODO: Re-evaluate deprecation of this, as it's being used internally - @Deprecated public List sortPlotsByModified(Collection input) { + private List sortPlotsByModified(Collection input) { List list; if (input instanceof List) { list = (List) input; } else { list = new ArrayList<>(input); } - Collections.sort(list, new Comparator() { - @Override public int compare(Plot a, Plot b) { - return Long.compare(ExpireManager.IMP.getTimestamp(a.owner), - ExpireManager.IMP.getTimestamp(b.owner)); - } - }); + list.sort(Comparator.comparingLong(a -> ExpireManager.IMP.getTimestamp(a.owner))); return list; } - public ArrayList sortPlots(Collection plots) { - return sortPlots(plots, SortType.DISTANCE_FROM_ORIGIN, null); - } - /** * Sort a collection of plots by world (with a priority world), then * by hashcode. @@ -764,7 +727,7 @@ import java.util.zip.ZipInputStream; } } else { for (PlotArea area : plotAreaManager.getAllPlotAreas()) { - map.put(area, new ArrayList(0)); + map.put(area, new ArrayList<>(0)); } Collection lastList = null; PlotArea lastWorld = null; @@ -779,17 +742,15 @@ import java.util.zip.ZipInputStream; } } List areas = Arrays.asList(plotAreaManager.getAllPlotAreas()); - Collections.sort(areas, new Comparator() { - @Override public int compare(PlotArea a, PlotArea b) { - if (priorityArea != null) { - if (a.equals(priorityArea)) { - return -1; - } else if (b.equals(priorityArea)) { - return 1; - } + areas.sort((a, b) -> { + if (priorityArea != null) { + if (a.equals(priorityArea)) { + return -1; + } else if (b.equals(priorityArea)) { + return 1; } - return a.hashCode() - b.hashCode(); } + return a.hashCode() - b.hashCode(); }); ArrayList toReturn = new ArrayList<>(plots.size()); for (PlotArea area : areas) { @@ -868,11 +829,8 @@ import java.util.zip.ZipInputStream; String world = entry.getKey(); PlotArea area = getPlotArea(world, null); if (area == null) { - HashMap map = this.plots_tmp.get(world); - if (map == null) { - map = new HashMap<>(); - this.plots_tmp.put(world, map); - } + HashMap map = + this.plots_tmp.computeIfAbsent(world, k -> new HashMap<>()); map.putAll(entry.getValue()); } else { for (Plot plot : entry.getValue().values()) { @@ -1025,11 +983,8 @@ import java.util.zip.ZipInputStream; } public boolean hasPlot(final UUID uuid) { - for (final PlotArea area : plotAreaManager.getAllPlotAreas()) { - if (area.hasPlot(uuid)) - return true; - } - return false; + return Arrays.stream(plotAreaManager.getAllPlotAreas()) + .anyMatch(area -> area.hasPlot(uuid)); } public Set getBasePlots(final UUID uuid) { @@ -1918,11 +1873,8 @@ import java.util.zip.ZipInputStream; } public int getPlotCount() { - int count = 0; - for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) { - count += area.getPlotCount(); - } - return count; + return Arrays.stream(this.plotAreaManager.getAllPlotAreas()).mapToInt(PlotArea::getPlotCount) + .sum(); } public Set getPlotAreas() { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java index 95166aa4a..6d8dc9c22 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java @@ -15,6 +15,8 @@ import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.stream.Collectors; +import java.util.stream.IntStream; @CommandDeclaration(command = "set", description = "Set a plot value", aliases = {"s"}, usage = "/plot set ", permission = "plots.set", @@ -100,11 +102,7 @@ import java.util.HashSet; current.setComponent(component, bucket); } MainUtil.sendMessage(player, C.GENERATING_COMPONENT); - GlobalBlockQueue.IMP.addTask(new Runnable() { - @Override public void run() { - plot.removeRunning(); - } - }); + GlobalBlockQueue.IMP.addTask(plot::removeRunning); return true; } } @@ -114,8 +112,8 @@ import java.util.HashSet; } public boolean noArgs(PlotPlayer player) { - ArrayList newValues = new ArrayList<>(); - newValues.addAll(Arrays.asList("biome", "alias", "home", "flag")); + ArrayList newValues = + new ArrayList<>(Arrays.asList("biome", "alias", "home", "flag")); Plot plot = player.getCurrentPlot(); if (plot != null) { newValues.addAll( @@ -153,13 +151,12 @@ import java.util.HashSet; // flag Flag flag = FlagManager.getFlag(args[0].toLowerCase()); if (Flags.getFlags().contains(flag)) { - StringBuilder a = new StringBuilder(); + String a = ""; if (args.length > 1) { - for (int x = 1; x < args.length; x++) { - a.append(" ").append(args[x]); - } + a = IntStream.range(1, args.length).mapToObj(x -> " " + args[x]) + .collect(Collectors.joining()); } - MainCommand.onCommand(player, ("flag set " + args[0] + a.toString()).split(" ")); + MainCommand.onCommand(player, ("flag set " + args[0] + a).split(" ")); return true; } return noArgs(player); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java index f1ca31b4d..6957e858a 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java @@ -87,7 +87,7 @@ public abstract class SchematicHandler { } else { MainUtil.sendMessage(null, "&7 - &a success: " + plot.getId()); } - TaskManager.runTask(() -> THIS.run()); + TaskManager.runTask(THIS::run); }); } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/expiry/ExpireManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/expiry/ExpireManager.java index 84f86daa2..4614cfe25 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/expiry/ExpireManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/expiry/ExpireManager.java @@ -97,28 +97,26 @@ public class ExpireManager { Iterator iter = plotsToDelete.iterator(); final Plot current = iter.next(); if (!isExpired(new ArrayDeque<>(tasks), current).isEmpty()) { - TaskManager.runTask(new Runnable() { - @Override public void run() { - pp.setMeta("ignoreExpireTask", true); - pp.teleport(current.getCenter()); - pp.deleteMeta("ignoreExpireTask"); - PlotMessage msg = new PlotMessage().text( - num + " " + (num > 1 ? "plots are" : "plot is") + " expired: ") - .color("$1").text(current.toString()).color("$2") - .suggest("/plot list expired").tooltip("/plot list expired") - //.text("\n - ").color("$3").text("Delete all (/plot delete expired)").color("$2").command("/plot delete expired") - .text("\n - ").color("$3").text("Delete this (/plot delete)") - .color("$2").suggest("/plot delete").tooltip("/plot delete") - .text("\n - ").color("$3").text("Remind later (/plot set keep 1d)") - .color("$2").suggest("/plot set keep 1d") - .tooltip("/plot set keep 1d").text("\n - ").color("$3") - .text("Keep this (/plot set keep true)").color("$2") - .suggest("/plot set keep true").tooltip("/plot set keep true") - .text("\n - ").color("$3").text("Don't show me this").color("$2") - .suggest("/plot toggle clear-confirmation") - .tooltip("/plot toggle clear-confirmation"); - msg.send(pp); - } + TaskManager.runTask(() -> { + pp.setMeta("ignoreExpireTask", true); + pp.teleport(current.getCenter()); + pp.deleteMeta("ignoreExpireTask"); + PlotMessage msg = new PlotMessage().text( + num + " " + (num > 1 ? "plots are" : "plot is") + " expired: ") + .color("$1").text(current.toString()).color("$2") + .suggest("/plot list expired").tooltip("/plot list expired") + //.text("\n - ").color("$3").text("Delete all (/plot delete expired)").color("$2").command("/plot delete expired") + .text("\n - ").color("$3").text("Delete this (/plot delete)") + .color("$2").suggest("/plot delete").tooltip("/plot delete") + .text("\n - ").color("$3").text("Remind later (/plot set keep 1d)") + .color("$2").suggest("/plot set keep 1d") + .tooltip("/plot set keep 1d").text("\n - ").color("$3") + .text("Keep this (/plot set keep true)").color("$2") + .suggest("/plot set keep true").tooltip("/plot set keep true") + .text("\n - ").color("$3").text("Don't show me this").color("$2") + .suggest("/plot toggle clear-confirmation") + .tooltip("/plot toggle clear-confirmation"); + msg.send(pp); }); return; } else { @@ -219,12 +217,7 @@ public class ExpireManager { public ArrayDeque getTasks(PlotArea area) { ArrayDeque queue = new ArrayDeque<>(tasks); - Iterator iter = queue.iterator(); - while (iter.hasNext()) { - if (!iter.next().applies(area)) { - iter.remove(); - } - } + queue.removeIf(expiryTask -> !expiryTask.applies(area)); return queue; } @@ -254,7 +247,7 @@ public class ExpireManager { } this.running = 2; final ConcurrentLinkedDeque plots = - new ConcurrentLinkedDeque(PlotSquared.get().getPlots()); + new ConcurrentLinkedDeque<>(PlotSquared.get().getPlots()); TaskManager.runTaskAsync(new Runnable() { @Override public void run() { final Runnable task = this; @@ -278,11 +271,7 @@ public class ExpireManager { } for (ExpiryTask expiryTask : expired) { if (!expiryTask.needsAnalysis()) { - expiredTask.run(newPlot, new Runnable() { - @Override public void run() { - TaskManager.IMP.taskLaterAsync(task, 1); - } - }, expiryTask.requiresConfirmation()); + expiredTask.run(newPlot, () -> TaskManager.IMP.taskLaterAsync(task, 1), expiryTask.requiresConfirmation()); return; } } @@ -291,26 +280,18 @@ public class ExpireManager { @Override public void run(final PlotAnalysis changed) { passesComplexity(changed, expired, new RunnableVal() { @Override public void run(Boolean confirmation) { - expiredTask.run(newPlot, new Runnable() { - @Override public void run() { - TaskManager.IMP.taskLaterAsync(task, 1); - } - }, confirmation); - } - }, new Runnable() { - @Override public void run() { - FlagManager - .addPlotFlag(newPlot, Flags.ANALYSIS, changed.asList()); - TaskManager.runTaskLaterAsync(task, 20); + expiredTask.run(newPlot, + () -> TaskManager.IMP.taskLaterAsync(task, 1), confirmation); } + }, () -> { + FlagManager + .addPlotFlag(newPlot, Flags.ANALYSIS, changed.asList()); + TaskManager.runTaskLaterAsync(task, 20); }); } }; - final Runnable doAnalysis = new Runnable() { - @Override public void run() { - HybridUtils.manager.analyzePlot(newPlot, handleAnalysis); - } - }; + final Runnable doAnalysis = + () -> HybridUtils.manager.analyzePlot(newPlot, handleAnalysis); PlotAnalysis analysis = newPlot.getComplexity(null); if (analysis != null) { @@ -318,11 +299,7 @@ public class ExpireManager { @Override public void run(Boolean value) { doAnalysis.run(); } - }, new Runnable() { - @Override public void run() { - TaskManager.IMP.taskLaterAsync(task, 1); - } - }); + }, () -> TaskManager.IMP.taskLaterAsync(task, 1)); } else { doAnalysis.run(); } @@ -330,12 +307,10 @@ public class ExpireManager { } if (plots.isEmpty()) { ExpireManager.this.running = 3; - TaskManager.runTaskLater(new Runnable() { - @Override public void run() { - if (ExpireManager.this.running == 3) { - ExpireManager.this.running = 2; - runTask(expiredTask); - } + TaskManager.runTaskLater(() -> { + if (ExpireManager.this.running == 3) { + ExpireManager.this.running = 2; + runTask(expiredTask); } }, 86400000); } else { @@ -363,7 +338,7 @@ public class ExpireManager { } public HashSet getPendingExpired() { - return plotsToDelete == null ? new HashSet() : plotsToDelete; + return plotsToDelete == null ? new HashSet<>() : plotsToDelete; } public void deleteWithMessage(Plot plot, Runnable whenDone) {