-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
RefreshTask.java
116 lines (99 loc) · 3.34 KB
/
RefreshTask.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.github.games647.scoreboardstats;
import com.github.games647.scoreboardstats.config.Settings;
import com.google.common.collect.Maps;
import java.util.Map;
import org.apache.commons.lang.mutable.MutableInt;
import org.bukkit.entity.Player;
/**
* Handling all updates for a player in a performance optimized variant. This
* class split the updates over the ticks much smoother.
*
* @see SbManager
* @see com.github.games647.scoreboardstats.variables.ReplaceManager
*/
public class RefreshTask implements Runnable {
private final ScoreboardStats plugin;
//Prevent duplicate entries and is faster than the delay queue
private final Map<Player, MutableInt> queue = Maps.newHashMapWithExpectedSize(100);
private int nextGlobalUpdate = 20 * Settings.getInterval();
/**
* Initialize refresh task
*
* @param instance ScoreboardStats instance
*/
public RefreshTask(ScoreboardStats instance) {
this.plugin = instance;
}
@Override
public void run() {
//let the players update smoother
int remainingUpdates = getNextUpdates();
for (Map.Entry<Player, MutableInt> entry : queue.entrySet()) {
Player player = entry.getKey();
MutableInt remainingTicks = entry.getValue();
if (remainingTicks.intValue() == 0) {
if (remainingUpdates != 0) {
//Smoother refreshing; limit the updates
plugin.getScoreboardManager().onUpdate(player);
remainingTicks.setValue(20 * Settings.getInterval());
remainingUpdates--;
}
} else {
remainingTicks.decrement();
}
}
nextGlobalUpdate--;
if (nextGlobalUpdate == 0) {
nextGlobalUpdate = 20 * Settings.getInterval();
//update globals
plugin.getReplaceManager().updateGlobals();
}
}
/**
* Add a player to the queue for updating him.
*
* @param request the player that should be added.
* @return true if it was successfully queued
*/
public boolean addToQueue(Player request) {
boolean alreadyQueued = queue.containsKey(request);
if (!alreadyQueued) {
//check if it isn't already in the queue
queue.put(request, new MutableInt(20 * Settings.getInterval()));
}
return !alreadyQueued;
}
/**
* Checks if the player is in the refresh queue.
*
* @param request player instance
* @return true if the player is in the refresh queue.
*/
public boolean contains(Player request) {
return queue.containsKey(request);
}
/**
* Explicity removes the player from the refresh queue.
*
* @param request player who should be removed
* @return if the last entry exists
*/
public boolean remove(Player request) {
return queue.remove(request) != null;
}
/**
* Clears the complete queue.
*/
public void clear() {
queue.clear();
}
private int getNextUpdates() {
int nextUpdates = queue.size() / 20;
if (nextUpdates <= 0) {
//just update minimum one player per tick. Otherwise servers with not much players
//won't receive any updates
return 1;
}
return nextUpdates;
}
}