1.17.x registerEvent on specific block?

Discussion in 'Spigot Plugin Development' started by Build_It_Builder, Jul 15, 2021.

  1. There is the PluginManager.registerEvent API for registering global events across all blocks, but how do you register an event only for one specific block at a particular location?

    I know that you can use PluginManager.registerEvent and then check if the position of the event is at the position of the block you want to listen to, but this seems like a terrible and senseless performance bottleneck. On a busy server, the event might be fired hundreds of thousands of times per second even though you only want to listen to just one block, which would only fire the event a small handful of times. So, is there a better, more performant, way to listen to only one specific block?

    Many thanks, everyone.
  2. You still need to use PluginManager.registerEvents. However, you need to create a condition with an if statement.
  3. This is not something you should worry about, especially if you're not too familiar with java/spigot api.
    Don't prematurely try to optimize code.
    Think about that for a second. Hypothetically, if spigot had this option, how would spigot determine whether to fire your event or not? Exactly, they would have to check the location. So you might as well just do the work yourself.
    Overall I don't know what you mean with
    What type of event are you referring to?
    • Agree Agree x 3
  4. Thank you for your response. I am well aware of how code works and that I can use PluginManager.registerEvent and then check if the position of the event is at the position of the block you want to listen to, as explained the 2nd paragraph of my question. However, I want to know if there is a better way to do this. Its terrible for performance to have a bunch of event listeners all listening to the same global event but only responding to specific blocks.
  5. It all depends on what you do inside the events. I can guarantee you that having a thousand (empty) events will probably be fine for the server
  6. 1. This isn't premature optimization. It's me trying to get to know the APIs better. Premature optimization involves going out of your way to do a long length process in order to try to boost performance. What I'm doing is just trying to find a drop-in replacement for the global listener, and this just tweaks a few lines of code instead of changing the algorithm at play. Additionally, this would be an incredibly easy way to boost both the performance and maintainability of my code.

    2. I would imagine that spigot would determine which event to fire based on the chunk the event originated in. Every chunk would have a list of event listeners, and any event in the chunk propagates to and fires these listeners. It still needs to check the location but doing events per-chunk would still be a major benefit for performance because most chunks don't need chunk-specific event listeners most of the time, so events not in these chunks wouldn't waste much CPU.

    3. A global event listener is an event listener which listens to all instances of the event, hence it is global because there's nowhere the event can propagate from that wont trigger the event listener.
  7. I respect your desire to worry about the performance ramifications later. I'm a well seasoned coder and global events on a server such as MineCraft don't sit well with me. For example, listening to block updates globally is performance suicide due to large redstone contraptions firing the event hundreds of thousands of times per second.

    This isn't premature optimization because I'm not going to rewrite a large section of my code in order to accommodate block-specific events, rather I'll rewrite just a line or two here and there to use block-specific events. It's simple, doesn't take extra time to develop, and has several benefits, so it's not really premature optimization.
  8. Good point.
    I haven't looked at the exact implementation of the event API, but I think it doesn't use the concept of "local/global" events. As long as a chunk is loaded on the server, it will fire events for it.
    If you only want to do something if a specific block or position is involved, you should just check it inside the "global" event.
  9. Strahan


    Checking if a block is within a certain Location bound is a negligible processing request. If that impacts your server in any meaningful way, the problem isn't your code it's that you are trying to run Spigot on a computer from the nineties ;)
    • Agree Agree x 1