PlayerJoinEvent Doesn't work

Discussion in 'Plugin Development' started by Ananaskirsche, May 13, 2014.

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

    Ananaskirsche

    Hello Guys,
    I was just making a new Plugin which uses The PlayerJoinEvent. But when I test it it doesnt work. I build in an Console-Output for testing too but the it weren't printed. So I think the Event doesn't work but here is my code:
    Code:java
    1. package listener;
    2.  
    3. import main.Main;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.EventPriority;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import filehandler.MessageConfig;
    9.  
    10. public class PlayerJoin implements Listener
    11. {
    12. main.Main plugin;
    13. public PlayerJoin(Main plugin)
    14. {
    15. this.plugin = plugin;
    16. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    17. }
    18.  
    19.  
    20. MessageConfig messages = new MessageConfig();
    21.  
    22.  
    23. @EventHandler(priority = EventPriority.HIGHEST)
    24. public void onPlayerJoin(PlayerJoinEvent ev)
    25. {
    26. String Input = messages.requestString("joinMessage");
    27. String Player = ev.getPlayer().getName();
    28. String Output = Input.replace("{player}", Player);
    29.  
    30. ev.setJoinMessage(Output);
    31. }
    32. }
    33.  


    The messages.requestString is my class which read out my config. I looked into the code for reading but there wasn't a mistake. I'm using the same class for a command. There it works. Can someone help me please?
     
  2. Offline

    JoeyDevs

    I dont think you understand Java & BukkitAPI so maybe first learn java and then BukkitAPI before start developing plugins

    But try this
    Code:java
    1. public void onEnable(){
    2. plugin.getServer().getPluginManager().registerEvents(this, this);
    3. }


    And maybe this is handy to:
    Code:java
    1. public class PlayerJoin extend JavaPlugin implements Listener
    2. {
     
  3. Offline

    Ananaskirsche

    I'm using a construktor which is working, i tested it. And you cant extend JavaPlugin 2 Times in one plugin ; )
     
  4. Offline

    JoeyDevs

    O this is not your main class?
    If it isn't you need to register the event in the main class by onEnable()

    Like so:
    Code:java
    1. plugin.getServer().getPluginManager().registerEvents(new class(), this);
     
  5. Offline

    Ananaskirsche

    I know, but in the construktor I can do it like in the Main class. It doesnt makes it different.
     
  6. Offline

    Rocoty

    Ananaskirsche Don't worry. Your code looks good. You should try adding a line at the beginning of the event handler method which prints a message, for debugging. plugin.getLogger().info("Blabla");

    If that message is not printed, then please show us your main class.

    EDIT: Of course, this message will get printed in the console. You should also check the console for any error messages.
     
  7. Offline

    Ananaskirsche

    There are no Errors in the Console or in the log. Has someone an Idea. I wanna to bring out a new update and can't because there seems to be a bug but not in mine code :(

    Sorry I cant use the Edit -.-

    Rocoty
    Thats another problem. I wrote an Output when the Event has to be activated but I could not see it in the console. It seems that there is a problem with the Event. I wish a BukkitDev could look at mine problem :rolleyes: Will try to register Event in the main class

    EDIT: Doesn't work :(

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

    Rocoty

  9. Offline

    Ananaskirsche

    Main
    Code:java
    1. package main;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import filehandler.*;
    5.  
    6. public class Main extends JavaPlugin
    7. {
    8.  
    9. //Statische Strings
    10. public static String prefix = "§c[§2ServerTeam§c]§6 ";
    11. public static String noPerms = "§4You do not have access to this command!";
    12. public static String consPerformedCmd = "[ServerTeam] You can do this only ingame!";
    13.  
    14. MessageConfig mc = new MessageConfig();
    15. TeamConfig tc = new TeamConfig();
    16.  
    17.  
    18. //Beim Starten ausführen
    19. public void onEnable()
    20. {
    21. mc.checkFile();
    22. tc.checkFile();
    23. executeCommands();
    24. System.out.println("[Serverteam] v" + getDescription().getVersion() + " coded by Ananaskirsche");
    25. }
    26.  
    27.  
    28. //Beim Stoppen ausführen
    29. public void onDisable()
    30. {
    31. System.out.println("[ServerTeam] Successfully saved configs!");
    32. System.out.println("[ServerTeam] stopped v" + getDescription().getVersion());
    33. }
    34.  
    35.  
    36.  
    37. public void executeCommands()
    38. {
    39. getCommand("team").setExecutor(new command.Team());
    40. getCommand("clearchat").setExecutor(new command.ClearChat());
    41. }
    42.  
    43.  
    44.  
    45. public void loadEvents()
    46. {
    47. getServer().getPluginManager().registerEvents(new listener.PlayerJoin(), this);
    48. new listener.PlayerQuit(this);
    49. new listener.Reload(this);
    50. }
    51. }
    52.  


    MessageConfig(The class who reads out of the config)
    Code:java
    1. package filehandler;
    2.  
    3. import java.io.*;
    4.  
    5. public class MessageConfig
    6. {
    7. //Erstellen der Virtuellen Datei
    8. private static String f = File.separator;
    9. private static File foulder = new File("plugins" + f + "ServerTeam");
    10. private static File teamFile = new File("plugins" + f + "ServerTeam" + f + "messages.yml");
    11.  
    12. //Erstellen der Output-Strings
    13. private String Input;
    14. private String Output;
    15.  
    16.  
    17. public String requestString(String request)
    18. {
    19. checkFile();
    20. try
    21. {
    22. //Erstellen der Reader
    23. FileReader reader = new FileReader(teamFile);
    24.  
    25. //Erstellen der Variable mit Zeilennummer
    26. byte zeile = 0;
    27.  
    28. //Ertsellen des Zeilenzählers
    29. byte line = 0;
    30.  
    31.  
    32.  
    33. //Bei den Zeilen immer noch 2 draufaddieren weil 2 Zeilen am Anfang hadder sind
    34. if(request == "joinMessage")
    35. {
    36. zeile = 3;
    37. }
    38. else if(request == "quitMessage")
    39. {
    40. zeile = 4;
    41. }
    42. else if(request == "cleanedChat")
    43. {
    44. zeile = 5;
    45. }
    46. else if(request == "reloadMessage")
    47. {
    48. zeile = 6;
    49. }
    50.  
    51.  
    52. //line wird so lange erhöht bis sie zeile entspricht
    53. while((Input = r.readLine()) != null)
    54. {
    55. line++;
    56. if(zeile == line)
    57. {
    58. r.close();
    59. break;
    60. }
    61.  
    62. }
    63.  
    64. //Filtert das passende Prefix aus dem Dateiinhalt raus um nur die eingabe des Users zu erhalten
    65. if(request == "joinMessage")
    66. {
    67. Output = Input.replace("joinMessage :", "");
    68. }
    69. else if(request == "quitMessage")
    70. {
    71. Output = Input.replace("quitMessage :", "");
    72. }
    73. else if(request == "cleanedChat")
    74. {
    75. Output = Input.replace("cleanedChat :", "");
    76. }
    77. else if(request == "reloadMessage")
    78. {
    79. Output = Input.replace("reloadMessage :", "");
    80. }
    81. }
    82. catch(IOException ex)
    83. {
    84. System.err.println("[ServerTeam]Could not read out message.yml");
    85. }
    86. return Output;
    87. }
    88.  
    89.  
    90.  
    91. //Checkt ob Datei vorhanden ist wenn nicht wird sie erstellt
    92. public void checkFile()
    93. {
    94. if(!teamFile.exists() || !teamFile.isFile())
    95. {
    96. try
    97. {
    98. //Erstellen der Datei
    99. foulder.mkdirs();
    100. teamFile.createNewFile();
    101.  
    102. //Erstellen der Reader
    103. FileWriter writer = new FileWriter(teamFile);
    104.  
    105. //Schreiben der Standart-Wert
    106. w.write("#You HAVE to write your values like this => joinMessage: welcome\n");
    107. w.write("\n");
    108. w.write("joinMessage: §e{player} joined the server\n");
    109. w.write("quitMessage: §e{player} left the server\n");
    110. w.write("cleanedChat: §c{player} cleared the chat\n");
    111. w.write("reloadMessage: §4§lServer-Reload countdown started!\n");
    112.  
    113. w.flush();
    114. w.close();
    115. }
    116. catch(IOException ex)
    117. {
    118. System.err.println("[ServerTeam] Could not create message.yml");
    119. }
    120. }
    121. }
    122. }
    123.  


    And my EventClass again
    Code:java
    1. package listener;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.EventPriority;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import filehandler.MessageConfig;
    9.  
    10. public class PlayerJoin implements Listener
    11. {
    12. // main.Main plugin;
    13. // public PlayerJoin(Main plugin)
    14. // {
    15. // this.plugin = plugin;
    16. // plugin.getServer().getPluginManager().registerEvents(this, plugin);
    17. // }
    18.  
    19.  
    20. MessageConfig messages = new MessageConfig();
    21.  
    22.  
    23. @EventHandler(priority = EventPriority.HIGHEST)
    24. public void onPlayerJoin(PlayerJoinEvent ev)
    25. {
    26. String Input = messages.requestString("joinMessage");
    27. String Player = ev.getPlayer().getName();
    28. String Output = Input.replace("{player}", Player);
    29.  
    30. ev.setJoinMessage("");
    31. Bukkit.broadcastMessage(Output);
    32. }
    33. }
    34.  


    I'm a german so ignore the comments:D

    bump
    Some help?

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

    Rocoty

    You are never calling the loadEvents method, therefore the events are never registered.

    Side note: Please don't expect me to return to the thread unless you tahg me.
     
  11. Offline

    Ananaskirsche

    Youre right #Imanidiot
     
Thread Status:
Not open for further replies.

Share This Page