From cca903bf8de4e2969e5786f98897035464bc67c2 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 8 Mar 2015 18:59:00 +1100 Subject: [PATCH 1/2] More options for plot listing --- .../plot/PlotSquared.java | 46 ++- .../plot/commands/Confirm.java | 11 - .../plot/commands/list.java | 263 +++++++++++------- .../plot/listeners/PlayerEvents.java | 1 + 4 files changed, 199 insertions(+), 122 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index 514e56f6c..16b6f6de1 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -10,6 +10,9 @@ import java.sql.ResultSet; import java.sql.SQLException; 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.LinkedHashMap; @@ -138,13 +141,37 @@ public class PlotSquared { } return new LinkedHashSet<>(newplots); } - - public static LinkedHashSet getPlotsSorted() { - final ArrayList newplots = new ArrayList<>(); - for (final HashMap world : plots.values()) { - newplots.addAll(world.values()); - } - return new LinkedHashSet<>(newplots); + + public static ArrayList sortPlots(Collection plots) { + ArrayList newPlots = new ArrayList<>(); + newPlots.addAll(plots); + Collections.sort(newPlots, new Comparator() { + @Override + public int compare(Plot p1, Plot p2) { + return p1.hashCode() + p1.world.hashCode() - p2.hashCode() + p2.world.hashCode(); + } + }); + return newPlots; + } + + public static ArrayList sortPlots(Collection plots, final String priorityWorld) { + ArrayList newPlots = new ArrayList<>(); + newPlots.addAll(plots); + Collections.sort(newPlots, new Comparator() { + @Override + public int compare(Plot p1, Plot p2) { + int w1 = 0; + int w2 = 0; + if (!p1.world.equals(priorityWorld)) { + w1 = p1.hashCode(); + } + if (!p2.world.equals(priorityWorld)) { + w2 = p2.hashCode(); + } + return p1.hashCode() + w1 - p2.hashCode() - w2; + } + }); + return newPlots; } public static Set getPlots(final String world, final String player) { @@ -193,7 +220,10 @@ public class PlotSquared { } public static Set getPlots(final PlotPlayer player) { - final UUID uuid = player.getUUID(); + return getPlots(player.getUUID()); + } + + public static Set getPlots(final UUID uuid) { final ArrayList myplots = new ArrayList<>(); for (final String world : plots.keySet()) { if (isPlotWorld(world)) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Confirm.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Confirm.java index 3a49b2be3..81e73bf20 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Confirm.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Confirm.java @@ -20,22 +20,12 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; -import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; -import com.intellectualcrafters.plot.database.DBFunc; -import com.intellectualcrafters.plot.flag.Flag; -import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.CmdInstance; -import com.intellectualcrafters.plot.object.Location; -import com.intellectualcrafters.plot.object.Plot; -import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.util.CmdConfirm; -import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.TaskManager; -import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; /** * @author Citymonstret @@ -47,7 +37,6 @@ public class Confirm extends SubCommand { @Override public boolean execute(final PlotPlayer plr, final String... args) { - String name = plr.getName(); CmdInstance command = CmdConfirm.getPending(plr); if (command == null) { MainUtil.sendMessage(plr, C.FAILED_CONFIRM); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java index b2135b91d..9b2c052f6 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -20,7 +20,11 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; import com.intellectualcrafters.plot.PlotSquared; @@ -52,128 +56,181 @@ public class list extends SubCommand { } return name; } + + public void noArgs(PlotPlayer plr) { + final StringBuilder builder = new StringBuilder(); + builder.append(C.SUBCOMMAND_SET_OPTIONS_HEADER.s()); + if (plr != null) { + if (PlotSquared.economy != null) { + builder.append(getArgumentList(new String[] { "mine", "shared", "world", "all", "unowned", "unknown", "forsale", "", ""})); + } + else { + builder.append(getArgumentList(new String[] { "mine", "shared", "world", "all", "unowned", "unknown", "", ""})); + } + } else { + builder.append(getArgumentList(new String[] { "world", "all", "unowned", "unknown", "", ""})); + } + MainUtil.sendMessage(plr, builder.toString()); + } @Override public boolean execute(final PlotPlayer plr, final String... args) { if (args.length < 1) { - final StringBuilder builder = new StringBuilder(); - builder.append(C.SUBCOMMAND_SET_OPTIONS_HEADER.s()); - if (plr != null) { - builder.append(getArgumentList(new String[] { "mine", "shared", "world", "all", "forsale" })); - } else { - builder.append(getArgumentList(new String[] { "all" })); - } - MainUtil.sendMessage(plr, builder.toString()); - return true; + noArgs(plr); + return false; } - if (args[0].equalsIgnoreCase("forsale") && (plr != null)) { - if (PlotSquared.economy == null) { - return sendMessage(plr, C.ECON_DISABLED); - } - final StringBuilder string = new StringBuilder(); - string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "buyable")).append("\n"); - int idx = 0; - for (final Plot p : PlotSquared.getPlots(plr.getLocation().getWorld()).values()) { - final Flag price = FlagManager.getPlotFlag(p, "price"); - if (price != null) { - string.append(C.PLOT_LIST_ITEM_ORDERED.s().replaceAll("%in", idx + 1 + "").replaceAll("%id", p.id.toString()).replaceAll("%world", price.getValueString()).replaceAll("%owner", getName(p.owner))).append("\n"); - idx++; - } - } - if (idx == 0) { - MainUtil.sendMessage(plr, C.NO_PLOTS); - return true; - } - string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "Includes").replaceAll("%num%", idx + "").replaceAll("%plot%", idx == 1 ? "plot" : "plots")); - MainUtil.sendMessage(plr, string.toString()); - return true; - } - if (args[0].equalsIgnoreCase("mine") && (plr != null)) { - final StringBuilder string = new StringBuilder(); - string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "your")).append("\n"); - int idx = 0; - for (final Plot p : PlotSquared.getPlots(plr)) { - string.append(C.PLOT_LIST_ITEM_ORDERED.s().replaceAll("%in", idx + 1 + "").replaceAll("%id", p.id.toString()).replaceAll("%world", p.world).replaceAll("%owner", getName(p.owner))).append("\n"); - idx++; - } - if (idx == 0) { - MainUtil.sendMessage(plr, C.NO_PLOTS); - return true; - } - string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "You have").replaceAll("%num%", idx + "").replaceAll("%plot%", idx == 1 ? "plot" : "plots")); - MainUtil.sendMessage(plr, string.toString()); - return true; - } else if (args[0].equalsIgnoreCase("shared") && (plr != null)) { - final StringBuilder string = new StringBuilder(); - string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "all")).append("\n"); - for (final Plot p : PlotSquared.getPlotsSorted()) { - if (p.helpers.contains(UUIDHandler.getUUID(plr))) { - string.append(C.PLOT_LIST_ITEM.s().replaceAll("%id", p.id.toString()).replaceAll("%world", p.world).replaceAll("%owner", getName(p.owner))).append("\n"); - } - } - string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "There are").replaceAll("%num%", PlotSquared.getPlotsSorted().size() + "").replaceAll("%plot%", PlotSquared.getPlotsSorted().size() == 1 ? "plot" : "plots")); - MainUtil.sendMessage(plr, string.toString()); - return true; - } else if (args[0].equalsIgnoreCase("all")) { - // Current page - int page = 0; - // is a page specified? else use 0 - if (args.length > 1) { - try { - page = Integer.parseInt(args[1]); - --page; - if (page < 0) { - page = 0; - } - } catch (final Exception e) { + int page = 0; + if (args.length > 1) { + try { + page = Integer.parseInt(args[1]); + --page; + if (page < 0) { page = 0; } + } catch (final Exception e) { + page = 0; } - // Get the total pages - // int totalPages = ((int) Math.ceil(12 * - // (PlotSquared.getPlotsSorted().size()) / 100)); - final int totalPages = (int) Math.ceil(PlotSquared.getPlotsSorted().size() / 12); - if (page > totalPages) { - page = totalPages; + } + + Collection plots = null; + + String world; + if (plr != null) { + world = plr.getLocation().getWorld(); + } + else { + Set worlds = PlotSquared.getPlotWorlds(); + if (worlds.size() == 0) { + world = "world"; } - // Only display 12! - int max = (page * 12) + 12; - if (max > PlotSquared.getPlotsSorted().size()) { - max = PlotSquared.getPlotsSorted().size(); + else { + world = worlds.iterator().next(); } - final StringBuilder string = new StringBuilder(); - string.append(C.PLOT_LIST_HEADER_PAGED.s().replaceAll("%cur", page + 1 + "").replaceAll("%max", totalPages + 1 + "").replaceAll("%word%", "all")).append("\n"); - Plot p; - // This might work xD - for (int x = (page * 12); x < max; x++) { - p = (Plot) PlotSquared.getPlotsSorted().toArray()[x]; - string.append(C.PLOT_LIST_ITEM_ORDERED.s().replaceAll("%in", x + 1 + "").replaceAll("%id", p.id.toString()).replaceAll("%world", p.world).replaceAll("%owner", getName(p.owner))).append("\n"); + } + + String arg = args[0].toLowerCase(); + switch (arg) { + case "mine": { + if (plr == null) { + break; + } + plots = PlotSquared.getPlots(plr); } - string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "There is").replaceAll("%num%", PlotSquared.getPlotsSorted().size() + "").replaceAll("%plot%", PlotSquared.getPlotsSorted().size() == 1 ? "plot" : "plots")); - MainUtil.sendMessage(plr, string.toString()); - return true; - } else if (args[0].equalsIgnoreCase("world") && (plr != null)) { - final StringBuilder string = new StringBuilder(); - string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "all")).append("\n"); - final HashMap plots = PlotSquared.getPlots(plr.getLocation().getWorld()); - for (final Plot p : plots.values()) { - string.append(C.PLOT_LIST_ITEM.s().replaceAll("%id", p.id.toString()).replaceAll("%world", p.world).replaceAll("%owner", getName(p.owner))).append("\n"); + case "shared": { + if (plr == null) { + break; + } + for (Plot plot : PlotSquared.getPlots()) { + if (plot.helpers.contains(plr.getUUID()) || plot.trusted.contains(plr.getUUID())) { + plots.add(plot); + } + } } - string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "There is").replaceAll("%num%", plots.values().size() + "").replaceAll("%plot%", plots.values().size() == 1 ? "plot" : "plots")); - MainUtil.sendMessage(plr, string.toString()); - return true; - } else { - // execute(plr); + case "world": { + plots = PlotSquared.getPlots(world).values(); + break; + } + case "all": { + plots = PlotSquared.getPlots(); + break; + } + case "forsale": { + if (PlotSquared.economy == null) { + break; + } + plots = new HashSet<>(); + for (Plot plot : PlotSquared.getPlots()) { + final Flag price = FlagManager.getPlotFlag(plot, "price"); + if (price != null) { + plots.add(plot); + } + } + break; + } + case "unowned": { + plots = new HashSet<>(); + for (Plot plot : PlotSquared.getPlots()) { + if (plot.owner == null) { + plots.add(plot); + } + } + break; + } + case "unknown": { + plots = new HashSet<>(); + for (Plot plot : PlotSquared.getPlots()) { + if (plot.owner == null) { + continue; + } + if (UUIDHandler.getName(plot.owner) == null) { + plots.add(plot); + } + } + break; + } + default: { + if (PlotSquared.isPlotWorld(args[0])) { + plots = PlotSquared.getPlots(args[0]).values(); + break; + } + UUID uuid = UUIDHandler.getUUID(args[0]); + if (uuid != null) { + plots = PlotSquared.getPlots(uuid); + break; + } + } + } + + if (plots == null) { sendMessage(plr, C.DID_YOU_MEAN, new StringComparison(args[0], new String[] { "mine", "shared", "world", "all" }).getBestMatch()); return false; } + + if (plots.size() == 0) { + MainUtil.sendMessage(plr, C.NO_PLOTS); + return false; + } + + displayPlots(plr, plots, page); + return true; } - + + public void displayPlots(PlotPlayer player, Collection oldPlots, int page) { + ArrayList plots = PlotSquared.sortPlots(oldPlots); + if (page < 0) { + page = 0; + } + // Get the total pages + // int totalPages = ((int) Math.ceil(12 * + // (PlotSquared.getPlotsSorted().size()) / 100)); + final int totalPages = (int) Math.ceil(plots.size() / 12); + if (page > totalPages) { + page = totalPages; + } + // Only display 12! + int max = (page * 12) + 12; + if (max > plots.size()) { + max = plots.size(); + } + final StringBuilder string = new StringBuilder(); + string.append(C.PLOT_LIST_HEADER_PAGED.s().replaceAll("%cur", page + 1 + "").replaceAll("%max", totalPages + 1 + "").replaceAll("%word%", "all")).append("\n"); + Plot p; + // This might work xD + for (int x = (page * 12); x < max; x++) { + p = (Plot) plots.toArray()[x]; + string.append(C.PLOT_LIST_ITEM_ORDERED.s().replaceAll("%in", x + 1 + "").replaceAll("%id", p.id.toString()).replaceAll("%world", p.world).replaceAll("%owner", getName(p.owner))).append("\n"); + } + string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "There is").replaceAll("%num%", plots.size() + "").replaceAll("%plot%", plots.size() == 1 ? "plot" : "plots")); + MainUtil.sendMessage(player, string.toString()); + } + private String getArgumentList(final String[] strings) { final StringBuilder builder = new StringBuilder(); + String prefix = ""; for (final String s : strings) { - builder.append(MainUtil.colorise('&', s)); + builder.append(prefix + MainUtil.colorise('&', s)); + prefix = " | "; } - return builder.toString().substring(1, builder.toString().length() - 1); + return builder.toString(); } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 83b89bb24..861df15e8 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -177,6 +177,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi passed = false; } if (passed) { + player.teleport(event.getTo()); final PlotPlayer pp = BukkitUtil.getPlayer(player); MainUtil.sendMessage(pp, C.BORDER); return; From ff708d4a32227876dac3787981d637f69010bb13 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 8 Mar 2015 19:03:29 +1100 Subject: [PATCH 2/2] imports --- .../main/java/com/intellectualcrafters/plot/commands/Swap.java | 2 -- .../main/java/com/intellectualcrafters/plot/commands/list.java | 2 -- .../com/intellectualcrafters/plot/listeners/PlayerEvents.java | 1 - .../java/com/intellectualcrafters/plot/util/CmdConfirm.java | 1 - 4 files changed, 6 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java index ed822bd49..424923313 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java @@ -22,12 +22,10 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; -import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java index 9b2c052f6..b47a9ae79 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -22,7 +22,6 @@ package com.intellectualcrafters.plot.commands; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.Set; import java.util.UUID; @@ -32,7 +31,6 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.Plot; -import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.StringComparison; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 861df15e8..8cf463d7a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -11,7 +11,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Chunk; import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/CmdConfirm.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/CmdConfirm.java index 9b4f51644..b238e91b0 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/CmdConfirm.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/CmdConfirm.java @@ -2,7 +2,6 @@ package com.intellectualcrafters.plot.util; import java.util.HashMap; -import com.intellectualcrafters.plot.commands.SubCommand; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.CmdInstance; import com.intellectualcrafters.plot.object.PlotPlayer;