From 9a41a989def5ae8980e8875f6297eba355fd78c0 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sat, 21 Feb 2015 16:48:49 +1100 Subject: [PATCH] main --- .../intellectualcrafters/plot/BukkitMain.java | 7 +- .../intellectualcrafters/plot/IPlotMain.java | 4 - .../plot/commands/BukkitCommand.java | 55 ++++++++++- .../plot/commands/CommandPermission.java | 3 +- .../plot/commands/MainCommand.java | 61 +++--------- .../plot/commands/SubCommand.java | 3 +- .../plot/listeners/InventoryListener.java | 1 + .../plot/util/MainUtil.java | 96 ++++++++++++++++--- 8 files changed, 158 insertions(+), 72 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index aa8241d28..00a0e3a88 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -21,6 +21,7 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; +import com.intellectualcrafters.plot.commands.BukkitCommand; import com.intellectualcrafters.plot.commands.Buy; import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualcrafters.plot.commands.WE_Anywhere; @@ -36,6 +37,7 @@ import com.intellectualcrafters.plot.listeners.PlayerEvents_1_8; import com.intellectualcrafters.plot.listeners.PlotPlusListener; import com.intellectualcrafters.plot.listeners.WorldEditListener; import com.intellectualcrafters.plot.object.PlotId; +import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.ConsoleColors; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.TaskManager; @@ -145,10 +147,11 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { @Override public void registerCommands() { final MainCommand command = new MainCommand(); + final BukkitCommand bcmd = new BukkitCommand(); final PluginCommand plotCommand = getCommand("plots"); - plotCommand.setExecutor(command); + plotCommand.setExecutor(bcmd); plotCommand.setAliases(Arrays.asList("p", "ps", "plotme", "plot")); - plotCommand.setTabCompleter(command); + plotCommand.setTabCompleter(bcmd); } @Override diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java index 1894c5d22..b00d4acad 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java @@ -42,8 +42,4 @@ public interface IPlotMain { public void getGenerator(String world, String name); public boolean callRemovePlot(String world, PlotId id); - - public boolean hasPermission(final PlotPlayer player, final String permission); - - public void sendMessage(final PlotPlayer player, final String message); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java index 9c6415e89..e6e435896 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/BukkitCommand.java @@ -1,19 +1,66 @@ package com.intellectualcrafters.plot.commands; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCommandExecutor; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; + +import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; /** * Created 2015-02-20 for PlotSquared * * @author Citymonstret */ -public class BukkitCommand implements CommandExecutor { +public class BukkitCommand implements CommandExecutor, TabCompleter { @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { - MainCommand - return false; + public boolean onCommand(CommandSender commandSender, Command command, String commandLabel, String[] args) { + Player player = null; + if (commandSender instanceof Player) { + player = (Player) commandSender; + } + return MainCommand.onCommand(BukkitUtil.getPlayer(player), commandLabel, args); + } + + @Override + public List onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] strings) { + if (!(commandSender instanceof Player)) { + return null; + } + final PlotPlayer player = BukkitUtil.getPlayer((Player) commandSender); + if (strings.length < 1) { + if ((strings.length == 0) || "plots".startsWith(s)) { + return Arrays.asList("plots"); + } + } + if (strings.length > 1) { + return null; + } + if (!command.getLabel().equalsIgnoreCase("plots")) { + return null; + } + final List tabOptions = new ArrayList<>(); + final String arg = strings[0].toLowerCase(); + for (final SubCommand cmd : MainCommand.subCommands) { + if (cmd.permission.hasPermission(player)) { + if (cmd.cmd.startsWith(arg)) { + tabOptions.add(cmd.cmd); + } else if (cmd.alias.get(0).startsWith(arg)) { + tabOptions.add(cmd.alias.get(0)); + } + } + } + if (tabOptions.size() > 0) { + return tabOptions; + } + return null; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CommandPermission.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CommandPermission.java index 3b6c827ec..17329e82f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CommandPermission.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/CommandPermission.java @@ -22,6 +22,7 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.BukkitMain; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.Permissions; /** * Created by Citymonstret on 2014-08-03. @@ -47,6 +48,6 @@ public class CommandPermission { * @return true of player has the required permission node */ public boolean hasPermission(final PlotPlayer player) { - return Permissions.hasPermission(BukkitUtil.getPlayer(player), this.permission); + return Permissions.hasPermission(player, this.permission); } } 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 8be6cf67d..c40b3a3b2 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -24,15 +24,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - import com.intellectualcrafters.plot.BukkitMain; import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.StringComparison; import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions; @@ -53,7 +50,7 @@ public class MainCommand { }; public static boolean no_permission(final PlotPlayer player, final String permission) { - MainUtil.sendMessage(BukkitUtil.getPlayer(player), C.NO_PERMISSION, permission); + MainUtil.sendMessage(player, C.NO_PERMISSION, permission); return false; } @@ -106,11 +103,11 @@ public class MainCommand { } private static String t(final String s) { - return ChatColor.translateAlternateColorCodes('&', s); + return MainUtil.colorise('&', s); } - public boolean onCommand(final PlotPlayer player, final String cmd, final String... args) { - if (!Permissions.hasPermission(BukkitUtil.getPlayer(player), PlotSquared.MAIN_PERMISSION)) { + public static boolean onCommand(final PlotPlayer player, final String cmd, final String... args) { + if (!Permissions.hasPermission(player, PlotSquared.MAIN_PERMISSION)) { return no_permission(player, PlotSquared.MAIN_PERMISSION); } if ((args.length < 1) || ((args.length >= 1) && (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("he")))) { @@ -121,7 +118,7 @@ public class MainCommand { builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", category.toString().toLowerCase()).replaceAll("%category_desc%", category.toString())); } builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", "all").replaceAll("%category_desc%", "Display all commands")); - return MainUtil.sendMessage(BukkitUtil.getPlayer(player), builder.toString()); + return MainUtil.sendMessage(player, builder.toString()); } final String cat = args[1]; SubCommand.CommandCategory cato = null; @@ -137,7 +134,7 @@ public class MainCommand { 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())); } - return MainUtil.sendMessage(BukkitUtil.getPlayer(player), builder.toString(), false); + return MainUtil.sendMessage(player, builder.toString(), false); } final StringBuilder help = new StringBuilder(); int page = 0; @@ -163,7 +160,7 @@ public class MainCommand { for (final String string : helpMenu(player, cato, page)) { help.append(string).append("\n"); } - player.sendMessage(ChatColor.translateAlternateColorCodes('&', help.toString())); + player.sendMessage(MainUtil.colorise('&', help.toString())); // return PlayerFunctions.sendMessage(player, help.toString()); } else { for (final SubCommand command : subCommands) { @@ -174,58 +171,24 @@ public class MainCommand { if ((player != null) || !command.isPlayer) { return command.execute(player, arguments); } else { - return !BukkitPlayerFunctions.sendMessage(null, C.IS_CONSOLE); + return !MainUtil.sendMessage(null, C.IS_CONSOLE); } } else { return no_permission(player, command.permission.permission.toLowerCase()); } } } - MainUtil.sendMessage(BukkitUtil.getPlayer(player), C.NOT_VALID_SUBCOMMAND); + MainUtil.sendMessage(player, C.NOT_VALID_SUBCOMMAND); final String[] commands = new String[subCommands.size()]; for (int x = 0; x < subCommands.size(); x++) { commands[x] = subCommands.get(x).cmd; } /* Let's try to get a proper usage string */ final String command = new StringComparison(args[0], commands).getBestMatch(); - return MainUtil.sendMessage(BukkitUtil.getPlayer(player), C.DID_YOU_MEAN, "/plot " + command); + return MainUtil.sendMessage(player, C.DID_YOU_MEAN, "/plot " + command); // PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new // StringComparsion(args[0], commands).getBestMatch()); } return true; } - - @Override - public List onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] strings) { - if (!(commandSender instanceof Player)) { - return null; - } - final Player player = (Player) commandSender; - if (strings.length < 1) { - if ((strings.length == 0) || "plots".startsWith(s)) { - return Arrays.asList("plots"); - } - } - if (strings.length > 1) { - return null; - } - if (!command.getLabel().equalsIgnoreCase("plots")) { - return null; - } - final List tabOptions = new ArrayList<>(); - final String arg = strings[0].toLowerCase(); - for (final SubCommand cmd : subCommands) { - if (cmd.permission.hasPermission(player)) { - if (cmd.cmd.startsWith(arg)) { - tabOptions.add(cmd.cmd); - } else if (cmd.alias.get(0).startsWith(arg)) { - tabOptions.add(cmd.alias.get(0)); - } - } - } - if (tabOptions.size() > 0) { - return tabOptions; - } - return null; - } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java index 03d625b7d..f2b83b4dc 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SubCommand.java @@ -25,6 +25,7 @@ import java.util.Arrays; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions; /** @@ -148,7 +149,7 @@ public abstract class SubCommand { * com.intellectualcrafters.plot.config.C, String...) */ public boolean sendMessage(final PlotPlayer plr, final C c, final String... args) { - BukkitPlayerFunctions.sendMessage(plr, c, args); + MainUtil.sendMessage(plr, c, args); return true; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/InventoryListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/InventoryListener.java index b7ca8cfb4..8ec12926f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/InventoryListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/InventoryListener.java @@ -10,6 +10,7 @@ import org.bukkit.inventory.Inventory; import com.intellectualcrafters.plot.object.InfoInventory; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; /** * Created 2014-11-18 for PlotSquared diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index fab915f6c..14d23a3f1 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -24,9 +24,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.UUID; -import org.bukkit.ChatColor; -import org.bukkit.util.ChatPaginator; - import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; @@ -61,8 +58,8 @@ public class MainUtil { final Plot bot = MainUtil.getBottomPlot(plot); // TODO -// boolean result = PlotSquared.IMP.callPlayerTeleportToPlotEvent(player, from, plot); - boolean result = true; + // boolean result = PlotSquared.IMP.callPlayerTeleportToPlotEvent(player, from, plot); + final boolean result = true; // TOOD ^ remove that if (!result) { @@ -794,18 +791,96 @@ public class MainUtil { public static boolean sendMessage(final PlotPlayer plr, final String msg) { return sendMessage(plr, msg, true); } - + + public static String colorise(final char alt, final String message) { + final char[] b = message.toCharArray(); + for (int i = 0; i < (b.length - 1); i++) { + if ((b[i] == alt) && ("0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[(i + 1)]) > -1)) { + b[i] = '§'; + b[(i + 1)] = Character.toLowerCase(b[(i + 1)]); + } + } + return new String(b); + } + public static boolean sendMessage(final PlotPlayer plr, final String msg, final boolean prefix) { if ((msg.length() > 0) && !msg.equals("")) { if (plr == null) { PlotSquared.log(C.PREFIX.s() + msg); } else { - sendMessageWrapped(plr, ChatColor.translateAlternateColorCodes('&', C.PREFIX.s() + msg)); + sendMessageWrapped(plr, colorise('&', C.PREFIX.s() + msg)); } } return true; } + public static String[] wordWrap(final String rawString, final int lineLength) { + if (rawString == null) { + return new String[] { "" }; + } + if ((rawString.length() <= lineLength) && (!rawString.contains("\n"))) { + return new String[] { rawString }; + } + final char[] rawChars = (rawString + ' ').toCharArray(); + StringBuilder word = new StringBuilder(); + StringBuilder line = new StringBuilder(); + final ArrayList lines = new ArrayList(); + int lineColorChars = 0; + for (int i = 0; i < rawChars.length; i++) { + final char c = rawChars[i]; + if (c == '§') { + word.append('§' + (rawChars[(i + 1)])); + lineColorChars += 2; + i++; + } else if ((c == ' ') || (c == '\n')) { + if ((line.length() == 0) && (word.length() > lineLength)) { + for (final String partialWord : word.toString().split("(?<=\\G.{" + lineLength + "})")) { + lines.add(partialWord); + } + } else if (((line.length() + word.length()) - lineColorChars) == lineLength) { + line.append(word); + lines.add(line.toString()); + line = new StringBuilder(); + lineColorChars = 0; + } else if (((line.length() + 1 + word.length()) - lineColorChars) > lineLength) { + for (final String partialWord : word.toString().split("(?<=\\G.{" + lineLength + "})")) { + lines.add(line.toString()); + line = new StringBuilder(partialWord); + } + lineColorChars = 0; + } else { + if (line.length() > 0) { + line.append(' '); + } + line.append(word); + } + word = new StringBuilder(); + if (c == '\n') { + lines.add(line.toString()); + line = new StringBuilder(); + } + } else { + word.append(c); + } + } + if (line.length() > 0) { + lines.add(line.toString()); + } + if ((lines.get(0).length() == 0) || (lines.get(0).charAt(0) != '§')) { + lines.set(0, "§f" + lines.get(0)); + } + for (int i = 1; i < lines.size(); i++) { + final String pLine = lines.get(i - 1); + final String subLine = lines.get(i); + + final char color = pLine.charAt(pLine.lastIndexOf('§') + 1); + if ((subLine.length() == 0) || (subLine.charAt(0) != '§')) { + lines.set(i, '§' + (color) + subLine); + } + } + return lines.toArray(new String[lines.size()]); + } + /** * \\previous\\ * @@ -813,8 +888,8 @@ public class MainUtil { * @param msg Was used to wrap the chat client length (Packets out--) */ public static void sendMessageWrapped(final PlotPlayer plr, String msg) { - if (msg.length() > ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH) { - final String[] ss = ChatPaginator.wordWrap(msg, ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH); + if (msg.length() > 65) { + final String[] ss = wordWrap(msg, 65); final StringBuilder b = new StringBuilder(); for (final String p : ss) { b.append(p).append(p.equals(ss[ss.length - 1]) ? "" : "\n "); @@ -905,7 +980,6 @@ public class MainUtil { return new Plot(id, null, new ArrayList(), new ArrayList(), world); } - /** * Returns the plot at a location (mega plots are not considered, all plots are treated as small plots) * @param loc @@ -953,7 +1027,7 @@ public class MainUtil { } public static Plot getPlot(final Location loc) { - PlotId id = getPlotId(loc); + final PlotId id = getPlotId(loc); if (id == null) { return null; }