Delay EventHandlers

Discussion in 'Plugin Development' started by Giorgio, Oct 26, 2012.

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

    Giorgio

    Hello everyone, Im recently working on a class for my project. This class is called Thor and when wielding a WOOD_AXE and right clicking with it it will set lightning there. I need it to be delayed. I took a look into the Scheduler_Programming Docs and could not understand it. Here is my code:
    Code:Java
    1. @EventHandler
    2. public void onPlayerInteractBlock(PlayerInteractEvent evt){
    3. if(evt.getPlayer().getItemInHand().getTypeId() == Material.WOOD_AXE.getId() && (evt.getAction() == Action.RIGHT_CLICK_BLOCK )){
    4. //maximal distance between player and thunder is 200 blocks
    5. evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    6. }
    7.  


    Also how would i make the block there looking at turn on fire? Thank you to anyone who can help me.
     
  2. Offline

    Milkywayz

    Something like:
    Code:
        @EventHandler
        public void onPlayerInteractBlock(final PlayerInteractEvent evt) {
            if (evt.getPlayer().getItemInHand().getTypeId() == Material.WOOD_AXE
                    .getId() && (evt.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                // maximal distance between player and thunder is 200 blocks
                int seconds = 3;
                Bukkit.getScheduler().scheduleSyncDelayedTask(null, new Runnable() {
                    @Override
                    public void run() {
                        evt.getPlayer()
                                .getWorld()
                                .strikeLightning(
                                        evt.getPlayer().getTargetBlock(null, 200)
                                                .getLocation());
                    }
                }, seconds * 20);
     
            }
        }
    Ofcourse replace the null with your reference to the main class.
     
    gomeow and ZeusAllMighty11 like this.
  3. Offline

    ZeusAllMighty11 Retired Staff

    Milkywayz also has a timer lib
     
  4. Offline

    Milkywayz

    Speaking of which, Im gonna work on simplifying it tomorrow, thanks for the reminder.
     
    ZeusAllMighty11 likes this.
  5. Offline

    Giorgio


    I get an Error:

    http://pastebin.com/acfrui4p
     
  6. Offline

    Milkywayz

    You didnt reference your plugin's main class like I explained at the end of the code paste.
     
  7. Offline

    Giorgio

    Ops didn't catch that bit my bad..

    ive made a refrence but it says
    Code:
        public Kitpvp plugin;
        public MyDeathlistener (Kitpvp instance){
            plugin = instance;
        }
     
     
                Bukkit.getScheduler().scheduleAsyncDelayedTask(Kitpvp, new Runnable() {
    Anyone help me please?

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

    Codex Arcanum

    Is the statement: Bukkit.getScheduler().scheduleAsyncDelayedTask(Kitpvp, new Runnable() {...
    inside of a method? Otherwise you will have problems.
     
  9. Offline

    Giorgio


    Yes,but i don't know how to refrence my Main class into the Listener class. I put this code into my class:
    Code:
    public class MyDeathlistener implements Listener{
        public Kitpvp plugin;
        public MyDeathlistener (Kitpvp instance){
            plugin = instance;
        }
    but it still doesn't Recognize Kitpvp.

    Bump

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

    Cirno

    Java is CaSe-SeNsItIve, so check to make sure it was Kitpvp. Other then that, I don't see any other problem. Are you instancing it properly via new MyDeathlistener(Some Kitpvp class)?
     
  11. Offline

    Giorgio

    Cirno
    What do you mean? "instancing it properly via new MyDeathlistener(Some Kitpvp class)"

    Please anyone help im trying to get this out there due date was Friday, but i wasn't expecting Update.

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

    Sagacious_Zed Bukkit Docs

    two things
    dont confuse the type of a variable for the name of the variable.
    never use a async task for bukkit api methods (minor exceptions apply but if you don't know an exception just don't do it).
     
  13. Offline

    Cirno

    Are you doing:
    MyDeathlistener.somefunction();

    or are you doing:
    MyDeathlistener listener;

    public void onEnable(){
    listener = new Mydeathlistener(this);
    /register listener here and rest of stuff
    }
     
  14. Offline

    Giorgio

    Cirno

    Im Registering the event, i don't have any of those lines above. Do they go in the main class or my listener?
     
  15. Offline

    Cirno

    Add this to your main class
    Outside the onEnable() function:
    Code:JAVA
    1. MyDeathlistener listener;

    Inside the onEnable() function:
    Code:JAVA
    1. listener = new MyDeathlistener(this);
     
  16. Offline

    Giorgio

    Cirno

    Added them, Still the Kitpvp in:
    Code:
                Bukkit.getScheduler().scheduleAsyncDelayedTask(Kitpvp, new Runnable() {
    is not recognized error:
     
  17. Offline

    Cirno

    Is it CaSeD Kitpvp or is it KitPVP or kitPVP etc etc.?
    EDIT: it would help if you posted your entire source code.
     
  18. Offline

    Giorgio

    Its just Kitpvp

    Main Class here:
    Code:Java
    1. package com.dyrocraft.kitpvp;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.logging.Logger;
    6. import com.dyrocraft.kitpvp.Soilderjump;
    7. import com.dyrocraft.kitpvp.Explosion;
    8. import com.dyrocraft.kitpvp.MyDeathlistener;
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Material;
    12. import org.bukkit.block.Block;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.enchantments.Enchantment;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.PlayerInventory;
    20. import org.bukkit.plugin.PluginManager;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.potion.PotionEffect;
    23. import org.bukkit.potion.PotionEffectType;
    24. import com.dyrocraft.kitpvp.Kitpvp;
    25.  
    26. public class Kitpvp extends JavaPlugin implements Listener{
    27. final private static Enchantment power = Enchantment.ARROW_DAMAGE;
    28. final private static Enchantment firebow = Enchantment.ARROW_FIRE;
    29. final private static Enchantment knockbow = Enchantment.ARROW_KNOCKBACK;
    30. final private static Enchantment sharpness = Enchantment.DAMAGE_ALL;
    31. final private static Enchantment knockback = Enchantment.KNOCKBACK;
    32. final private static Enchantment fire = Enchantment.FIRE_ASPECT;
    33. public final Logger logger = Logger.getLogger("Minecraft");
    34. public static Kitpvp plugin;
    35. public final Explosion ex = new Explosion(null);
    36. public final MyDeathlistener dl = new MyDeathlistener(null);
    37. public final Soilderjump sj = new Soilderjump();
    38. public final HashMap<Player, ArrayList<Block>> hashmap = new HashMap<Player, ArrayList<Block>>();
    39. MyDeathlistener listener;
    40. @Override
    41. public void onDisable() {
    42. this.logger.info("Disabled Kitpvp");
    43.  
    44. }
    45.  
    46. @Override
    47. public void onEnable() {
    48. this.logger.info("Enabled Kitpvp!");
    49. PluginManager pm = Bukkit.getPluginManager();
    50. listener = new MyDeathlistener(this);
    51. pm.registerEvents(ex,this);
    52. pm.registerEvents(dl,this);
    53. pm.registerEvents(sj,this);
    54.  
    55. }
    56. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    57. Player player = (Player) sender;


    Here is my Listener:
    Code:Java
    1. package com.dyrocraft.kitpvp;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.entity.PlayerDeathEvent;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import com.dyrocraft.kitpvp.Kitpvp;
    11.  
    12. public class MyDeathlistener implements Listener{
    13. public Kitpvp plugin;
    14. public MyDeathlistener (Kitpvp instance){
    15. plugin = instance;
    16. }
    17. public MyDeathlistener listener;
    18. @EventHandler
    19. public void onPlayerDeathEvent(PlayerDeathEvent event) {
    20. event.getDrops().clear();
    21. }
    22.  
    23. @EventHandler
    24. public void onPlayerInteractBlock(final PlayerInteractEvent evt) {
    25. if (evt.getPlayer().getItemInHand().getTypeId() == Material.WOOD_AXE
    26. .getId() && (evt.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    27. // maximal distance between player and thunder is 200 blocks
    28. int seconds = 3;
    29. Bukkit.getScheduler().scheduleAsyncDelayedTask(Kitpvp, new Runnable() {
    30. @Override
    31. public void run() {
    32. evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    33. }
    34. }, seconds * 20);
    35.  
    36. }
    37. }
    38. }
     
Thread Status:
Not open for further replies.

Share This Page