TF-EssentialsX/Essentials/src/com/earth2me/essentials/EssentialsTimer.java

124 lines
2.5 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.entity.Player;
public class EssentialsTimer implements Runnable
{
private final transient IEssentials ess;
private final transient Set<String> onlineUsers = new HashSet<String>();
2013-04-23 05:51:25 +00:00
private transient long lastPoll = System.nanoTime();
private final LinkedList<Double> history = new LinkedList<Double>();
private int skip1 = 0;
private int skip2 = 0;
private final long maxTime = 10 * 1000000;
2013-04-23 05:51:25 +00:00
private final long tickInterval = 50;
2013-05-18 20:42:02 +00:00
EssentialsTimer(final IEssentials ess)
{
this.ess = ess;
2013-05-18 20:42:02 +00:00
history.add(20d);
}
2013-05-18 20:42:02 +00:00
@Override
public void run()
{
final long startTime = System.nanoTime();
2013-04-23 05:55:02 +00:00
final long currentTime = System.currentTimeMillis();
2013-04-23 05:51:25 +00:00
long timeSpent = (startTime - lastPoll) / 1000;
if (timeSpent == 0)
{
timeSpent = 1;
}
if (history.size() > 10)
{
history.remove();
}
2013-04-23 05:51:25 +00:00
double tps = tickInterval * 1000000.0 / timeSpent;
2013-05-18 20:42:02 +00:00
if (tps <= 21)
{
history.add(tps);
}
2013-04-23 05:55:02 +00:00
lastPoll = startTime;
int count = 0;
for (Player player : ess.getServer().getOnlinePlayers())
{
count++;
if (skip1 > 0)
{
skip1--;
continue;
}
2013-04-21 23:11:13 +00:00
if (count % 10 == 0)
{
if (System.nanoTime() - startTime > maxTime / 2)
{
skip1 = count - 1;
break;
}
}
try
{
final User user = ess.getUser(player);
onlineUsers.add(user.getName());
user.setLastOnlineActivity(currentTime);
user.checkActivity();
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, "EssentialsTimer Error:", e);
}
}
2013-05-18 20:42:02 +00:00
count = 0;
final Iterator<String> iterator = onlineUsers.iterator();
while (iterator.hasNext())
{
count++;
if (skip2 > 0)
{
skip2--;
continue;
}
2013-04-21 23:11:13 +00:00
if (count % 10 == 0)
{
if (System.nanoTime() - startTime > maxTime)
{
skip2 = count - 1;
break;
}
}
final User user = ess.getUser(iterator.next());
if (user.getLastOnlineActivity() < currentTime && user.getLastOnlineActivity() > user.getLastLogout())
{
2013-06-02 16:45:56 +00:00
if (!user.isHidden()) {
user.setLastLogout(user.getLastOnlineActivity());
}
iterator.remove();
continue;
}
user.checkMuteTimeout(currentTime);
user.checkJailTimeout(currentTime);
2012-03-27 19:14:38 +00:00
user.resetInvulnerabilityAfterTeleport();
}
}
2013-05-18 20:42:02 +00:00
2013-04-23 05:51:25 +00:00
public double getAverageTPS()
{
2013-04-23 05:51:25 +00:00
double avg = 0;
for (Double f : history)
{
if (f != null)
{
avg += f;
}
2013-05-18 20:42:02 +00:00
}
return avg / history.size();
}
}