Solved Custom Join Message

Discussion in 'Plugin Development' started by Ty Cleere, Oct 6, 2013.

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

    Ty Cleere

    Ok so i am trying to make a simple join message plugin. But i am also making it say how many people have joined the server ever. But it isnt working. Can someone tell me what i am doing wrong? Im also looking for a way to make a config save the number of people that have joined the server so if you could help me on that too that would be great! :D

    Here is my code.
    Code:java
    1. public class join extends JavaPlugin implements Listener {
    2.  
    3. public join plugin;
    4.  
    5. public void onEnable() {
    6. getConfig().options().copyDefaults(true);
    7. saveConfig();
    8. }
    9.  
    10. @EventHandler
    11. public void onPlayerJoin(PlayerJoinEvent event) {
    12. // sends the player a welcome message
    13. int counter = 0;
    14. Player player = event.getPlayer();
    15. if (!player.hasPlayedBefore()) {
    16. counter++;
    17. player.sendMessage(Bukkit.getPluginManager()
    18. .getPlugin("JoinMessage").getConfig()
    19. .getString("Join Message:"));
    20. Bukkit.broadcastMessage(ChatColor.AQUA
    21. + event.getPlayer().getName() + " has joined for the "
    22. + ChatColor.DARK_PURPLE + "first " + ChatColor.AQUA
    23. + "time!" + ChatColor.AQUA + " There have been a total of "
    24. + ChatColor.DARK_PURPLE + counter + ChatColor.AQUA
    25. + " people ever on this server!");
    26. } else {
    27. player.sendMessage(Bukkit.getPluginManager()
    28. .getPlugin("JoinMessage").getConfig()
    29. .getString("Join Message:"));
    30. }
    31. }
    32. }
     
  2. Offline

    mydeblob

    Ty Cleere
    What's not working with it? And the integer variable counter will reset every reload so you need to be putting the amount of players that have joined in a yml file
     
  3. Offline

    Ty Cleere

    mydeblob Idk it doesnt give me an error or anything. And the code i put in here is code that should have the amount of players but it doesnt.
     
  4. Offline

    mydeblob

    Ty Cleere
    Well, Like I said every time you reload the server the amount of players will reset because you are storing it in a integer value. You need to store it in a config. And why are you getting the plugin when you are getting the config? Just do this
    Code:java
    1. player.sendMessage(this.getConfig()
    2. .getString("Join Message:"));

    And also none of your event will be working becaue you haven't registered it in your onEnable. Put this in your onEnable
    Code:
    getServer().getPluginManager().registerEvents(this, this);
     
  5. Offline

    Ty Cleere

    mydeblob Ok i fixed those. And i think i have the code for storing the amount of players, but can you look at it?

    Code:java
    1. int players = Bukkit.getPluginManager().getPlugin("JoinMessage")
    2. .getConfig().getInt("Total Players:");
    3. Player player = event.getPlayer();
    4. if (!player.hasPlayedBefore()) {
    5. players++;
    6. Bukkit.getPluginManager().getPlugin("JoinMessage").saveConfig();
    7. }
     
  6. Offline

    mydeblob

    Ty Cleere
    You can do it that way, however this way would be a bit more stable
    Code:java
    1. public class join extends JavaPlugin implements Listener {
    2.  
    3. public join plugin;
    4.  
    5. public void onEnable() {
    6. getConfig().options().copyDefaults(true);
    7. getServer().getPluginManager().registerEvents(this,this);
    8. saveConfig();
    9. }
    10.  
    11. @EventHandler
    12. public void onPlayerJoin(PlayerJoinEvent event) {
    13. // sends the player a welcome message
    14. int counter;
    15. Player player = event.getPlayer();
    16. if (!player.hasPlayedBefore()) {
    17. counter++;
    18. this.getConfig().set("Total Players", counter);
    19. player.sendMessage(Bukkit.getPluginManager()
    20. .getPlugin("JoinMessage").getConfig()
    21. .getString("Join Message:"));
    22. Bukkit.broadcastMessage(ChatColor.AQUA
    23. + event.getPlayer().getName() + " has joined for the "
    24. + ChatColor.DARK_PURPLE + "first " + ChatColor.AQUA
    25. + "time!" + ChatColor.AQUA + " There have been a total of "
    26. + ChatColor.DARK_PURPLE + this.getConfig().getInt("Total Players") + ChatColor.AQUA
    27. + " people ever on this server!");
    28. } else {
    29. player.sendMessage(Bukkit.getPluginManager()
    30. .getPlugin("JoinMessage").getConfig()
    31. .getString("Join Message:"));
    32. }
    33. }
    34. }

    There may be a few errors in that as I didn't write it in eclipse.
     
  7. Offline

    PatoTheBest

    What is the difference between PlayerLoginEvent and PlayerJoinEvent?
     
    Ty Cleere likes this.
  8. Offline

    Ty Cleere

  9. Offline

    Quantix

    I could be wrong about this, but the PlayerLoginEvent is fired when the player is still trying to log into the server. Therefore you can allow the player to log in or stop the player from logging in by kicking him. You can also get the address the player is logging in from etc. The PlayerJoinEvent is called after the PlayerLoginEvent was called and the player allowed to log in. ThePlayerJoin event cannot be "denied". The PlayerJoinEvent also allows you to change the login message that is sent to all players.
     
  10. Offline

    kevinspl2000

    PatoTheBest The difference is that PlayerJoinEvent means it is when the player actually joins the server, but PlayerLoginEvent is when the player is still connecting. For example if I was doing a plugin that says "<Player> HAS JOINED THE SERVER FOR THE FIRST TIME", I would do something like
    Code:java
    1. @EventHandler
    2. public void onPlayerLoginEvent(final PlayerLoginEvent event) {
    3. Player player = event.getPlayer();
    4. if (!player.hasPlayedBefore()) {
    5. for(Player p : Bukkit.getOnlinePlayers()){
    6. String pname = player.getName();
    7. String color = Util.getColour(player);
    8. p.sendMessage(color + pname + ChatColor.DARK_RED + " HAS JOINED FOR THE FIRST TIME!");

    And if I used PlayerJoinEvent it would already say I played before because he is playing right now but PlayerLoginEvent means he is still joining and has never played.
     
  11. Offline

    Ty Cleere

    How do i add 1 to a int in the config file? Im not doing it right.

    nvm guys i got it. Thanks for the help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  12. Offline

    PatoTheBest

Thread Status:
Not open for further replies.

Share This Page