本家命令工具优化更新,合并两个工具

This commit is contained in:
坏黑 2019-06-10 21:25:25 +08:00
parent 4b1cbd6349
commit 00a4f61a68
11 changed files with 438 additions and 22 deletions

View File

@ -5,7 +5,7 @@ plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4' id 'com.github.johnrengelman.shadow' version '4.0.4'
} }
group = 'me.skymc' group = 'me.skymc'
version = '4.82' version = '4.83'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
targetCompatibility = 1.8 targetCompatibility = 1.8

View File

@ -1,12 +1,11 @@
package me.skymc.taboolib.commands.internal; package me.skymc.taboolib.commands.internal;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.ilummc.tlib.resources.TLocale; import com.ilummc.tlib.resources.TLocale;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.commands.internal.type.CommandField; import me.skymc.taboolib.commands.internal.type.*;
import me.skymc.taboolib.commands.internal.type.CommandRegister;
import me.skymc.taboolib.commands.internal.type.CommandType;
import me.skymc.taboolib.string.ArrayUtils; import me.skymc.taboolib.string.ArrayUtils;
import me.skymc.taboolib.string.StringUtils; import me.skymc.taboolib.string.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -113,7 +112,24 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
@Override @Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { public List<String> 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<String> 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 @Override

View File

@ -12,6 +12,7 @@ public class CommandArgument {
private String name; private String name;
private boolean required; private boolean required;
private CommandTab tab;
public String getName() { public String getName() {
return name; return name;
@ -21,13 +22,26 @@ public class CommandArgument {
return required; return required;
} }
public CommandTab getTab() {
return tab;
}
public CommandArgument(String name) { public CommandArgument(String name) {
this(name, true); this(name, true);
} }
public CommandArgument(String name, CommandTab tab) {
this(name, true, tab);
}
public CommandArgument(String name, boolean required) { public CommandArgument(String name, boolean required) {
this(name, required, null);
}
public CommandArgument(String name, boolean required, CommandTab tab) {
this.name = name; this.name = name;
this.required = required; this.required = required;
this.tab = tab;
} }
@Override @Override
@ -44,11 +58,13 @@ public class CommandArgument {
return false; return false;
} }
CommandArgument that = (CommandArgument) o; 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 @Override
public int hashCode() { public int hashCode() {
return Objects.hash(getName(), isRequired()); return Objects.hash(getName(), isRequired(), tab);
} }
} }

View File

@ -0,0 +1,14 @@
package me.skymc.taboolib.commands.internal.type;
import java.util.List;
/**
* 为了防止与 TabExecutor 混淆所以名称改为 CompleterTab
*
* @author sky
*/
public interface CommandTab {
List<String> run();
}

View File

@ -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.CommandArgument;
import me.skymc.taboolib.commands.internal.type.CommandRegister; import me.skymc.taboolib.commands.internal.type.CommandRegister;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -105,7 +104,7 @@ public class TabooLibLocaleCommand extends BaseMainCommand {
target.forEach(x -> TLocale.sendTo(x, args[1], arguments)); 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)); TLocale.sendTo(sender, "COMMANDS.TLOCALE.SEND.SUCCESS-SEND", String.valueOf(System.currentTimeMillis() - time));
} }
} }

View File

@ -37,16 +37,16 @@ public class TabooLibPluginCommand extends BaseMainCommand {
return TLocale.asString("COMMANDS.TPLUGIN.COMMAND-TITLE"); return TLocale.asString("COMMANDS.TPLUGIN.COMMAND-TITLE");
} }
@Override // @Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { // public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {
if (args.length == 1) { // 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()); // 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])) { // } 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()); // 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 { // } else {
return null; // return null;
} // }
} // }
@CommandRegister(priority = 1) @CommandRegister(priority = 1)
BaseSubCommand load = new BaseSubCommand() { BaseSubCommand load = new BaseSubCommand() {
@ -113,7 +113,9 @@ public class TabooLibPluginCommand extends BaseMainCommand {
@Override @Override
public CommandArgument[] getArguments() { 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 @Override
@ -155,7 +157,9 @@ public class TabooLibPluginCommand extends BaseMainCommand {
@Override @Override
public CommandArgument[] getArguments() { 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 @Override

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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 +
'}';
}
}

View File

@ -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) +
'}';
}
}

View File

@ -21,7 +21,7 @@ public class MenuBuilder {
private Plugin plugin; private Plugin plugin;
private String title; private String title;
private int rows; private int rows;
private char[][] items; private char[][] items ;
private ClickTask clickTask; private ClickTask clickTask;
private CloseTask closeTask; private CloseTask closeTask;
private boolean lockHand; private boolean lockHand;