Solved Single or multiple files for events

Discussion in 'Plugin Development' started by torpkev, Dec 12, 2018.

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

    torpkev

    I have what is probably a silly question.

    Is there any detriment to creating multiple files for registering events?

    Right now I'm using a single file per event in most cases unless related (for example, I have BlockIgniteEvent, BlockSpreadEvent and BlockBurnEvent in a single file ("Fire")) - but things like BlockBreakEvent, BlockPlaceEvent, EntityDamageEvent, EntityDamageByEntityEvent etc. in their own files.

    Code:
    private BlockBreak onBlockBreak = new BlockBreak();
    private BlockPlace onBlockPlace = new BlockPlace();
    private EntityDamage onEntityDamage = new EntityDamage();
    private EntityDamageByEntity onEntityDamageByEntity = new EntityDamageByEntity();
    private EntityExplode onEntityExplode = new EntityExplode();
    private Fire onFire = new Fire();
    
    Bukkit.getPluginManager().registerEvents(onBlockBreak, this);
    Bukkit.getPluginManager().registerEvents(onBlockPlace, this);
    Bukkit.getPluginManager().registerEvents(onEntityDamage, this);
    Bukkit.getPluginManager().registerEvents(onEntityDamageByEntity, this);
    Bukkit.getPluginManager().registerEvents(onEntityExplode, this);
    Bukkit.getPluginManager().registerEvents(onFire, this);
    Would I be better off creating a single file for the parent (Block, Player, Entity etc.) or does it really not matter by the time it gets compiled?

    From a readability standpoint, keeping them apart is easier for me, but not if it is at the expense of doing it properly.
     
  2. Offline

    The_Spaceman

    I don't know, but I think that putting all events in 1 class is faster (but not much faster). I'll say make it the easiest way for you to read and understand and edit things later.
    But why are you saving the classes in private variables?
     
  3. Offline

    torpkev

    Whatever tutorial I was reading back when I first started (not all that long ago) did it that way.

    better to skip the variable and go right with something like this?

    Code:
    Bukkit.getPluginManager().registerEvents(new BlockBreak(), this);
    Bukkit.getPluginManager().registerEvents(new BlockPlace(), this);
     
  4. Offline

    DutchJellyV2

    I'd suggest not making more classes than you need. If you really like readablility, your best bet is to make classes like BlockEvent or EntityEvent. This is because I think making many classes without much content or without a real addition to the project in terms of catagorizing code, reduces the readability of your project to code readers, which will be you in a couple weeks when you forget what you did. They'll have to browse in your files more in order to see some actual code. Right now it seems like you're confusing classes with functions: A class contains functions and a class isn't a function.

    In terms of efficiency I don't think making seperate listener classes matters enough to notice it. It's only inefficient in theory because you have to make more objects and trashcollect more.

    And to comment to what's said above about private variables: I don't think it matters at all as long as you don't create more than one instance of the class which you don't do. Making the private objects does improve readability of your project and accesability to your objects.

    I hope my opinion on this subject was supported well enough for you to understand it.
     
  5. Offline

    torpkev

    Thanks @DutchJellyV2 - I understand what you're saying.

    How it is currently set up was basically predicated on having 3-4 events to start with and "I know that BlockBreak handles my block break code" so keeping them split out made sense - but as scope-creep has kicked in and I get further into my project I've gone to 19-20 different events and it was really starting to add up. And when I've changed some overall concept I'm having to open a ton of files to make my fixes.. not a massive difference in time, but still. But good to know that I haven't dragged the code into the dirt using my approach so far, though none of my previous plugins have been anywhere near this scope.

    I think I'll go ahead and split them as you suggest, more into the parent like "Block, Player, Entity" etc. It should clean things up some.

    I went ahead and pulled the private objects - if I'm not going to access them again, not sure there really is any benefit to keeping them there, and means I just have those extra lines of code to write when I'm adding to them.

    Not to go off on a tangent too much, but as I'm still fairly new to all this, worth asking the stupid questions.

    Is it worthwhile pulling a single event in (for example, BlockEvent) and then using if instanceof BlockBreakEvent and sending the event to a different function?

    Also, I'm using Eclipse.. anyone know how to make my functions auto-collapse when I open the class? I have a class with a ton of stuff in it and finding anything is a pain in the backside.

    Thanks
     
  6. Offline

    DutchJellyV2

    I think your question is not stupid at all! The simple or basic questions are the best ones most of the times.
    I think it's more efficient to just make functions for BlockBreakEvent and others that extend BlockEvent that handle the event on their own. But, depending on your code it could be efficient to use the BlockEvent and test the event to see if it's an instance of BlockBreakEvent. Maybe you want to do something on all BlockEvents and an additional thing if it's a BlockBreakEvent.

    But to clarify: I think bukkit already does what you do with the instanceof. What I mean is that the BlockBreakEvent is not a "standalone" event but a subevent of BlockEvent. So I think the Bukkit API already does what you want to do with instanceof behind the scenes.
     
  7. Offline

    torpkev

    Perfect, thanks!

    And answering my own secondary question in case anyone else was looking for it.

    Ctrl + Shift + / (on numpad) collapses all code
    Ctrl + Shift + * (on numpad) uncollapses all code
     
    Last edited: Dec 13, 2018
    DutchJellyV2 likes this.
  8. Offline

    The_Spaceman

    not sure if your question got answerd, but yes... but only if you don't need the classes anywhere else for and it needs to be singleton.
     
    torpkev likes this.
Thread Status:
Not open for further replies.

Share This Page