-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
NarrateCommand.java
128 lines (101 loc) · 4.44 KB
/
NarrateCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.FormatScriptContainer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
/**
* Sends a message to the Player.
*
* @author Jeremy Schroeder
* Version 1.0 Last Updated 11/29 1:11
*/
public class NarrateCommand extends AbstractCommand {
@Override
public void onEnable() {
// Nothing to do here.
}
/*
* Arguments: [] - Required, () - Optional
* ['Text to announce'] sets the text.
* (PLAYER:player_name)
* (FORMAT:format)
*
* Example Usage:
* NARRATE 'Hello, world!'
* NARRATE PLAYER:<NPC.OWNER> 'ALERT! Intruder! intruder! <PLAYER.NAME> has infiltrated your base!'
*/
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
String text = null;
FormatScriptContainer format = null;
List<Player> targets = new ArrayList<Player>();
if (scriptEntry.getArguments().size() > 4)
throw new InvalidArgumentsException(Messages.ERROR_LOTS_OF_ARGUMENTS);
// Iterate through arguments
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesValueArg("FORMAT", arg, aH.ArgumentType.String)) {
String formatStr = aH.getStringFrom(arg);
format = ScriptRegistry.getScriptContainerAs(formatStr, FormatScriptContainer.class);
if(format != null) dB.echoDebug("... format set to: " + formatStr);
else dB.echoError("... could not find format for: " + formatStr);
}
// Add players to target list
else if (aH.matchesValueArg("target, targets", arg, ArgumentType.Custom)) {
Entity entity = null;
for (String target : aH.getListFrom(arg)) {
entity = dEntity.valueOf(target).getBukkitEntity();
if (entity != null && entity instanceof Player) {
targets.add((Player) entity);
}
else {
dB.echoError("Invalid target '%s'!", target);
}
}
}
else {
text = aH.getStringFrom(arg);
}
}
// If there are no targets, check if you can add this player
// to the targets
if (targets.size() == 0) {
if (scriptEntry.getPlayer() == null) {
throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER);
}
else {
targets.add(scriptEntry.getPlayer().getPlayerEntity());
}
}
if (text == null) throw new InvalidArgumentsException(Messages.ERROR_NO_TEXT);
scriptEntry.addObject("text", text)
.addObject("format", format)
.addObject("targets", targets);;
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
List<Player> targets = (List<Player>) scriptEntry.getObject("targets");
String text = (String) scriptEntry.getObject("text");
FormatScriptContainer format = (FormatScriptContainer) scriptEntry.getObject("format");
// Report to dB
dB.report(getName(),
aH.debugObj("Narrating", text)
+ aH.debugObj("Targets", targets)
+ (format != null ? aH.debugObj("Format", format.getName()) : ""));
for (Player player : targets) {
player.sendMessage(format != null ? format.getFormattedText(scriptEntry) : text);
}
}
}