mirror of
https://github.com/2006-Scape/2006Scape.git
synced 2026-07-04 00:31:54 +00:00
Rename & Repackage
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.rs2.event;
|
||||
|
||||
/**
|
||||
* What the event must implement
|
||||
*
|
||||
* @author Stuart <RogueX>
|
||||
*/
|
||||
public abstract class CycleEvent {
|
||||
|
||||
/**
|
||||
* Code which should be ran when the event is executed
|
||||
*
|
||||
* @param container
|
||||
*/
|
||||
public abstract void execute(CycleEventContainer container);
|
||||
|
||||
/**
|
||||
* Code which should be ran when the event stops
|
||||
*/
|
||||
public abstract void stop();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package com.rs2.event;
|
||||
|
||||
/**
|
||||
* The wrapper for our event
|
||||
*
|
||||
* @author Stuart <RogueX>
|
||||
* @author Null++
|
||||
*/
|
||||
|
||||
public class CycleEventContainer {
|
||||
|
||||
/**
|
||||
* Event owner
|
||||
*/
|
||||
private final Object owner;
|
||||
|
||||
/**
|
||||
* Is the event running or not
|
||||
*/
|
||||
private boolean isRunning;
|
||||
|
||||
/**
|
||||
* The amount of cycles per event execution
|
||||
*/
|
||||
private int tick;
|
||||
|
||||
/**
|
||||
* The actual event
|
||||
*/
|
||||
private final CycleEvent event;
|
||||
|
||||
/**
|
||||
* The current amount of cycles passed
|
||||
*/
|
||||
private int cyclesPassed;
|
||||
|
||||
/**
|
||||
* The event ID
|
||||
*/
|
||||
private final int eventID;
|
||||
|
||||
/**
|
||||
* Sets the event containers details
|
||||
*
|
||||
* @param owner
|
||||
* , the owner of the event
|
||||
* @param event
|
||||
* , the actual event to run
|
||||
* @param tick
|
||||
* , the cycles between execution of the event
|
||||
*/
|
||||
public CycleEventContainer(int id, Object owner, CycleEvent event, int tick) {
|
||||
eventID = id;
|
||||
this.owner = owner;
|
||||
this.event = event;
|
||||
isRunning = true;
|
||||
cyclesPassed = 0;
|
||||
this.tick = tick;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the contents of the event
|
||||
*/
|
||||
public void execute() {
|
||||
event.execute(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the event from running
|
||||
*/
|
||||
public void stop() {
|
||||
isRunning = false;
|
||||
event.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the event need to be ran?
|
||||
*
|
||||
* @return true yes false no
|
||||
*/
|
||||
public boolean needsExecution() {
|
||||
if (!isRunning()) {
|
||||
return false;
|
||||
}
|
||||
if (++cyclesPassed >= tick) {
|
||||
cyclesPassed = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the owner of the event
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Object getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the event running?
|
||||
*
|
||||
* @return true yes false no
|
||||
*/
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the event id
|
||||
*
|
||||
* @return id
|
||||
*/
|
||||
public int getID() {
|
||||
return eventID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current cycle/tick.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getTick() {
|
||||
return tick;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the amount of cycles between the execution
|
||||
*
|
||||
* @param tick
|
||||
*/
|
||||
public void setTick(int tick) {
|
||||
this.tick = tick;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.rs2.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Handles all of our cycle based events
|
||||
*
|
||||
* @author Stuart <RogueX>
|
||||
* @author Null++
|
||||
*/
|
||||
public class CycleEventHandler {
|
||||
|
||||
/**
|
||||
* The instance of this class
|
||||
*/
|
||||
private static CycleEventHandler instance;
|
||||
|
||||
/**
|
||||
* Returns the instance of this class
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static CycleEventHandler getSingleton() {
|
||||
if (instance == null) {
|
||||
instance = new CycleEventHandler();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds all of our events currently being ran
|
||||
*/
|
||||
private final List<CycleEventContainer> events;
|
||||
|
||||
/**
|
||||
* Creates a new instance of this class
|
||||
*/
|
||||
public CycleEventHandler() {
|
||||
events = new ArrayList<CycleEventContainer>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an event to the list
|
||||
*
|
||||
* @param id
|
||||
* @param owner
|
||||
* @param event
|
||||
* @param cycles
|
||||
*/
|
||||
public CycleEventContainer addEvent(int id, Object owner, CycleEvent event, int cycles) {
|
||||
CycleEventContainer container = new CycleEventContainer(id, owner, event, cycles);
|
||||
events.add(container);
|
||||
return container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an event to the list
|
||||
*
|
||||
* @param owner
|
||||
* @param event
|
||||
* @param cycles
|
||||
*/
|
||||
public CycleEventContainer addEvent(Object owner, CycleEvent event, int cycles) {
|
||||
CycleEventContainer container = new CycleEventContainer(-1, owner, event, cycles);
|
||||
events.add(container);
|
||||
return container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute and remove events
|
||||
*/
|
||||
public void process() {
|
||||
List<CycleEventContainer> eventsCopy = new ArrayList<CycleEventContainer>(
|
||||
events);
|
||||
List<CycleEventContainer> remove = new ArrayList<CycleEventContainer>();
|
||||
for (CycleEventContainer c : eventsCopy) {
|
||||
if (c != null) {
|
||||
if (c.needsExecution() && c.isRunning()) {
|
||||
c.execute();
|
||||
if (!c.isRunning()) {
|
||||
remove.add(c);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (CycleEventContainer c : remove) {
|
||||
events.remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the amount of events currently running
|
||||
*
|
||||
* @return amount
|
||||
*/
|
||||
public int getEventsCount() {
|
||||
return events.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all events for a specific owner and id
|
||||
*
|
||||
* @param owner
|
||||
*/
|
||||
public void stopEvents(Object owner) {
|
||||
for (CycleEventContainer c : events) {
|
||||
if (c.getOwner() == owner) {
|
||||
c.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all events for a specific owner and id
|
||||
*
|
||||
* @param owner
|
||||
* @param id
|
||||
*/
|
||||
public void stopEvents(Object owner, int id) {
|
||||
for (CycleEventContainer c : events) {
|
||||
if (c.getOwner() == owner && id == c.getID()) {
|
||||
c.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all events for a specific owner and id
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public void stopEvents(int id) {
|
||||
for (CycleEventContainer c : events) {
|
||||
if (id == c.getID()) {
|
||||
c.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user