From 87773d4929be037bad1b204eccde28b8a776a1b0 Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Thu, 6 Nov 2014 09:29:49 +0100 Subject: [PATCH 1/3] Created a better plot hashcode thingy --- .../main/java/com/intellectualcrafters/plot/Plot.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java index ffae6bb9f..6fbcea5bb 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java @@ -277,4 +277,14 @@ public class Plot implements Cloneable { PlotHelper.clear(plr, this); } + @Override + public int hashCode() { + final int + x = getId().x, + z = getId().y; + String + xL = (x + "").length() + "", + zL = (z + "").length() + ""; + return Integer.parseInt(xL + x + zL + z); + } } From 0ff50aed1110eae59b4dec854e2f5ebe71d697dc Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Thu, 6 Nov 2014 09:37:46 +0100 Subject: [PATCH 2/3] Experimenting with String comparison, and help methods --- .../com/intellectualcrafters/plot/Plot.java | 9 ++++ .../plot/StringComparsion.java | 5 ++ .../plot/commands/MainCommand.java | 48 +++++-------------- 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java index 6fbcea5bb..3004da105 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Plot.java @@ -277,6 +277,15 @@ public class Plot implements Cloneable { PlotHelper.clear(plr, this); } + /** + * Get the plot hashcode + * + * @return integer. You can easily make this a character array
+ * xI = c[0] + * x = c[1 -> xI...] + * yI = c[xI ... + 1] + * y = c[xI ... + 2 -> yI ...] + */ @Override public int hashCode() { final int diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java index e5aefa907..7caa0360b 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java @@ -11,6 +11,7 @@ import java.util.Collections; public class StringComparsion { private String bestMatch; + private Object bestMatchObject; private double match; public StringComparsion(final String input, final Object[] objects) { @@ -19,10 +20,14 @@ public class StringComparsion { if ((c = compare(input, o.toString())) > this.match) { this.match = c; this.bestMatch = o.toString(); + this.bestMatchObject = o; } } } + public Object getMatchObject() { + return this.bestMatchObject; + } public String getBestMatch() { return this.bestMatch; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index ef8e08892..2c6f673fa 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -8,10 +8,10 @@ package com.intellectualcrafters.plot.commands; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - +import com.intellectualcrafters.plot.C; +import com.intellectualcrafters.plot.PlayerFunctions; +import com.intellectualcrafters.plot.PlotMain; +import com.intellectualcrafters.plot.StringComparsion; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -20,10 +20,9 @@ import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import org.bukkit.util.ChatPaginator; -import com.intellectualcrafters.plot.C; -import com.intellectualcrafters.plot.PlayerFunctions; -import com.intellectualcrafters.plot.PlotMain; -import com.intellectualcrafters.plot.StringComparsion; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; /** * PlotMain command class @@ -139,7 +138,10 @@ public class MainCommand implements CommandExecutor, TabCompleter { commands[x] = subCommands.get(x).cmd; } - PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new StringComparsion(args[0], commands).getBestMatch()); + /* Let's try to get a proper usage string */ + SubCommand command = (SubCommand) new StringComparsion(args[0], commands).getMatchObject(); + PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, command.usage.contains("plot") ? command.usage : "/plot " + command.usage); + //PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new StringComparsion(args[0], commands).getBestMatch()); } return false; } @@ -154,34 +156,6 @@ public class MainCommand implements CommandExecutor, TabCompleter { return cmds; } - /* - * // 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++) { - */ public static ArrayList helpMenu(final Player player, final SubCommand.CommandCategory category, int page) { final List commands = getCommands(category, player); // final int totalPages = ((int) Math.ceil(12 * (commands.size()) / From 536ae7fe2f29ccf43eeb1d2da5303da17436ea9d Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Thu, 6 Nov 2014 09:46:37 +0100 Subject: [PATCH 3/3] Some minor improvements to the MainCommand code. --- .../plot/PlayerFunctions.java | 72 +++++++++++-------- .../plot/commands/MainCommand.java | 27 ++++--- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlayerFunctions.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlayerFunctions.java index 2dbded55f..f7c7a5f79 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlayerFunctions.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlayerFunctions.java @@ -9,20 +9,12 @@ package com.intellectualcrafters.plot; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Biome; import org.bukkit.entity.Player; +import java.util.*; + /** * Functions involving players, plots and locations. * @@ -32,8 +24,7 @@ import org.bukkit.entity.Player; public class PlayerFunctions { /** - * @param player - * player + * @param player player * @return */ public static boolean isInPlot(final Player player) { @@ -41,8 +32,7 @@ public class PlayerFunctions { } /** - * @param plot - * plot + * @param plot plot * @return */ public static boolean hasExpired(final Plot plot) { @@ -173,9 +163,8 @@ public class PlayerFunctions { /** * Updates a given plot with another instance * - * @deprecated - * * @param plot + * @deprecated */ @Deprecated public static void set(final Plot plot) { @@ -237,8 +226,7 @@ public class PlayerFunctions { * \\previous\\ * * @param plr - * @param msg - * Was used to wrap the chat client length (Packets out--) + * @param msg Was used to wrap the chat client length (Packets out--) */ public static void sendMessageWrapped(final Player plr, final String msg) { plr.sendMessage(msg); @@ -247,14 +235,23 @@ public class PlayerFunctions { /** * Send a message to the player * - * @param plr - * Player to recieve message - * @param msg - * Message to send + * @param plr Player to recieve message + * @param msg Message to send + * @return true + * Can be used in things such as commands (return PlayerFunctions.sendMessage(...)) */ - public static void sendMessage(final Player plr, final String msg) { + public static boolean sendMessage(final Player plr, final String msg) { + if (msg.length() > 0 && !msg.equals("")) { + if (plr == null) { + PlotMain.sendConsoleSenderMessage(C.PREFIX.s() + msg); + } else { + sendMessageWrapped(plr, ChatColor.translateAlternateColorCodes('&', C.PREFIX.s() + msg)); + } + } + return true; + /* if ((msg.length() == 0) || msg.equalsIgnoreCase("")) { - return; + return ; } if (plr == null) { @@ -263,18 +260,30 @@ public class PlayerFunctions { } sendMessageWrapped(plr, ChatColor.translateAlternateColorCodes('&', C.PREFIX.s() + msg)); + */ } /** * Send a message to the player * - * @param plr - * Player to recieve message - * @param c - * Caption to send + * @param plr Player to recieve message + * @param c Caption to send */ - public static void sendMessage(final Player plr, final C c, final String... args) { - + public static boolean sendMessage(final Player plr, final C c, final String... args) { + if (c.s().length() > 1) { + if (plr == null) + PlotMain.sendConsoleSenderMessage(c); + else { + String msg = c.s(); + if ((args != null) && (args.length > 0)) { + for (final String str : args) + msg = msg.replaceFirst("%s", str); + } + sendMessage(plr, msg); + } + } + return true; + /* if (plr == null) { PlotMain.sendConsoleSenderMessage(c); return; @@ -290,5 +299,6 @@ public class PlayerFunctions { } } sendMessage(plr, msg); + */ } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index 2c6f673fa..8f0007552 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -31,6 +31,9 @@ import java.util.List; */ public class MainCommand implements CommandExecutor, TabCompleter { + public static final String + MAIN_PERMISSION = "plots.use"; + private static SubCommand[] _subCommands = new SubCommand[] { new Claim(), new Paste(), new Copy(), new Clipboard(), new Auto(), new Home(), new Visit(), new TP(), new Set(), new Clear(), new Delete(), new SetOwner(), new Denied(), new Helpers(), new Trusted(), new Info(), new list(), new Help(), new Debug(), new Schematic(), new plugin(), new Inventory(), new Purge(), new Reload(), new Merge(), new Unlink(), new Kick(), new Setup(), new DebugClaimTest(), new Inbox(), new Comment(), new Swap(), new MusicSubcommand() }; public static ArrayList subCommands = new ArrayList() { @@ -48,9 +51,8 @@ public class MainCommand implements CommandExecutor, TabCompleter { public boolean onCommand(final CommandSender sender, final Command cmd, final String commandLabel, final String[] args) { final Player player = (sender instanceof Player) ? (Player) sender : null; - if (!PlotMain.hasPermission(player, "plots.use")) { - return no_permission(player, "plots.use"); - } + if (!PlotMain.hasPermission(player, MAIN_PERMISSION)) + return no_permission(player, MAIN_PERMISSION); if ((args.length < 1) || ((args.length >= 1) && (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("he")))) { if (args.length < 2) { @@ -59,8 +61,7 @@ public class MainCommand implements CommandExecutor, TabCompleter { for (final SubCommand.CommandCategory category : SubCommand.CommandCategory.values()) { builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", category.toString().toLowerCase()).replaceAll("%category_desc%", category.toString())); } - PlayerFunctions.sendMessage(player, builder.toString()); - return true; + return PlayerFunctions.sendMessage(player, builder.toString()); } final String cat = args[1]; SubCommand.CommandCategory cato = null; @@ -76,8 +77,7 @@ public class MainCommand implements CommandExecutor, TabCompleter { for (final SubCommand.CommandCategory category : SubCommand.CommandCategory.values()) { builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", category.toString().toLowerCase()).replaceAll("%category_desc%", category.toString())); } - PlayerFunctions.sendMessage(player, builder.toString()); - return true; + return PlayerFunctions.sendMessage(player, builder.toString()); } final StringBuilder help = new StringBuilder(); int page = 0; @@ -109,8 +109,7 @@ public class MainCommand implements CommandExecutor, TabCompleter { help.append(string).append("\n"); } - PlayerFunctions.sendMessage(player, help.toString()); - return true; + return PlayerFunctions.sendMessage(player, help.toString()); } else { for (final SubCommand command : subCommands) { @@ -122,8 +121,7 @@ public class MainCommand implements CommandExecutor, TabCompleter { return command.execute(player, arguments); } else { - PlayerFunctions.sendMessage(null, C.IS_CONSOLE); - return false; + return !PlayerFunctions.sendMessage(null, C.IS_CONSOLE); } } else { @@ -140,10 +138,9 @@ public class MainCommand implements CommandExecutor, TabCompleter { /* Let's try to get a proper usage string */ SubCommand command = (SubCommand) new StringComparsion(args[0], commands).getMatchObject(); - PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, command.usage.contains("plot") ? command.usage : "/plot " + command.usage); + return PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, command.usage.contains("plot") ? command.usage : "/plot " + command.usage); //PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new StringComparsion(args[0], commands).getBestMatch()); } - return false; } public static List getCommands(final SubCommand.CommandCategory category, final Player player) { @@ -212,7 +209,7 @@ public class MainCommand implements CommandExecutor, TabCompleter { if (strings.length < 1) { if ((strings.length == 0) || "plots".startsWith(s)) { - return Arrays.asList(new String[] { "plots" }); + return Arrays.asList("plots"); } } if (strings.length > 1) { @@ -221,7 +218,7 @@ public class MainCommand implements CommandExecutor, TabCompleter { if (!command.getLabel().equalsIgnoreCase("plots")) { return null; } - final List tabOptions = new ArrayList(); + final List tabOptions = new ArrayList<>(); final String arg = strings[0].toLowerCase(); for (final SubCommand cmd : subCommands) { if (cmd.permission.hasPermission(player)) {