From b7aeeb151d0f70a3b24777f1e8e6329204dbcac5 Mon Sep 17 00:00:00 2001 From: matt <4009945+MattBDev@users.noreply.github.com> Date: Tue, 19 Feb 2019 18:39:43 -0500 Subject: [PATCH 1/2] Replace RunnableVal with java Consumer Signed-off-by: matt <4009945+MattBDev@users.noreply.github.com> --- .../plotsquared/commands/Argument.java | 8 +- .../plotsquared/plot/PlotSquared.java | 116 +++++++----------- .../plotsquared/plot/commands/Buy.java | 36 +++--- .../plotsquared/plot/commands/Owner.java | 4 +- .../plotsquared/plot/commands/Reload.java | 86 +++++++------ .../plotsquared/plot/commands/Trust.java | 22 ++-- .../plotsquared/plot/flag/Flags.java | 60 ++++----- .../plotsquared/plot/object/Plot.java | 4 +- .../plotsquared/plot/object/PlotArea.java | 17 ++- .../plotsquared/plot/object/PlotPlayer.java | 32 ++--- .../plotsquared/plot/util/ArrayUtil.java | 4 +- .../plotsquared/plot/util/UUIDHandler.java | 19 +-- 12 files changed, 174 insertions(+), 234 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Argument.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Argument.java index dd5221a3a..bcd2932af 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Argument.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Argument.java @@ -2,6 +2,8 @@ package com.github.intellectualsites.plotsquared.commands; import com.github.intellectualsites.plotsquared.plot.object.PlotId; +import java.util.stream.Stream; + public abstract class Argument { public static final Argument Integer = new Argument("int", 16) { @@ -17,11 +19,9 @@ public abstract class Argument { public static final Argument Boolean = new Argument("boolean", true) { @Override public Boolean parse(String in) { Boolean value = null; - if (in.equalsIgnoreCase("true") || in.equalsIgnoreCase("Yes") || in - .equalsIgnoreCase("1")) { + if (Stream.of("true", "Yes", "1").anyMatch(in::equalsIgnoreCase)) { value = true; - } else if (in.equalsIgnoreCase("false") || in.equalsIgnoreCase("No") || in - .equalsIgnoreCase("0")) { + } else if (Stream.of("false", "No", "0").anyMatch(in::equalsIgnoreCase)) { value = false; } return value; 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 87f8f209d..f465b1987 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 @@ -30,7 +30,6 @@ import lombok.Getter; import lombok.NonNull; import lombok.Setter; -import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.*; import java.net.MalformedURLException; @@ -342,12 +341,10 @@ import java.util.zip.ZipInputStream; 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); - } + forEachPlotRaw(plot -> { + if (plot.hasOwner() && plot.temp != -1) { + if (UUIDHandler.getName(plot.owner) == null) { + UUIDHandler.implementation.unknown.add(plot.owner); } } }); @@ -516,14 +513,12 @@ import java.util.zip.ZipInputStream; public Set getBasePlots() { int size = getPlotCount(); final Set result = new HashSet<>(size); - foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea value) { - for (Plot plot : value.getPlots()) { - if (!plot.isBasePlot()) { - continue; - } - result.add(plot); + forEachPlotArea(value -> { + for (Plot plot : value.getPlots()) { + if (!plot.isBasePlot()) { + continue; } + result.add(plot); } }); return Collections.unmodifiableSet(result); @@ -784,23 +779,21 @@ import java.util.zip.ZipInputStream; */ public Set getPlots(final PlotFilter... filters) { final HashSet set = new HashSet<>(); - foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea value) { + forEachPlotArea(value -> { + for (PlotFilter filter : filters) { + if (!filter.allowsArea(value)) { + return; + } + } + loop: + for (Entry entry2 : value.getPlotEntries()) { + Plot plot = entry2.getValue(); for (PlotFilter filter : filters) { - if (!filter.allowsArea(value)) { - return; + if (!filter.allowsPlot(plot)) { + continue loop; } } - loop: - for (Entry entry2 : value.getPlotEntries()) { - Plot plot = entry2.getValue(); - for (PlotFilter filter : filters) { - if (!filter.allowsPlot(plot)) { - continue loop; - } - } - set.add(plot); - } + set.add(plot); } }); return set; @@ -814,11 +807,7 @@ import java.util.zip.ZipInputStream; public Set getPlots() { int size = getPlotCount(); final Set result = new HashSet<>(size); - foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea value) { - result.addAll(value.getPlots()); - } - }); + forEachPlotArea(value -> result.addAll(value.getPlots())); return result; } @@ -935,11 +924,7 @@ import java.util.zip.ZipInputStream; public Collection getPlots(String world) { final Set set = new HashSet<>(); - foreachPlotArea(world, new RunnableVal() { - @Override public void run(PlotArea value) { - set.addAll(value.getPlots()); - } - }); + forEachPlotArea(world, value -> set.addAll(value.getPlots())); return set; } @@ -973,11 +958,9 @@ import java.util.zip.ZipInputStream; */ public Set getPlots(final UUID uuid) { final Set plots = new HashSet<>(); - foreachPlot(new RunnableVal() { - @Override public void run(Plot value) { - if (value.isOwnerAbs(uuid)) { - plots.add(value); - } + forEachPlot(value -> { + if (value.isOwnerAbs(uuid)) { + plots.add(value); } }); return Collections.unmodifiableSet(plots); @@ -990,11 +973,9 @@ import java.util.zip.ZipInputStream; public Set getBasePlots(final UUID uuid) { final Set plots = new HashSet<>(); - foreachBasePlot(new RunnableVal() { - @Override public void run(Plot value) { - if (value.isOwner(uuid)) { - plots.add(value); - } + forEachBasePlot(value -> { + if (value.isOwner(uuid)) { + plots.add(value); } }); return Collections.unmodifiableSet(plots); @@ -1008,11 +989,9 @@ import java.util.zip.ZipInputStream; */ public Set getPlotsAbs(final UUID uuid) { final Set plots = new HashSet<>(); - foreachPlot(new RunnableVal() { - @Override public void run(Plot value) { - if (value.isOwnerAbs(uuid)) { - plots.add(value); - } + forEachPlot(value -> { + if (value.isOwnerAbs(uuid)) { + plots.add(value); } }); return Collections.unmodifiableSet(plots); @@ -1538,11 +1517,7 @@ import java.util.zip.ZipInputStream; // Validate that all data in the db is correct final HashSet plots = new HashSet<>(); try { - foreachPlotRaw(new RunnableVal() { - @Override public void run(Plot value) { - plots.add(value); - } - }); + forEachPlotRaw(plots::add); } catch (final Exception ignored) { } DBFunc.validatePlots(plots); @@ -1824,43 +1799,36 @@ import java.util.zip.ZipInputStream; } } - public void foreachPlotArea(@Nonnull final RunnableVal runnable) { - for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) { - runnable.run(area); - } - } - - public void foreachPlotArea(@NonNull final String world, - @NonNull final RunnableVal runnable) { + public void forEachPlotArea(@NonNull final String world, Consumer runnable) { final PlotArea[] array = this.plotAreaManager.getPlotAreas(world, null); if (array == null) { return; } for (final PlotArea area : array) { - runnable.run(area); + runnable.accept(area); } } - public void foreachPlot(@NonNull final RunnableVal runnable) { + public void forEachPlot(Consumer runnable) { for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) { - area.getPlots().forEach(runnable::run); + area.getPlots().forEach(runnable); } } - public void foreachPlotRaw(@NonNull final RunnableVal runnable) { + public void forEachPlotRaw(Consumer runnable) { for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) { - area.getPlots().forEach(runnable::run); + area.getPlots().forEach(runnable); } if (this.plots_tmp != null) { for (final HashMap entry : this.plots_tmp.values()) { - entry.values().forEach(runnable::run); + entry.values().forEach(runnable); } } } - public void foreachBasePlot(@NonNull final RunnableVal run) { + public void forEachBasePlot(Consumer run) { for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) { - area.foreachBasePlot(run); + area.forEachBasePlot(run); } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Buy.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Buy.java index 25f9484b1..8ba07d900 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Buy.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Buy.java @@ -46,27 +46,23 @@ import java.util.Set; final double price = flag.get(); checkTrue(player.getMoney() >= price, C.CANNOT_AFFORD_PLOT); player.withdraw(price); - confirm.run(this, new Runnable() { - @Override // Success - public void run() { - C.REMOVED_BALANCE.send(player, price); - EconHandler.manager - .depositMoney(UUIDHandler.getUUIDWrapper().getOfflinePlayer(plot.owner), price); - PlotPlayer owner = UUIDHandler.getPlayer(plot.owner); - if (owner != null) { - C.PLOT_SOLD.send(owner, plot.getId(), player.getName(), price); - } - plot.removeFlag(Flags.PRICE); - plot.setOwner(player.getUUID()); - C.CLAIMED.send(player); - whenDone.run(Buy.this, CommandResult.SUCCESS); - } - }, new Runnable() { - @Override // Failure - public void run() { - player.deposit(price); - whenDone.run(Buy.this, CommandResult.FAILURE); + // Failure + // Success + confirm.run(this, () -> { + C.REMOVED_BALANCE.send(player, price); + EconHandler.manager + .depositMoney(UUIDHandler.getUUIDWrapper().getOfflinePlayer(plot.owner), price); + PlotPlayer owner = UUIDHandler.getPlayer(plot.owner); + if (owner != null) { + C.PLOT_SOLD.send(owner, plot.getId(), player.getName(), price); } + plot.removeFlag(Flags.PRICE); + plot.setOwner(player.getUUID()); + C.CLAIMED.send(player); + whenDone.run(Buy.this, CommandResult.SUCCESS); + }, () -> { + player.deposit(price); + whenDone.run(Buy.this, CommandResult.FAILURE); }); } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Owner.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Owner.java index 4095c15c9..2ef7ece5b 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Owner.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Owner.java @@ -9,6 +9,7 @@ import com.github.intellectualsites.plotsquared.plot.util.*; import java.util.Set; import java.util.UUID; +import java.util.stream.Stream; @CommandDeclaration(command = "setowner", permission = "plots.set.owner", description = "Set the plot owner", usage = "/plot setowner ", @@ -31,8 +32,7 @@ import java.util.UUID; name = name == null ? value : name; } if (uuid == null || value.equalsIgnoreCase("-")) { - if (value.equalsIgnoreCase("none") || value.equalsIgnoreCase("null") || value - .equalsIgnoreCase("-")) { + if (Stream.of("none", "null", "-").anyMatch(value::equalsIgnoreCase)) { if (!Permissions .hasPermission(player, C.PERMISSION_ADMIN_COMMAND_SETOWNER.s(), true)) { return false; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Reload.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Reload.java index 71ee2e840..bee93e15d 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Reload.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Reload.java @@ -6,9 +6,7 @@ import com.github.intellectualsites.plotsquared.configuration.MemorySection; import com.github.intellectualsites.plotsquared.configuration.file.YamlConfiguration; import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.C; -import com.github.intellectualsites.plotsquared.plot.object.PlotArea; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; -import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import java.io.IOException; @@ -24,53 +22,51 @@ import java.util.Objects; // loaded during startup unfortunately. PlotSquared.get().setupConfigs(); C.load(PlotSquared.get().translationFile); - PlotSquared.get().foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea area) { - ConfigurationSection worldSection = PlotSquared.get().worlds - .getConfigurationSection("worlds." + area.worldname); - if (worldSection == null) { - return; + PlotSquared.get().forEachPlotArea(area -> { + ConfigurationSection worldSection = + PlotSquared.get().worlds.getConfigurationSection("worlds." + area.worldname); + if (worldSection == null) { + return; + } + if (area.TYPE != 2 || !worldSection.contains("areas")) { + area.saveConfiguration(worldSection); + area.loadDefaultConfiguration(worldSection); + } else { + ConfigurationSection areaSection = worldSection.getConfigurationSection( + "areas." + area.id + "-" + area.getMin() + "-" + area.getMax()); + YamlConfiguration clone = new YamlConfiguration(); + for (String key : areaSection.getKeys(true)) { + if (areaSection.get(key) instanceof MemorySection) { + continue; + } + if (!clone.contains(key)) { + clone.set(key, areaSection.get(key)); + } } - if (area.TYPE != 2 || !worldSection.contains("areas")) { - area.saveConfiguration(worldSection); - area.loadDefaultConfiguration(worldSection); - } else { - ConfigurationSection areaSection = worldSection.getConfigurationSection( - "areas." + area.id + "-" + area.getMin() + "-" + area.getMax()); - YamlConfiguration clone = new YamlConfiguration(); - for (String key : areaSection.getKeys(true)) { - if (areaSection.get(key) instanceof MemorySection) { - continue; - } - if (!clone.contains(key)) { - clone.set(key, areaSection.get(key)); - } + for (String key : worldSection.getKeys(true)) { + if (worldSection.get(key) instanceof MemorySection) { + continue; } - for (String key : worldSection.getKeys(true)) { - if (worldSection.get(key) instanceof MemorySection) { - continue; - } - if (!key.startsWith("areas") && !clone.contains(key)) { - clone.set(key, worldSection.get(key)); - } + if (!key.startsWith("areas") && !clone.contains(key)) { + clone.set(key, worldSection.get(key)); } - area.saveConfiguration(clone); - // netSections is the combination of - for (String key : clone.getKeys(true)) { - if (clone.get(key) instanceof MemorySection) { - continue; - } - if (!worldSection.contains(key)) { - worldSection.set(key, clone.get(key)); - } else { - Object value = worldSection.get(key); - if (Objects.equals(value, clone.get(key))) { - areaSection.set(key, clone.get(key)); - } - } - } - area.loadDefaultConfiguration(clone); } + area.saveConfiguration(clone); + // netSections is the combination of + for (String key : clone.getKeys(true)) { + if (clone.get(key) instanceof MemorySection) { + continue; + } + if (!worldSection.contains(key)) { + worldSection.set(key, clone.get(key)); + } else { + Object value = worldSection.get(key); + if (Objects.equals(value, clone.get(key))) { + areaSection.set(key, clone.get(key)); + } + } + } + area.loadDefaultConfiguration(clone); } }); PlotSquared.get().worlds.save(PlotSquared.get().worldsFile); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Trust.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Trust.java index 6e6f0c5ab..c8c7ef77b 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Trust.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Trust.java @@ -61,21 +61,19 @@ import java.util.UUID; checkTrue(!uuids.isEmpty(), null); checkTrue(size <= plot.getArea().MAX_PLOT_MEMBERS || Permissions .hasPermission(player, C.PERMISSION_ADMIN_COMMAND_TRUST), C.PLOT_MAX_MEMBERS); - confirm.run(this, new Runnable() { - @Override // Success - public void run() { - for (UUID uuid : uuids) { - if (uuid != DBFunc.EVERYONE) { - if (!plot.removeMember(uuid)) { - if (plot.getDenied().contains(uuid)) { - plot.removeDenied(uuid); - } + // Success + confirm.run(this, () -> { + for (UUID uuid : uuids) { + if (uuid != DBFunc.EVERYONE) { + if (!plot.removeMember(uuid)) { + if (plot.getDenied().contains(uuid)) { + plot.removeDenied(uuid); } } - plot.addTrusted(uuid); - EventUtil.manager.callTrusted(player, plot, uuid, true); - MainUtil.sendMessage(player, C.TRUSTED_ADDED); } + plot.addTrusted(uuid); + EventUtil.manager.callTrusted(player, plot, uuid, true); + MainUtil.sendMessage(player, C.TRUSTED_ADDED); } }, null); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java index 6b86a890a..7c57b2dc1 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java @@ -1,9 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.flag; import com.github.intellectualsites.plotsquared.plot.PlotSquared; -import com.github.intellectualsites.plotsquared.plot.object.Plot; -import com.github.intellectualsites.plotsquared.plot.object.PlotArea; -import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.MathMan; import com.github.intellectualsites.plotsquared.plot.util.StringMan; @@ -163,42 +160,37 @@ public final class Flags { public static void registerFlag(final Flag flag) { final Flag duplicate = flags.put(flag.getName(), flag); if (duplicate != null) { - PlotSquared.get().foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea value) { - Object remove; - if (value.DEFAULT_FLAGS.containsKey(duplicate)) { - remove = value.DEFAULT_FLAGS.remove(duplicate); - try { - if (remove instanceof Collection - && remove.getClass().getMethod("toString").getDeclaringClass() - == Object.class) { - value.DEFAULT_FLAGS.put(flag, - flag.parseValue(StringMan.join((Collection) remove, ','))); - } else { - value.DEFAULT_FLAGS.put(flag, flag.parseValue("" + remove)); - } - } catch (NoSuchMethodException neverHappens) { - neverHappens.printStackTrace(); + PlotSquared.get().forEachPlotArea(value -> { + if (value.DEFAULT_FLAGS.containsKey(duplicate)) { + Object remove = value.DEFAULT_FLAGS.remove(duplicate); + try { + if (remove instanceof Collection + && remove.getClass().getMethod("toString").getDeclaringClass() + == Object.class) { + value.DEFAULT_FLAGS.put(flag, + flag.parseValue(StringMan.join((Collection) remove, ','))); + } else { + value.DEFAULT_FLAGS.put(flag, flag.parseValue("" + remove)); } + } catch (NoSuchMethodException neverHappens) { + neverHappens.printStackTrace(); } } }); - PlotSquared.get().foreachPlotRaw(new RunnableVal() { - @Override public void run(Plot value) { - if (value.getFlags().containsKey(duplicate)) { - Object remove = value.getFlags().remove(duplicate); - try { - if (remove instanceof Collection - && remove.getClass().getMethod("toString").getDeclaringClass() - == Object.class) { - value.getFlags().put(flag, - flag.parseValue(StringMan.join((Collection) remove, ','))); - } else { - value.getFlags().put(flag, flag.parseValue("" + remove)); - } - } catch (NoSuchMethodException neverHappens) { - neverHappens.printStackTrace(); + PlotSquared.get().forEachPlotRaw(value -> { + if (value.getFlags().containsKey(duplicate)) { + Object remove = value.getFlags().remove(duplicate); + try { + if (remove instanceof Collection + && remove.getClass().getMethod("toString").getDeclaringClass() + == Object.class) { + value.getFlags().put(flag, + flag.parseValue(StringMan.join((Collection) remove, ','))); + } else { + value.getFlags().put(flag, flag.parseValue("" + remove)); } + } catch (NoSuchMethodException neverHappens) { + neverHappens.printStackTrace(); } } }); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 53b74d402..23f561233 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -32,6 +32,7 @@ import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * The plot class
@@ -509,8 +510,7 @@ public class Plot { * @return is the plot merged or not */ public boolean isMerged() { - return getSettings().getMerged(0) || getSettings().getMerged(2) || getSettings() - .getMerged(1) || getSettings().getMerged(3); + return IntStream.of(0, 2, 1, 3).anyMatch(i -> getSettings().getMerged(i)); } /** diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java index 7d0c84255..bcfdfb967 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java @@ -22,6 +22,7 @@ import javax.annotation.Nullable; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; /** * @author Jesse Boyd, Alexander Söderberg @@ -516,11 +517,9 @@ public abstract class PlotArea { return Collections.emptySet(); } final HashSet myPlots = new HashSet<>(); - foreachPlotAbs(new RunnableVal() { - @Override public void run(Plot value) { - if (uuid.equals(value.owner)) { - myPlots.add(value); - } + forEachPlotAbs(value -> { + if (uuid.equals(value.owner)) { + myPlots.add(value); } }); return myPlots; @@ -664,16 +663,16 @@ public abstract class PlotArea { return myPlots; } - private void foreachPlotAbs(@Nonnull final RunnableVal run) { + private void forEachPlotAbs(Consumer run) { for (final Entry entry : this.plots.entrySet()) { - run.run(entry.getValue()); + run.accept(entry.getValue()); } } - public void foreachBasePlot(@Nonnull final RunnableVal run) { + public void forEachBasePlot(Consumer run) { for (final Plot plot : getPlots()) { if (plot.isBasePlot()) { - run.run(plot); + run.accept(plot); } } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java index 3e41d1cb5..c0d9ec726 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java @@ -202,17 +202,15 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { } final AtomicInteger count = new AtomicInteger(0); final UUID uuid = getUUID(); - PlotSquared.get().foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea value) { - if (!Settings.Done.COUNTS_TOWARDS_LIMIT) { - for (Plot plot : value.getPlotsAbs(uuid)) { - if (!plot.hasFlag(Flags.DONE)) { - count.incrementAndGet(); - } + PlotSquared.get().forEachPlotArea(value -> { + if (!Settings.Done.COUNTS_TOWARDS_LIMIT) { + for (Plot plot : value.getPlotsAbs(uuid)) { + if (!plot.hasFlag(Flags.DONE)) { + count.incrementAndGet(); } - } else { - count.addAndGet(value.getPlotsAbs(uuid).size()); } + } else { + count.addAndGet(value.getPlotsAbs(uuid).size()); } }); return count.get(); @@ -223,12 +221,10 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { return getClusterCount(getLocation().getWorld()); } final AtomicInteger count = new AtomicInteger(0); - PlotSquared.get().foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea value) { - for (PlotCluster cluster : value.getClusters()) { - if (cluster.isOwner(getUUID())) { - count.incrementAndGet(); - } + PlotSquared.get().forEachPlotArea(value -> { + for (PlotCluster cluster : value.getClusters()) { + if (cluster.isOwner(getUUID())) { + count.incrementAndGet(); } } }); @@ -519,11 +515,7 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { */ public int getPlayerClusterCount() { final AtomicInteger count = new AtomicInteger(); - PlotSquared.get().foreachPlotArea(new RunnableVal() { - @Override public void run(PlotArea value) { - count.addAndGet(value.getClusters().size()); - } - }); + PlotSquared.get().forEachPlotArea(value -> count.addAndGet(value.getClusters().size())); return count.get(); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ArrayUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ArrayUtil.java index 6052c2a93..e902faffd 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ArrayUtil.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ArrayUtil.java @@ -5,9 +5,7 @@ import java.util.Arrays; public class ArrayUtil { public static final T[] concatAll(T[] first, T[]... rest) { int totalLength = first.length; - for (T[] array : rest) { - totalLength += array.length; - } + totalLength += Arrays.stream(rest).mapToInt(array -> array.length).sum(); T[] result = Arrays.copyOf(first, totalLength); int offset = first.length; for (T[] array : rest) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java index aafba9da9..c2aebe971 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java @@ -1,7 +1,10 @@ package com.github.intellectualsites.plotsquared.plot.util; import com.github.intellectualsites.plotsquared.plot.PlotSquared; -import com.github.intellectualsites.plotsquared.plot.object.*; +import com.github.intellectualsites.plotsquared.plot.object.OfflinePlotPlayer; +import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; +import com.github.intellectualsites.plotsquared.plot.object.StringWrapper; import com.github.intellectualsites.plotsquared.plot.uuid.UUIDWrapper; import com.google.common.collect.BiMap; @@ -52,14 +55,12 @@ public class UUIDHandler { public static HashSet getAllUUIDS() { final HashSet uuids = new HashSet<>(); - PlotSquared.get().foreachPlotRaw(new RunnableVal() { - @Override public void run(Plot plot) { - if (plot.hasOwner()) { - uuids.add(plot.owner); - uuids.addAll(plot.getTrusted()); - uuids.addAll(plot.getMembers()); - uuids.addAll(plot.getDenied()); - } + PlotSquared.get().forEachPlotRaw(plot -> { + if (plot.hasOwner()) { + uuids.add(plot.owner); + uuids.addAll(plot.getTrusted()); + uuids.addAll(plot.getMembers()); + uuids.addAll(plot.getDenied()); } }); return uuids; From df47697ca63e362fc27f71814d71a96ecc909884 Mon Sep 17 00:00:00 2001 From: matt <4009945+MattBDev@users.noreply.github.com> Date: Tue, 19 Feb 2019 19:00:37 -0500 Subject: [PATCH 2/2] CLeaning of code Signed-off-by: matt <4009945+MattBDev@users.noreply.github.com> --- .../bukkit/object/BukkitPlayer.java | 3 ++- .../plotsquared/plot/database/AbstractDB.java | 3 ++- .../plotsquared/plot/database/SQLManager.java | 3 ++- .../plot/object/ConsolePlayer.java | 3 ++- .../plotsquared/plot/object/Plot.java | 21 +++++++++++++------ .../plotsquared/plot/object/PlotPlayer.java | 3 ++- .../plotsquared/plot/util/UUIDHandler.java | 6 ++---- .../plot/database/AbstractDBTest.java | 5 +++-- 8 files changed, 30 insertions(+), 17 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java index 60c1351d0..888de164a 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java @@ -19,6 +19,7 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.plugin.RegisteredListener; +import javax.annotation.Nonnull; import java.util.Arrays; import java.util.Set; import java.util.UUID; @@ -53,7 +54,7 @@ public class BukkitPlayer extends PlotPlayer { return location == null ? BukkitUtil.getLocation(this.player) : location; } - @Override public UUID getUUID() { + @Nonnull @Override public UUID getUUID() { if (this.uuid == null) { this.uuid = UUIDHandler.getUUID(this); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDB.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDB.java index ae4a1fa76..0eecd6539 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDB.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDB.java @@ -4,6 +4,7 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.object.*; import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment; +import javax.annotation.Nonnull; import java.util.*; public interface AbstractDB { @@ -300,7 +301,7 @@ public interface AbstractDB { * * @param plot The Plot to get comments from */ - void getComments(Plot plot, String inbox, RunnableVal> whenDone); + void getComments(@Nonnull Plot plot, String inbox, RunnableVal> whenDone); void createPlotAndSettings(Plot plot, Runnable whenDone); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java index 2995a448d..876a29121 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java @@ -13,6 +13,7 @@ import com.github.intellectualsites.plotsquared.plot.util.StringMan; import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import com.google.common.base.Charsets; +import javax.annotation.Nonnull; import java.sql.*; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -2168,7 +2169,7 @@ import java.util.concurrent.atomic.AtomicInteger; }); } - @Override public void getComments(final Plot plot, final String inbox, + @Override public void getComments(@Nonnull Plot plot, final String inbox, final RunnableVal> whenDone) { addPlotTask(plot, new UniqueStatement("getComments_" + plot) { @Override public void set(PreparedStatement statement) throws SQLException { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java index 18a0ce7bc..6f386b3cb 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java @@ -6,6 +6,7 @@ import com.github.intellectualsites.plotsquared.plot.database.DBFunc; import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode; import com.github.intellectualsites.plotsquared.plot.util.PlotWeather; +import javax.annotation.Nonnull; import java.util.UUID; public class ConsolePlayer extends PlotPlayer { @@ -45,7 +46,7 @@ public class ConsolePlayer extends PlotPlayer { return getLocation(); } - @Override public UUID getUUID() { + @Nonnull @Override public UUID getUUID() { return DBFunc.EVERYONE; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 23f561233..fd76d9c87 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -21,6 +21,7 @@ import com.google.common.collect.BiMap; import com.google.common.collect.ImmutableSet; import com.sk89q.jnbt.CompoundTag; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.awt.geom.Area; import java.awt.geom.PathIterator; @@ -322,7 +323,7 @@ public class Plot { * @param uuid the player uuid * @return if the provided uuid is the owner of the plot */ - public boolean isOwner(UUID uuid) { + public boolean isOwner(@Nonnull UUID uuid) { if (uuid.equals(this.owner)) { return true; } @@ -339,6 +340,9 @@ public class Plot { /** * Gets a immutable set of owner UUIDs for a plot (supports multi-owner mega-plots). + *

+ * This method cannot be used to add or remove owners from a plot. + *

* * @return the plot owners */ @@ -752,8 +756,9 @@ public class Plot { public boolean setOwner(UUID owner, PlotPlayer initiator) { boolean result = EventUtil.manager .callOwnerChange(initiator, this, owner, hasOwner() ? this.owner : null, hasOwner()); - if (!result) + if (!result) { return false; + } if (!hasOwner()) { this.owner = owner; create(); @@ -948,8 +953,9 @@ public class Plot { * @param name name */ public void setSign(final String name) { - if (!isLoaded()) + if (!isLoaded()) { return; + } if (!PlotSquared.get().isMainThread(Thread.currentThread())) { TaskManager.runTask(() -> Plot.this.setSign(name)); return; @@ -1165,8 +1171,9 @@ public class Plot { Location bot = corners[1]; Location loc = new Location(this.getWorldName(), MathMan.average(bot.getX(), top.getX()), MathMan.average(bot.getY(), top.getY()), MathMan.average(bot.getZ(), top.getZ())); - if (!isLoaded()) + if (!isLoaded()) { return loc; + } int y = isLoaded() ? WorldUtil.IMP.getHighestBlock(getWorldName(), loc.getX(), loc.getZ()) : 62; if (area.ALLOW_SIGNS) { @@ -1201,8 +1208,9 @@ public class Plot { Location bot = this.getBottomAbs(); Location loc = new Location(bot.getWorld(), bot.getX() + home.x, bot.getY() + home.y, bot.getZ() + home.z, home.yaw, home.pitch); - if (!isLoaded()) + if (!isLoaded()) { return loc; + } if (!WorldUtil.IMP.getBlock(loc).isAir()) { loc.setY(Math.max( 1 + WorldUtil.IMP.getHighestBlock(this.getWorldName(), loc.getX(), loc.getZ()), @@ -1514,6 +1522,7 @@ public class Plot { } //TODO Better documentation needed. + /** * Return the top location for the plot. * @@ -1526,6 +1535,7 @@ public class Plot { } //TODO Better documentation needed. + /** * Return the bottom location for the plot. */ @@ -2616,7 +2626,6 @@ public class Plot { } /** - * * Do the plot entry tasks for each player in the plot
* - Usually called when the plot state changes (unclaimed/claimed/flag change etc) */ diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java index c0d9ec726..b3df60558 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java @@ -14,6 +14,7 @@ import com.github.intellectualsites.plotsquared.plot.util.*; import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager; import lombok.NonNull; +import javax.annotation.Nonnull; import java.nio.ByteBuffer; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -326,7 +327,7 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { * * @return UUID */ - @Override public abstract UUID getUUID(); + @Override @Nonnull public abstract UUID getUUID(); public boolean canTeleport(Location loc) { Location current = getLocationFull(); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java index c2aebe971..6696edbd5 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/UUIDHandler.java @@ -8,6 +8,7 @@ import com.github.intellectualsites.plotsquared.plot.object.StringWrapper; import com.github.intellectualsites.plotsquared.plot.uuid.UUIDWrapper; import com.google.common.collect.BiMap; +import javax.annotation.Nonnull; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -82,10 +83,7 @@ public class UUIDHandler { implementation.add(toAdd); } - public static UUID getUUID(PlotPlayer player) { - if (implementation == null) { - return null; - } + @Nonnull public static UUID getUUID(PlotPlayer player) { return implementation.getUUID(player); } diff --git a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java index 1ef04abba..1ef5c92ec 100644 --- a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java +++ b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java @@ -4,6 +4,7 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.object.*; import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment; +import javax.annotation.Nonnull; import java.util.*; public class AbstractDBTest implements AbstractDB { @@ -148,8 +149,8 @@ public class AbstractDBTest implements AbstractDB { @Override public void setComment(Plot plot, PlotComment comment) { } - @Override - public void getComments(Plot plot, String inbox, RunnableVal> whenDone) { + @Override public void getComments(@Nonnull Plot plot, String inbox, + RunnableVal> whenDone) { } @Override public void createPlotAndSettings(Plot plot, Runnable whenDone) {