From 00a4f61a68222804504a8c7bc0dd38a46042a4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Mon, 10 Jun 2019 21:25:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=AE=B6=E5=91=BD=E4=BB=A4=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E4=BC=98=E5=8C=96=E6=9B=B4=E6=96=B0=EF=BC=8C=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=B8=A4=E4=B8=AA=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../commands/internal/BaseMainCommand.java | 24 ++- .../internal/type/CommandArgument.java | 20 ++- .../commands/internal/type/CommandTab.java | 14 ++ .../locale/TabooLibLocaleCommand.java | 3 +- .../plugin/TabooLibPluginCommand.java | 28 ++-- .../skymc/taboolib/cronus/CronusParser.java | 70 ++++++++ .../me/skymc/taboolib/cronus/CronusUtils.java | 153 ++++++++++++++++++ .../taboolib/cronus/bukkit/ItemStack.java | 79 +++++++++ .../taboolib/cronus/bukkit/Location.java | 65 ++++++++ .../inventory/builder/v2/MenuBuilder.java | 2 +- 11 files changed, 438 insertions(+), 22 deletions(-) create mode 100644 src/main/scala/me/skymc/taboolib/commands/internal/type/CommandTab.java create mode 100644 src/main/scala/me/skymc/taboolib/cronus/CronusParser.java create mode 100644 src/main/scala/me/skymc/taboolib/cronus/CronusUtils.java create mode 100644 src/main/scala/me/skymc/taboolib/cronus/bukkit/ItemStack.java create mode 100644 src/main/scala/me/skymc/taboolib/cronus/bukkit/Location.java diff --git a/build.gradle b/build.gradle index 109b036..4e58a02 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'com.github.johnrengelman.shadow' version '4.0.4' } group = 'me.skymc' -version = '4.82' +version = '4.83' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java b/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java index 72d2031..6f10d22 100644 --- a/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java +++ b/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java @@ -1,12 +1,11 @@ package me.skymc.taboolib.commands.internal; import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; import com.ilummc.tlib.resources.TLocale; import me.skymc.taboolib.Main; import me.skymc.taboolib.TabooLib; -import me.skymc.taboolib.commands.internal.type.CommandField; -import me.skymc.taboolib.commands.internal.type.CommandRegister; -import me.skymc.taboolib.commands.internal.type.CommandType; +import me.skymc.taboolib.commands.internal.type.*; import me.skymc.taboolib.string.ArrayUtils; import me.skymc.taboolib.string.StringUtils; import org.bukkit.Bukkit; @@ -113,7 +112,24 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { @Override public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { - return args.length == 1 ? subCommands.stream().filter(subCommand -> !hideInHelp(subCommand) && hasPermission(commandSender, subCommand) && (args[0].isEmpty() || subCommand.getLabel().toLowerCase().startsWith(args[0].toLowerCase()))).map(BaseSubCommand::getLabel).collect(Collectors.toList()) : null; + if (args.length == 1) { + List label = Lists.newArrayList(); + subCommands.stream().filter(subCommand -> !hideInHelp(subCommand) && hasPermission(commandSender, subCommand)).forEach(l -> { + label.add(l.getLabel()); + label.addAll(Lists.newArrayList(l.getAliases())); + }); + return label.stream().filter(l -> args[0].isEmpty() || l.toLowerCase().startsWith(args[0].toLowerCase())).collect(Collectors.toList()); + } + for (BaseSubCommand subCommand : subCommands) { + CommandArgument[] arguments = subCommand.getArguments(); + if (args[0].equalsIgnoreCase(subCommand.getLabel()) && args.length - 1 <= arguments.length) { + CommandTab commandTab = arguments[args.length - 2].getTab(); + if (commandTab != null) { + return commandTab.run().stream().filter(l -> args[args.length - 1].isEmpty() || l.toLowerCase().startsWith(args[args.length - 1].toLowerCase())).collect(Collectors.toList()); + } + } + } + return null; } @Override diff --git a/src/main/scala/me/skymc/taboolib/commands/internal/type/CommandArgument.java b/src/main/scala/me/skymc/taboolib/commands/internal/type/CommandArgument.java index 410df75..20b66a3 100644 --- a/src/main/scala/me/skymc/taboolib/commands/internal/type/CommandArgument.java +++ b/src/main/scala/me/skymc/taboolib/commands/internal/type/CommandArgument.java @@ -12,6 +12,7 @@ public class CommandArgument { private String name; private boolean required; + private CommandTab tab; public String getName() { return name; @@ -21,13 +22,26 @@ public class CommandArgument { return required; } + public CommandTab getTab() { + return tab; + } + public CommandArgument(String name) { this(name, true); } + public CommandArgument(String name, CommandTab tab) { + this(name, true, tab); + } + public CommandArgument(String name, boolean required) { + this(name, required, null); + } + + public CommandArgument(String name, boolean required, CommandTab tab) { this.name = name; this.required = required; + this.tab = tab; } @Override @@ -44,11 +58,13 @@ public class CommandArgument { return false; } CommandArgument that = (CommandArgument) o; - return Objects.equals(getName(), that.getName()) && isRequired() == that.isRequired(); + return isRequired() == that.isRequired() && + Objects.equals(getName(), that.getName()) && + Objects.equals(tab, that.tab); } @Override public int hashCode() { - return Objects.hash(getName(), isRequired()); + return Objects.hash(getName(), isRequired(), tab); } } diff --git a/src/main/scala/me/skymc/taboolib/commands/internal/type/CommandTab.java b/src/main/scala/me/skymc/taboolib/commands/internal/type/CommandTab.java new file mode 100644 index 0000000..bfa552d --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/commands/internal/type/CommandTab.java @@ -0,0 +1,14 @@ +package me.skymc.taboolib.commands.internal.type; + +import java.util.List; + +/** + * 为了防止与 TabExecutor 混淆所以名称改为 CompleterTab + * + * @author sky + */ +public interface CommandTab { + + List run(); + +} \ No newline at end of file diff --git a/src/main/scala/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java b/src/main/scala/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java index da87278..855743e 100644 --- a/src/main/scala/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java +++ b/src/main/scala/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java @@ -8,7 +8,6 @@ import me.skymc.taboolib.commands.internal.TCommand; import me.skymc.taboolib.commands.internal.type.CommandArgument; import me.skymc.taboolib.commands.internal.type.CommandRegister; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -105,7 +104,7 @@ public class TabooLibLocaleCommand extends BaseMainCommand { target.forEach(x -> TLocale.sendTo(x, args[1], arguments)); } - if (sender instanceof Player && ((Player) sender).getItemInHand().getType().equals(Material.COMMAND)) { + if (sender instanceof Player && ((Player) sender).getItemInHand().getType().name().contains("COMMAND")) { TLocale.sendTo(sender, "COMMANDS.TLOCALE.SEND.SUCCESS-SEND", String.valueOf(System.currentTimeMillis() - time)); } } diff --git a/src/main/scala/me/skymc/taboolib/commands/plugin/TabooLibPluginCommand.java b/src/main/scala/me/skymc/taboolib/commands/plugin/TabooLibPluginCommand.java index 56f8b2f..79c7a67 100644 --- a/src/main/scala/me/skymc/taboolib/commands/plugin/TabooLibPluginCommand.java +++ b/src/main/scala/me/skymc/taboolib/commands/plugin/TabooLibPluginCommand.java @@ -37,16 +37,16 @@ public class TabooLibPluginCommand extends BaseMainCommand { return TLocale.asString("COMMANDS.TPLUGIN.COMMAND-TITLE"); } - @Override - public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { - if (args.length == 1) { - return getSubCommands().stream().filter(internalCommandExecutor -> internalCommandExecutor != null && (args[0].isEmpty() || internalCommandExecutor.getLabel().toLowerCase().startsWith(args[0].toLowerCase()))).map(BaseSubCommand::getLabel).collect(Collectors.toList()); - } else if (args.length > 1 && isPluginCommand(args[0])) { - return Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(x -> !PluginUtils.isIgnored(x)).collect(Collectors.toList()).stream().filter(plugin -> args[1].isEmpty() || plugin.getName().toLowerCase().startsWith(args[1].toLowerCase())).map(Plugin::getName).collect(Collectors.toList()); - } else { - return null; - } - } +// @Override +// public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { +// if (args.length == 1) { +// return getSubCommands().stream().filter(internalCommandExecutor -> internalCommandExecutor != null && (args[0].isEmpty() || internalCommandExecutor.getLabel().toLowerCase().startsWith(args[0].toLowerCase()))).map(BaseSubCommand::getLabel).collect(Collectors.toList()); +// } else if (args.length > 1 && isPluginCommand(args[0])) { +// return Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(x -> !PluginUtils.isIgnored(x)).collect(Collectors.toList()).stream().filter(plugin -> args[1].isEmpty() || plugin.getName().toLowerCase().startsWith(args[1].toLowerCase())).map(Plugin::getName).collect(Collectors.toList()); +// } else { +// return null; +// } +// } @CommandRegister(priority = 1) BaseSubCommand load = new BaseSubCommand() { @@ -113,7 +113,9 @@ public class TabooLibPluginCommand extends BaseMainCommand { @Override public CommandArgument[] getArguments() { - return new CommandArgument[] {new CommandArgument(TLocale.asString("COMMANDS.TPLUGIN.UNLOAD.ARGUMENTS.0"), true)}; + return new CommandArgument[] {new CommandArgument(TLocale.asString("COMMANDS.TPLUGIN.UNLOAD.ARGUMENTS.0"), true, () -> { + return Arrays.stream(Bukkit.getPluginManager().getPlugins()).map(Plugin::getName).collect(Collectors.toList()); + })}; } @Override @@ -155,7 +157,9 @@ public class TabooLibPluginCommand extends BaseMainCommand { @Override public CommandArgument[] getArguments() { - return new CommandArgument[] {new CommandArgument(TLocale.asString("COMMANDS.TPLUGIN.RELOAD.ARGUMENTS.0"), true)}; + return new CommandArgument[] {new CommandArgument(TLocale.asString("COMMANDS.TPLUGIN.RELOAD.ARGUMENTS.0"), true, () -> { + return Arrays.stream(Bukkit.getPluginManager().getPlugins()).map(Plugin::getName).collect(Collectors.toList()); + })}; } @Override diff --git a/src/main/scala/me/skymc/taboolib/cronus/CronusParser.java b/src/main/scala/me/skymc/taboolib/cronus/CronusParser.java new file mode 100644 index 0000000..937d1df --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/cronus/CronusParser.java @@ -0,0 +1,70 @@ +package me.skymc.taboolib.cronus; + +import me.skymc.taboolib.cronus.bukkit.ItemStack; +import me.skymc.taboolib.cronus.bukkit.Location; +import org.bukkit.Bukkit; +import org.bukkit.util.NumberConversions; + +import java.util.Arrays; + +/** + * @Author 坏黑 + * @Since 2019-05-23 22:43 + */ +public class CronusParser { + + public static Location toLocation(Object in) { + String str = String.valueOf(in); + if (str.toLowerCase().startsWith("area=")) { + String[] area = str.substring("area=".length()).split("~"); + return new Location(Location.Mode.AREA, new org.bukkit.Location[] {toBukkitLocation(area[0]), toBukkitLocation(area[area.length > 1 ? 1 : 0])}, null); + } else { + return new Location(Location.Mode.POINT, null, Arrays.stream(str.split(";")).map(CronusParser::toBukkitLocation).toArray(org.bukkit.Location[]::new)); + } + } + + public static ItemStack toItemStack(Object in) { + String type = null; + String name = null; + String lore = null; + int damage = -1; + int amount = 1; + for (String v : String.valueOf(in).split(",")) { + if (v.toLowerCase().startsWith("type=")) { + type = v.substring("type=".length()); + } else if (v.toLowerCase().startsWith("t=")) { + type = v.substring("t=".length()); + } else if (v.toLowerCase().startsWith("name=")) { + name = v.substring("name=".length()); + } else if (v.toLowerCase().startsWith("n=")) { + name = v.substring("n=".length()); + } else if (v.toLowerCase().startsWith("lore=")) { + lore = v.substring("lore=".length()); + } else if (v.toLowerCase().startsWith("l=")) { + lore = v.substring("l=".length()); + } else if (v.toLowerCase().startsWith("damage=")) { + damage = NumberConversions.toInt(v.substring("damage=".length())); + } else if (v.toLowerCase().startsWith("d=")) { + damage = NumberConversions.toInt(v.substring("d=".length())); + } else if (v.toLowerCase().startsWith("amount=")) { + amount = NumberConversions.toInt(v.substring("amount=".length())); + } else if (v.toLowerCase().startsWith("a=")) { + amount = NumberConversions.toInt(v.substring("a=".length())); + } else { + type = v; + } + } + return new ItemStack(type, name, lore, damage, amount); + } + + public static org.bukkit.Location toBukkitLocation(Object in) { + String[] v = String.valueOf(in).split(","); + return new org.bukkit.Location( + v.length > 0 ? Bukkit.getWorld(v[0]) : Bukkit.getWorlds().iterator().next(), + v.length > 1 ? NumberConversions.toDouble(v[1]) : 0, + v.length > 2 ? NumberConversions.toDouble(v[2]) : 0, + v.length > 3 ? NumberConversions.toDouble(v[3]) : 0, + v.length > 4 ? NumberConversions.toFloat(v[4]) : 0, + v.length > 5 ? NumberConversions.toFloat(v[5]) : 0); + } +} diff --git a/src/main/scala/me/skymc/taboolib/cronus/CronusUtils.java b/src/main/scala/me/skymc/taboolib/cronus/CronusUtils.java new file mode 100644 index 0000000..fa8b483 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/cronus/CronusUtils.java @@ -0,0 +1,153 @@ +package me.skymc.taboolib.cronus; + +import com.ilummc.tlib.util.Strings; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.NumberConversions; + +import java.util.Optional; + +/** + * @Author 坏黑 + * @Since 2019-05-11 15:42 + */ +public class CronusUtils { + + public static void addItem(Player player, ItemStack item) { + player.getInventory().addItem(item).values().forEach(e -> player.getWorld().dropItem(player.getLocation(), e)); + } + + public static String NonNull(String in) { + return Strings.isBlank(in) ? "-" : in; + } + + public static ItemStack NonNull(ItemStack itemStack) { + return Optional.ofNullable(itemStack).orElse(new ItemStack(Material.STONE)); + } + + public static org.bukkit.inventory.ItemStack getUsingItem(Player player, Material material) { + return player.getItemInHand().getType() == material ? player.getItemInHand() : player.getInventory().getItemInOffHand(); + } + + public static boolean isInt(String in) { + try { + Integer.parseInt(in); + return true; + } catch (Throwable ignored) { + } + return false; + } + + public static boolean isDouble(String in) { + try { + Double.parseDouble(in); + return true; + } catch (Throwable ignored) { + } + return false; + } + + public static boolean isBoolean(String in) { + try { + Boolean.parseBoolean(in); + return true; + } catch (Throwable ignored) { + } + return false; + } + + public static void setTotalExperience(Player player, int exp) { + player.setExp(0); + player.setLevel(0); + player.setTotalExperience(0); + int amount = exp; + while (amount > 0) { + int expToLevel = getExpAtLevel(player); + amount -= expToLevel; + if (amount >= 0) { + player.giveExp(expToLevel); + } else { + amount += expToLevel; + player.giveExp(amount); + amount = 0; + } + } + } + + private static int getExpAtLevel(Player player) { + return getExpAtLevel(player.getLevel()); + } + + public static int getExpAtLevel(int level) { + if (level <= 15) { + return (2 * level) + 7; + } + if ((level >= 16) && (level <= 30)) { + return (5 * level) - 38; + } + return (9 * level) - 158; + + } + + public static int getExpToLevel(int level) { + int currentLevel = 0; + int exp = 0; + while (currentLevel < level) { + exp += getExpAtLevel(currentLevel); + currentLevel++; + } + if (exp < 0) { + exp = Integer.MAX_VALUE; + } + return exp; + } + + public static int getTotalExperience(Player player) { + int exp = Math.round(getExpAtLevel(player) * player.getExp()); + int currentLevel = player.getLevel(); + + while (currentLevel > 0) { + currentLevel--; + exp += getExpAtLevel(currentLevel); + } + if (exp < 0) { + exp = Integer.MAX_VALUE; + } + return exp; + } + + public static int getExpUntilNextLevel(Player player) { + int exp = Math.round(getExpAtLevel(player) * player.getExp()); + int nextLevel = player.getLevel(); + return getExpAtLevel(nextLevel) - exp; + } + + public static long toMillis(String in) { + long time = 0; + StringBuilder current = new StringBuilder(); + for (String charAt : in.toLowerCase().split("")) { + if (isInt(charAt)) { + current.append(charAt); + } else { + switch (charAt) { + case "d": + time += NumberConversions.toInt(current.toString()) * 24L * 60L * 60L * 1000L; + break; + case "h": + time += NumberConversions.toInt(current.toString()) * 60L * 60L * 1000L; + break; + case "m": + time += NumberConversions.toInt(current.toString()) * 60L * 1000L; + break; + case "s": + time += NumberConversions.toInt(current.toString()) * 1000L; + break; + } + current = new StringBuilder(); + } + } + return time; + } + +} diff --git a/src/main/scala/me/skymc/taboolib/cronus/bukkit/ItemStack.java b/src/main/scala/me/skymc/taboolib/cronus/bukkit/ItemStack.java new file mode 100644 index 0000000..128d39a --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/cronus/bukkit/ItemStack.java @@ -0,0 +1,79 @@ +package me.skymc.taboolib.cronus.bukkit; + +import me.skymc.taboolib.inventory.ItemUtils; + +/** + * @Author 坏黑 + * @Since 2019-05-23 22:45 + */ +public class ItemStack { + + private String type; + private String name; + private String lore; + private int damage; + private int amount; + + public ItemStack(String type, String name, String lore, int damage, int amount) { + this.type = type; + this.name = name; + this.lore = lore; + this.damage = damage; + this.amount = amount; + } + + public boolean isType(org.bukkit.inventory.ItemStack itemStack) { + return type == null || itemStack.getType().name().equalsIgnoreCase(type); + } + + public boolean isName(org.bukkit.inventory.ItemStack itemStack) { + return name == null || ItemUtils.getCustomName(itemStack).contains(name); + } + + public boolean isLore(org.bukkit.inventory.ItemStack itemStack) { + return lore == null || ItemUtils.hasLore(itemStack, lore); + } + + public boolean isDamage(org.bukkit.inventory.ItemStack itemStack) { + return damage == -1 || itemStack.getDurability() == damage; + } + + public boolean isAmount(org.bukkit.inventory.ItemStack itemStack) { + return itemStack.getAmount() >= amount; + } + + public boolean isItem(org.bukkit.inventory.ItemStack itemStack) { + return isType(itemStack) && isName(itemStack) && isLore(itemStack) && isDamage(itemStack) && isAmount(itemStack); + } + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + public String getLore() { + return lore; + } + + public int getAmount() { + return amount; + } + + public int getDamage() { + return damage; + } + + @Override + public String toString() { + return "ItemStack{" + + "type='" + type + '\'' + + ", name='" + name + '\'' + + ", lore='" + lore + '\'' + + ", damage=" + damage + + ", amount=" + amount + + '}'; + } +} diff --git a/src/main/scala/me/skymc/taboolib/cronus/bukkit/Location.java b/src/main/scala/me/skymc/taboolib/cronus/bukkit/Location.java new file mode 100644 index 0000000..f5392ec --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/cronus/bukkit/Location.java @@ -0,0 +1,65 @@ +package me.skymc.taboolib.cronus.bukkit; + +import java.util.Arrays; + +/** + * @Author 坏黑 + * @Since 2019-05-24 14:09 + */ +public class Location { + + private Mode mode; + private org.bukkit.Location[] area; + private org.bukkit.Location[] points; + + public Location(Mode mode, org.bukkit.Location[] area, org.bukkit.Location[] points) { + this.mode = mode; + this.area = area; + this.points = points; + } + + public org.bukkit.Location toBukkit() { + return points[0]; + } + + public boolean isBukkit() { + try { + return toBukkit() != null; + } catch (Throwable ignored) { + } + return false; + } + + public boolean inSelect(org.bukkit.Location location) { + if (!isSelectWorld(location)) { + return false; + } + if (mode == Mode.AREA) { + return location.toVector().isInAABB(area[0].toVector(), area[1].toVector()); + } else { + return Arrays.asList(points).contains(location); + } + } + + public boolean isSelectWorld(org.bukkit.Location location) { + if (mode == Mode.AREA) { + return location.getWorld().equals(area[0].getWorld()); + } else { + return Arrays.stream(points).anyMatch(p -> p.getWorld().equals(location.getWorld())); + } + } + + public enum Mode { + + AREA, POINT + } + + @Override + public String toString() { + return "Location{" + + "mode=" + mode + + ", area=" + Arrays.toString(area) + + ", points=" + Arrays.toString(points) + + '}'; + } +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java index 930284a..0e98b75 100644 --- a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java @@ -21,7 +21,7 @@ public class MenuBuilder { private Plugin plugin; private String title; private int rows; - private char[][] items; + private char[][] items ; private ClickTask clickTask; private CloseTask closeTask; private boolean lockHand;