From c5bd4c4935ec2455159a1b9768270529726eb44d Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Sun, 2 Nov 2014 21:14:47 +0100 Subject: [PATCH] Better list! Even added argument suggestion (so proud of that class xD<3) Fixes #8 --- .../java/com/intellectualcrafters/plot/C.java | 4 +- .../intellectualcrafters/plot/PlotMain.java | 20 ++++- .../plot/commands/list.java | 75 ++++++++++++------- .../plot/database/AbstractDB.java | 16 ++-- .../plot/database/DBFunc.java | 14 ++-- .../plot/database/SQLManager.java | 4 +- .../plot/generator/DefaultPlotWorld.java | 4 +- 7 files changed, 86 insertions(+), 51 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java index 5109678e2..2b27b3478 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java @@ -254,8 +254,10 @@ public enum C { /* * List */ + PLOT_LIST_HEADER_PAGED("&c(Page &6%cur&c/&6%max&c) &6List of %word% plots"), PLOT_LIST_HEADER("&6List of %word% plots"), - PLOT_LIST_ITEM("&c>> &6%id% &c- &6%owner%"), + PLOT_LIST_ITEM("&c>> &6%id&c:&6%world &c- &6%owner"), + PLOT_LIST_ITEM_ORDERED("&c[&6%in&c] >> &6%id&c:&6%world &c- &6%owner"), PLOT_LIST_FOOTER("&c>> &6%word% a total of &c%num% &6claimed %plot%."), /* * Left diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index a8f1dc5b6..ceaa9f8c0 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -222,7 +222,7 @@ public class PlotMain extends JavaPlugin { * List of all plots * DO NOT USE EXCEPT FOR DATABASE PURPOSES */ - private static HashMap> plots; + private static LinkedHashMap> plots; /** * All loaded plot worlds */ @@ -234,12 +234,20 @@ public class PlotMain extends JavaPlugin { * * @return HashMap containing the plot ID and the plot object. */ - public static Set getPlots() { + public static Set getPlots() { + ArrayList myplots = new ArrayList<>(); + for (HashMap world : plots.values()) { + myplots.addAll(world.values()); + } + return new HashSet<>(myplots); + } + + public static LinkedHashSet getPlotsSorted() { ArrayList myplots = new ArrayList<>(); for (HashMap world : plots.values()) { myplots.addAll(world.values()); } - return new HashSet<>(myplots); + return new LinkedHashSet<>(myplots); } /** @@ -1515,8 +1523,12 @@ public class PlotMain extends JavaPlugin { return plots; } - public static void setAllPlotsRaw(HashMap> plots) { + public static void setAllPlotsRaw(LinkedHashMap> plots) { PlotMain.plots = plots; } + public static void setAllPlotsRaw(HashMap> plots) { + PlotMain.plots = new LinkedHashMap>(); + PlotMain.plots.putAll(plots); + } } 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 df09855e6..328e45fe5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -8,18 +8,12 @@ package com.intellectualcrafters.plot.commands; -import java.util.HashMap; -import java.util.UUID; - +import com.intellectualcrafters.plot.*; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import com.intellectualcrafters.plot.C; -import com.intellectualcrafters.plot.PlayerFunctions; -import com.intellectualcrafters.plot.Plot; -import com.intellectualcrafters.plot.PlotId; -import com.intellectualcrafters.plot.PlotMain; -import com.intellectualcrafters.plot.UUIDHandler; +import java.util.HashMap; +import java.util.UUID; /** * @author Citymonstret @@ -49,8 +43,7 @@ public class list extends SubCommand { string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "your") + "\n"); int idx = 0; for (Plot p : PlotMain.getPlots(plr)) { - string.append(C.PLOT_LIST_ITEM.s().replaceAll("%id%", p.id.x + ";" + p.id.y + ";" + p.world).replaceAll("%owner%", getName(p.owner)) - + "\n"); + 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) { @@ -66,27 +59,55 @@ public class list extends SubCommand { if (args[0].equalsIgnoreCase("shared") && plr!=null) { StringBuilder string = new StringBuilder(); string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "all") + "\n"); - for (Plot p : PlotMain.getPlots()) { + for (Plot p : PlotMain.getPlotsSorted()) { if (p.helpers.contains(plr.getUniqueId())) { - string.append(C.PLOT_LIST_ITEM.s().replaceAll("%id%", p.id.x + ";" + p.id.y + ";" + p.world).replaceAll("%owner%", getName(p.owner)) - + "\n"); + 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%", PlotMain.getPlots().size() - + "").replaceAll("%plot%", PlotMain.getPlots().size() == 1 ? "plot" : "plots")); + string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "There are").replaceAll("%num%", PlotMain.getPlotsSorted().size() + + "").replaceAll("%plot%", PlotMain.getPlotsSorted().size() == 1 ? "plot" : "plots")); PlayerFunctions.sendMessage(plr, string.toString()); return true; } else if (args[0].equalsIgnoreCase("all")) { - StringBuilder string = new StringBuilder(); - string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "all") + "\n"); - for (Plot p : PlotMain.getPlots()) { - string.append(C.PLOT_LIST_ITEM.s().replaceAll("%id%", p.id.x + ";" + p.id.y + ";" + p.world).replaceAll("%owner%", getName(p.owner)) - + "\n"); + // Current page + int page = 0; + + //is a page specified? else use 0 + if(args.length > 1) { + try { + page = Integer.parseInt(args[1]); + } catch(Exception e) { + page = 0; + } + } + + //Get the total pages + int totalPages = ((int) Math.ceil(12 * (PlotMain.getPlotsSorted().size()) / 100)); + + if(page > totalPages) + page = totalPages; + + //Only display 12! + int max = (page * 12) + 12; + + if(max > PlotMain.getPlotsSorted().size()) + max = PlotMain.getPlotsSorted().size(); + + StringBuilder string = new StringBuilder(); + + string.append(C.PLOT_LIST_HEADER_PAGED.s().replaceAll("%cur", page + 1 + "").replaceAll("%max", totalPages + 1 + "").replaceAll("%word%", "all") + "\n"); + Plot p; + + //This might work xD + for (int x = (page * 12); x < max; x++) { + p = (Plot) PlotMain.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.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "There is").replaceAll("%num%", PlotMain.getPlots().size() - + "").replaceAll("%plot%", PlotMain.getPlots().size() == 1 ? "plot" : "plots")); + + string.append(C.PLOT_LIST_FOOTER.s().replaceAll("%word%", "There is").replaceAll("%num%", PlotMain.getPlotsSorted().size() + + "").replaceAll("%plot%", PlotMain.getPlotsSorted().size() == 1 ? "plot" : "plots")); PlayerFunctions.sendMessage(plr, string.toString()); return true; } @@ -96,8 +117,7 @@ public class list extends SubCommand { string.append(C.PLOT_LIST_HEADER.s().replaceAll("%word%", "all") + "\n"); HashMap plots = PlotMain.getPlots(plr.getWorld()); for (Plot p : plots.values()) { - string.append(C.PLOT_LIST_ITEM.s().replaceAll("%id%", p.id.x + ";" + p.id.y + ";" + p.world).replaceAll("%owner%", getName(p.owner)) - + "\n"); + 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 is").replaceAll("%num%", plots.values().size() + "").replaceAll("%plot%", plots.values().size() == 1 ? "plot" : "plots")); @@ -105,8 +125,9 @@ public class list extends SubCommand { return true; } else { - execute(plr); - return false; + //execute(plr); + sendMessage(plr, C.DID_YOU_MEAN, new StringComparsion(args[0], new String[] { "mine", "shared", "world", "all" }).getBestMatch()); + return false; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index 8967eb188..70d6a7154 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -8,17 +8,17 @@ package com.intellectualcrafters.plot.database; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.UUID; - -import org.bukkit.OfflinePlayer; - import com.intellectualcrafters.plot.Flag; import com.intellectualcrafters.plot.Plot; import com.intellectualcrafters.plot.PlotComment; import com.intellectualcrafters.plot.PlotId; +import org.bukkit.OfflinePlayer; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.UUID; /** * @author Citymonstret @@ -78,7 +78,7 @@ public abstract class AbstractDB { /** * @return */ - public abstract HashMap> getPlots(); + public abstract LinkedHashMap> getPlots(); public abstract void setMerged(final String world, final Plot plot, final boolean[] merged); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java index 7fc7cf900..f25a333bf 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -8,16 +8,16 @@ package com.intellectualcrafters.plot.database; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.UUID; - -import org.bukkit.OfflinePlayer; - import com.intellectualcrafters.plot.Flag; import com.intellectualcrafters.plot.Plot; import com.intellectualcrafters.plot.PlotComment; import com.intellectualcrafters.plot.PlotId; +import org.bukkit.OfflinePlayer; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.UUID; /** * @author Citymonstret @@ -111,7 +111,7 @@ public class DBFunc { /** * @return */ - public static HashMap> getPlots() { + public static LinkedHashMap> getPlots() { return dbManager.getPlots(); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index 5cdac3e54..56e1318a9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -377,8 +377,8 @@ public class SQLManager extends AbstractDB { * @return */ @Override - public HashMap> getPlots() { - HashMap> newplots = new HashMap>(); + public LinkedHashMap> getPlots() { + LinkedHashMap> newplots = new LinkedHashMap>(); try { DatabaseMetaData data = connection.getMetaData(); ResultSet rs = data.getColumns(null, null, PREFIX+"plot", "plot_id"); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotWorld.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotWorld.java index 5b0e90fb6..afa6333d9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotWorld.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotWorld.java @@ -146,7 +146,7 @@ public class DefaultPlotWorld extends PlotWorld { new ConfigurationNode("plot.filling", DefaultPlotWorld.MAIN_BLOCK_DEFAULT, "Plot block", Configuration.BLOCKLIST, true), new ConfigurationNode("plot.floor", DefaultPlotWorld.TOP_BLOCK_DEFAULT, "Plot floor block", Configuration.BLOCKLIST, true), new ConfigurationNode("wall.block", DefaultPlotWorld.WALL_BLOCK_DEFAULT, "Top wall block", Configuration.BLOCK, true), - new ConfigurationNode("wall.block.claimed", DefaultPlotWorld.CLAIMED_WALL_BLOCK_DEFAULT, "Wall block (claimed)", Configuration.BLOCK, true), + new ConfigurationNode("wall.block_claimed", DefaultPlotWorld.CLAIMED_WALL_BLOCK_DEFAULT, "Wall block (claimed)", Configuration.BLOCK, true), new ConfigurationNode("road.width", DefaultPlotWorld.ROAD_WIDTH_DEFAULT, "Road width", Configuration.INTEGER, true), new ConfigurationNode("road.height", DefaultPlotWorld.ROAD_HEIGHT_DEFAULT, "Road height", Configuration.INTEGER, true), new ConfigurationNode("road.enable_stripes", DefaultPlotWorld.ROAD_STRIPES_ENABLED_DEFAULT, "Enable road stripes", Configuration.BOOLEAN, true), @@ -182,6 +182,6 @@ public class DefaultPlotWorld extends PlotWorld { this.ROAD_STRIPES = (PlotBlock) Configuration.BLOCK.parseString(config.getString("road.stripes")); this.WALL_FILLING = (PlotBlock) Configuration.BLOCK.parseString(config.getString("wall.filling")); this.WALL_HEIGHT = config.getInt("wall.height"); - this.CLAIMED_WALL_BLOCK = (PlotBlock) Configuration.BLOCK.parseString(config.getString("wall.block.claimed")); + this.CLAIMED_WALL_BLOCK = (PlotBlock) Configuration.BLOCK.parseString(config.getString("wall.block_claimed")); } }