-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
137 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
core/src/main/java/com/sekwah/advancedportals/core/util/GameScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.sekwah.advancedportals.core.util; | ||
|
||
import javax.inject.Singleton; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* For all delayed and repeating tasks. | ||
*/ | ||
@Singleton | ||
public final class GameScheduler { | ||
|
||
private final ArrayList<DelayedGameTickEvent> newTickEvents = new ArrayList<>(); | ||
private final ArrayList<DelayedGameTickEvent> delayedTickEvents = new ArrayList<>(); | ||
|
||
|
||
public void tick() { | ||
this.delayedTickEvents.addAll(this.newTickEvents); | ||
this.newTickEvents.clear(); | ||
Iterator<DelayedGameTickEvent> tickEventIterator = this.delayedTickEvents.iterator(); | ||
while (tickEventIterator.hasNext()) { | ||
DelayedGameTickEvent event = tickEventIterator.next(); | ||
event.tick(); | ||
if (event.shouldRun()) { | ||
event.run(); | ||
if(!(event instanceof DelayedGameIntervalEvent)) | ||
tickEventIterator.remove(); | ||
} | ||
} | ||
} | ||
|
||
public void delayedTickEvent(String name, Runnable consumer, int tickDelay) { | ||
this.newTickEvents.add(new DelayedGameTickEvent(name, consumer, tickDelay)); | ||
} | ||
|
||
public void intervalTickEvent(String name, Runnable consumer, int tickDelay, int interval) { | ||
this.newTickEvents.add(new DelayedGameIntervalEvent(name, consumer, tickDelay, interval)); | ||
} | ||
|
||
public void clearAllEvents() { | ||
this.newTickEvents.clear(); | ||
this.delayedTickEvents.clear(); | ||
} | ||
|
||
public static class DelayedGameTickEvent { | ||
|
||
// So we can find it later and remove it if needed | ||
public final String name; | ||
public final Runnable consumer; | ||
public int ticks; | ||
|
||
public DelayedGameTickEvent(String name, Runnable consumer, int ticks) { | ||
this.name = name; | ||
this.consumer = consumer; | ||
this.ticks = ticks; | ||
} | ||
|
||
public void tick() { | ||
this.ticks--; | ||
} | ||
|
||
public boolean shouldRun() { | ||
return this.ticks <= 0; | ||
} | ||
|
||
public void run() { | ||
this.consumer.run(); | ||
} | ||
} | ||
|
||
public static class DelayedGameIntervalEvent extends DelayedGameTickEvent { | ||
|
||
public int interval; | ||
|
||
public DelayedGameIntervalEvent(String name, Runnable consumer, int ticks, int interval) { | ||
super(name, consumer, ticks); | ||
this.interval = interval; | ||
} | ||
|
||
public void run() { | ||
this.ticks = interval; | ||
super.run(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters