From cefb8a12a8b64918936f22e73dfae00d7a6b651e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Wed, 19 Sep 2018 23:46:03 +0800 Subject: [PATCH] =?UTF-8?q?+=20=E4=B8=80=E4=BA=9B=E5=8D=8A=E6=88=90?= =?UTF-8?q?=E5=93=81=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E7=A2=A7=E6=B2=B9=E9=B8=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tlib/util/asm/AsmClassTransformer.java | 4 + .../me/skymc/taboolib/TabooLibLoader.java | 21 ++- .../taboolib/anvil/AnvilContainerAPI.java | 6 +- .../taboolib/anvil/AnvilContainerAsm.java | 5 - .../taboolib/common/pathfinder/SimpleAI.java | 23 +++ .../common/pathfinder/SimpleAISelector.java | 88 +++++++++++ .../internal/ParentPathfinderGoal.java | 41 +++++ .../internal/ParentPathfinderGoalAsm.java | 143 ++++++++++++++++++ .../PlayerContainerLoader.java | 3 +- .../versioncontrol/SimpleClassVisitor.java | 51 +++++++ .../versioncontrol/SimpleMethodVisitor.java | 60 ++++++++ .../versioncontrol/SimpleVersionControl.java | 79 ++++++++++ .../inventory/builder/ItemBuilder.java | 10 ++ .../taboolib/listener/TListenerHandler.java | 3 +- src/main/resources/config.yml | 1 + 15 files changed, 516 insertions(+), 22 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAI.java create mode 100644 src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAISelector.java create mode 100644 src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal.java create mode 100644 src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoalAsm.java create mode 100644 src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java create mode 100644 src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java create mode 100644 src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java diff --git a/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java b/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java index 59ef090..14c6b02 100644 --- a/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java +++ b/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java @@ -50,6 +50,10 @@ public class AsmClassTransformer extends ClassVisitor implements Opcodes { return new Builder().toVersion(Bukkit.getServer().getClass().getName().split("\\.")[3]); } + public static Builder builder(String ver) { + return new Builder().toVersion(ver); + } + @Override public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { MethodVisitor visitor = super.visitMethod(access, name, replace(descriptor), replace(signature), replace(exceptions)); diff --git a/src/main/java/me/skymc/taboolib/TabooLibLoader.java b/src/main/java/me/skymc/taboolib/TabooLibLoader.java index 4348b67..53b4daa 100644 --- a/src/main/java/me/skymc/taboolib/TabooLibLoader.java +++ b/src/main/java/me/skymc/taboolib/TabooLibLoader.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; /** * @Author sky @@ -35,16 +34,6 @@ public class TabooLibLoader implements Listener { static HashMap> pluginClasses = new HashMap<>(); - @EventHandler (priority = EventPriority.LOWEST) - public void onEnable(PluginEnableEvent e) { - pluginClasses.remove(e.getPlugin().getName()); - } - - @EventHandler (priority = EventPriority.MONITOR) - public void onDisable(PluginDisableEvent e) { - setupClasses(e.getPlugin()); - } - static void setup() { testInternet(); setupDataFolder(); @@ -166,4 +155,14 @@ public class TabooLibLoader implements Listener { Metrics metrics = new Metrics(TabooLib.instance()); metrics.addCustomChart(new Metrics.SingleLineChart("plugins_using_taboolib", () -> Math.toIntExact(Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(plugin -> plugin.getDescription().getDepend().contains("TabooLib")).count()))); } + + @EventHandler(priority = EventPriority.LOWEST) + public void onEnable(PluginEnableEvent e) { + pluginClasses.remove(e.getPlugin().getName()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onDisable(PluginDisableEvent e) { + setupClasses(e.getPlugin()); + } } diff --git a/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAPI.java b/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAPI.java index d15768a..fdfbe38 100644 --- a/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAPI.java +++ b/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAPI.java @@ -2,7 +2,8 @@ package me.skymc.taboolib.anvil; import com.ilummc.tlib.util.asm.AsmClassLoader; import me.skymc.taboolib.TabooLib; -import me.skymc.taboolib.listener.TListener; +import me.skymc.taboolib.object.Instantiable; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -11,7 +12,7 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent; /** * @author sky */ -@TListener +@Instantiable("AnvilContainerAPI") public class AnvilContainerAPI implements Listener { private static Class impl; @@ -19,6 +20,7 @@ public class AnvilContainerAPI implements Listener { public AnvilContainerAPI() { try { impl = AsmClassLoader.createNewClass("me.skymc.taboolib.anvil.AnvilContainer", AnvilContainerAsm.create(TabooLib.getVersion())); + Bukkit.getPluginManager().registerEvents(this, TabooLib.instance()); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAsm.java b/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAsm.java index 608f39e..090dceb 100644 --- a/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAsm.java +++ b/src/main/java/me/skymc/taboolib/anvil/AnvilContainerAsm.java @@ -8,16 +8,12 @@ import org.objectweb.asm.*; public class AnvilContainerAsm { public static byte[] create(String version) { - ClassWriter cw = new ClassWriter(0); FieldVisitor fv; MethodVisitor mv; AnnotationVisitor av0; - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "me/skymc/taboolib/anvil/AnvilContainer", null, "net/minecraft/server/" + version + "/ContainerAnvil", null); - cw.visitSource("AnvilContainer.java", null); - { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "(Lnet/minecraft/server/" + version + "/EntityHuman;)V", null, null); mv.visitCode(); @@ -139,7 +135,6 @@ public class AnvilContainerAsm { mv.visitEnd(); } cw.visitEnd(); - return cw.toByteArray(); } } diff --git a/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAI.java b/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAI.java new file mode 100644 index 0000000..7f33afc --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAI.java @@ -0,0 +1,23 @@ +package me.skymc.taboolib.common.pathfinder; + +/** + * @Author sky + * @Since 2018-09-19 19:42 + */ +public abstract class SimpleAI { + + public abstract boolean shouldExecute(); + + public boolean continueExecute() { + return shouldExecute(); + } + + public void startTask() { + } + + public void resetTask() { + } + + public void updateTask() { + } +} diff --git a/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAISelector.java b/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAISelector.java new file mode 100644 index 0000000..655162c --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAISelector.java @@ -0,0 +1,88 @@ +package me.skymc.taboolib.common.pathfinder; + +import com.ilummc.tlib.util.asm.AsmClassLoader; +import me.skymc.taboolib.TabooLib; +import me.skymc.taboolib.common.pathfinder.internal.ParentPathfinderGoalAsm; +import me.skymc.taboolib.nms.NMSUtils; +import me.skymc.taboolib.object.Instantiable; +import org.bukkit.entity.LivingEntity; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * @Author sky + * @Since 2018-09-19 20:31 + */ +@Instantiable("SimpleAISelector") +public class SimpleAISelector { + + private static Class parentPathfinderGoal; + private static Class pathfinderGoalFollowOwner; + private static Class pathfinderGoalSelector; + private static Class pathfinderGoal; + private static Class entityInsentient; + private static Field targetSelector; + private static Field goalSelector; + private static Field navigation; + private static Method a; + + public SimpleAISelector() { + try { + parentPathfinderGoal = AsmClassLoader.createNewClass("me.skymc.taboolib.common.pathfinder.internal.ParentPathfinderGoal", ParentPathfinderGoalAsm.create(TabooLib.getVersion())); + pathfinderGoalFollowOwner = NMSUtils.getNMSClass("PathfinderGoalFollowOwner"); + pathfinderGoalSelector = NMSUtils.getNMSClass("PathfinderGoalSelector"); + pathfinderGoal = NMSUtils.getNMSClass("PathfinderGoal"); + entityInsentient = NMSUtils.getNMSClass("EntityInsentient"); + targetSelector = entityInsentient.getField("targetSelector"); + goalSelector = entityInsentient.getField("goalSelector"); + navigation = entityInsentient.getField("navigation"); + a = pathfinderGoalSelector.getDeclaredMethod("a", Integer.TYPE, pathfinderGoal); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static Object getNavigation(LivingEntity entity) { + try { + return navigation.get(entity.getClass().getMethod("getHandle").invoke(entity)); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static Object getGoalSelector(LivingEntity entity) { + try { + return goalSelector.get(entity.getClass().getMethod("getHandle").invoke(entity)); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static Object getTargetSelector(LivingEntity entity) { + try { + return targetSelector.get(entity.getClass().getMethod("getHandle").invoke(entity)); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static void setGoalAI(LivingEntity entity, Object ai, int priority) { + try { + a.invoke(getGoalSelector(entity), priority, ai); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void setTargetAI(LivingEntity entity, Object ai, int priority) { + try { + a.invoke(getTargetSelector(entity), priority, ai); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal.java b/src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal.java new file mode 100644 index 0000000..d66282c --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal.java @@ -0,0 +1,41 @@ +package me.skymc.taboolib.common.pathfinder.internal; + +import me.skymc.taboolib.common.pathfinder.SimpleAI; + +/** + * @Author sky + * @Since 2018-09-19 22:31 + */ +public class ParentPathfinderGoal extends net.minecraft.server.v1_12_R1.PathfinderGoal { + + private final SimpleAI simpleAI; + + public ParentPathfinderGoal(SimpleAI simpleAI) { + this.simpleAI = simpleAI; + } + + @Override + public boolean a() { + return simpleAI.shouldExecute(); + } + + @Override + public boolean b() { + return simpleAI.continueExecute(); + } + + @Override + public void c() { + simpleAI.startTask(); + } + + @Override + public void d() { + simpleAI.resetTask(); + } + + @Override + public void e() { + simpleAI.updateTask(); + } +} diff --git a/src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoalAsm.java b/src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoalAsm.java new file mode 100644 index 0000000..ac94be3 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoalAsm.java @@ -0,0 +1,143 @@ +package me.skymc.taboolib.common.pathfinder.internal; + +import org.objectweb.asm.*; + +/** + * @author sky + */ +public class ParentPathfinderGoalAsm { + + public static byte[] create(String version) { + + ClassWriter cw = new ClassWriter(0); + FieldVisitor fv; + MethodVisitor mv; + AnnotationVisitor av0; + + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal", null, "net/minecraft/server/" + version + "/PathfinderGoal", null); + + cw.visitSource("ParentPathfinderGoal.java", null); + + { + fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;", null, null); + fv.visitEnd(); + } + { + mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "(Lme/skymc/taboolib/common/pathfinder/SimpleAI;)V", null, null); + mv.visitCode(); + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(13, l0); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "net/minecraft/server/" + version + "/PathfinderGoal", "", "()V", false); + Label l1 = new Label(); + mv.visitLabel(l1); + mv.visitLineNumber(14, l1); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitVarInsn(Opcodes.ALOAD, 1); + mv.visitFieldInsn(Opcodes.PUTFIELD, "me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal", "simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;"); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitLineNumber(15, l2); + mv.visitInsn(Opcodes.RETURN); + Label l3 = new Label(); + mv.visitLabel(l3); + mv.visitLocalVariable("this", "Lme/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal;", null, l0, l3, 0); + mv.visitLocalVariable("simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;", null, l0, l3, 1); + mv.visitMaxs(2, 2); + mv.visitEnd(); + } + { + mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "a", "()Z", null, null); + mv.visitCode(); + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(19, l0); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitFieldInsn(Opcodes.GETFIELD, "me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal", "simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;"); + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "me/skymc/taboolib/common/pathfinder/SimpleAI", "shouldExecute", "()Z", false); + mv.visitInsn(Opcodes.IRETURN); + Label l1 = new Label(); + mv.visitLabel(l1); + mv.visitLocalVariable("this", "Lme/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal;", null, l0, l1, 0); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + { + mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "b", "()Z", null, null); + mv.visitCode(); + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(24, l0); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitFieldInsn(Opcodes.GETFIELD, "me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal", "simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;"); + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "me/skymc/taboolib/common/pathfinder/SimpleAI", "continueExecute", "()Z", false); + mv.visitInsn(Opcodes.IRETURN); + Label l1 = new Label(); + mv.visitLabel(l1); + mv.visitLocalVariable("this", "Lme/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal;", null, l0, l1, 0); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + { + mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "c", "()V", null, null); + mv.visitCode(); + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(29, l0); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitFieldInsn(Opcodes.GETFIELD, "me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal", "simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;"); + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "me/skymc/taboolib/common/pathfinder/SimpleAI", "startTask", "()V", false); + Label l1 = new Label(); + mv.visitLabel(l1); + mv.visitLineNumber(30, l1); + mv.visitInsn(Opcodes.RETURN); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitLocalVariable("this", "Lme/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal;", null, l0, l2, 0); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + { + mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "d", "()V", null, null); + mv.visitCode(); + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(34, l0); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitFieldInsn(Opcodes.GETFIELD, "me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal", "simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;"); + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "me/skymc/taboolib/common/pathfinder/SimpleAI", "resetTask", "()V", false); + Label l1 = new Label(); + mv.visitLabel(l1); + mv.visitLineNumber(35, l1); + mv.visitInsn(Opcodes.RETURN); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitLocalVariable("this", "Lme/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal;", null, l0, l2, 0); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + { + mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "e", "()V", null, null); + mv.visitCode(); + Label l0 = new Label(); + mv.visitLabel(l0); + mv.visitLineNumber(39, l0); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitFieldInsn(Opcodes.GETFIELD, "me/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal", "simpleAI", "Lme/skymc/taboolib/common/pathfinder/SimpleAI;"); + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "me/skymc/taboolib/common/pathfinder/SimpleAI", "updateTask", "()V", false); + Label l1 = new Label(); + mv.visitLabel(l1); + mv.visitLineNumber(40, l1); + mv.visitInsn(Opcodes.RETURN); + Label l2 = new Label(); + mv.visitLabel(l2); + mv.visitLocalVariable("this", "Lme/skymc/taboolib/common/pathfinder/internal/ParentPathfinderGoal;", null, l0, l2, 0); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + cw.visitEnd(); + + return cw.toByteArray(); + } +} diff --git a/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java b/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java index 3047d10..120bd71 100644 --- a/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java +++ b/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java @@ -48,8 +48,7 @@ public class PlayerContainerLoader implements Listener { field.setAccessible(true); try { pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(pluginClass), annotation.uniqueId())); - } catch (IllegalAccessException e) { - e.printStackTrace(); + } catch (IllegalAccessException ignored) { } } } diff --git a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java new file mode 100644 index 0000000..1638a68 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java @@ -0,0 +1,51 @@ +package me.skymc.taboolib.common.versioncontrol; + +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +import java.util.stream.IntStream; + +/** + * 我不信 ClassNotFound 的邪,自己写了一个发现还是一样。。。 + * + * @Author sky + * @Since 2018-09-19 21:17 + */ +public class SimpleClassVisitor extends ClassVisitor { + + private final SimpleVersionControl simpleVersionControl; + + public SimpleClassVisitor(SimpleVersionControl simpleVersionControl, ClassVisitor classVisitor) { + super(Opcodes.ASM6, classVisitor); + this.simpleVersionControl = simpleVersionControl; + } + + @Override + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + super.visit(version, access, name, signature, translate(superName), translate(interfaces)); + } + + @Override + public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { + return super.visitField(access, name, translate(descriptor), translate(signature), value); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { + return new SimpleMethodVisitor(simpleVersionControl, super.visitMethod(access, name, translate(descriptor), translate(signature), translate(exceptions))); + } + + private String translate(String target) { + return target == null ? null : target.replace("/" + simpleVersionControl.getFrom() + "/", "/" + simpleVersionControl.getTo() + "/"); + } + + private String[] translate(String[] target) { + if (target == null) { + return target; + } + IntStream.range(0, target.length).forEach(i -> target[i] = translate(target[i])); + return target; + } +} diff --git a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java new file mode 100644 index 0000000..4ae9df8 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java @@ -0,0 +1,60 @@ +package me.skymc.taboolib.common.versioncontrol; + +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +import java.util.stream.IntStream; + +/** + * 我不信 ClassNotFound 的邪,自己写了一个发现还是一样。。。 + * + * @Author sky + * @Since 2018-9-19 21:33 + */ +public class SimpleMethodVisitor extends MethodVisitor { + + private final SimpleVersionControl simpleVersionControl; + + public SimpleMethodVisitor(SimpleVersionControl simpleVersionControl, MethodVisitor methodVisitor) { + super(Opcodes.ASM6, methodVisitor); + this.simpleVersionControl = simpleVersionControl; + } + + @Override + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { + super.visitMethodInsn(opcode, translate(owner), name, translate(descriptor), isInterface); + } + + @Override + public void visitLdcInsn(Object value) { + super.visitLdcInsn(value instanceof String ? translate((String) value) : value); + } + + @Override + public void visitTypeInsn(int opcode, String type) { + super.visitTypeInsn(opcode, translate(type)); + } + + @Override + public void visitFieldInsn(int opcode, String owner, String name, String descriptor) { + super.visitFieldInsn(opcode, translate(owner), name, translate(descriptor)); + } + + @Override + public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) { + super.visitLocalVariable(name, translate(descriptor), translate(signature), start, end, index); + } + + private String translate(String target) { + return target == null ? null : target.replaceAll("/" + simpleVersionControl.getFrom() + "/", "/" + simpleVersionControl.getTo() + "/"); + } + + private String[] translate(String[] target) { + if (target == null) { + return target; + } + IntStream.range(0, target.length).forEach(i -> target[i] = translate(target[i])); + return target; + } +} \ No newline at end of file diff --git a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java new file mode 100644 index 0000000..fe7dd36 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java @@ -0,0 +1,79 @@ +package me.skymc.taboolib.common.versioncontrol; + +import com.ilummc.tlib.util.asm.AsmClassLoader; +import me.skymc.taboolib.TabooLib; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; + +import java.io.IOException; + +/** + * 我不信 ClassNotFound 的邪,自己写了一个发现还是一样。。。 + * + * @Author sky + * @Since 2018-09-19 21:05 + */ +public class SimpleVersionControl { + + private String target; + private String from; + private String to; + + SimpleVersionControl() { + } + + public static SimpleVersionControl create() { + return new SimpleVersionControl().to(TabooLib.getVersion()); + } + + public static SimpleVersionControl create(String toVersion) { + return new SimpleVersionControl().to(toVersion); + } + + public SimpleVersionControl target(Class target) { + this.target = target.getName(); + return this; + } + + public SimpleVersionControl target(String target) { + this.target = target; + return this; + } + + public SimpleVersionControl from(String from) { + this.from = from.startsWith("v") ? from : "v" + from; + return this; + } + + public SimpleVersionControl to(String to) { + this.to = to.startsWith("v") ? to : "v" + to; + return this; + } + + public Class translate() throws IOException { + ClassReader classReader = new ClassReader(target); + ClassWriter classWriter = new ClassWriter(0); + ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter); + classReader.accept(classVisitor, 0); + return AsmClassLoader.createNewClass(target, classWriter.toByteArray()); + } + + // ********************************* + // + // Getter and Setter + // + // ********************************* + + public String getTarget() { + return target; + } + + public String getFrom() { + return from; + } + + public String getTo() { + return to; + } +} diff --git a/src/main/java/me/skymc/taboolib/inventory/builder/ItemBuilder.java b/src/main/java/me/skymc/taboolib/inventory/builder/ItemBuilder.java index 9b01229..6e3a7d5 100644 --- a/src/main/java/me/skymc/taboolib/inventory/builder/ItemBuilder.java +++ b/src/main/java/me/skymc/taboolib/inventory/builder/ItemBuilder.java @@ -187,4 +187,14 @@ public class ItemBuilder { } return buildItem; } + + /** + * 从文本中获取物品(name:名字;lore:描述||描述;material:材质) + * + * @param str + * @return + */ + public static ItemStack fromString(String str) { + return null; + } } diff --git a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java index 02fd37c..76f6c6d 100644 --- a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java +++ b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java @@ -59,8 +59,7 @@ public class TListenerHandler implements Listener { // 实例化监听器 Listener listener = plugin.getClass().equals(pluginClass) ? (Listener) plugin : (Listener) ReflectionUtils.instantiateObject(pluginClass); listeners.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(listener); - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception ignored) { } } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 01fa46a..a1b71a1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -75,6 +75,7 @@ ENABLE-UUID: false HIDE-NOTIFY: true # 数据库信息 +# 该功能在当前版本下无法使用,请勿启用 MYSQL: # 是否启用数据库 ENABLE: false