summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java
blob: 23173661e54cedc2fd06b40e18d6578ae16cc4a0 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.earth2me.essentials.commands;

import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.EssentialsUpgrade;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.UserMap;
import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.NumberUtil;
import com.google.common.base.Charsets;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

// This command has 4 undocumented behaviours #EasterEgg
public class Commandessentials extends EssentialsCommand
{
	public Commandessentials()
	{
		super("essentials");
	}
	private transient int taskid;
	private final transient Map<Player, Block> noteBlocks = new HashMap<Player, Block>();

	@Override
	public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length == 0)
		{
			run_disabled(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("debug"))
		{
			run_debug(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("nya"))
		{
			run_nya(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("moo"))
		{
			run_moo(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("reset"))
		{
			run_reset(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("opt-out"))
		{
			run_optout(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("cleanup"))
		{
			run_cleanup(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("uuidconvert"))
		{
			run_uuidconvert(server, sender, commandLabel, args);
		}
		else if (args[0].equalsIgnoreCase("uuidtest"))
		{
			run_uuidtest(server, sender, commandLabel, args);
		}
		else
		{
			run_reload(server, sender, commandLabel, args);
		}
	}

	//If you do not supply an argument this command will list 'overridden' commands.
	private void run_disabled(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		sender.sendMessage("/<command> <reload/debug>");

		final StringBuilder disabledCommands = new StringBuilder();
		for (Map.Entry<String, String> entry : ess.getAlternativeCommandsHandler().disabledCommands().entrySet())
		{
			if (disabledCommands.length() > 0)
			{
				disabledCommands.append(", ");
			}
			disabledCommands.append(entry.getKey()).append(" => ").append(entry.getValue());
		}
		if (disabledCommands.length() > 0)
		{
			sender.sendMessage(tl("blockList"));
			sender.sendMessage(disabledCommands.toString());
		}
	}

	private void run_reset(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2)
		{
			throw new Exception("/<command> reset <player>");
		}
		final User user = getPlayer(server, args, 1, true, true);
		user.reset();
		sender.sendMessage("Reset Essentials userdata for player: " + user.getDisplayName());
	}

	private void run_debug(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		ess.getSettings().setDebug(!ess.getSettings().isDebug());
		sender.sendMessage("Essentials " + ess.getDescription().getVersion() + " debug mode " + (ess.getSettings().isDebug() ? "enabled" : "disabled"));
	}

	private void run_reload(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		ess.reload();
		sender.sendMessage(tl("essentialsReload", ess.getDescription().getVersion()));
	}

	private void run_nya(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		final Map<String, Float> noteMap = new HashMap<String, Float>();
		noteMap.put("1F#", 0.5f);
		noteMap.put("1G", 0.53f);
		noteMap.put("1G#", 0.56f);
		noteMap.put("1A", 0.6f);
		noteMap.put("1A#", 0.63f);
		noteMap.put("1B", 0.67f);
		noteMap.put("1C", 0.7f);
		noteMap.put("1C#", 0.76f);
		noteMap.put("1D", 0.8f);
		noteMap.put("1D#", 0.84f);
		noteMap.put("1E", 0.9f);
		noteMap.put("1F", 0.94f);
		noteMap.put("2F#", 1.0f);
		noteMap.put("2G", 1.06f);
		noteMap.put("2G#", 1.12f);
		noteMap.put("2A", 1.18f);
		noteMap.put("2A#", 1.26f);
		noteMap.put("2B", 1.34f);
		noteMap.put("2C", 1.42f);
		noteMap.put("2C#", 1.5f);
		noteMap.put("2D", 1.6f);
		noteMap.put("2D#", 1.68f);
		noteMap.put("2E", 1.78f);
		noteMap.put("2F", 1.88f);
		final String tuneStr = "1D#,1E,2F#,,2A#,1E,1D#,1E,2F#,2B,2D#,2E,2D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D#,1E,2F#,1B,,1A#,,1B,,1F#,1G#,1B,,1F#,1G#,1B,1C#,1D#,1B,1E,1D#,1E,2F#,1B,,1B,,1F#,1G#,1B,1F#,1E,1D#,1C#,1B,,,,1F#,1B,,1F#,1G#,1B,,1F#,1G#,1B,1B,1C#,1D#,1B,1F#,1G#,1F#,1B,,1B,1A#,1B,1F#,1G#,1B,1E,1D#,1E,2F#,1B,,1A#,,1B,,1F#,1G#,1B,,1F#,1G#,1B,1C#,1D#,1B,1E,1D#,1E,2F#,1B,,1B,,1F#,1G#,1B,1F#,1E,1D#,1C#,1B,,,,1F#,1B,,1F#,1G#,1B,,1F#,1G#,1B,1B,1C#,1D#,1B,1F#,1G#,1F#,1B,,1B,1A#,1B,1F#,1G#,1B,1E,1D#,1E,2F#,1B,,1B,,";
		final String[] tune = tuneStr.split(",");
		taskid = ess.scheduleSyncRepeatingTask(new Runnable()
		{
			int i = 0;

			@Override
			public void run()
			{
				final String note = tune[i];
				i++;
				if (i >= tune.length)
				{
					Commandessentials.this.stopTune();
				}
				if (note == null || note.isEmpty())
				{
					return;
				}
				for (Player onlinePlayer : server.getOnlinePlayers())
				{
					onlinePlayer.playSound(onlinePlayer.getLocation(), Sound.NOTE_PIANO, 1, noteMap.get(note));
				}
			}
		}, 20, 2);
	}

	private void stopTune()
	{
		ess.getScheduler().cancelTask(taskid);
		for (Block block : noteBlocks.values())
		{
			if (block.getType() == Material.NOTE_BLOCK)
			{
				block.setType(Material.AIR);
			}
		}
		noteBlocks.clear();
	}
	private final String[] consoleMoo = new String[]
	{
		"         (__)",
		"         (oo)",
		"   /------\\/",
		"  / |    ||",
		" *  /\\---/\\",
		"    ~~   ~~",
		"....\"Have you mooed today?\"..."
	};
	private final String[] playerMoo = new String[]
	{
		"            (__)",
		"            (oo)",
		"   /------\\/",
		"  /  |      | |",
		" *  /\\---/\\",
		"    ~~    ~~",
		"....\"Have you mooed today?\"..."
	};

	private void run_moo(final Server server, final CommandSource sender, final String command, final String args[])
	{
		if (args.length == 2 && args[1].equals("moo"))
		{
			for (String s : consoleMoo)
			{
				logger.info(s);
			}
			for (Player player : ess.getServer().getOnlinePlayers())
			{
				player.sendMessage(playerMoo);
				player.playSound(player.getLocation(), Sound.COW_IDLE, 1, 1.0f);
			}
		}
		else
		{
			if (sender.isPlayer())
			{
				sender.getSender().sendMessage(playerMoo);
				final Player player = sender.getPlayer();
				player.playSound(player.getLocation(), Sound.COW_IDLE, 1, 1.0f);

			}
			else
			{
				sender.getSender().sendMessage(consoleMoo);
			}
		}
	}

	private void run_optout(final Server server, final CommandSource sender, final String command, final String args[])
	{
		final Metrics metrics = ess.getMetrics();
		try
		{
			sender.sendMessage("Essentials collects simple metrics to highlight which features to concentrate work on in the future.");
			if (metrics.isOptOut())
			{
				metrics.enable();
			}
			else
			{
				metrics.disable();
			}
			sender.sendMessage("Anonymous Metrics are now " + (metrics.isOptOut() ? "disabled" : "enabled") + " for all plugins.");
		}
		catch (IOException ex)
		{
			sender.sendMessage("Unable to modify 'plugins/PluginMetrics/config.yml': " + ex.getMessage());
		}
	}

	private void run_cleanup(final Server server, final CommandSource sender, final String command, final String args[]) throws Exception
	{
		if (args.length < 2 || !NumberUtil.isInt(args[1]))
		{
			sender.sendMessage("This sub-command will delete users who havent logged in in the last <days> days.");
			sender.sendMessage("Optional parameters define the minium amount required to prevent deletion.");
			sender.sendMessage("Unless you define larger default values, this command wil ignore people who have more than 0 money/homes.");
			throw new Exception("/<command> cleanup <days> [money] [homes]");
		}
		sender.sendMessage(tl("cleaning"));

		final long daysArg = Long.parseLong(args[1]);
		final double moneyArg = args.length >= 3 ? Double.parseDouble(args[2].replaceAll("[^0-9\\.]", "")) : 0;
		final int homesArg = args.length >= 4 && NumberUtil.isInt(args[3]) ? Integer.parseInt(args[3]) : 0;
		final UserMap userMap = ess.getUserMap();

		ess.runTaskAsynchronously(new Runnable()
		{
			@Override
			public void run()
			{
				Long currTime = System.currentTimeMillis();
				for (UUID u : userMap.getAllUniqueUsers())
				{
					final User user = ess.getUserMap().getUser(u);
					if (user == null)
					{
						continue;
					}

					long lastLog = user.getLastLogout();
					if (lastLog == 0)
					{
						lastLog = user.getLastLogin();
					}
					if (lastLog == 0)
					{
						user.setLastLogin(currTime);
					}

					if (user.isNPC())
					{
						continue;
					}

					long timeDiff = currTime - lastLog;
					long milliDays = daysArg * 24L * 60L * 60L * 1000L;
					int homeCount = user.getHomes().size();
					double moneyCount = user.getMoney().doubleValue();

					if ((lastLog == 0) || (timeDiff < milliDays)
						|| (homeCount > homesArg) || (moneyCount > moneyArg))
					{
						continue;
					}

					if (ess.getSettings().isDebug())
					{
						ess.getLogger().info("Deleting user: " + user.getName() + " Money: " + moneyCount + " Homes: " + homeCount + " Last seen: " + DateUtil.formatDateDiff(lastLog));
					}

					user.reset();
				}
				sender.sendMessage(tl("cleaned"));
			}
		});

	}

	private void run_uuidconvert(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		sender.sendMessage("Starting Essentials UUID userdata conversion, this may lag the server.");

		Boolean ignoreUFCache = (args.length > 2 && args[1].toLowerCase(Locale.ENGLISH).contains("ignore"));
		EssentialsUpgrade.uuidFileConvert(ess, ignoreUFCache);

		sender.sendMessage("UUID conversion complete, check your server log for more information.");
	}

	private void run_uuidtest(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2)
		{
			throw new Exception("/<command> uuidtest <name>");
		}
		String name = args[1];
		sender.sendMessage("Looking up UUID for " + name);

		UUID onlineUUID = null;

		for (Player player : server.getOnlinePlayers())
		{
			if (player.getName().equalsIgnoreCase(name))
			{
				onlineUUID = player.getUniqueId();
				break;
			}
		}

		UUID essUUID = ess.getUserMap().getUser(name).getConfigUUID();

		org.bukkit.OfflinePlayer player = ess.getServer().getOfflinePlayer(name);
		UUID bukkituuid = player.getUniqueId();
		sender.sendMessage("Bukkit Lookup: " + bukkituuid.toString());

		if (onlineUUID != null && onlineUUID != bukkituuid)
		{
			sender.sendMessage("Online player: " + onlineUUID.toString());
		}

		if (essUUID != null && essUUID != bukkituuid)
		{
			sender.sendMessage("Essentials config: " + essUUID.toString());
		}

		UUID npcuuid = UUID.nameUUIDFromBytes(("NPC:" + name).getBytes(Charsets.UTF_8));
		sender.sendMessage("NPC UUID: " + npcuuid.toString());

		UUID offlineuuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
		sender.sendMessage("Offline Mode UUID: " + offlineuuid.toString());
	}
}