[Solved] yet another "My first plugin" help thread

Discussion in 'Plugin Development' started by evilaviator, Aug 23, 2012.

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

    evilaviator

    I hate to make this thread because I realize that there are probably one hundred of these posted a day. I also recognize that a lot of the time these threads are solved through the plugin.yml file, and I'm sure that this is one of my problems. I'm very new to java and bukkit dev, so I'm posting this thread asking for help (like many other threads).

    Most of what I've learned has been from either tutorials, or other threads. I wanted to start off with a simple plugin that simply gets the players name when they join and displays it on chat.(not to hard right?)

    My code:
    Code:
    package me.evilaviator.first;
     
        import org.bukkit.Bukkit;
        import org.bukkit.entity.Player;
        import org.bukkit.event.EventHandler;
     
        public class First {
           
        @EventHandler
        public boolean onJoin(Player JoinEvent){
            Player p = Bukkit.getPlayer("Playername");
            String name = p.getName();
            if(JoinEvent instanceof Player){
            Player player = (Player)JoinEvent;
            player.sendMessage("Your name is:" + name);
            return true;
            }else{
            p.sendMessage("You must be a player.");{
            return true;
            }
            }
           
           
               
        }   
        }
    
    I know that the code is probably all wrong, but it has no errors in eclipse which is a good thing I guess.

    The error that I run into is:

    My plugin.yml:
    Any help is greatly appreciated, and I'm sorry for creating this thread because it's probably really annoying.
     
  2. Offline

    Jnorr44

    change:

    public class First

    to:

    public class First extends JavaPlugin


    also:

    *your event should be:

    public void onJoin(PlayerJoinEvent event){
    //stuff in here
    }

    then inside that you can do:

    Player p = event.getPlayer.getName();
     
    evilaviator likes this.
  3. Offline

    evilaviator

    Thanks for the response.
    Just two quick questions, when I type in Player p = event.getPlayer.getName(); the event is underlined with red. Do I need to import event from bukkit? If I do that it changes event to Event, is this still correct?

    Also since this is a void, I'm guessing I don't need return values?
     
  4. Offline

    Megolas

    you need to use onenable and ondisable, also, events wont work unless you use register events. (Very popular mistake, you can find it easly in google/forum)
     
    evilaviator likes this.
  5. Offline

    evilaviator

    Thanks!
     
  6. Offline

    Postkutsche

    PlayerJoinEvent can never be a Player. I wonder this even compiles since you can't use "return true" in a void.

    So, this is the code:
    Code:
    package me.evilaviator.first;
     
        import org.bukkit.Bukkit;
        import org.bukkit.entity.Player;
        import org.bukkit.event.EventHandler;
     
        public class First extends JavaPlugin implements Listener {
           
    
       public void onDisable() {}
       public void onEnable() {
            getServer().getPluginManger().registerEvents(this, this);
       }
    
        @EventHandler
        public void onJoin(Player JoinEvent){
            Player p = Bukkit.getPlayer("Playername");
            String name = p.getName();
            p.sendMessage("Your name is:" + name);    
        }
    
    As you see you need to implement Listener and register the event in onEnable().

    EDIT: changed boolean to void.
     
  7. Offline

    chaseoes

    The "my first plugin" threads are usually fine since they're the easiest to answer! :p
     
    evilaviator likes this.
  8. Offline

    evilaviator

    So I don't need "return true" since this is a void, would I need to change boolean to void? Sorry if this sounds stupid :p.
     
  9. Offline

    chaseoes

    Yes, events are usually void (do boolean ones even work?). If it's void you don't need to return anything at all.
     
    evilaviator likes this.
  10. Offline

    evilaviator

    Thanks. I'm guessing booleans don't work considering it would require a return value, I changed it to void and there are no errors.
     
  11. Offline

    Postkutsche

    Edited post above, typing on a smartphone isn't that easy. :D

    I don't know but I don't think so.
     
    evilaviator likes this.
  12. Offline

    evilaviator

    Haha thanks!

    I see what you've done, and when I try
    Code:
    getServer().getPluginManager().registerEvents(this, this);
    It asks me to create a method for getServer(), what does this mean?

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

    Postkutsche

    Do you have "extends JavaPlugin" like I have?
     
    evilaviator likes this.
  14. Offline

    evilaviator

    Ah, thank you. It was a very stupid mistake I had:
    Code:
    public class Login implements Listener{
    when it should have been:
    Code:
    public class First extends JavaPlugin implements Listener {
     
  15. Offline

    Postkutsche

    Well... you have to import org.bukkit.plugin.java.JavaPlugin.
     
  16. Offline

    evilaviator

    Thank you for the help, I understand that now.
     
  17. Offline

    Postkutsche

    You could put your Listener in an separeted class as well, than would have been right.
    Registering the event looks like this:
    Code:
    getServer().getPluginManager().registerEvents(new Login(), this);
     
  18. Offline

    evilaviator

    Now that I have done all of that, Bukkit has no problem running my plugin. Except it says in the console
     
  19. Offline

    Postkutsche

    Post your code please.
     
  20. Offline

    evilaviator

    Code:
    package me.evilaviator.first;
     
    import org.bukkit.Bukkit;
    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 Login extends JavaPlugin implements Listener {
     
     
    public void onDisable(){}
    public void onEnable(){
        getServer().getPluginManager().registerEvents(this, this);
     
    }
    @EventHandler
    public void onJoin(Player JoinEvent){
        Player p = Bukkit.getPlayer("Playername");
        String name = p.getName();
        p.sendMessage("Your name is:" + name);
    }
    I changed the name of the class to Login.
     
  21. Offline

    Postkutsche

    Compare your code of the @EventHandler with mine above.
     
  22. Offline

    evilaviator

    I'm sorry, am I missing something? I copied what you put and it doesn't make a difference.
     
  23. Offline

    WolfMaster

    Are You Refreshing Before Exporting it??
     
  24. Offline

    evilaviator

    Herp, no I was not. But I refreshed and exported. Same problem.
     
  25. Offline

    Postkutsche

    Sorry... you're right I did't check the code enough. :p

    Code:
     @EventHandler
    public void onJoin(PlayerJoinEvent event){
        Player p = event.getPlayer();
        String name = p.getName();
        p.sendMessage("Your name is:" + name);
    } 
     
    evilaviator likes this.
  26. Offline

    evilaviator

    Aha! Thanks!
     
  27. Offline

    Speaw

    class editor name :?
     
Thread Status:
Not open for further replies.

Share This Page