Add health listener
This commit is contained in:
parent
63bbeef5ac
commit
a4737f548a
44
Util/src/io/reisub/dreambot/util/event/AbstractEvent.java
Normal file
44
Util/src/io/reisub/dreambot/util/event/AbstractEvent.java
Normal file
@ -0,0 +1,44 @@
|
||||
package io.reisub.dreambot.util.event;
|
||||
|
||||
import java.util.EventListener;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractEvent {
|
||||
private Thread thread;
|
||||
protected volatile boolean run = false;
|
||||
protected List<EventListener> listeners = new LinkedList<>();
|
||||
|
||||
public void start() {
|
||||
if (run) return;
|
||||
|
||||
run = true;
|
||||
thread = new Thread(this::run);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
run = false;
|
||||
thread = null;
|
||||
}
|
||||
|
||||
public abstract void run();
|
||||
|
||||
public void addListener(EventListener listener) {
|
||||
this.listeners.add(listener);
|
||||
|
||||
if (this.listeners.size() == 1) this.start();
|
||||
}
|
||||
|
||||
public void removeListener(EventListener listener) {
|
||||
this.listeners.remove(listener);
|
||||
|
||||
if (this.listeners.size() == 0) this.stop();
|
||||
}
|
||||
|
||||
public void removeAllListeners() {
|
||||
this.listeners.clear();
|
||||
|
||||
this.stop();
|
||||
}
|
||||
}
|
43
Util/src/io/reisub/dreambot/util/event/ListenerManager.java
Normal file
43
Util/src/io/reisub/dreambot/util/event/ListenerManager.java
Normal file
@ -0,0 +1,43 @@
|
||||
package io.reisub.dreambot.util.event;
|
||||
|
||||
import io.reisub.dreambot.util.event.health.HealthEvent;
|
||||
import io.reisub.dreambot.util.event.health.HealthListener;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
public class ListenerManager {
|
||||
private static ListenerManager instance;
|
||||
|
||||
private HealthEvent healthEvent;
|
||||
|
||||
public static ListenerManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ListenerManager();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private AbstractEvent getEventForListener(EventListener listener) {
|
||||
if (listener instanceof HealthListener) {
|
||||
if (healthEvent == null) {
|
||||
healthEvent = new HealthEvent();
|
||||
}
|
||||
return healthEvent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addListener(EventListener listener) {
|
||||
getEventForListener(listener).addListener(listener);
|
||||
}
|
||||
|
||||
public void removeListener(EventListener listener) {
|
||||
getEventForListener(listener).removeListener(listener);
|
||||
}
|
||||
|
||||
public void removeAllListeners() {
|
||||
healthEvent.removeAllListeners();
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package io.reisub.dreambot.util.event.health;
|
||||
|
||||
import io.reisub.dreambot.util.event.AbstractEvent;
|
||||
import org.dreambot.api.Client;
|
||||
import org.dreambot.api.methods.skills.Skill;
|
||||
import org.dreambot.api.methods.skills.Skills;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
public class HealthEvent extends AbstractEvent {
|
||||
private int lastHealth = -1;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (run && Client.getInstance().getScriptManager().isRunning()) {
|
||||
try {
|
||||
Thread.sleep(600);
|
||||
} catch (InterruptedException ignored) {}
|
||||
|
||||
if (Client.getInstance().getScriptManager().isPaused()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lastHealth == -1) {
|
||||
lastHealth = Skills.getBoostedLevels(Skill.HITPOINTS);
|
||||
continue;
|
||||
}
|
||||
|
||||
int currentHealth = Skills.getBoostedLevels(Skill.HITPOINTS);
|
||||
|
||||
if (currentHealth > lastHealth) {
|
||||
for (EventListener l : listeners) {
|
||||
((HealthListener) l).onHealthIncreased(lastHealth, currentHealth);
|
||||
((HealthListener) l).onHealthChanged(lastHealth, currentHealth);
|
||||
}
|
||||
} else if (currentHealth < lastHealth) {
|
||||
for (EventListener l : listeners) {
|
||||
((HealthListener) l).onHealthDecreased(lastHealth, currentHealth);
|
||||
((HealthListener) l).onHealthChanged(lastHealth, currentHealth);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package io.reisub.dreambot.util.event.health;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
public interface HealthListener extends EventListener {
|
||||
default void onHealthChanged(int oldValue, int newValue) {}
|
||||
|
||||
default void onHealthDecreased(int oldValue, int newValue) {}
|
||||
|
||||
default void onHealthIncreased(int oldValue, int newValue) {}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user