Lightning Strike EventHandler Help

Discussion in 'Plugin Development' started by lcpvp, Jan 1, 2013.

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

    lcpvp

    What am I missing here??


    Code:
    package com.ultimatehg.thunder;
     
    import org.bukkit.Material;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class UHG extends JavaPlugin implements Listener{
     
        @EventHandler
        public void onPlayerInteractBlock(PlayerInteractEvent evt){
            if(evt.getPlayer().getItemInHand().getTypeId() == Material.STONE_AXE.getId()){
                //maximal distance between player and thunder is 200 blocks
                evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
                PluginManager pm = this.getServer().getPluginManager();
                pm.registerEvents(this, this);
            }
        }
    }
     
  2. Offline

    chasechocolate

    Register your events in your onEnable(), not in your listener:
    Code:java
    1. public void onEnable(){
    2. PluginManager pm = getServer().getPluginManager();
    3. getLogger().info("Enabled!");
    4. pm.registerEvents(this, this);
    5. }
     
  3. Offline

    lcpvp

    Code:
    package com.ultimatehg.thunder;
     
    import org.bukkit.Material;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class UHG extends JavaPlugin implements Listener{
     
        @EventHandler
        public void onPlayerInteractBlock(PlayerInteractEvent evt){
            if(evt.getPlayer().getItemInHand().getTypeId() == Material.STONE_AXE.getId()){
                //maximal distance between player and thunder is 200 blocks
                evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
                PluginManager pm = this.getServer().getPluginManager();
                pm.registerEvents(this, this);
            }
        }
     
    public void onEnable(){
    PluginManager pm = getServer().getPluginManager();
    getLogger().info("Enabled!");
    pm.registerEvents(this, this);
    }
    }
    Not sure if I put the onEnable in right spot ,or what to do with it
     
  4. Offline

    chasechocolate

    Yeah, but you don't need to register the events in the listener method (onPlayerInteractBlock).

    So your class would look like this:
    Code:java
    1. package com.ultimatehg.thunder;
    2.  
    3. import org.bukkit.Material;
    4.  
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class UHG extends JavaPlugin implements Listener{
    12.  
    13. public void onEnable(){
    14. this.getServer().getPluginManager().registerEvents(this, this);
    15. }
    16.  
    17. public void onDisable(){
    18. }
    19.  
    20. @EventHandler
    21. public void onPlayerInteractBlock(PlayerInteractEvent evt){
    22. if(evt.getPlayer().getItemInHand().getTypeId() == Material.STONE_AXE.getId()){
    23. //maximal distance between player and thunder is 200 blocks
    24. evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    25. }
    26. }
    27. }


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

    fireblast709

    lcpvp please note that your code would fail with an error if you look up in the sky (as there would be no target block, so getTargetBlock() would return null, and calling .getLocation() on null would throw a NullPointerException)
     
  6. Offline

    lcpvp

    Alright, thanks.

    How would i add a cooldown??

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

    fireblast709

    HashMap<String,Long> which stores the playername and the timestamp of the last used.
    The HashMap:
    Code:java
    1. HashMap<String,Long> cooldown = new HashMap<String,Long>();

    The cooldown part:
    Code:java
    1. if(System.currentTimeMillis() - (cooldown.contains(player.getName()) ? cooldown.get(player.getName()) : 0) >= COOLDOWN_IN_MILLISECONDS)
    2. {
    3. // do command/event
    4. cooldown.put(player.getName(), System.currentTimeMillis());
    5. }
    6. else
    7. {
    8. player.sendMessage("You cannot use this yet, still cooling...");
    9. }
     
  8. Offline

    lcpvp

    i dont really get hashmaps...
     
  9. Offline

    fireblast709

    How not?
     
  10. Offline

    bc112354

    lcpvp
    If you ever learned python, they are like dictionaries.

    So, pretty much, you start a HashMap like this:
    Code:java
    1. HashMap<Object, Object> hashmap_name = new HashMap<Object, Object>();

    And, whenever you want to put something in, you do this:
    Code:java
    1. hashmap_name.put(Object, Object);


    So lets say a plugin has error codes from 1 to 100. The dev wants to translate the error codes to String messages. A HashMap would be good here.
    Code:java
    1. package hashmap;
    2.  
    3. public class ErrorHandeling {
    4.  
    5. HashMap<Integer, String> error = new HashMap<Integer, String>();
    6.  
    7. public ErrorHandeling(){
    8.  
    9. error.put(1, "This is an error");
    10. .
    11. .
    12. .
    13. error.put(100, "This is the final error");
    14.  
    15. }
    16.  
    17. public String getErrorMessage(int key){
    18.  
    19. return error.getKey(key);
    20.  
    21. }
    22.  


    So, whenever an error 1 occurrs, the plugin will send the CommandSender:

    Code:
    This is an error.
    
    I hope this helped.

    EDIT: Just realized this was almost exactly a year until last post
     
Thread Status:
Not open for further replies.

Share This Page