It is currently Thu May 23, 2019 11:15 pm


Code running wrong way round

Need Technical Help?

Code running wrong way round

Postby Callan2256 » Tue Dec 16, 2014 8:02 pm

I've tried coding a plugin that has a custom login message when the player logs in and then extends the login message by sending some messages to that player, but for some reason, the private messages get to the player BEFORE the custom login message shows up....

Heres the code:

Code: Select all
package me.Callan2256.Learning;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.EventHandler;

public class MyPlayerListener implements Listener{
   
   public static Learning plugin;
   
   
   @EventHandler
   public void onPlayerJoin(PlayerJoinEvent event){
      Player player = event.getPlayer();
      event.setJoinMessage(ChatColor.GOLD + "Welcome " + player.getName() + ChatColor.GOLD + " To The " + ChatColor.DARK_GREEN + ChatColor.BOLD + "NZN MINIGAMES " + ChatColor.GOLD + "Server!");
      player.sendMessage("Test");
      
   }

}



But for some reason the player.sendmessage showed up before the event.setjoinmessage

I was talking to sori and he suggested adding

try
{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
/*Do nothing, let it fail quietly*/
}

But that didn't work either

Does anyone know how to make the login message appear before the player.sendmessage does?"

Thanks,
Callan
Image

Callan2256 wrote:lets find something else to destroy

Callan2256 was blown up by Zombie Pigman
User avatar
Callan2256
 
Posts: 166
Joined: Fri Jun 28, 2013 5:23 pm
Location: Taranaki - New Plymouth

Re: Code running wrong way round

Postby sorifiend » Tue Dec 16, 2014 10:54 pm

The problem is that you use two different methods to send the messages to the player. Each method has its own queue, and will send in its own time.

Can't you just use player.sendMessage for both messages? Then they will be in the correct order.

Quick correction below so others don't take me for a coding fool =)
I didn't suggest you use thread.sleep, I recommended against it, I was just answering you question about how to add a simple delay. As I said it should not be used unless you are threading properly and understand what it is doing.
Image
User avatar
sorifiend
Global Admin
 
Posts: 3001
Joined: Sun Nov 07, 2010 8:01 pm
Location: Bay Of Plenty

Re: Code running wrong way round

Postby sorifiend » Tue Dec 16, 2014 10:59 pm

Additional note:
I am on mobile so don't have the bukkit API handy, but doesn't the event.setJoinMessage just set/change a message rather than broadcasting it right away? That is the cause of your problem.

Edit:
Also why not use PlayerLoginEvent instead of PlayerJoinEvent?
You are correct, JointEvent is the correct listener.
Image
User avatar
sorifiend
Global Admin
 
Posts: 3001
Joined: Sun Nov 07, 2010 8:01 pm
Location: Bay Of Plenty

Re: Code running wrong way round

Postby frogman786 » Wed Dec 17, 2014 5:35 pm

I assume you're only dong two different message to get them on separate lines?
you could go with something like
Code: Select all
player.sendMessage("this is on the first line\nthis is on the second");

or
Code: Select all
StringBuilder message = new StringBuilder();
message.append("this is on the first line"+"\n");
message.append("this is on the second");
player.sendMessage(message.toString());

^ this method is only really useful when you're trying to add lots of stuff to one message e.g. a loop that compiles all the online players into a formatted list /who
[Reveal] Spoiler: "example"
Code: Select all
StringBuilder online = new StringBuilder();
            Player[] players = Bukkit.getOnlinePlayers();
            for(Player listplayer : players){
               // If a player is hidden from the sender don't show them in the list
               if(sender instanceof Player && !((Player) sender).canSee(listplayer))
                  continue;                         
                  if(online.length() > 0){
                     online.append(", ");
                  }   
                  online.append(listplayer.getDisplayName());
               }
               sender.sendMessage(ChatColor.GREEN +"Online players " + ChatColor.RED + players.length + ChatColor.WHITE + " : " + online.toString());
               return true;
Image
~Head monkey trainer at the Shakespearean institute of arts~
User avatar
frogman786
Minecraft Staff
 
Posts: 998
Joined: Fri Dec 23, 2011 8:06 pm

Re: Code running wrong way round

Postby Callan2256 » Wed Dec 17, 2014 8:21 pm

THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Image

Callan2256 wrote:lets find something else to destroy

Callan2256 was blown up by Zombie Pigman
User avatar
Callan2256
 
Posts: 166
Joined: Fri Jun 28, 2013 5:23 pm
Location: Taranaki - New Plymouth

Re: Code running wrong way round

Postby Callan2256 » Wed Apr 24, 2019 11:03 am

Wow I was really bad at coding, also why did I just get an email notification about this topic lel
Image

Callan2256 wrote:lets find something else to destroy

Callan2256 was blown up by Zombie Pigman
User avatar
Callan2256
 
Posts: 166
Joined: Fri Jun 28, 2013 5:23 pm
Location: Taranaki - New Plymouth

Re: Code running wrong way round

Postby mcpeachpies » Wed Apr 24, 2019 11:06 am

Callan2256 wrote:Wow I was really bad at coding, also why did I just get an email notification about this topic lel


Some guy from Denmark made a reply
Image
Image
https://twitter.com/mcpeachpies
http://event.nznetwork.co.nz/
Oblitus_Sicarius wrote:I finally found some pants


ExplorerCorp, come see the Galaxy with us
User avatar
mcpeachpies
 
Posts: 746
Joined: Mon Sep 16, 2013 9:47 pm
Location: ༼つ◕_◕༽つ that way

Re: Code running wrong way round

Postby usb_hub » Sun Apr 28, 2019 10:09 am

mcpeachpies wrote:
Callan2256 wrote:Wow I was really bad at coding, also why did I just get an email notification about this topic lel


Some guy from Denmark made a reply

Pretty sure it was a bot. :P
by quog38 » Sun May 27, 2012 5:17 pm

quog38 wrote:
usb_hub wrote:I'd like to thank quog on becoming a mod. I know this is late, but better late than never.

This is now my signature.
User avatar
usb_hub
Global Moderator
 
Posts: 1577
Joined: Mon Jul 25, 2011 8:52 pm
Location: Christchurch


Return to Tech Support

Who is online

Users browsing this forum: No registered users and 1 guest