Solved OnPlayerJoin double..

Discussion in 'Plugin Development' started by AppleMen, Nov 8, 2013.

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

    AppleMen

    Hello,

    I made a code that sends a message to the player when they join, but it is sent 2 times.. I don't know how:

    My code:
    Code:java
    1. //*Join-Leave Messages*\\
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent event){
    4. Player p = event.getPlayer();
    5. event.setJoinMessage(ChatColor.GOLD + p.getPlayerListName() + ChatColor.GRAY + " has joined the game!");
    6. p.sendMessage(ChatColor.DARK_GREEN + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------------");
    7. p.sendMessage(ChatColor.AQUA + "Welcome to the Castle Crystals server!");
    8. Integer amount = Bukkit.getOnlinePlayers().length;
    9. p.sendMessage(ChatColor.BLUE + "There are now " + ChatColor.YELLOW + amount + ChatColor.BLUE + " player(s) online!");
    10. p.sendMessage(ChatColor.DARK_GREEN + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------------");
    11. if(!p.getPlayer().hasPlayedBefore()){
    12. Bukkit.broadcastMessage(ChatColor.GREEN + "Welcome to the server " + ChatColor.YELLOW + p.getPlayerListName() + ChatColor.GREEN + "!");
    13. Firework(event.getPlayer());
    14. }
    15. }


    Screenshot:
    [​IMG]
     
  2. Offline

    BungeeTheCookie

    AppleMen You have to either create a new if statement if the player has played before, or add a return statement before the if !player.hasPlayedBefore()
     
  3. Offline

    AppleMen

    Can you show me how? I got some errors while trying it :/
     
  4. Offline

    AndyMcB1

    Add a 10 tick delay to the message. See what happens.
     
  5. Offline

    hellboyPS

    Did you register it twice?
     
  6. Offline

    Tss1410

    you run the code one time for every time a player logs in. and then if you add if(!(player.hasPlayedBefore())){
    if that is true, it will send the message that you first sent, and then if its true, it will send the other.


    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event){
    3.  
    4. Player p = event.getPlayer();
    5. if(p.hasPlayedBefore()){
    6. event.setJoinMessage(ChatColor.GOLD + p.getPlayerListName() + ChatColor.GRAY + " has joined the game!");
    7. p.sendMessage(ChatColor.DARK_GREEN + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------------");
    8. p.sendMessage(ChatColor.AQUA + "Welcome to the Castle Crystals server!");
    9. Integer amount = Bukkit.getOnlinePlayers().length;
    10. p.sendMessage(ChatColor.BLUE + "There are now " + ChatColor.YELLOW + amount + ChatColor.BLUE + " player(s) online!");
    11. p.sendMessage(ChatColor.DARK_GREEN + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------------");
    12. } else {
    13. Bukkit.broadcastMessage(ChatColor.GREEN + "Welcome to the server " + ChatColor.YELLOW + p.getPlayerListName() + ChatColor.GREEN + "!");
    14. Firework(event.getPlayer());
    15. }
    16. }
     
  7. Offline

    The_Doctor_123

    Try out other events and see if they run twice. If so, you must've registered events twice.
     
  8. Offline

    AppleMen

    Nop

    Its still duplicating :/
     
  9. Offline

    1Rogue

    Does your plugin have a reload function?
     
  10. Offline

    AppleMen

    No. Do I have to add one?
     
  11. Offline

    1Rogue

    Nope, just reminiscing on a bug I had before. Can you show your main class?
     
  12. Offline

    hellboyPS

    Im like 99% sure you registered it twice.
    Have you tried (if youre using eclipse) searching for constructor calls? (Select constructor method name with cursor, press Ctrl+Shift+G)

    Edit: The other 1% is you have loaded the plugin twice (maybe under a different name? check your plugin folder)
     
  13. Offline

    The_Doctor_123

    AppleMen
    Try what I said.. that would answer a lot of things.
     
  14. Offline

    AppleMen

    Code:java
    1. package me.matthijs110.CustomTeleport;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Color;
    8. import org.bukkit.FireworkEffect;
    9. import org.bukkit.Location;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.EntityType;
    13. import org.bukkit.entity.Firework;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.event.player.PlayerQuitEvent;
    19. import org.bukkit.inventory.meta.FireworkMeta;
    20. import org.bukkit.plugin.PluginManager;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.potion.PotionEffect;
    23.  
    24. public class Main extends JavaPlugin implements Listener {
    25.  
    26. public void onEnable(){
    27. PluginManager pm = getServer().getPluginManager();
    28. pm.registerEvents(new Main(), this);
    29. pm.registerEvents(new ColoredSigns(), this);
    30. pm.registerEvents(new AutoMessage(), this);
    31. saveDefaultConfig();
    32. }
    33.  
    34. public void Firework(Player player){
    35. Firework fw = (Firework) player.getWorld().spawnEntity(player.getLocation(), EntityType.FIREWORK);
    36. FireworkMeta fwmeta = fw.getFireworkMeta();
    37. FireworkEffect.Builder builder = FireworkEffect.builder();
    38. builder.withTrail();
    39. builder.withFlicker();
    40. builder.withFade(Color.RED);
    41. builder.withColor(Color.ORANGE);
    42. builder.withColor(Color.YELLOW);
    43. builder.withColor(Color.LIME);
    44. builder.withColor(Color.TEAL);
    45. builder.withColor(Color.PURPLE);
    46. builder.withColor(Color.FUCHSIA);
    47. builder.with(FireworkEffect.Type.BALL);
    48. fwmeta.addEffects(builder.build());
    49. fwmeta.setPower(1);
    50. fw.setFireworkMeta(fwmeta);
    51. }
    52.  
    53. ArrayList <String> afkList = new ArrayList <String> ();
    54. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    55. Player player = (Player) sender;
    56. Location helploc = new Location(Bukkit.getServer().getWorld("World"), 120, 54, 365);
    57. Location spawnloc = new Location(Bukkit.getServer().getWorld("World"), 346, 84, 336);
    58.  
    59. //*Help teleport*\\
    60. if (sender.hasPermission("server.teleport.help")) {
    61. if(cmd.getName().equalsIgnoreCase("help")){
    62. player.teleport(helploc);
    63. player.sendMessage(ChatColor.AQUA + "Teleported to the Info Wall");
    64. } else {
    65. player.sendMessage(ChatColor.WHITE + "Unknown command. Type \"help\" for help");
    66. }
    67. }
    68.  
    69. //*Spawn Location*\\
    70. if (sender.hasPermission("server.teleport.spawn")) {
    71. if(cmd.getName().equalsIgnoreCase("spawn")){
    72. player.sendMessage(ChatColor.GREEN + "Teleporting to spawn..");
    73. player.teleport(spawnloc);
    74. }
    75. }
    76.  
    77. //*Heal Command*\\
    78. if (sender.hasPermission("server.heal")) {
    79. if(cmd.getName().equalsIgnoreCase("heal")) {
    80. player.setHealth(20.0);
    81. player.setFoodLevel(20);
    82. sender.sendMessage(ChatColor.GREEN + "You have been healted!");
    83. }
    84. for (PotionEffect effect : player.getActivePotionEffects())
    85. {
    86. player.removePotionEffect(effect.getType());
    87. }
    88. if (sender.hasPermission("server.heal.other")) {
    89. Player target = Bukkit.getServer().getPlayer(args[0]);
    90. if (target != null) {
    91. target.setHealth(20.0);
    92. } else {
    93. sender.sendMessage(ChatColor.WHITE + "Unknown command. Type \"help\" for help");
    94. }
    95.  
    96. //*Suicide Command*\\
    97. if (sender.hasPermission("server.suicide")) {
    98. if(cmd.getName().equalsIgnoreCase("kill")) {
    99. player.setHealth(0.0);
    100. sender.sendMessage(ChatColor.DARK_RED + "You committed suicide!");
    101. Bukkit.broadcastMessage(ChatColor.GOLD + player.getName() + ChatColor.DARK_AQUA + " Committed suicide");
    102. }
    103. }
    104.  
    105. //*AFK Command*\\
    106. if (sender.hasPermission("server.afk"))
    107. if (cmd.getName().equalsIgnoreCase("afk")) {
    108. if (!afkList.contains(player.getName())) {
    109. afkList.add(player.getName());
    110. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + player.getName() + ChatColor.GRAY + " is now AFK");
    111. } else {
    112. afkList.remove(player.getName());
    113. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + player.getName() + ChatColor.GRAY + " is no longer AFK");
    114. }
    115. }
    116.  
    117. //*Broadcasting*\\
    118. if(cmd.getName().equalsIgnoreCase("broadcast"))
    119. {
    120. if(sender.isOp() || sender.hasPermission("server.broadcast"))
    121. {
    122. if(args.length == 0)
    123. {
    124. sender.sendMessage(ChatColor.YELLOW + "Usage: " + ChatColor.GOLD + "/broadcast" + ChatColor.GREEN + "<message>");
    125. }
    126. else
    127. {
    128. String msg = "";
    129. for (int i = 0; i < args.length; i++)
    130. {
    131. msg = msg + args[i] + " ";
    132. }
    133. {
    134. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', msg));
    135. }
    136. }
    137. }
    138. else
    139. {
    140. sender.sendMessage(ChatColor.WHITE + "Unknown command. Type \"help\" for help");
    141. }
    142. }
    143. }
    144. }
    145. return false;
    146. }
    147. //*Join-Leave Messages*\\
    148. @EventHandler
    149. public void onPlayerJoin(PlayerJoinEvent event){
    150. Player p = event.getPlayer();
    151. if(p.hasPlayedBefore()){
    152. event.setJoinMessage(ChatColor.GOLD + p.getPlayerListName() + ChatColor.GRAY + " has joined the game!");
    153. p.sendMessage(ChatColor.DARK_GREEN + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------------");
    154. p.sendMessage(ChatColor.AQUA + "Welcome to the Castle Crystals server!");
    155. Integer amount = Bukkit.getOnlinePlayers().length;
    156. p.sendMessage(ChatColor.BLUE + "There are now " + ChatColor.YELLOW + amount + ChatColor.BLUE + " player(s) online!");
    157. p.sendMessage(ChatColor.DARK_GREEN + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------------");
    158. } else {
    159. Bukkit.broadcastMessage(ChatColor.GREEN + "Welcome to the server " + ChatColor.YELLOW + p.getPlayerListName() + ChatColor.GREEN + "!");
    160. Firework(event.getPlayer());
    161. }
    162. }
    163. @EventHandler
    164. public void onPlayerQuit(PlayerQuitEvent event){
    165. Player p = event.getPlayer();
    166. event.setQuitMessage(ChatColor.GOLD + p.getPlayerListName() + ChatColor.GRAY + " has left the game!");
    167. }
    168. }[/i]
     
  15. Offline

    hellboyPS

    Replace Line 28 with:
    pm.registerEvents(this, this);

    Dunno if it will solve the problem, but do it anyway...
     
  16. Offline

    The_Doctor_123

    AppleMen
    Huge issue there.. You're creating a secondary instance of your plugin class which causes trouble. Do what hellboyPS said.
     
  17. Offline

    AppleMen

  18. Offline

    The_Doctor_123

    AppleMen
    Like I asked earlier, do all events in your Main class run twice or just the one listening for the PlayerJoinEvent?
     
  19. Offline

    AppleMen

    one listening for the PlayerJoinEvent
     
  20. Offline

    1Rogue

    What are your other listeners?
     
  21. Offline

    AppleMen

    How you mean?
     
  22. Offline

    The_Doctor_123

    So the PlayerQuitEvent doesn't execute twice as well? Possibly you made a duplicate in your other Listeners?
     
  23. Offline

    AppleMen

    Should I try to make a separate class? For the Message on Join
     
  24. Offline

    The_Doctor_123

    AppleMen
    Please read the questions I asked you.
     
  25. Offline

    AppleMen

    Like I asked earlier, do all events in your Main class run twice or just the one listening for the PlayerJoinEvent?

    I don't know :(
     
  26. Offline

    The_Doctor_123

    AppleMen
    They do if you register them twice.. Please answer my questions, they're not that hard.

    I'm asking you to test out ANY other event besides the PlayerJoinEvent and see if it runs twice just like your PlayerJoinEvent. I'm also asking if you possibly have a duplicate method in another one of your Listeners(and show us the code for those classes).
     
  27. Offline

    AppleMen

    I decided to start over.. So this is my class now: Only the Firwork and Join event!

    Code:java
    1. public void Firework(Player player){
    2. Firework fw = (Firework) player.getWorld().spawnEntity(player.getLocation(), EntityType.FIREWORK);
    3. FireworkMeta fwmeta = fw.getFireworkMeta();
    4. FireworkEffect.Builder builder = FireworkEffect.builder();
    5. builder.withTrail();
    6. builder.withFlicker();
    7. builder.withFade(Color.RED);
    8. builder.withColor(Color.ORANGE);
    9. builder.withColor(Color.YELLOW);
    10. builder.withColor(Color.LIME);
    11. builder.withColor(Color.TEAL);
    12. builder.withColor(Color.PURPLE);
    13. builder.withColor(Color.FUCHSIA);
    14. builder.with(FireworkEffect.Type.BALL);
    15. fwmeta.addEffects(builder.build());
    16. fwmeta.setPower(1);
    17. fw.setFireworkMeta(fwmeta);
    18. }
    19.  
    20. }


    That one doesn't work at all :/
     
  28. Offline

    The_Doctor_123

    AppleMen
    What are you doing..?

    I'm sorry, but if you can't answer a couple simple questions, we cannot help you.
     
  29. Offline

    AppleMen

    Its just easier for myself. So I have more overview.. So my question now is, how can i get this class to work? Its the only and Main class.
     
  30. Offline

    The_Doctor_123

    AppleMen
    I can't answer you until you answer me! Do you think I'm asking questions for the fun of it? Be thankful that I've put up with you not listening this far.
     
Thread Status:
Not open for further replies.

Share This Page