+ update SimpleVersionControl
This commit is contained in:
@@ -60,13 +60,13 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
}
|
||||
});
|
||||
}
|
||||
baseMainCommand.getLinkClasses().forEach(clazz -> java.util.Arrays.stream(clazz.getDeclaredFields()).filter(field -> field.getAnnotation(SubCommand.class) != null && field.getType().equals(io.izzel.taboolib.module.command.base.BaseSubCommand.class)).forEach(field -> fields.add(new CommandField(field, clazz))));
|
||||
baseMainCommand.getLinkClasses().forEach(clazz -> java.util.Arrays.stream(clazz.getDeclaredFields()).filter(field -> field.getAnnotation(SubCommand.class) != null && field.getType().equals(BaseSubCommand.class)).forEach(field -> fields.add(new CommandField(field, clazz))));
|
||||
if (fields.size() > 0) {
|
||||
fields.sort(Comparator.comparingDouble(commandField -> commandField.getField().getAnnotation(SubCommand.class).priority()));
|
||||
fields.forEach(commandField -> {
|
||||
try {
|
||||
commandField.getField().setAccessible(true);
|
||||
io.izzel.taboolib.module.command.base.BaseSubCommand subCommand = (io.izzel.taboolib.module.command.base.BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance());
|
||||
BaseSubCommand subCommand = (BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance());
|
||||
subCommand.setLabel(commandField.getField().getName());
|
||||
baseMainCommand.registerSubCommand(subCommand);
|
||||
} catch (Exception ignored) {
|
||||
@@ -90,11 +90,11 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
return linkClasses;
|
||||
}
|
||||
|
||||
public List<io.izzel.taboolib.module.command.base.BaseSubCommand> getSubCommands() {
|
||||
public List<BaseSubCommand> getSubCommands() {
|
||||
return subCommands;
|
||||
}
|
||||
|
||||
public void registerSubCommand(io.izzel.taboolib.module.command.base.BaseSubCommand subCommand) {
|
||||
public void registerSubCommand(BaseSubCommand subCommand) {
|
||||
if (subCommand != null) {
|
||||
Preconditions.checkArgument(subCommand.getLabel() != null, "Command label can not be null");
|
||||
Preconditions.checkArgument(subCommand.getArguments() != null, "Command arguments can not be null");
|
||||
@@ -122,7 +122,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
});
|
||||
return label.stream().filter(l -> args[0].isEmpty() || l.toLowerCase().startsWith(args[0].toLowerCase())).collect(Collectors.toList());
|
||||
}
|
||||
for (io.izzel.taboolib.module.command.base.BaseSubCommand subCommand : subCommands) {
|
||||
for (BaseSubCommand subCommand : subCommands) {
|
||||
Argument[] arguments = subCommand.getArguments();
|
||||
if (args[0].equalsIgnoreCase(subCommand.getLabel()) && args.length - 1 <= arguments.length) {
|
||||
CommandTab commandTab = arguments[args.length - 2].getTab();
|
||||
@@ -139,7 +139,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
if (args.length == 0) {
|
||||
onCommandHelp(sender, command, label, args);
|
||||
} else {
|
||||
for (io.izzel.taboolib.module.command.base.BaseSubCommand subCommand : subCommands) {
|
||||
for (BaseSubCommand subCommand : subCommands) {
|
||||
if (subCommand == null || !(args[0].equalsIgnoreCase(subCommand.getLabel()) || java.util.Arrays.stream(subCommand.getAliases()).anyMatch(args[0]::equalsIgnoreCase)) || !hasPermission(sender, subCommand)) {
|
||||
continue;
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
List<io.izzel.taboolib.module.command.base.BaseSubCommand> commandCompute = subCommands.stream().filter(x -> x != null && hasPermission(sender, x)).sorted((b, a) -> Double.compare(Strings.similarDegree(args[0], a.getLabel()), Strings.similarDegree(args[0], b.getLabel()))).collect(Collectors.toList());
|
||||
List<BaseSubCommand> commandCompute = subCommands.stream().filter(x -> x != null && hasPermission(sender, x)).sorted((b, a) -> Double.compare(Strings.similarDegree(args[0], a.getLabel()), Strings.similarDegree(args[0], b.getLabel()))).collect(Collectors.toList());
|
||||
if (commandCompute.size() > 0) {
|
||||
TLocale.sendTo(sender, "COMMANDS.INTERNAL.ERROR-COMMAND", args[0], commandCompute.get(0).getCommandString(label).trim());
|
||||
}
|
||||
@@ -223,7 +223,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hideInHelp(io.izzel.taboolib.module.command.base.BaseSubCommand baseSubCommand) {
|
||||
private boolean hideInHelp(BaseSubCommand baseSubCommand) {
|
||||
return baseSubCommand != null && baseSubCommand.hideInHelp();
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.izzel.taboolib.common.plugin.InternalPlugin;
|
||||
import io.izzel.taboolib.common.plugin.bridge.BridgeLoader;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import io.izzel.taboolib.util.IO;
|
||||
import io.izzel.taboolib.util.KV;
|
||||
import io.izzel.taboolib.util.Ref;
|
||||
import io.izzel.taboolib.util.asm.AsmClassLoader;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@@ -21,6 +22,8 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
@@ -35,6 +38,9 @@ public class SimpleVersionControl {
|
||||
private Plugin plugin;
|
||||
private boolean useCache;
|
||||
private boolean useNMS;
|
||||
private boolean mapping;
|
||||
private List<KV<String, String>> mappingList = Lists.newArrayList();
|
||||
private ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
SimpleVersionControl() {
|
||||
useCache = false;
|
||||
@@ -112,6 +118,14 @@ public class SimpleVersionControl {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将转换结果打印至 plugins/TabooLib/asm-mapping
|
||||
*/
|
||||
public SimpleVersionControl mapping() {
|
||||
this.mapping = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Class<?> translate() throws IOException {
|
||||
return translate(plugin);
|
||||
}
|
||||
@@ -133,6 +147,10 @@ public class SimpleVersionControl {
|
||||
classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
|
||||
classWriter.visitEnd();
|
||||
classVisitor.visitEnd();
|
||||
// 打印
|
||||
if (mapping || plugin instanceof InternalPlugin) {
|
||||
executorService.submit(this::printMapping);
|
||||
}
|
||||
// 因第三方插件调用该方法时会出现找不到类,所以第三方插件使用 BridgeLoader 加载类
|
||||
return plugin instanceof InternalPlugin ? AsmClassLoader.createNewClass(target, classWriter.toByteArray()) : BridgeLoader.createNewClass(target, classWriter.toByteArray());
|
||||
}
|
||||
@@ -148,9 +166,47 @@ public class SimpleVersionControl {
|
||||
classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
|
||||
classWriter.visitEnd();
|
||||
classVisitor.visitEnd();
|
||||
if (mapping || plugin instanceof InternalPlugin) {
|
||||
executorService.submit(this::printMapping);
|
||||
}
|
||||
return BridgeLoader.createNewClass(target, classWriter.toByteArray());
|
||||
}
|
||||
|
||||
public String replace(String origin) {
|
||||
String replace = origin;
|
||||
if (useNMS) {
|
||||
replace = replace
|
||||
.replaceAll("net/minecraft/server/.*?/", "net/minecraft/server/" + to + "/")
|
||||
.replaceAll("org/bukkit/craftbukkit/.*?/", "org/bukkit/craftbukkit/" + to + "/");
|
||||
}
|
||||
for (String from : from) {
|
||||
replace = replace.replace("/" + from + "/", "/" + to + "/");
|
||||
}
|
||||
if ((mapping || plugin instanceof InternalPlugin) && !replace.equals(origin)) {
|
||||
mappingList.add(new KV<>(origin, replace));
|
||||
}
|
||||
return replace;
|
||||
}
|
||||
|
||||
public void printMapping() {
|
||||
if (mappingList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<KV<String, String>> list = Lists.newArrayList(mappingList);
|
||||
list.sort((b, a) -> Integer.compare(a.getKey().length(), b.getKey().length()));
|
||||
int length = list.get(0).getKey().length();
|
||||
Files.write(Files.file(TabooLib.getPlugin().getDataFolder(), "asm-mapping/" + target.replace("/", ".") + ".txt"), b -> {
|
||||
for (KV<String, String> kv : mappingList) {
|
||||
StringBuilder builder = new StringBuilder(kv.getKey());
|
||||
for (int i = kv.getKey().length(); i < length; i++) {
|
||||
builder.append(" ");
|
||||
}
|
||||
b.write(builder.toString() + " -> " + kv.getValue());
|
||||
b.newLine();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
@@ -169,13 +225,23 @@ public class SimpleVersionControl {
|
||||
return to;
|
||||
}
|
||||
|
||||
public String replace(String origin) {
|
||||
if (useNMS) {
|
||||
origin = origin.replaceAll("net/minecraft/server/.*?/", "net/minecraft/server/" + to + "/").replaceAll("org/bukkit/craftbukkit/.*?/", "org/bukkit/craftbukkit/" + to + "/");
|
||||
}
|
||||
for (String from : from) {
|
||||
origin = origin.replace("/" + from + "/", "/" + to + "/");
|
||||
}
|
||||
return origin;
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public boolean isUseCache() {
|
||||
return useCache;
|
||||
}
|
||||
|
||||
public boolean isUseNMS() {
|
||||
return useNMS;
|
||||
}
|
||||
|
||||
public boolean isMapping() {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
public List<KV<String, String>> getMappingList() {
|
||||
return mappingList;
|
||||
}
|
||||
}
|
||||
@@ -116,11 +116,11 @@ public class TLocaleLoader {
|
||||
}
|
||||
|
||||
private static void infoLogger(String path, String... args) {
|
||||
TLogger.getGlobalLogger().info(Strings.replaceWithOrder(io.izzel.taboolib.TabooLib.getInst().getInternal().getString(path), args));
|
||||
TLogger.getGlobalLogger().info(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString(path), args));
|
||||
}
|
||||
|
||||
private static void errorLogger(String path, String... args) {
|
||||
TLogger.getGlobalLogger().error(Strings.replaceWithOrder(io.izzel.taboolib.TabooLib.getInst().getInternal().getString(path), args));
|
||||
TLogger.getGlobalLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString(path), args));
|
||||
}
|
||||
|
||||
private static File getLocaleFile(Plugin plugin) {
|
||||
|
||||
Reference in New Issue
Block a user