【尝试】半教程半制作半探索半学习的JAVA插件帖



  • @Crazy_Bull player.getHandle()?
    推荐使用protocollib

    是咸鱼,总会翻身的

    0


  • @Death_fish 我后来研究过了,PVPIN的“玩家”是CraftPlayer类,所以可以直接使用





  • @terry_ma 要挂VPN吗?



  • @Crazy_Bull 算了,我搬过来吧

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    
    /**
     * Created by magang on 16/6/10.
     */
    public class TitleAPI {
    
        public static void sendPacket(Player player, Object packet) {
            try {
                Object handle = player.getClass().getMethod("getHandle").invoke(player);
                Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
                playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static Class<?> getNMSClass(String name) {
            String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
            try {
                return Class.forName("net.minecraft.server." + version + "." + name);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        // 全屏标题
        public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle) {
            try {
                Object e;
                Object chatTitle;
                Object chatSubtitle;
                Constructor subtitleConstructor;
                Object titlePacket;
                Object subtitlePacket;
    
                if (title != null) {
                    title = ChatColor.translateAlternateColorCodes('&', title);
                    title = title.replaceAll("%player%", player.getDisplayName());
                    // Times packets
                    e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TIMES").get((Object) null);
                    chatTitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[]{String.class}).invoke((Object) null, new Object[]{"{\"text\":\"" + title + "\"}"});
                    subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[]{getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE});
                    titlePacket = subtitleConstructor.newInstance(new Object[]{e, chatTitle, fadeIn, stay, fadeOut});
                    sendPacket(player, titlePacket);
    
                    e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TITLE").get((Object) null);
                    chatTitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[]{String.class}).invoke((Object) null, new Object[]{"{\"text\":\"" + title + "\"}"});
                    subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[]{getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent")});
                    titlePacket = subtitleConstructor.newInstance(new Object[]{e, chatTitle});
                    sendPacket(player, titlePacket);
                }
    
                if (subtitle != null) {
                    subtitle = ChatColor.translateAlternateColorCodes('&', subtitle);
                    subtitle = subtitle.replaceAll("%player%", player.getDisplayName());
                    // Times packets
                    e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TIMES").get((Object) null);
                    chatSubtitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[]{String.class}).invoke((Object) null, new Object[]{"{\"text\":\"" + title + "\"}"});
                    subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[]{getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE});
                    subtitlePacket = subtitleConstructor.newInstance(new Object[]{e, chatSubtitle, fadeIn, stay, fadeOut});
                    sendPacket(player, subtitlePacket);
    
                    e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("SUBTITLE").get((Object) null);
                    chatSubtitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[]{String.class}).invoke((Object) null, new Object[]{"{\"text\":\"" + subtitle + "\"}"});
                    subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[]{getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE});
                    subtitlePacket = subtitleConstructor.newInstance(new Object[]{e, chatSubtitle, fadeIn, stay, fadeOut});
                    sendPacket(player, subtitlePacket);
                }
            } catch (Exception var11) {
                var11.printStackTrace();
            }
        }
    
        public static void clearTitle(Player player) {
            sendTitle(player, 0, 0, 0, "", "");
        }
    
        // tab标题
        public static void sendTabTitle(Player player, String header, String footer) {
            if (header == null) header = "";
            header = ChatColor.translateAlternateColorCodes('&', header);
    
            if (footer == null) footer = "";
            footer = ChatColor.translateAlternateColorCodes('&', footer);
    
            header = header.replaceAll("%player%", player.getDisplayName());
            footer = footer.replaceAll("%player%", player.getDisplayName());
    
            try {
                Object tabHeader = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + header + "\"}");
                Object tabFooter = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + footer + "\"}");
                Constructor<?> titleConstructor = getNMSClass("PacketPlayOutPlayerListHeaderFooter").getConstructor(getNMSClass("IChatBaseComponent"));
                Object packet = titleConstructor.newInstance(tabHeader);
                Field field = packet.getClass().getDeclaredField("b");
                field.setAccessible(true);
                field.set(packet, tabFooter);
                sendPacket(player, packet);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
        }
    
        // 发送到工具栏上方
        public static void sendOutChat(Player player, String text) {
            if (text == null) text = "";
            text = ChatColor.translateAlternateColorCodes('&', text);
    
            try {
                Object chatText = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + text + "\"}");
                Constructor<?> titleConstructor = getNMSClass("PacketPlayOutChat").getConstructor(getNMSClass("IChatBaseComponent"), Byte.TYPE);
                Object packet = titleConstructor.newInstance(chatText, (byte) 2);
                sendPacket(player, packet);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    }
    
    


  • 此回复已被删除!


  • 我的期末考试三科都考得非常好[名列前茅]!
    老妈每天给我玩2个小时电脑!
    [滑稽]

    有时候,一点微不足道的肯定,对我却意义非凡。

    0


  • @Crazy_Bull

    谁告诉你这是NMS了?

    出去千万别说Constructor,Method,Class是NMS啊 。



  • @terry_ma 这个不是java.lang.reflect的吗…说这些是nms的不笑掉大牙→_→

    是咸鱼,总会翻身的

    0


  • 好吧,前面有两个人在无情的嘲讽我
    现在继续开始更贴。

    为了能够正确的运行,我还是决定把老马的代码黏贴到IDE里。
    当然版权我会说明的。

    事实上研究其代码后发现,这个里用了一个十分巧妙地结构。

    player.getClass().getMethod("getHandle").invoke(player);
    

    如果说这个Player是CraftPlayer类型的,那么

    player.getClass()
    

    将会返回一个CraftPlayer的类
    然后调用一些方法



  • PS 从上文中至少找出5个错别字
    第一个全部找对的人将会得我的5钻石奖励!
    PPSS 自从我作死买了进阶教程后存款就不多了



  • 找出了错误
    是HashMap没有实例化
    (作为老司机的我怎么会犯这种错误,哎)



  • 怪不得总是NullPointerException



  • 楼主前八天在美国比赛,无法更新,现在又要过年了,暂停更新



  • @Crazy_Bull 牛逼哦,在美国比赛什么?



  • @terry_ma 数学比赛,结果没有拿到好名次



  • @Crazy_Bull 叫什么名字



  • @terry_ma EMCC



  • @Crazy_Bull【尝试】半教程半制作半探索半学习的JAVA插件帖 中说:

    EMCC

    天呢,你上的是贵族学校



  • 此回复已被删除!

登录后回复