Solved 3 packets sending at the same time.

Discussion in 'Plugin Development' started by Banjer_HD, Jan 2, 2017.

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

    Banjer_HD

    New post:
    It isn't needed anymore... Only thing that is not solved is:
    I have an title(+ lenght) + tablist sending packets at the same time... So the title doesn't register the lenght, and shows up for only 3 secs. Does anyone know how to solve this? Thanks!


    OLD POST:
    Problem:

    Is there a way to check if a player is still in the "Loading terain" screen?
    I am making a plugin that gives you a welcome title + news (multi-line) subtitle...
    Some players have an great internet speed, and some dont, if they dont have an great internet speed, the news is already at the 3th string...

    Code:
    Pastebin: http://pastebin.com/2gL941AE
    Hastebin: Hastebin is not working right now... sorry :p

    Code:
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
      
            if(e.getPlayer().hasPlayedBefore()) {
          
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("onjoin.title.has-joined")) + "\"}"));  
                PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getList("onjoin.subtitle").size() * 4 * 20, 10);
          
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(title);
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(length);
          
            }else {
          
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("onjoin.title.never-joined")) + "\"}"));
                PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getList("onjoin.subtitle").size() * 4 * 20, 10);
          
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(title);
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(length);
          
            }
              
            for(String s : plugin.getConfig().getStringList("onjoin.subtitle")) {
          
                PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(s) + "\"}"));  
                PacketPlayOutTitle length = new PacketPlayOutTitle(5, 30, 5);
          
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(subtitle);
                ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(length);
          
                Bukkit.broadcastMessage("TEST: " + s);
          
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException er) {
                    er.printStackTrace();
                }
          
            }
      
        }
    
    Console log:
    The console is printing: "TEST: 'in the config set value' " but it is printing that when the player is in the loading screen... the "Thread.sleep(4000);" is working becouse it is printing 4 secs later.

    Thanks for reading! I hope someone can help me :)
     
    Last edited: Jan 2, 2017
  2. Online

    timtower Moderator Moderator

    @Banjer_HD The Thread.sleep is freezing your entire server for 4 seconds though.
     
  3. Offline

    Banjer_HD

    @timtower Thanks for your reply! Is there an other way to do this without Thread.sleep?
     
  4. Online

    timtower Moderator Moderator

  5. Offline

    Banjer_HD

    Okay.. Thanks, I did that @timtower... And it works :)

    But that is not the answer to my question xD :p

    It isn't needed anymore... Only thing that is not solved is:
    I have an title(+ lenght) + tablist sending packets at the same time... So the title doesn't register the lenght, and shows up for only 3 secs. Does anyone know how to solve this? Thanks!

    Code:
    Pastebin: http://pastebin.com/3GDxxJzp
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
          
            final Player p = e.getPlayer();
          
            //TITLE:
            if(e.getPlayer().hasPlayedBefore()) {
              
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("onjoin.title.has-joined")) + "\"}"));      
                PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getList("onjoin.subtitles").size() * plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks") - 10, 10);
              
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(title);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
              
            }else {
              
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("onjoin.title.never-joined")) + "\"}"));
                PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getList("onjoin.subtitles").size() * plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks") - 10, 10);
              
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(title);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
                //THE 2 PACKETS^
              
            }
          
            //TODO: HERE ARE THE SUBTITLES
          
            new BukkitRunnable() {
                int next = 0;
                boolean didTwice = false;
              
                public void run() {
                    if(plugin.getConfig().getStringList("onjoin.subtitles").size() >= next + 1) {
                      
                        String s = plugin.getConfig().getStringList("onjoin.subtitles").get(next);
                        Bukkit.broadcastMessage("Next: " + next);
                      
                        if(next + 1 == plugin.getConfig().getStringList("onjoin.subtitles").size()){
                          
                            PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(s) + "\"}"));      
                            PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks") - 10, 10);
                                                  
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(subtitle);
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
                          
                        }else {
                          
                            PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(s) + "\"}"));      
                            PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks"), 0);
                                                  
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(subtitle);
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
                          
                        }
                      
                        next++;
                      
                    }else {
                        this.cancel();
                    }
                }
            }.runTaskTimer(plugin, 0, plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks"));
          
            //TABLIST:
            IChatBaseComponent headerText = ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("tablist.header")) + "\"}");
            IChatBaseComponent footerText = ChatSerializer.a("{\"text\": \"" + utils.toColor(plugin.getConfig().getString("tablist.footer")) + "\"}");
          
          
           PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
        
           try {
            
               Field headerField = packet.getClass().getDeclaredField("a");
               headerField.setAccessible(true);
               headerField.set(packet, headerText);
               headerField.setAccessible(!headerField.isAccessible());
            
               Field footerField = packet.getClass().getDeclaredField("b");
               footerField.setAccessible(true);
               footerField.set(packet, footerText);
               footerField.setAccessible(!footerField.isAccessible());
              
           } catch (Exception er) {
               er.printStackTrace();
           }
          
            ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(packet);
          
          
        }
    
    Okay.. Thanks, I did that @timtower... And it works :)

    But that is not the answer to my question xD :p

    It isn't needed anymore... Only thing that is not solved is:
    I have an title(+ lenght) + tablist sending packets at the same time... So the title doesn't register the lenght, and shows up for only 3 secs. Does anyone know how to solve this? Thanks!

    Never mind, it is all fixed :)
    SOLVED!


    Code:
    Pastebin: http://pastebin.com/3GDxxJzp
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
        
            final Player p = e.getPlayer();
        
            //TITLE:
            if(e.getPlayer().hasPlayedBefore()) {
            
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("onjoin.title.has-joined")) + "\"}"));    
                PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getList("onjoin.subtitles").size() * plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks") - 10, 10);
            
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(title);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
            
            }else {
            
                PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("onjoin.title.never-joined")) + "\"}"));
                PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getList("onjoin.subtitles").size() * plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks") - 10, 10);
            
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(title);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
                //THE 2 PACKETS^
            
            }
        
            //TODO: HERE ARE THE SUBTITLES
        
            new BukkitRunnable() {
                int next = 0;
                boolean didTwice = false;
            
                public void run() {
                    if(plugin.getConfig().getStringList("onjoin.subtitles").size() >= next + 1) {
                    
                        String s = plugin.getConfig().getStringList("onjoin.subtitles").get(next);
                        Bukkit.broadcastMessage("Next: " + next);
                    
                        if(next + 1 == plugin.getConfig().getStringList("onjoin.subtitles").size()){
                        
                            PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(s) + "\"}"));    
                            PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks") - 10, 10);
                                                
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(subtitle);
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
                        
                        }else {
                        
                            PacketPlayOutTitle subtitle = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, ChatSerializer.a("{\"text\":\"" + utils.toColor(s) + "\"}"));    
                            PacketPlayOutTitle length = new PacketPlayOutTitle(0, plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks"), 0);
                                                
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(subtitle);
                            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(length);
                        
                        }
                    
                        next++;
                    
                    }else {
                        this.cancel();
                    }
                }
            }.runTaskTimer(plugin, 0, plugin.getConfig().getInt("onjoin.subtitle.stay-per-subtitle-ticks"));
        
            //TABLIST:
            IChatBaseComponent headerText = ChatSerializer.a("{\"text\":\"" + utils.toColor(plugin.getConfig().getString("tablist.header")) + "\"}");
            IChatBaseComponent footerText = ChatSerializer.a("{\"text\": \"" + utils.toColor(plugin.getConfig().getString("tablist.footer")) + "\"}");
        
        
           PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
      
           try {
          
               Field headerField = packet.getClass().getDeclaredField("a");
               headerField.setAccessible(true);
               headerField.set(packet, headerText);
               headerField.setAccessible(!headerField.isAccessible());
          
               Field footerField = packet.getClass().getDeclaredField("b");
               footerField.setAccessible(true);
               footerField.set(packet, footerText);
               footerField.setAccessible(!footerField.isAccessible());
            
           } catch (Exception er) {
               er.printStackTrace();
           }
        
            ((CraftPlayer) e.getPlayer()).getHandle().playerConnection.sendPacket(packet);
        
        
        }
    
    [/QUOTE]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 2, 2017
Thread Status:
Not open for further replies.

Share This Page