Title fading far too early

Discussion in 'Plugin Development' started by Rixterz, Nov 27, 2015.

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

    Rixterz

    Hi,

    I have the following code to display a countdown from 10 - 0 to all players. Regardless what I set the 3 parameters to (fadeIn, stay, fadeOut), both the title and subtitle fade when countdown = 6.

    Code:
    for(Player p : getServer().getOnlinePlayers()){
                String titleText = "Starting game...";
                String titleColor = "gold";
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"" + titleText + "\",\"color\":\"" + titleColor + "\"}"), 20, 40, 20);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(title);
            }
            String subtitleColor = "red";
            for(int countdown = 10; countdown > 0; countdown --){
                final int fCountdown = countdown;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    @Override
                    public void run(){
                        String subtitleText = String.valueOf(fCountdown);
                        for(Player p : getServer().getOnlinePlayers()){
                            PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"" + subtitleText + "\",\"color\":\"" + subtitleColor + "\"}"), 20, 40, 20);
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(subtitle);
                        }                  
                    }
                }, (10 - countdown) * 20);
            }
    Bug, or just my stupidity?
     
  2. Offline

    FabeGabeMC

    @Rixterz
    You need to send another PacketPlayOutTitle, but with the EnumTitleAction.TIMES
     
  3. Offline

    Rixterz

    I've never heard of it and there's no documentation on it. What do I put as the parameters I mentioned in my existing code? and when do I send the times packet?
     
  4. Offline

    tommyhoogstra

    forgive me for ignoring the rest, but a better alternative to this these days would be

    getServer().getOnlinePlayers().forEach(p -> {
    // CODE HERE, USE p to refer to each player as you would.
    });
     
  5. Offline

    G_4s

    i used this before in my plugin this is my packet sender class:
    with fi fade in time d as duration and fo as fadeout time all in ticks
    Code:
    public class PacketSender {
        public PacketSender() {
        }
        public void sendTitle(String title, String subtitle, int fi, int d, int fo) {
            for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                this.send(title, subtitle, player , fi, d, fo);
              
            }
        }
        public void send(String title, String subtitle, Player player , int fi , int d , int fo) {
            PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
    
            PacketPlayOutTitle packetPlayOutTimes = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, null, fi, d, fo);
            connection.sendPacket(packetPlayOutTimes);
    
            IChatBaseComponent titleSub = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + translateAlternateColorCodes('&', subtitle) + "\"}");
            PacketPlayOutTitle packetPlayOutSubTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, titleSub);
            connection.sendPacket(packetPlayOutSubTitle);
    
            IChatBaseComponent titleMain = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + translateAlternateColorCodes('&', title) + "\"}");
            PacketPlayOutTitle packetPlayOutTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, titleMain);
            connection.sendPacket(packetPlayOutTitle);      
        }
    }
    for imports where i somethimes was stuck check my github file of it : https://github.com/GeForcez/G_4sYel...a/com/GeForce/G_4sYeller/PacketSender.java#L8
     
  6. Offline

    Mrs. bwfctower

    Only if servers all use Java 8, which still isn't the case. Any Java 7 server tries to use the plugin, and it's broken.

    @Rixterz Even easier way, which doesn't need packets, is to use Bukkit#dispatchCommand. May not be very 'clean', but packets are even messier, especially once multiple server versions start trying to use it.
     
  7. Offline

    tommyhoogstra

    If servers arent running java 8 by this point i really question why people are running them at all.
     
  8. Offline

    Mrs. bwfctower

    I agree that servers should be running Java 8. But the fact is, they aren't, either because their hosts aren't the best and only have Java 7, or they don't see a need to upgrade.
     
    tommyhoogstra likes this.
  9. Offline

    mcdorli

    Currently, more than 50% of the servers use java 7. You know, changing would take at least 10 minutes. What do you think?
     
Thread Status:
Not open for further replies.

Share This Page