Java Reflection

Discussion in 'Plugin Development' started by Ultimate_n00b, Aug 26, 2013.

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


    For my plugin's API, trying to make a custom listener system. Basically it uses classes that implement my custom listener/use its custom annotation, but uses bukkit's events. You know, so I can call them myself? So with my limited reflection knowledge, I got this:

    1. public void callEvent(Arena arena, Event event) {
    2. for (Map.Entry<ArenaListener, Arena> entry : listeners.entrySet()) {
    3. if (entry.getValue().equals(arena)) {
    4. for (Method m : entry.getKey().getClass().getMethods()) {
    5. if (m.isAnnotationPresent(ArenaEvent.class) && m.getParameterTypes().length == 1) {
    6. for (Class<?> c : m.getParameterTypes()) {
    7. for (Event e : events) {
    8. if (c.equals(e.getClass())) {
    9. try {
    10. m.invoke(entry.getKey(), e);
    11. } catch (Exception e1) {
    12. e1.printStackTrace();
    13. }
    14. }
    15. }
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }

    I feel like I am missing something though. But it should:
    • Get each listener from a hashmap
    • Check if it is in the right arena (you can ignore this)
    • Loop through each of the methods
    • Check if it has my custom EventHandler annotation & has only one parameter
    • Loop through the parameter types so I can check it again a certain class later
    • Loop through the different registered events
    • Check if the parameter type is equal to the event class
    • Then run the method with an instance of the listener and the event
    As I said, I feel like I am missing something. And if someone knows a faster way, that would help too. I feel like this would be called too much for an event such as PlayerMoveEvent.
  2. Offline


  3. Offline


    .. this isn't for custom events. I am making an API where I can the event when I want to call it.
  4. Offline


    Napkin222 you're not understanding, an API is something like Bukkit and CraftBukkit, that offers new methods for plugin developers.
  5. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Bukkit.getServer().getPluginManager().callEvent(new MyCustomEvent());
  6. Offline


    I think he understands fine. You don't need to screw with Reflection to achieve this, you can just use Custom Events..
  7. Offline


    I don't want a custom event. I want to call ALREADY DEFINED EVENTS but when I chose to call them.
    For example, PlayerMoveEvent through an ArenaListener is only called for players in an arena. While I could custom events, I would rather use this instead.
    Samthelord1 likes this.
  8. Offline


    Hm. OK. Well, I would still not use Reflection for this. Have you considered EventBus? It's an API that works very much like the subscriber-publisher API that Bukkit plugins use, no annotations necessary.
    EDIT: I lied, EventBus now uses Annotations - it didn't when I last used it. Code sample:

    The Subscriber
    1. public class MySubscriber {
    2. public MySubscriber() {
    3. AnnotationProcessor.process(this);
    4. }
    5. @EventSubscriber(eventClass=MyEvent.class)
    6. public void onEvent(MyEvent event) { // This can be named w/e
    7. // Do things
    8. }
    9. }

    The Publisher
    1. EventBus.publish(new MyEvent());

    Event Object
    1. public class MyEvent {
    2. }
  9. Offline


    Soo.. does my method not work? I kinda was asking for advice for my current code..
  10. Offline


    I thought you were trying to "get stuff done" as it were, in which case, I would have suggested the above.
    As for your code, I haven't run it, but it looks fine to me. It's just a case of pragmatism.
    Also note that Reflection is not the speediest of things and you can accomplish the same goal by using EventBus that you are trying to achieve now.

    However if you are deadset on an answer then I'd say "yeah, it'll probably work"
  11. Offline


    > PlayerMoveEvent in 'ArenaListener'
    > If player is not in the arena, return, otherwise continue
    > If you wanted to use custom events, call a custom PlayerMoveInArenaEvent ?

    Not sure why you would need reflection to achieve this, unless I'm failing to comprehend what you're trying to do. It appears to me you are trying to over complicate things just for the sake of over complicating things.
  12. Offline


    You could check out how the authors BattleArena are doing it, because they have a custom @MatchEventHandler annotation for bukkit events.
  13. Offline


    I understand I could use Custom Events, but there are other things I am doing here. All I was asking here is if I did this part of the code right.
  14. Offline


    What is your definition of 'right'?

    - Does it compile?
    - Does it run as you intended it to?

    Instead of the time invested in asking if it's done right, why not actively debug / revise your code until it is right for you?
  15. Offline


    You're doing it wrong. You have the wrong impression of custom events and how they work. you CAN call the existing bukkit events whenever you want. you CAN create your own subclasses of the bukkit events and you CAN call them whenever you want, using the code that mbaxter provided.. there really is no need for all of the work you're doing here.
    Milkywayz likes this.
  16. Offline


    Tl;dr - yes, your code works, however, unless you are complicating things for the sake of complication, using custom events would certainly be easier for you. if you are trying to learn the reflection library then "if you did it right" is a question of "does it work as you want it to"

Thread Status:
Not open for further replies.

Share This Page