Experience Listener

Discussion in 'Tutorials & Resources' started by The_Scrub, Dec 7, 2014.

  1. The_Scrub

    Joined:
    Nov 6, 2013
    Messages:
    16
    Likes Received:
    4
    Useful for scripts bots like slayer and such

    Code (Text):
    1. package com.scrubscripting.listeners;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.EventListener;
    5. import java.util.HashMap;
    6. import java.util.List;
    7.  
    8. import com.runemate.game.api.hybrid.local.Skill;
    9.  
    10. public class EventDispatcher {
    11.     private final List<EventListener> listeners = new ArrayList<EventListener>();
    12.     private HashMap<Skill, Integer> cacheMap = new HashMap<Skill, Integer>();
    13.     private boolean set = false;
    14.     private boolean running = false;
    15.     private Object lock = new Object();
    16.  
    17.     public EventDispatcher() {
    18.         running = true;
    19.         Thread t = new Thread(new Runnable() {
    20.  
    21.             @Override
    22.             public void run() {
    23.                 while (running) {
    24.                     updateSkills();
    25.  
    26.                 }
    27.             }
    28.         });
    29.         t.setName("Scrub's Event Dispatcher");
    30.         t.start();
    31.     }
    32.  
    33.     public void stop() {
    34.         running = false;
    35.     }
    36.  
    37.     private void init() {
    38.         set = true;
    39.         for (Skill skill : Skill.values()) {
    40.             cacheMap.put(skill, skill.getExperience());
    41.         }
    42.  
    43.     }
    44.  
    45.     public void addListener(final EventListener event) {
    46.         synchronized (lock) {
    47.             listeners.add(event);
    48.         }
    49.     }
    50.  
    51.     public void removeListener(final EventListener event) {
    52.         synchronized (lock) {
    53.             listeners.remove(event);
    54.         }
    55.     }
    56.  
    57.     public void clear() {
    58.         synchronized (lock) {
    59.             listeners.clear();
    60.         }
    61.     }
    62.  
    63.     public void fireEvent(ExperienceEvent event) {
    64.         if (event != null) {
    65.             synchronized (lock) {
    66.                 for (int i = 0; i < listeners.size(); i++) {
    67.                     EventListener el = listeners.get(i);
    68.                     if (el instanceof ExperienceListener)
    69.                         ((ExperienceListener) el).onExperienceGain(event);
    70.                 }
    71.             }
    72.         }
    73.     }
    74.  
    75.     public void updateSkills() {
    76.  
    77.         if (!set) {
    78.             init();
    79.         } else {
    80.             for (Skill skill : Skill.values())
    81.                 if (skill.getExperience() > cacheMap.get(skill)) {
    82.                     fireEvent(new ExperienceEvent(skill, cacheMap.get(skill), skill.getExperience()));
    83.                     cacheMap.remove(skill);
    84.                     cacheMap.put(skill, skill.getExperience());
    85.                 }
    86.         }
    87.  
    88.         try {
    89.             Thread.sleep(444);
    90.         } catch (InterruptedException e) {
    91.  
    92.             e.printStackTrace();
    93.         }
    94.  
    95.     }
    96. }
    97.  
    Code (Text):
    1.  
    2. package com.scrubscripting.listeners;
    3.  
    4. import java.util.EventListener;
    5. import java.util.EventObject;
    6.  
    7. import com.runemate.game.api.hybrid.local.Skill;
    8.  
    9. public class ExperienceEvent extends EventObject {
    10.  
    11.    private static final long serialVersionUID = -2302038860845746230L;
    12.    private final int oldExperience;
    13.    private final int newExperience;
    14.    private final Skill skill;
    15.    private static final Object source = new Object();
    16.  
    17.    public ExperienceEvent(Skill skill, int oldExperience, int newExperience) {
    18.      super(source);
    19.      this.skill = skill;
    20.      this.oldExperience = oldExperience;
    21.      this.newExperience = newExperience;
    22.  
    23.    }
    24.  
    25.    public int getOld() {
    26.      return oldExperience;
    27.    }
    28.  
    29.    public int getNew() {
    30.      return newExperience;
    31.    }
    32.  
    33.    public int getChange() {
    34.      return newExperience - oldExperience;
    35.    }
    36.  
    37.    public Skill getSkill() {
    38.      return skill;
    39.    }
    40.  
    41.    public void fireEvent(EventListener el) {
    42.      ((ExperienceListener) el).onExperienceGain(this);
    43.    }
    44.  
    45.    @Override
    46.    public String toString() {
    47.      return "ExperienceEvent [OldExperience=" + oldExperience + ", NewExperience=" + newExperience + ", Skill=" + skill + ", Change=" + getChange() + "]";
    48.    }
    49.  
    50. }
    51.  
    52.  
    Code (Text):
    1.  
    2. package com.scrubscripting.listeners;
    3.  
    4. import java.util.EventListener;
    5.  
    6. public interface ExperienceListener extends EventListener {
    7.    public abstract void onExperienceGain(ExperienceEvent ev);
    8.  
    9. }
    10.  
    11.  
    Example usage

    Code (Text):
    1.  
    2.  
    3. // implement the interface
    4. public class Core extends LoopingScript implements PaintListener ,ExperienceListener
    5.     EventDispatcher eventDispatcher;
    6.    // on start set the listener
    7.     eventDispatcher = new EventDispatcher();
    8.      eventDispatcher.addListener(this);
    9.  
    10.  
    11. // on exit
    12. eventDispatcher .stop();
    13.  
     
    Arbiter likes this.
  2. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,419
    Likes Received:
    1,003
    @Cloud I like this idea. Should be implemented to the API.
     
  3. Cloud

    Cloud The Developer

    Joined:
    Jul 28, 2013
    Messages:
    2,300
    Likes Received:
    729
    We already have the SkillListener for monitoring level ups and it could easily be extended to monitor experience too.
     
  4. Cloud

    Cloud The Developer

    Joined:
    Jul 28, 2013
    Messages:
    2,300
    Likes Received:
    729
    This feature has been implemented in the latest release within the SkillListener.
     
  5. The_Scrub

    Joined:
    Nov 6, 2013
    Messages:
    16
    Likes Received:
    4
    Good job i saw the level listener and i was like no experience w/ee
     
  6. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,419
    Likes Received:
    1,003
    Props for the good idea. :D
     

Share This Page

Loading...