-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
NameplateCommand.java
97 lines (76 loc) · 3.34 KB
/
NameplateCommand.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
package net.aufdemrand.denizen.scripts.commands.entity;
import java.util.List;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.npc.traits.NameplateTrait;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
/**
* Modifies the nameplate of the given NPC
*
* @author spaceemotion
*/
public class NameplateCommand extends AbstractCommand {
/* Example usage:
* - NAMEPLATE COLOR:RED
* - NAMEPLATE COLOR:GOLD PLAYER:Notch
*
* Arguments: [] - Required, () - Optional
*
* [COLOR] The color to set. See the Bukkit documentation for available colors.
*
* (PLAYER) The player to apply the change to (can be per-player!).
*
*/
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
ChatColor color = null;
boolean player = false;
String text = null;
List<String> args = scriptEntry.getArguments();
for(String arg : args) {
if(aH.matchesValueArg("COLOR", arg, ArgumentType.String))
try { color = ChatColor.valueOf(aH.getStringFrom(arg).toUpperCase()); } catch( Exception e) {
dB.echoDebug("...COLOR could not be set: '%s' is an invalid color!", aH.getStringFrom(arg)); }
else if(aH.matchesValueArg("TARGET", arg, ArgumentType.Word)) {
player = true;
scriptEntry.setPlayer(aH.getPlayerFrom(arg));
}
else if (aH.matchesValueArg("SET", arg, ArgumentType.Custom))
text = aH.getStringFrom(arg);
}
scriptEntry.addObject("color", color)
.addObject("player", player)
.addObject("text", text);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Boolean player = (Boolean) scriptEntry.getObject("player");
ChatColor color = (ChatColor) scriptEntry.getObject("color");
String text = (String) scriptEntry.getObject("text");
if (text != null) {
if (text.equalsIgnoreCase("none")) {
scriptEntry.getNPC().getEntity().setCustomNameVisible(false);
dB.echoDebug("none");
} else {
scriptEntry.getNPC().getEntity().setCustomNameVisible(true);
scriptEntry.getNPC().getEntity().setCustomName(text);
dB.echoDebug(text);
}
if (scriptEntry.getNPC().getEntity() instanceof Player)
((Player) scriptEntry.getNPC().getEntity()).setDisplayName(text);
}
if(color != null) {
if (!scriptEntry.getNPC().getCitizen().hasTrait(NameplateTrait.class))
scriptEntry.getNPC().getCitizen().addTrait(NameplateTrait.class);
NameplateTrait trait = scriptEntry.getNPC().getCitizen().getTrait(NameplateTrait.class);
if (player) trait.setColor(color, scriptEntry.getPlayer().getName());
else trait.setColor(color);
}
}
}