summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandskull.java
blob: 4f00e991ffd5949a57c1b2af9f8702b104de2bbc (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
package com.earth2me.essentials.commands;

import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;

public class Commandskull extends EssentialsCommand
{
	public Commandskull()
	{
		super("skull");
	}
	
	@Override
	protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		String owner;

		if (args.length > 0 && user.isAuthorized("essentials.skull.others")) {
			if (!args[0].matches("^[A-Za-z0-9_]+$")) {
				throw new IllegalArgumentException(tl("alphaNames"));
			}
			owner = args[0];
		}
		else {
			owner = user.getName();
		}

		ItemStack itemSkull = user.getBase().getItemInHand();
		SkullMeta metaSkull = null;
		boolean spawn = false;

		if (itemSkull != null && itemSkull.getType() == Material.SKULL_ITEM && itemSkull.getDurability() == 3) {
			metaSkull = (SkullMeta) itemSkull.getItemMeta();
		}
		else if (user.isAuthorized("essentials.skull.spawn"))
		{
			itemSkull = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
			metaSkull = (SkullMeta) itemSkull.getItemMeta();
			spawn = true;
		}
		else {
			throw new Exception(tl("invalidSkull"));
		}

		if (metaSkull.hasOwner() && !user.isAuthorized("essentials.skull.modify"))
		{
			throw new Exception(tl("noPermissionSkull"));
		}

		metaSkull.setDisplayName("§fSkull of " + owner);
		metaSkull.setOwner(owner);

		itemSkull.setItemMeta(metaSkull);

		if (spawn) {
			InventoryWorkaround.addItems(user.getBase().getInventory(), itemSkull);
			user.sendMessage(tl("givenSkull", owner));
		}
		else {
			user.sendMessage(tl("skullChanged", owner));
		}
	}

}