General Event Handler (no specific event)

Discussion in 'Spigot Plugin Development' started by jankingDE, May 15, 2016.

  1. Hey!

    I am wondering if there is a kind of EventHandler to listen on all things of stuff.
    For example:

    If I want to check if a Player stands on a specific block I use a PlayerMoveEvent. but actually I don't care if the player is moving. I just want to know which block he is standing on.

    If I just care about the block, do I than put the checkBlock Code in a Class without event Handler or is there a kind of "spacer" Event Handler?

    Best regards and have a nice day!
     
  2. just make a runnable and check all the players locations and what block their standing on.
     
    • Like Like x 1
  3. I think you mean something like

    Code (Text):

    myPlugin.getServer().getScheduler().scheduleAsyncRepeatingTask(myPlugin, new Runnable() {

    public void run() {
    //player checker etc.
    }
    }, 60L, 200L);
     
    I'm really new to Java, sorry.
     
  4. that isnt a runnable

    new BukkitRunnable(){
    public void run(){

    }
    }. timer type

    put that inside a method to call it.
     
  5. Your code is correct, that's indeed a Runnable. Just make sure you're not calling any Bukkit API methods while you're in async. Checking stuff should work just fine in async.

    @ProJoosh's method is also correct, but I prefer using the Runnable interface because I can replace it with a lambda..
    Code (Text):

    myPlugin.getServer().getScheduler().scheduleAsyncRepeatingTask(myPlugin, () -> {
    // player checker
    }, 60L, 200L);
     
    EDIT: Above methods are deprecated, but they still work fine. Check the JavaDocs for more info.
     
    • Like Like x 1
  6. @FineasGavre are they really deprecated? I thought only when you put a BukkitRunnable it was.

    EDIT: Oh I guess the async ones are. I believe you can still use runTaskTimerAsynchronuously inside the scheduler class
     
  7. My bad, only the ones starting with "schedule" and the ones using BukkitRunnables are deprecated. The ones starting with runTask and using Runnables are not. As I said, it's all in the JavaDocs (https://hub.spigotmc.org/javadocs/spigot/org/bukkit/scheduler/BukkitScheduler.html)

    EDIT: I think they were deprecated because of the misleading name. (quote: This name is misleading, as it does not schedule "a sync" task, but rather, "an async" task)
     
  8. So I can't check for example for p.getPosition because this is a bukkit API function? If so using this Code is irrelevant for me. I try to use your code :)

    Thank you guys a lot! <3
     
  9. I think that getting positions works, I always use Async first, then if I get an error I switch to Sync or make a new Sync Runnable inside the Async one (I don't know if that's good practice). I've only noticed errors when I was changing HP and kicking players.
     
    • Like Like x 1
  10. Sorry again I think I'll need a little more help if you want to ^^

    So here is my EventHandler Code:

    Code (Text):

        private long lastTime = System.currentTimeMillis();
        [USER=17846]@EventHandler[/USER]
        public void on(PlayerMoveEvent e) {
            //Run the EventHandler every 1.5 Seconds
            if (lastTime < System.currentTimeMillis() - 1500) {
                Player p = e.getPlayer();
                 
                    //get the block
                    if(p.getLocation().subtract(0D, 1D, 0D).getBlock().getType() == Material.STAINED_CLAY) {
                        Block block = p.getLocation().getBlock();

                        //Choose and drop Item out of an list (German, haha)
                        Random ran = new Random();
                        int auswahl = ran.nextInt(2);
                        int zahl = ran.nextInt(main.Drops.size());

                        ItemStack itemstack = main.Drops.get(zahl);
                        block.getWorld().dropItemNaturally(p.getLocation(), itemstack);
                     
                        //make a pop sound
                        try {
                            //Minecraft 1.9
                            p.getWorld().playSound(p.getLocation(),Sound.valueOf("ENTITY_CHICKEN_EGG"), 1.0f, 1.0f);
                        } catch (Exception ev) {
                            //Minecraft 1.8.8
                            p.getWorld().playSound(p.getLocation(),Sound.valueOf("CHICKEN_EGG_POP"), 1.0f, 1.0f);
                        }
                     
                    }
                lastTime = System.currentTimeMillis();
            }
        }
     
    If I now want to switch over to the Scheduler (or whatever it is called) I would need to redefine
    Code (Text):
    Player p = e.getPlayer();
    because I don't have an event, right? How would you do this? ^^

    I looked up this stuff at http://wiki.bukkit.org/Scheduler_Programming to find a solution to replace the "e.getPlayer" somehow. but I cannot find anything.
     
  11. Maybe you should learn basic java first before trying to work with bukkit. That's like driving a car when you can't even walk
     
  12. Think you're right there. At the moment I'm just like testing what are the basics of Bukkit.

    I just thought like.. I just want to drive that Bukkit car, so why do I need to walk if I can sit in my car :D But I now see I was wrong.
     
  13. You would need to get all online players and iterate through them.

    Code (Text):

    for (Player p : Bukkit.getOnlinePlayers) {
      // p is the current player
    }
     
    EDIT: Post your edited code so we can check for common mistakes :p
     
    • Like Like x 1