summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/debug/LagMeasureTask.java
blob: 4acb5a5f2fced65d8d07e8091dac96befd892a9a (plain)
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
package com.earth2me.essentials.anticheat.debug;

import com.earth2me.essentials.anticheat.NoCheat;
import org.bukkit.World;


/**
 * A task running in the background that measures tick time vs. real time
 *
 */
public class LagMeasureTask implements Runnable
{
	private int ingameseconds = 1;
	private long lastIngamesecondTime = System.currentTimeMillis();
	private long lastIngamesecondDuration = 2000L;
	private boolean skipCheck = false;
	private int lagMeasureTaskId = -1;
	private final NoCheat plugin;

	public LagMeasureTask(NoCheat plugin)
	{
		this.plugin = plugin;
	}

	public void start()
	{
		// start measuring with a delay of 10 seconds
		lagMeasureTaskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this, 20, 20);
	}

	public void run()
	{
		try
		{
			boolean oldStatus = skipCheck;
			// If the previous second took to long, skip checks during
			// this second
			skipCheck = lastIngamesecondDuration > 2000;

			if (plugin.getConfig((World)null).logging.debugmessages)
			{
				if (oldStatus != skipCheck && skipCheck)
				{
					plugin.getLogger().warning("detected server lag, some checks will not work.");
				}
				else if (oldStatus != skipCheck && !skipCheck)
				{
					plugin.getLogger().info("server lag seems to have stopped, reenabling checks.");
				}
			}

			long time = System.currentTimeMillis();
			lastIngamesecondDuration = time - lastIngamesecondTime;
			if (lastIngamesecondDuration < 1000)
			{
				lastIngamesecondDuration = 1000;
			}
			else if (lastIngamesecondDuration > 3600000)
			{
				lastIngamesecondDuration = 3600000; // top limit of 1
				// hour per "second"
			}
			lastIngamesecondTime = time;
			ingameseconds++;

			// Check if some data is outdated now and let it be removed
			if (ingameseconds % 62 == 0)
			{
				plugin.cleanDataMap();
			}
		}
		catch (Exception e)
		{
			// Just prevent this thread from dying for whatever reason
		}

	}

	public void cancel()
	{
		if (lagMeasureTaskId != -1)
		{
			try
			{
				plugin.getServer().getScheduler().cancelTask(lagMeasureTaskId);
			}
			catch (Exception e)
			{
				plugin.getLogger().warning("Couldn't cancel LagMeasureTask: " + e.getMessage());
			}
			lagMeasureTaskId = -1;
		}
	}

	public boolean skipCheck()
	{
		return skipCheck;
	}
}