Plugin Help...

Discussion in 'Plugin Development' started by TheChinski, Apr 22, 2012.

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

    TheChinski

    Hi,

    Im fairly new (Completely new :p) to Java and I have spent awhile poking around with it. I have been having a go at making a simple plugin that lightning's a player when they login. All the guides recommend you pick a plugin and work towards it, so thats what im doing :D

    This is what i've come up with so far:

    Code:
    package com.sludgecraft.LightLogin;
     
    import java.util.logging.Logger;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerJoin;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class LightLogin extends JavaPlugin {
     
        Logger log;
     
        public void onEnable(){
            log = this.getLogger();
            log.info("LightLogin Has Been Enabled!");
        }
       
        public void onDisable(){
            log.info("LightLogin Has Been Disabled!");
        }
       
        public void onPlayerJoin(PlayerJoinEvent event) {
            event.getEntity().getWorld().strikeLightning(event.getEntity().getLocation());
        }
     
    }
    
    I don seem to be having too much luck... :/

    Any help would be much appreciated - And remember, I am very new :D
     
  2. Offline

    Jogy34

  3. Offline

    TheChinski

    Hi,

    Thanks for your help :) I have altered the code, and it works fine - although it doesn't do anything. :p There arent any errors, and it enables and disables fine, but no lightning?

    Code:
    package com.sludgecraft.LightLogin;
     
    import java.util.logging.Logger;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerLoginEvent;
     
    public class LightLogin extends JavaPlugin {
     
        Logger log;
     
        public void onEnable(){
            log = this.getLogger();
            log.info("LightLogin Has Been Enabled!");
        }
     
        public void onDisable(){
            log.info("LightLogin Has Been Disabled!");
        }
     
        public class myPlayerListener implements Listener {
            @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerLogin(PlayerLoginEvent event) {
                event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
            }
        }
     
    }
    
    And I have been testing it on a clean server with no other plugins, and on my big, too-many-plugins server too...
     
  4. Offline

    Jogy34

    in myPlayerListener put this above the eventHandler
    Code:
        public myPlayerListener (LightLogin plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
    then put new myPlayerListener(this) in your onEnable
     
  5. Offline

    TheChinski

    Eclipse doesn't seem happy about putting myPlayerListener(this) in onEnable... It says the method is undefined?

    And just to make sure I got this bit right:

    Code:
        public class myPlayerListener implements Listener {
            public myPlayerListener (LightLogin plugin)
            {
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
            }
            @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerLogin(PlayerLoginEvent event) {
                event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
            }
     
  6. Offline

    VeryBIgCorp

    You're not calling a method, you're instantiating a new object of your listener.

    Code:
    public void onEnable() {
       myPlayerListener listener = new myPlayerListener(this);
        // rest of onEnable
    }
    
    Although you may want to make the variable in the class and then instantiate it in onEnable so the JRE doesn't release it.
     
  7. Offline

    TheChinski

    Thanks, ill give that a go... :rolleyes:

    Abit off-topic, but I was wondering, how did you all learn Java? Did you bother with thick books, or did you just dive head-first in and have fun?

    Thanks,
     
  8. Offline

    VeryBIgCorp

    Well I mostly just experimented, but Bucky (thenewboston) on YouTube has amazing tutorials that helped me immensely when I was just starting out. Also, perusing various programming forums (such as these or StackOverflow) and looking at replies really helps for a more broad knowledge of other aspects of the language that you can't really figure out on your own ;)

    Good luck on your learning! If you have any other questions just ask :)
     
    TheChinski likes this.
  9. Offline

    Jogy34

    With just putting myPlayerListener(this) you are trying to call a non-existing method. I wanted you to call the constructor for 'myPlayerListener' like this:
    Code:
     new myPlayerListener(this) 
    Sorry if I made that unclear
     
  10. Offline

    TheChinski

    Thanks for the help everyone, and thanks for the tips on learning Java VeryBigCorp :)

    I know I must be close now...
    Code:
    package com.sludgecraft.LightLogin;
     
    import java.util.logging.Logger;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerLoginEvent;
     
    public class LightLogin extends JavaPlugin {
     
        Logger log;
     
        public void onEnable() {
            new myPlayerListener(this);
            log = this.getLogger();
            log.info("LightLogin Has Been Enabled!");
        }
       
        public void onDisable(){
            log.info("LightLogin Has Been Disabled!");
        }
     
       
        public class myPlayerListener implements Listener {
            public myPlayerListener (LightLogin plugin)
            {
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
            }
            @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerLogin(PlayerLoginEvent event) {
                event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
            }
        }
     
    }
    
    There doesn't seem to be a lightning. :'( Unless, could it be that its just striking me before my client has a chance to load? I can try it with another player tonight...
     
  11. Offline

    Theodossis

    Try this:
    Code:
            @EventHandler
            public void onPlayerJoin(PlayerJoinEvent event) {
                Player p = event.getPlayer();
                if(p.isOnline())
                event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
            }
    In my motd plugin it works :)
    And DONT PUT ; at if(p.isOnline()) :p
     
    TheChinski likes this.
  12. Offline

    Iron_Crystal

    Like Theodossis said, use the PlayerJoinEvent. The PlayerLoginEvent is triggered before the player actually joins the game. I think it is triggered as soon as the player joins, but the world and everything else hasn't loaded yet.
     
  13. Offline

    the_merciless

    Cant u just add a 3 second delay
     
  14. Offline

    TheChinski

    Thankyou sooo much :D It works!

    Thankyou so much everyone for all your help :)

    [diamond]'s for all!

    [diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond][diamond]


    EDIT: I don't suppose anyone would be willing to share with me how to add a small delay? :p Turns out, most times the lightning comes during the 'Loading Terrain' screen...
     
  15. Offline

    Jogy34

    I had the same problem when I wanted to use the onPlayerLoginEvent. I fixed it with this
    Code:
    plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
    {
    public void run()
    {
    //Do Stuff
    }
    }, 1/*Delay in Ticks*/);
    
     
  16. Offline

    TheChinski

    Right. So, where would I put that it? I seem to get a happy bunch of errors everytime I put it anywhere :eek:

    Code:
    package com.sludgecraft.LightLogin;
     
    import java.util.logging.Logger;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerJoinEvent;
     
     
    public class LightLogin extends JavaPlugin {
     
     
     
        Logger log;
     
        public void onEnable() {
            new myPlayerListener(this);
            log = this.getLogger();
            log.info("LightLogin Has Been Enabled!");
        }
       
        public void onDisable(){
            log.info("LightLogin Has Been Disabled!");
           
        }
     
       
            public class myPlayerListener implements Listener {
                public myPlayerListener (LightLogin plugin)
                {
                    plugin.getServer().getPluginManager().registerEvents(this, plugin);
                   
                }
                @EventHandler(priority = EventPriority.HIGHEST)
                public void onPlayerJoin(PlayerJoinEvent event) {
                   
                    if(player.hasPermission("LightLogin.Login")) {
                       
                            event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
                  }
                }
            }
        }
    
    And yes, I have been having a shot at permissions too :p - Failing at that too :rolleyes: Hehehe... Its gonna take awhile for me to learn all this...
     
  17. Offline

    Iron_Crystal

    What is wrong with your permissions?

    Also, you have not made a player variable if that is your problem
     
  18. Offline

    VeryBIgCorp

    Right after you check if they have the permission:
    Code:Java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(final PlayerJoinEvent event){
    4. if(event.getPlayer().hasPermission("LightLogin.Login")){
    5. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    6. event.getPlayer().getWorld().strikeLightning(e.getPlayer().getLocation());
    7. }, 20L);
    8. }
    9. }
    10.  

    You need the final in the parameters because the compiler wouldn't be able to find event in the new Runnable object. Also, the delay is in server ticks, so it would be 20 ticks for one second. In addition, it's better to use a sync task because you may get a ConcurrentModificationException if a value in the player's object is changed when the task executes.
     
  19. Offline

    TheChinski

    Ok, there are a few errors popping up.

    Runnable(){ = The type new Runnable(){} must implement the inherited abstract method Runnable.run()
    event.getPlayer() = Syntax error on token(s), misplaced construct(s)
    20L); = Syntax error, insert ";" to complete Statement - Which is odd because isn't there a ; there?
     
  20. Offline

    Jogy34

    You must have a public void run() in a runnable like this
    Code:
    public void run()
    {
    //stuff to do after the delay
    }
    
    I don't know for certain since I don't know what you have in your code but I would guess that you don't have it within the event call.
    This is saying that you need to add a ';'
     
  21. Offline

    TheChinski

    public void run() has got rid of most of the errors in one big go :)

    Everything in the upper half of the script is good, although i'm having issues with the brakets:

    PHP:
            public class myPlayerListener implements Listener {
                public 
    myPlayerListener (LightLogin plugin)
                {
                    
    plugin.getServer().getPluginManager().registerEvents(thisplugin);
                   
                }
                @
    EventHandler
                
    public void onPlayerJoin(final PlayerJoinEvent event){
                    if(
    event.getPlayer().hasPermission("LightLogin.Login")){
                    
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                        public 
    void run(){
                       
    event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
                    }, 
    20L);
                   }
                    }
                }
    The ones on the first, second and fourth line up from the bottom are being mean. :( The first and the fourth line up ones want me to insert "}" to complete ClassBody, whereas the other wants me to 'delete this token'
     
  22. Offline

    Jogy34

    If your using eclipse and you put your cursor on the end of a bracket it will highlight the bracket that goes with it.
    Here's what you have but cleaned up and fixed:

    Code:
    public class myPlayerListener implements Listener
    {
        public myPlayerListener (LightLogin plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
        @EventHandler
        public void onPlayerJoin(final PlayerJoinEvent event)
        {
            if(event.getPlayer().hasPermission("LightLogin.Login"))
            {
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
                {
                    public void run()
                    {
                        event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
                    }, 20L);
                }
            }
        }
    }
    Also if you are in eclipse you can go to the source tab then select 'Correct Indentation' or do Ctr-I to clean up your code a little
     
  23. Offline

    TheChinski

    So are all the spaces etc really necessary? Or are they just there to make it all easier to navigate? :confused:

    EDIT: And I still seem to be getting the errors on the same brakets as before... :/

    Here is the whole script:

    Code:
    package com.sludgecraft.LightLogin;
     
    import java.util.logging.Logger;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerJoinEvent;
     
     
    public class LightLogin extends JavaPlugin {
     
     
     
        Logger log;
     
        public void onEnable() {
            new myPlayerListener(this);
            log = this.getLogger();
            log.info("LightLogin Has Been Enabled!");
        }
     
        public void onDisable(){
            log.info("LightLogin Has Been Disabled!");
     
        }
     
        public class myPlayerListener implements Listener
        {
            public myPlayerListener (LightLogin plugin)
            {
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
            }
            @EventHandler
            public void onPlayerJoin(final PlayerJoinEvent event)
            {
                if(event.getPlayer().hasPermission("LightLogin.Login"))
                {
                    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
                    {
                        public void run()
                        {
                            event.getPlayer().getWorld().strikeLightning(event.getPlayer().getLocation());
                        }, 20L);
                    }
                }
            }
        }
    Oh, and thanks again for all your help - It really is very useful :)
     
  24. Offline

    Jogy34

    I use them to make it easier to read but in eclipse I would normally use 1 Tab but tabs don't work on these forums.

    Also as for your problem you aren't ending your LightLogin class. Put another closing bracket underneath your onDisable()
     
  25. Offline

    TheChinski

    Hmmm, everytime I put a braket under the onDisable(), it seems to cut of the myPlayerListener, although the error on the first line up has gone now...
     
  26. Offline

    VeryBIgCorp

    It looks like you didn't put your last curly brace when declaring your Runnable.
     
  27. Offline

    TheChinski

    I think its there, all the brakets seem to link up...
     
  28. Offline

    VeryBIgCorp

    Well I mean before the comma before the 20L, there needs to be a brace to close the Runnable object that you made.
     
  29. Offline

    TheChinski

    Ah, thankyou :) Just need to set the variable for plugin (I think) and it should be done... :rolleyes:

    (Hopefully...)
     
  30. Offline

    TheChinski

    There - Done :D

    Thankyou so much to everyone who helped - And now i'm starting my next plugin, i'm sure you'll be seeing me around soon :p

    [diamond]
     
Thread Status:
Not open for further replies.

Share This Page