[Doc] TitleAPI - Portugues - Br

Discussion in 'Spigot Plugin Development' started by MistereMDev, Jun 14, 2015.

  1. Como instalar

    1- Crie uma class TitleAPI e extenda ao Java Plugin.

    2-Cole na sua class. Mas deixe a sua package!



    Code (Text):
    import java.lang.reflect.Field;

    import net.minecraft.server.v1_8_R2.IChatBaseComponent;
    import net.minecraft.server.v1_8_R2.PacketPlayOutPlayerListHeaderFooter;
    import net.minecraft.server.v1_8_R2.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R2.PlayerConnection;
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class TitleAPI extends JavaPlugin {
        @Deprecated
        public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String message) {
            sendTitle(player, fadeIn, stay, fadeOut, message, null);
        }
        @Deprecated
        public static void sendSubtitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String message) {
            sendTitle(player, fadeIn, stay, fadeOut, null, message);
        }
        @Deprecated
        public static void sendFullTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle) {
            sendTitle(player, fadeIn, stay, fadeOut, title, subtitle);
        }
        public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle) {
            PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
            PacketPlayOutTitle packetPlayOutTimes = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, null, fadeIn, stay, fadeOut);
            connection.sendPacket(packetPlayOutTimes);
            if (subtitle != null) {
                subtitle = subtitle.replaceAll("%player%", player.getDisplayName());
                subtitle = ChatColor.translateAlternateColorCodes('&', subtitle);
                IChatBaseComponent titleSub = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + subtitle + "\"}");
                PacketPlayOutTitle packetPlayOutSubTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, titleSub);
                connection.sendPacket(packetPlayOutSubTitle);
            }
            if (title != null) {
                title = title.replaceAll("%player%", player.getDisplayName());
                title = ChatColor.translateAlternateColorCodes('&', title);
                IChatBaseComponent titleMain = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + title + "\"}");
                PacketPlayOutTitle packetPlayOutTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, titleMain);
                connection.sendPacket(packetPlayOutTitle);
            }
        }
        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());
            PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
            IChatBaseComponent tabTitle = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + header + "\"}");
            IChatBaseComponent tabFoot = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + footer + "\"}");
            PacketPlayOutPlayerListHeaderFooter headerPacket = new PacketPlayOutPlayerListHeaderFooter(tabTitle);
            try {
                Field field = headerPacket.getClass().getDeclaredField("b");
                field.setAccessible(true);
                field.set(headerPacket, tabFoot);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                connection.sendPacket(headerPacket);
            }
        }
    }



    USOS

    Code (Text):
    @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            TitleAPI.sendTitle(p, fadeIn, stay, fadeOut, message);
         
            // ==== Dicas === //
            fade-in = Tempo para aparecer a mensagem.
            stay = Tempo que vai ficar a mensagem.
            fade-out = Tempo que leva para desaparecer a mensagem.
         
            20 = 1 segundo.
        }

    Obrigado.
    obs: peguei esse codigo nao lembro da onde, mas nao estava consiguindo usar o original, dae editei e esta funcionando...
     
    #1 MistereMDev, Jun 14, 2015
    Last edited: Jun 24, 2015