I don't know what I'm listening for

Discussion in 'Plugin Development' started by felixfritz, Dec 6, 2013.

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

    felixfritz

    This might be more Java-Based rather than Bukkit-Based, but hopefully someone still might know what I want.

    I have some sort of "Semi"-API for a Minigame. "Semi" here stands for the fact that it works on its own, but is easily expandable.

    The game contains a challenge. Each challenge contains one or more goals that the player has to achieve in order to complete the challenge. Some examples for a goal might be "Kill a mob", "Eat that item", "Find 32 diamonds". Therefore, I created an abstract super-class Goal that would build the base for all the other goals.

    To make this as efficient as possible, I wanted the Goal-class to "ask" the sub-classes for their Listeners, the Events that should be called. So with the example goals I stated before, it would be:
    - "Kill a mob": EntityDamageByEntityEvent
    - "Eat that item": PlayerItemConsumeEvent
    - "Find 32 diamonds": PlayerPickupItemEvent

    My plan was to make Bukkit listen for those events as soon as the game starts and call the implemented method, as soon as the event is triggered. This is, what it would look like with the Eat that item goal:
    Code:java
    1. public class EatItemGoal extends Goal {
    2.  
    3. private final Material type;
    4.  
    5. public EatItemGoal(Material type) {
    6. this.type = type;
    7. }
    8.  
    9. //some additional methods that are not important here
    10.  
    11. //the implemented method that has to be there from the super-class
    12. //it returns true, if the goal has been reached
    13. @Override
    14. public boolean eventTriggered(Event evt) {
    15.  
    16. PlayerItemConsumeEvent event = (PlayerItemConsumeEvent) evt;
    17. if(event.getItem().getType() == type)
    18. return true;
    19. return false;
    20. }
    21. }

    Seems pretty simple. The only problem I have: I don't know, how to listen for those events.

    In the goal-class, I only have a List of Events that I would like to listen for
    Code:java
    1. private List<Event> eventsToListenFor;
    2.  
    3. //...
    4.  
    5. public void addEvent(Event evt) {
    6. eventsToListenFor.add(evt);
    7. }

    But that's just the list. I don't have a way of telling the pluginmanager: "Hey there, could you... could you just listen to my list for a minute?"
    I tried to implement the Goal with the Listener interface and then add some sort of the same @EventHandler system that bukkit had, but I just don't know how.

    Does anyone know another way of doing this? Or a correct way? Or if that's even possible?
    (I hope I was clear enough in this thread. I don't want to hear anyone say: "you forgot the constructor there", "call eventTriggered from another class" etc., I know how to do that and I left out a lot of information in the classes out for simplicity's sake)

    ZeusAllMighty11 Exactly what I wanted to say! Dynamically listen for events that extend the Event class. Outstanding. Is that possible?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    ZeusAllMighty11

  3. Offline

    felixfritz

    ZeusAllMighty11 Sorry, but that's not what I'm looking for. It doesn't explain how to listen for events I don't know about. As I said, it's up to the subclasses, what events they need to have - am I supposed to add an "implements Listener" to all the subclasses and then register each individual class when adding the goals to the challenge?

    ZeusAllMighty11 Actually, that might work. I just let the Goal-class implement the Listener class. But won't that be a little bit resource intensive, if there were like 30 or 40 goals, each with their own listener and everything?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    ZeusAllMighty11

    Yes. So instead, create a single listener which could get as specific as possible without being too intrusive (I think it's the word), then call all child classes which follow the plan.

    Not a very good example, but here is what I did a while back:

    Show Spoiler

    Show Spoiler

    Show Spoiler
     
Thread Status:
Not open for further replies.

Share This Page