summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/user/UserBase.java
blob: bcec3c8503ac228f3eb36953b7d416018c751bb9 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package net.ess3.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import lombok.Delegate;
import net.ess3.api.IEssentials;
import net.ess3.api.ISettings;
import net.ess3.api.IUser;
import net.ess3.api.InvalidNameException;
import net.ess3.permissions.Permissions;
import net.ess3.storage.AsyncStorageObjectHolder;
import net.ess3.storage.IStorageObjectHolder;
import net.ess3.storage.StoredLocation.WorldNotLoadedException;
import net.ess3.utils.Util;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;


public abstract class UserBase extends AsyncStorageObjectHolder<UserData> implements OfflinePlayer, CommandSender, IStorageObjectHolder<UserData>
{
	@Delegate
	protected final OfflinePlayer offlinePlayer;

	public UserBase(final OfflinePlayer base, final IEssentials ess) throws InvalidNameException
	{
		super(ess, UserData.class, ess.getUserMap().getUserFile(base.getName()));
		this.offlinePlayer = base;
		onReload();
	}

	@Override
	public void sendMessage(String string)
	{
		final Player player = offlinePlayer.getPlayer();
		if (player != null)
		{
			player.sendMessage(string);
		}
	}

	@Override
	public void sendMessage(String[] strings)
	{
		final Player player = offlinePlayer.getPlayer();
		if (player != null)
		{
			player.sendMessage(strings);
		}
	}

	@Override
	public Server getServer()
	{
		return ess.getServer();
	}

	@Override
	public boolean isPermissionSet(String string)
	{
		final Player player = offlinePlayer.getPlayer();
		if (player != null)
		{
			return player.isPermissionSet(string);
		}
		else
		{
			return false;
		}
	}

	@Override
	public boolean isPermissionSet(Permission prmsn)
	{
		final Player player = offlinePlayer.getPlayer();
		if (player != null)
		{
			return player.isPermissionSet(prmsn);
		}
		else
		{
			return false;
		}
	}

	@Override
	public boolean hasPermission(String string)
	{
		final Player player = offlinePlayer.getPlayer();
		if (player != null)
		{
			return player.hasPermission(string);
		}
		else
		{
			return false;
		}
	}

	@Override
	public boolean hasPermission(Permission prmsn)
	{
		final Player player = offlinePlayer.getPlayer();
		if (player != null)
		{
			return player.hasPermission(prmsn);
		}
		else
		{
			return false;
		}
	}

	@Override
	public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln)
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PermissionAttachment addAttachment(Plugin plugin)
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln, int i)
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PermissionAttachment addAttachment(Plugin plugin, int i)
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void removeAttachment(PermissionAttachment pa)
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void recalculatePermissions()
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Set<PermissionAttachmentInfo> getEffectivePermissions()
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public long getTimestamp(final UserData.TimestampType name)
	{
		return getData().getTimestamp(name);
	}

	public void setTimestamp(final UserData.TimestampType name, final long value)
	{
		getData().setTimestamp(name, value);
		queueSave();
	}

	public void setMoney(final double value)
	{

		final ISettings settings = ess.getSettings();

		final UserData data = getData();
		final double maxMoney = settings.getData().getEconomy().getMaxMoney();
		if (Math.abs(value) > maxMoney)
		{
			data.setMoney(value < 0 ? -maxMoney : maxMoney);
		}
		else
		{
			data.setMoney(value);
		}
		queueSave();
	}

	public double getMoney()
	{
		Double money = getData().getMoney();

		final ISettings settings = ess.getSettings();

		if (money == null)
		{
			money = (double)settings.getData().getEconomy().getStartingBalance();
		}
		if (Math.abs(money) > settings.getData().getEconomy().getMaxMoney())
		{
			money = money < 0 ? -settings.getData().getEconomy().getMaxMoney() : settings.getData().getEconomy().getMaxMoney();
		}
		return money;
	}

	public void setHome(String name, Location loc)
	{
		getData().addHome(Util.sanitizeKey(name), loc);
		queueSave();
	}

	public boolean toggleAfk()
	{
		boolean ret = !getData().isAfk();
		getData().setAfk(ret);
		queueSave();
		return ret;
	}

	public void setGodModeEnabled(boolean set)
	{
		getData().setGodmode(set);
		queueSave();
	}

	public void setMuted(boolean mute)
	{
		getData().setMuted(mute);
		queueSave();
	}

	public boolean toggleTeleportEnabled()
	{
		final boolean ret = !getData().isTeleportEnabled();
		getData().setTeleportEnabled(ret);
		queueSave();
		return ret;
	}

	public boolean isIgnoringPlayer(final IUser user)
	{
		return getData().getIgnore() == null ? false : getData().getIgnore().contains(
				user.getName().toLowerCase(Locale.ENGLISH)) && Permissions.CHAT_IGNORE_EXEMPT.isAuthorized(user);
	}

	public void setIgnoredPlayer(final IUser user, final boolean set)
	{
		getData().setIgnore(user.getName().toLowerCase(Locale.ENGLISH), set);
		queueSave();
	}

	public void addMail(String string)
	{
		getData().addMail(string);
		queueSave();
	}

	public List<String> getMails()
	{

		return getData().getMails();
	}

	public Location getHome(String name) throws Exception
	{
		if (getData().getHomes() == null)
		{
			return null;
		}
		try
		{
			return getData().getHomes().get(Util.sanitizeKey(name)).getStoredLocation();
		}
		catch (WorldNotLoadedException ex)
		{
			return null;
		}
	}

	public Location getHome(Location loc)
	{
		if (getData().getHomes() == null)
		{
			return null;
		}
		ArrayList<Location> worldHomes = new ArrayList<Location>();
		for (net.ess3.storage.StoredLocation location : getData().getHomes().values())
		{
			if (location.getWorldName().equals(loc.getWorld().getName()))
			{
				try
				{
					worldHomes.add(location.getStoredLocation());
				}
				catch (WorldNotLoadedException ex)
				{
					continue;
				}
			}
		}
		if (worldHomes.isEmpty())
		{
			return null;
		}
		if (worldHomes.size() == 1)
		{
			return worldHomes.get(0);
		}
		double distance = Double.MAX_VALUE;
		Location target = null;
		for (Location location : worldHomes)
		{
			final double d = loc.distanceSquared(location);
			if (d < distance) // Shouldnt this just use Double.isInfinite(v); rather than create a new Double of maxval?
			{
				target = location;
				distance = d;
			}
		}
		return target;
	}

	public Set<String> getHomes()
	{
		return getData().getHomes().keySet();
	}
}