+ update SimpleVersionControl

This commit is contained in:
坏黑 2019-09-18 22:15:55 +08:00
parent b0623575ab
commit 60131c4f91
10 changed files with 200 additions and 25 deletions

View File

@ -181,13 +181,13 @@ public class TabooLibLoader {
public interface Loader { public interface Loader {
default void preLoad(org.bukkit.plugin.Plugin plugin, Class<?> pluginClass) { default void preLoad(Plugin plugin, Class<?> pluginClass) {
} }
default void postLoad(org.bukkit.plugin.Plugin plugin, Class<?> pluginClass) { default void postLoad(Plugin plugin, Class<?> pluginClass) {
} }
default void activeLoad(org.bukkit.plugin.Plugin plugin, Class<?> pluginClass) { default void activeLoad(Plugin plugin, Class<?> pluginClass) {
} }
default void unload(Plugin plugin, Class<?> pluginClass) { default void unload(Plugin plugin, Class<?> pluginClass) {

View File

@ -8,7 +8,7 @@ import org.bukkit.Bukkit;
*/ */
public enum Version { public enum Version {
v1_7(10700), v1_8(10800), v1_9(10900), v1_10(11000), v1_11(11100), v1_12(11200), v1_13(11300), v1_14(11400), vNull(0); v1_7(10700), v1_8(10800), v1_9(10900), v1_10(11000), v1_11(11100), v1_12(11200), v1_13(11300), v1_14(11400), v1_15(11500), vNull(0);
private int versionInt; private int versionInt;
@ -50,6 +50,8 @@ public enum Version {
return v1_13; return v1_13;
} else if (nmsVersion.startsWith("v1_14")) { } else if (nmsVersion.startsWith("v1_14")) {
return v1_14; return v1_14;
} else if (nmsVersion.startsWith("v1_15")) {
return v1_15;
} else { } else {
return vNull; return vNull;
} }

View File

@ -26,7 +26,7 @@ public class CronusUtils {
return Optional.ofNullable(itemStack).orElse(new ItemStack(Material.STONE)); return Optional.ofNullable(itemStack).orElse(new ItemStack(Material.STONE));
} }
public static org.bukkit.inventory.ItemStack getUsingItem(Player player, Material material) { public static ItemStack getUsingItem(Player player, Material material) {
return player.getItemInHand().getType() == material ? player.getItemInHand() : player.getInventory().getItemInOffHand(); return player.getItemInHand().getType() == material ? player.getItemInHand() : player.getInventory().getItemInOffHand();
} }

View File

@ -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) { if (fields.size() > 0) {
fields.sort(Comparator.comparingDouble(commandField -> commandField.getField().getAnnotation(SubCommand.class).priority())); fields.sort(Comparator.comparingDouble(commandField -> commandField.getField().getAnnotation(SubCommand.class).priority()));
fields.forEach(commandField -> { fields.forEach(commandField -> {
try { try {
commandField.getField().setAccessible(true); 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()); subCommand.setLabel(commandField.getField().getName());
baseMainCommand.registerSubCommand(subCommand); baseMainCommand.registerSubCommand(subCommand);
} catch (Exception ignored) { } catch (Exception ignored) {
@ -90,11 +90,11 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
return linkClasses; return linkClasses;
} }
public List<io.izzel.taboolib.module.command.base.BaseSubCommand> getSubCommands() { public List<BaseSubCommand> getSubCommands() {
return subCommands; return subCommands;
} }
public void registerSubCommand(io.izzel.taboolib.module.command.base.BaseSubCommand subCommand) { public void registerSubCommand(BaseSubCommand subCommand) {
if (subCommand != null) { if (subCommand != null) {
Preconditions.checkArgument(subCommand.getLabel() != null, "Command label can not be null"); Preconditions.checkArgument(subCommand.getLabel() != null, "Command label can not be null");
Preconditions.checkArgument(subCommand.getArguments() != null, "Command arguments 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()); 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(); Argument[] arguments = subCommand.getArguments();
if (args[0].equalsIgnoreCase(subCommand.getLabel()) && args.length - 1 <= arguments.length) { if (args[0].equalsIgnoreCase(subCommand.getLabel()) && args.length - 1 <= arguments.length) {
CommandTab commandTab = arguments[args.length - 2].getTab(); CommandTab commandTab = arguments[args.length - 2].getTab();
@ -139,7 +139,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
if (args.length == 0) { if (args.length == 0) {
onCommandHelp(sender, command, label, args); onCommandHelp(sender, command, label, args);
} else { } 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)) { if (subCommand == null || !(args[0].equalsIgnoreCase(subCommand.getLabel()) || java.util.Arrays.stream(subCommand.getAliases()).anyMatch(args[0]::equalsIgnoreCase)) || !hasPermission(sender, subCommand)) {
continue; continue;
} }
@ -159,7 +159,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
@Override @Override
public void run() { 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) { if (commandCompute.size() > 0) {
TLocale.sendTo(sender, "COMMANDS.INTERNAL.ERROR-COMMAND", args[0], commandCompute.get(0).getCommandString(label).trim()); 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(); return baseSubCommand != null && baseSubCommand.hideInHelp();
} }

View File

@ -7,6 +7,7 @@ import io.izzel.taboolib.common.plugin.InternalPlugin;
import io.izzel.taboolib.common.plugin.bridge.BridgeLoader; import io.izzel.taboolib.common.plugin.bridge.BridgeLoader;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
import io.izzel.taboolib.util.IO; import io.izzel.taboolib.util.IO;
import io.izzel.taboolib.util.KV;
import io.izzel.taboolib.util.Ref; import io.izzel.taboolib.util.Ref;
import io.izzel.taboolib.util.asm.AsmClassLoader; import io.izzel.taboolib.util.asm.AsmClassLoader;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -21,6 +22,8 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* @Author sky * @Author sky
@ -35,6 +38,9 @@ public class SimpleVersionControl {
private Plugin plugin; private Plugin plugin;
private boolean useCache; private boolean useCache;
private boolean useNMS; private boolean useNMS;
private boolean mapping;
private List<KV<String, String>> mappingList = Lists.newArrayList();
private ExecutorService executorService = Executors.newSingleThreadExecutor();
SimpleVersionControl() { SimpleVersionControl() {
useCache = false; useCache = false;
@ -112,6 +118,14 @@ public class SimpleVersionControl {
return this; return this;
} }
/**
* 将转换结果打印至 plugins/TabooLib/asm-mapping
*/
public SimpleVersionControl mapping() {
this.mapping = true;
return this;
}
public Class<?> translate() throws IOException { public Class<?> translate() throws IOException {
return translate(plugin); return translate(plugin);
} }
@ -133,6 +147,10 @@ public class SimpleVersionControl {
classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES); classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
classWriter.visitEnd(); classWriter.visitEnd();
classVisitor.visitEnd(); classVisitor.visitEnd();
// 打印
if (mapping || plugin instanceof InternalPlugin) {
executorService.submit(this::printMapping);
}
// 因第三方插件调用该方法时会出现找不到类所以第三方插件使用 BridgeLoader 加载类 // 因第三方插件调用该方法时会出现找不到类所以第三方插件使用 BridgeLoader 加载类
return plugin instanceof InternalPlugin ? AsmClassLoader.createNewClass(target, classWriter.toByteArray()) : BridgeLoader.createNewClass(target, classWriter.toByteArray()); 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); classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
classWriter.visitEnd(); classWriter.visitEnd();
classVisitor.visitEnd(); classVisitor.visitEnd();
if (mapping || plugin instanceof InternalPlugin) {
executorService.submit(this::printMapping);
}
return BridgeLoader.createNewClass(target, classWriter.toByteArray()); 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 // Getter and Setter
@ -169,13 +225,23 @@ public class SimpleVersionControl {
return to; return to;
} }
public String replace(String origin) { public Plugin getPlugin() {
if (useNMS) { return plugin;
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 + "/"); public boolean isUseCache() {
return useCache;
} }
return origin;
public boolean isUseNMS() {
return useNMS;
}
public boolean isMapping() {
return mapping;
}
public List<KV<String, String>> getMappingList() {
return mappingList;
} }
} }

View File

@ -116,11 +116,11 @@ public class TLocaleLoader {
} }
private static void infoLogger(String path, String... args) { 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) { 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) { private static File getLocaleFile(Plugin plugin) {

View File

@ -287,6 +287,40 @@ public class Files {
return null; return null;
} }
public static void read(File file, ReadHandle readHandle) {
try (FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader)) {
readHandle.read(bufferedReader);
} catch (Throwable t) {
t.printStackTrace();
}
}
public static void read(InputStream in, ReadHandle readHandle) {
try (InputStreamReader inputStreamReader = new InputStreamReader(in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
readHandle.read(bufferedReader);
} catch (Throwable t) {
t.printStackTrace();
}
}
public static void write(File file, WriteHandle writeHandle) {
try (FileWriter fileWriter = new FileWriter(file); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
writeHandle.write(bufferedWriter);
bufferedWriter.flush();
} catch (Throwable t) {
t.printStackTrace();
}
}
public static void write(OutputStream out, WriteHandle writeHandle) {
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
writeHandle.write(bufferedWriter);
bufferedWriter.flush();
} catch (Throwable t) {
t.printStackTrace();
}
}
public static String encodeYAML(FileConfiguration file) { public static String encodeYAML(FileConfiguration file) {
return Base64Coder.encodeLines(file.saveToString().getBytes()).replaceAll("\\s+", ""); return Base64Coder.encodeLines(file.saveToString().getBytes()).replaceAll("\\s+", "");
} }
@ -315,4 +349,14 @@ public class Files {
} }
return null; return null;
} }
public interface ReadHandle {
void read(BufferedReader reader) throws IOException;
}
public interface WriteHandle {
void write(BufferedWriter writer) throws IOException;
}
} }

View File

@ -0,0 +1,63 @@
package io.izzel.taboolib.util;
import java.util.Objects;
/**
* @Author sky
* @Since 2019-09-18 21:23
*/
public class KV<K, V> {
private K key;
private V value;
public KV() {
}
public KV(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof KV)) {
return false;
}
KV<?, ?> kv = (KV<?, ?>) o;
return Objects.equals(key, kv.key) &&
Objects.equals(value, kv.value);
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
@Override
public String toString() {
return "KV{" +
"key=" + key +
", value=" + value +
'}';
}
}

View File

@ -173,7 +173,7 @@ public class Items {
public static boolean hasItem(Inventory inventory, Matcher matcher, int amount) { public static boolean hasItem(Inventory inventory, Matcher matcher, int amount) {
int checkAmount = amount; int checkAmount = amount;
for (org.bukkit.inventory.ItemStack itemStack : inventory.getContents()) { for (ItemStack itemStack : inventory.getContents()) {
if (!isNull(itemStack) && matcher.match(itemStack)) { if (!isNull(itemStack) && matcher.match(itemStack)) {
checkAmount -= itemStack.getAmount(); checkAmount -= itemStack.getAmount();
if (checkAmount <= 0) { if (checkAmount <= 0) {

View File

@ -649,7 +649,7 @@ public enum Particles {
effect.display(color, offX, offY, offZ, amount, lc, p); effect.display(color, offX, offY, offZ, amount, lc, p);
break; break;
case 2: case 2:
Particles.NOTE.display(new Particles.NoteColor(random.nextInt(24)), offX, offY, offZ, amount, lc, /*p.getPlayer(),*/ p); Particles.NOTE.display(new NoteColor(random.nextInt(24)), offX, offY, offZ, amount, lc, /*p.getPlayer(),*/ p);
break; break;
case 0: case 0:
effect.display(offX, offY, offZ, 0.001, amount, lc, p); effect.display(offX, offY, offZ, 0.001, amount, lc, p);