/
HeadCommand.java
146 lines (126 loc) · 5.95 KB
/
HeadCommand.java
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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.blocks.MaterialCompat;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.trait.trait.Equipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import java.util.Arrays;
import java.util.List;
public class HeadCommand extends AbstractCommand {
// <--[command]
// @Name Head
// @Syntax head (<entity>|...) [skin:<player_name>]
// @Required 1
// @Short Makes players or NPCs wear a specific player's head.
// @Group entity
//
// @Description
// Equips a player's head onto the player(s) or npc(s) specified. If no player or npc is specified, it defaults
// to the player attached to the script queue. It accepts a single entity or list of entities.
//
// @Tags
// <ItemTag.skin>
// <ItemTag.has_skin>
//
// @Usage
// Use to stick a player's head on your head with the head command.
// - head <player> skin:Notch
//
// @Usage
// Use to equip an npc with id 5 with your own head.
// - head n@5 skin:<player.name>
//
// @Usage
// Use to equip all online players with Notch's head.
// - head <server.list_online_players> skin:Notch
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("material")
&& arg.matchesArgumentType(MaterialTag.class)
&& !arg.matchesPrefix("skin", "s")) {
scriptEntry.addObject("material", arg.asType(MaterialTag.class));
}
else if (!scriptEntry.hasObject("skin")
&& (arg.matchesPrefix("skin", "s"))) {
scriptEntry.addObject("skin", arg.asElement());
}
else if (!scriptEntry.hasObject("entities")
&& arg.matches("player")
&& Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("entities", Arrays.asList(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity()));
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
// Use the NPC or the Player as the default entity
scriptEntry.defaultObject("entities",
(Utilities.entryHasNPC(scriptEntry) ? Arrays.asList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()) : null),
(Utilities.entryHasPlayer(scriptEntry) ? Arrays.asList(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity()) : null));
if (!scriptEntry.hasObject("skin") && !scriptEntry.hasObject("material")) {
throw new InvalidArgumentsException("Must specify a skin or material!");
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
ElementTag skin = scriptEntry.getElement("skin");
MaterialTag material = scriptEntry.getObjectTag("material");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
ArgumentHelper.debugObj("entities", entities.toString()) +
(skin != null ? skin.debug() : "") + (material != null ? material.debug() : ""));
}
ItemStack item = null;
// Create head item with chosen skin, or item/skin
if (skin != null) {
item = MaterialCompat.createPlayerHead();
ItemMeta itemMeta = item.getItemMeta();
((SkullMeta) itemMeta).setOwner(skin.asString().replaceAll("[pP]@", "")); // TODO: 1.12 and up - switch to setOwningPlayer?
item.setItemMeta(itemMeta);
}
else if (material != null) {
item = new ItemStack(material.getMaterial());
}
// Loop through entities, apply the item/skin
for (EntityTag entity : entities) {
if (entity.isCitizensNPC()) {
if (!entity.getDenizenNPC().getCitizen().hasTrait(Equipment.class)) {
entity.getDenizenNPC().getCitizen().addTrait(Equipment.class);
}
Equipment trait = entity.getDenizenNPC().getCitizen().getTrait(Equipment.class);
trait.set(1, item);
}
else if (entity.isPlayer()) {
entity.getPlayer().getInventory().setHelmet(item);
}
else {
if (entity.isLivingEntity() && entity.getLivingEntity().getEquipment() != null) {
entity.getLivingEntity().getEquipment().setHelmet(item);
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), entity.identify() + " is not a living entity!");
}
}
}
}
}