/
WorldUpdater.java
82 lines (72 loc) · 2.15 KB
/
WorldUpdater.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package info.jupiter;
import info.RS2Server;
import info.jupiter.util.WaitOnCompletionExecutor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Handles the updating of an individual world. <p>
* (NOTE, this will be fused with the world class at a later date).
* @author Advocatus <davidcntt@hotmail.com>
*
*/
public class WorldUpdater implements Runnable {
/**
* The WorldUpdater instance.
*/
private static WorldUpdater updater = null;
/**
* Lazily initialized singleton.
* @return the WorldUpdater instance.
*/
public static WorldUpdater getWorldUpdater() {
if (updater == null)
updater = new WorldUpdater();
return updater;
}
/**
* List of all the 'process()' tasks. Aka, TickTasks in Hyperion.
*/
private List<Runnable> preUpdate = new ArrayList<Runnable>();
/**
* List of all the Player/NPC 'UpdateTask' tasks.
*/
private List<Runnable> clientUpdate = new ArrayList<Runnable>();
/**
* List of all the reset tasks to be run after updating.
*/
private List<Runnable> postUpdate = new ArrayList<Runnable>();
/**
* A reference to the WaitOnCompletionExecutor instance for parallel logic.
*/
private WaitOnCompletionExecutor executor = RS2Server.getInstance().getThreadPool();
/**
* Run every cycle.
*/
@Override
public void run() {
preUpdate.clear();
clientUpdate.clear();
postUpdate.clear();
for (Iterator<Npc> it$ = World.getWorld().getNpcs().iterator(); it$.hasNext();) {
Npc npc = it$.next();
preUpdate.add(new RunnableNpc(npc, Stage.PRE_UPDATE));
postUpdate.add(new RunnableNpc(npc, Stage.POST_UPDATE));
}
for (Iterator<Player> it$ = World.getWorld().getPlayers().iterator(); it$.hasNext();) {
Player player = it$.next();
if (player.getSession().getSocketChannel().isOpen()) {
preUpdate.add(new RunnablePlayer(player, Stage.PRE_UPDATE));
clientUpdate.add(new RunnablePlayer(player, Stage.UPDATE));
postUpdate.add(new RunnablePlayer(player, Stage.POST_UPDATE));
} else
it$.remove();
}
executor.submit(preUpdate);
executor.await();
executor.submit(clientUpdate);
executor.await();
executor.submit(postUpdate);
executor.await();
}
}