summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/NoCheatPlayerImpl.java
blob: 8fc81110acb281f896fcad9c4fcaf974f0cd4b63 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.earth2me.essentials.anticheat;



import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.config.ConfigurationCacheStore;
import com.earth2me.essentials.anticheat.data.DataStore;
import com.earth2me.essentials.anticheat.data.ExecutionHistory;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.MobEffectList;
import org.bukkit.GameMode;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;


public class NoCheatPlayerImpl implements NoCheatPlayer
{
	private Player player;
	private final NoCheat plugin;
	private final DataStore data;
	private ConfigurationCacheStore config;
	private long lastUsedTime;
	private final ExecutionHistory history;

	public NoCheatPlayerImpl(Player player, NoCheat plugin)
	{

		this.player = player;
		this.plugin = plugin;
		this.data = new DataStore();
		this.history = new ExecutionHistory();

		this.lastUsedTime = System.currentTimeMillis();
	}

	public void refresh(Player player)
	{
		this.player = player;
		this.config = plugin.getConfig(player);
	}

	@Override
	public boolean isDead()
	{
		return this.player.getHealth() <= 0 || this.player.isDead();
	}

	@Override
	public boolean hasPermission(String permission)
	{
		return player.hasPermission(permission);
	}

	@Override
	public DataStore getDataStore()
	{
		return data;
	}

	@Override
	public ConfigurationCacheStore getConfigurationStore()
	{
		return config;
	}

	@Override
	public Player getPlayer()
	{
		return player;
	}

	@Override
	public String getName()
	{
		return player.getName();
	}

	@Override
	public int getTicksLived()
	{
		return player.getTicksLived();
	}

	@Override
	public float getSpeedAmplifier()
	{
		EntityPlayer ep = ((CraftPlayer)player).getHandle();
		if (ep.hasEffect(MobEffectList.FASTER_MOVEMENT))
		{
			// Taken directly from Minecraft code, should work
			return 1.0F + 0.2F * (float)(ep.getEffect(MobEffectList.FASTER_MOVEMENT).getAmplifier() + 1); // TODO
		}
		else
		{
			return 1.0F;
		}
	}

	@Override
	public float getJumpAmplifier()
	{
		EntityPlayer ep = ((CraftPlayer)player).getHandle();
		if (ep.hasEffect(MobEffectList.JUMP))
		{
			int amp = ep.getEffect(MobEffectList.JUMP).getAmplifier();
			// Very rough estimates only
			// TODO
			if (amp > 20)
			{
				return 1.5F * (float)(ep.getEffect(MobEffectList.JUMP).getAmplifier() + 1);
			}
			else
			{
				return 1.2F * (float)(ep.getEffect(MobEffectList.JUMP).getAmplifier() + 1);
			}
		}
		else
		{
			return 1.0F;
		}
	}

	@Override
	public boolean isSprinting()
	{
		return player.isSprinting();
	}

	public void setLastUsedTime(long currentTimeInMilliseconds)
	{
		this.lastUsedTime = currentTimeInMilliseconds;
	}

	public boolean shouldBeRemoved(long currentTimeInMilliseconds)
	{
		if (lastUsedTime > currentTimeInMilliseconds)
		{
			// Should never happen, but if it does, fix it somewhat
			lastUsedTime = currentTimeInMilliseconds;
		}
		return lastUsedTime + 60000L < currentTimeInMilliseconds;
	}

	@Override
	public boolean isCreative()
	{
		return player.getGameMode() == GameMode.CREATIVE || player.getAllowFlight();
	}

	@Override
	public ExecutionHistory getExecutionHistory()
	{
		return history;
	}

	@Override
	public void dealFallDamage()
	{
		EntityPlayer p = ((CraftPlayer)player).getHandle();
		p.b(0D, true);
	}
}