/
ListenCommand.java
149 lines (123 loc) · 5.22 KB
/
ListenCommand.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
147
148
149
package net.aufdemrand.denizen.scripts.commands.core;
import java.util.ArrayList;
import java.util.List;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
/**
* Initiates/finishes/cancels a 'quest listener'.
*
* @author Jeremy Schroeder
*/
public class ListenCommand extends AbstractCommand {
/* LISTEN can be used several ways:
*
* Issuing a new quest listener:
* LISTEN Listener_Type ID:ListenerID [Listener Arguments]
* See documentation for Listener_Types and specific arguments for each type.
*
* Canceling a quest listener in progress:
* LISTEN CANCEL ID:ListenerID
* Cancels a listener.
*
* Force-finishing a listener in progress:
* LISTEN FINISH ID:ListenerID
* Force finishes a listener.. the outcome is exactly the same as the Player
* completing the listener.
*
* Remember: A PLAYER:player_name argument can always be used to specify a
* specific player if necessary.
*/
/*
* Arguments: [] - Required, () - Optional
*
* [Listener_Type] The name of the type of listener. Only required when
* issuing a new listener. See documentation for available types.
*
* [ID] The unique name/id of the listener. This should be unique to the
* player since it is used with replaceable tag data and cancelling/force
* finishing.
*
*/
private enum ListenAction { NEW, CANCEL, FINISH }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
String id = null;
ListenAction listenAction = ListenAction.NEW;
String listenerType = null;
List<String> listenerArguments;
// Set some defaults based on the scriptEntry
listenerArguments = new ArrayList<String>();
// Parse Arguments
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesArg("CANCEL", arg)) {
listenAction = ListenAction.CANCEL;
dB.echoDebug("...marked to CANCEL.");
} else if (aH.matchesScript(arg)) {
scriptEntry.setScript(aH.getStringFrom(arg));
dB.echoDebug(Messages.DEBUG_SET_SCRIPT, arg);
} else if (aH.matchesArg("FINISH", arg)) {
listenAction = ListenAction.FINISH;
dB.echoDebug("...marked to FINISH.");
} else if (aH.matchesValueArg("ID", arg, ArgumentType.String)) {
id = aH.getStringFrom(arg);
dB.echoDebug("...ID set: '%s'", id);
} else if (denizen.getListenerRegistry().get(arg) != null) {
listenerType = arg;
dB.echoDebug("...TYPE set: '%s'", listenerType);
} else listenerArguments.add(arg);
}
if (id == null) id = scriptEntry.getScript().getName();
if (scriptEntry.getPlayer() == null && !(listenAction == ListenAction.CANCEL && scriptEntry.getOfflinePlayer() != null))
throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER);
scriptEntry.addObject("action", listenAction);
scriptEntry.addObject("type", listenerType);
scriptEntry.addObject("args", listenerArguments);
scriptEntry.addObject("id", id);
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
ListenAction listenAction = (ListenAction) scriptEntry.getObject("action");
String listenerType = (String) scriptEntry.getObject("type");
List<String> listenerArguments = (List<String>) scriptEntry.getObject("args");
String id = (String) scriptEntry.getObject("id");
switch (listenAction) {
case NEW:
try {
denizen.getListenerRegistry().get(listenerType).createInstance(scriptEntry.getPlayer(), id)
.build(scriptEntry.getPlayer(),
id,
listenerType,
listenerArguments,
scriptEntry.getScript().getName(),
scriptEntry.getNPC());
} catch (Exception e) {
dB.echoDebug("Cancelled creation of NEW listener!");
try { denizen.getListenerRegistry().getListenerFor(scriptEntry.getPlayer(), id).cancel(); }
catch (Exception ex) { }
}
break;
case FINISH:
if (denizen.getListenerRegistry().getListenerFor(scriptEntry.getPlayer(), id) != null)
denizen.getListenerRegistry().getListenerFor(scriptEntry.getPlayer(), id).finish();
break;
case CANCEL:
if (scriptEntry.getPlayer() != null) {
if (denizen.getListenerRegistry().getListenerFor(scriptEntry.getPlayer(), id) != null)
denizen.getListenerRegistry().getListenerFor(scriptEntry.getPlayer(), id).cancel();
break;
}
else denizen.getSaves().set("Listeners." + scriptEntry.getOfflinePlayer().getName() + "." + id, null);
}
}
@Override
public void onEnable() {
// Nothing to do here.
}
}