/
RandomCommand.java
96 lines (77 loc) · 2.97 KB
/
RandomCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
/**
* Randomly selects a random script entry from the proceeding entries, discards
* the rest.
*
* <ol><tt>Usage: RANDOM [#]</tt></ol>
*
* [#] of entries to randomly select from. Will select 1 of # to execute and
* discard the rest.<br/><br/>
*
* Example Usage:<br/>
* <ul style="list-style-type: none;">
* <li><tt>Script:</tt></li>
* <li><tt>- RANDOM 3</tt></li>
* <li><tt>- CHAT Random Message 1</tt></li>
* <li><tt>- CHAT Random Message 2</tt></li>
* <li><tt>- CHAT Random Message 3 </tt></li>
* </ul>
*
* @author Jeremy Schroeder
*/
public class RandomCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry)
throws InvalidArgumentsException {
int possibilities = 1;
ScriptQueue queue;
queue = scriptEntry.getResidingQueue();
for (String arg : scriptEntry.getArguments()) {
//
// Make sure the random number is an integer.
//
if (aH.matchesInteger(arg)) {
possibilities = aH.getIntegerFrom(arg);
} else {
throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
}
if (possibilities <= 1) {
throw new InvalidArgumentsException("Must randomly select more than one item.");
}
if (queue.getQueueSize() < possibilities) {
throw new InvalidArgumentsException("Invalid Size! RANDOM [#] must not be larger than the script!");
}
scriptEntry.addObject("possibilities", possibilities);
scriptEntry.addObject("queue", queue);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Integer possibilities = (Integer) scriptEntry.getObject("possibilities");
ScriptQueue queue = (ScriptQueue) scriptEntry.getObject("queue");
int selected = Utilities.getRandom().nextInt(possibilities);
ScriptEntry keeping = null;
dB.echoDebug("...random number generator selected '%s'", String.valueOf(selected + 1));
for (int x = 0; x < possibilities; x++) {
if (x != selected) {
dB.echoDebug("...removing '%s'", queue.getEntry(0).getCommandName());
queue.removeEntry(0);
} else {
dB.echoDebug("...selected '%s'", queue.getEntry(0).getCommandName() + ": "
+ queue.getEntry(0).getArguments());
keeping = queue.getEntry(0);
queue.removeEntry(0);
}
}
queue.injectEntry(keeping, 0);
}
}