Need help with Title and SubTitle

Discussion in 'Plugin Development' started by AnonyBart, May 4, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    AnonyBart

    Hello Guys,
    I found this example in internet:
    Code:
    package me.pogo.packets;
    
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle.EnumTitleAction;
    
    public class Title extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            if (e.getMessage().equals("title")) {
                // Title or subtitle, text, fade in (ticks), display time (ticks), fade out (ticks).
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"Welcome\"}"), 20, 40, 20);
                PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"to my server\"}"), 20, 40, 20);
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(title);
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(subtitle);
            }
        }
    }
    
    Problems are:
    1) I want to place instead of ChatSerializer.a("{\"text\":\"Welcome\"}") a String and i don't know how.
    2) In my case i am doing something like:
    Code:
    for (Player Admin :  Bukkit.getOnlinePlayers()) {
        if (Admin.hasPermission(blabla) || Admin.isOp()) {
         
    PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"Welcome\"}"),20, 40, 20);
    PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"to my server\"}"), 20, 40, 20);
    ((CraftPlayer) Admin).getHandle().playerConnection.sendPacket(title);
    ((CraftPlayer) Admin).getHandle().playerConnection.sendPacket(subtitle);
        }
    }
    I can't cast the player Admin to CraftPlayer, how to resolve? Error: CraftPlayer cannot be resolved to a type



    Thanks to all!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @AnonyBart That has to do with the fact that you are building with Bukkit while CraftPlayer is in CraftBukkit.
    Why not just run the title command instead or use an API where you don't need CraftPlayer?
     
  3. Offline

    AnonyBart

    I am using Spigot, and I want to solve it with code and not with title command or an plugin that provides the api.
    :)
     
    Last edited by a moderator: May 4, 2016
  4. Offline

    timtower Administrator Administrator Moderator

    @AnonyBart Nms when you are having issues already isn't clever.
    Then build on spigot.
     
  5. Offline

    AnonyBart

    Ok thanks, can I wait for other answers?
     
    Last edited by a moderator: May 4, 2016
  6. Offline

    timtower Administrator Administrator Moderator

    Nobody says that you can't.
     
    AnonyBart likes this.
  7. Offline

    AnonyBart

Thread Status:
Not open for further replies.

Share This Page