Simple Plugin won't work! HELP!

Discussion in 'Plugin Development' started by Bernabeast, Jun 17, 2013.

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

    Bernabeast

    I'm a new Java Dev. and I created a plugin that does 3 things
    ***Says "Test has been Enabled" during a server start
    ***Says "Test has been Disabled" during a server shutdown
    ***Sends a welcome message when you join the server that reads "Welcome to Libertycraft [name]!"

    For any reason, the welcome message I cant get to show up.
    The disable and enable messages work fine,
    please help!
    ***HERE IS THE CODE***

    package me.Bernabeast.plugin;

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

    public class Test extends JavaPlugin implements Listener{

    @Override
    public void onEnable(){
    System.out.println("Test has been Enabled!");

    }
    @Override
    public void onDisable(){
    System.out.println("Test has been Disabled!");
    }
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
    Player player = event.getPlayer();
    event.setJoinMessage(ChatColor.AQUA + "Welcome to Libertycraft " + player.getName() + "!");
    }




    }
     
  2. Offline

    ximsilentx

    Code:java
    1. package me.Bernabeast.plugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Test extends JavaPlugin implements Listener{
    11.  
    12. public final Logger log = Logger.getLogger("Minecraft"); <----The Defined Logger
    13.  
    14. @Override
    15. public void onEnable(){
    16. PluginManager pm = getServer().getPluginManager(); <-----This is the plugin manager
    17. this.log.info("Test Has been enabled!");
    18. pm.registerEvents(this, this); <-------you always have to register the listeners
    19. }
    20. @Override
    21. public void onDisable(){
    22. this.log.info(" Test Has been disabled!");
    23. }
    24. @EventHandler
    25. public void onPlayerJoin(PlayerJoinEvent event){
    26. Player player = event.getPlayer();
    27. event.setJoinMessage(ChatColor.AQUA + "Welcome to Libertycraft " + player.getName() + "!");
    28. }
    29. }


    The rest looked ok
    there were a few things wrong that i noticed.
    1.) You did not have a logger defined.... i put that in.
    a logger is what the plugin uses to contact the console

    2.)you did not have a way to register your events. for this you have to use the plugin manager
    i added that to the onEnable and took the time to register the event for you. you have to register all Listeners in the main class in the onEnable section. becuase you used the main class as a listener it was easy

    3.) you cant use system.out.println in a plugin. you have to use the logger

    ex. this.log.info(msg)
     
  3. Offline

    jdawgerj515

    Instead of:
    Code:
    event.setJoinMessage(ChatColor.AQUA + "Welcome to Libertycraft " + player.getName() + "!");
    Try this:
    Code:
    player.sendMessage(ChatColor.AQUA + "Welcome to Libertycraft " + player.getName() + "!");
     
  4. Offline

    Bernabeast

  5. Offline

    ximsilentx

    whats wrong still?
     
  6. Offline

    tylersyme

    You need to register your events in the onEnable() method with getServer().getPluginManager().registerEvents(this, this);
     
  7. Offline

    Bernabeast

    Wait what? i'm new so i dont get anything you said...
     
  8. Offline

    tylersyme

    Ahh, ok, so every single class that has events in it needs to be "registered" in the onEnable() method.
    getServer().getPluginManager().registerEvents(this(the class with the events in it), this(the class that registered the events));

    So, because your event is inside the same exact same class as the onEnable method, you insert "this" and "this" into the two parameter slots. If your events were in a different class (like test.class) then this is how you would do it
    getServer().getPluginManager().registerEvents(new test(),this);
     
  9. Offline

    B3NJO

    Did you write your YAML?
     
  10. Offline

    jdawgerj515

    What he means is put this in your onEnable:

    Code:
        pm = getServer().getPluginManager();
    pm.registerEvents(this, this);
    And this at the top inside your class. (Underneath public class Test extends JavaPlugin implements Listener{)

    Code:
    public PluginManager pm;


    This is needed in every plugin basically.

    Also just a few tips:

    1. When displaying code in forums use the [CODE ][/ CODE] or [syntax = java ][ /syntax], without the spaces, to keep formatting ("somewhat")

    2. If you are extremely to Java then I recomend reading some tutorials everyday and applying them to your test plugins. Most people would tell you to quit trying to use Bukkit api until you have learned Java but I am not most people.

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

    Wantsome909

    Code:
    public void onEnabled(){
    server().getPluginManager().registerEvents(this, this);
    }
     
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
    Player player = event.getPlayer();
    player.setJoinMessage(ChatColor.AQUA + "Welcome to Libertycraft " + player.getName() + "!");
    }
    
    Bernabeast try that

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

    ximsilentx

    i messaged him and he got it sorted out.... i was new so my post had to be viewed by moderator............
     
  13. Offline

    Bernabeast

    Someone fixed it for me thanks anyway guys!
     
Thread Status:
Not open for further replies.

Share This Page