/
FlaggedRequirement.java
91 lines (74 loc) · 2.97 KB
/
FlaggedRequirement.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
package net.aufdemrand.denizen.scripts.requirements.core;
import net.aufdemrand.denizen.exceptions.RequirementCheckException;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.scripts.requirements.AbstractRequirement;
import net.aufdemrand.denizen.scripts.requirements.RequirementsContext;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import java.util.List;
public class FlaggedRequirement extends AbstractRequirement {
/* FLAGGED TYPE FLAG:value
* Example: FLAGGED PLAYER Cookies:3
*
* Arguments: [] - Required, () - Optional
*/
private enum Type { GLOBAL, NPC, PLAYER }
@Override
public boolean check(RequirementsContext context, List<String> args) throws RequirementCheckException {
boolean outcome = false;
String name = null;
String value = "true";
String index = "";
Type type = Type.PLAYER;
for(String arg: args) {
if (aH.matchesArg("GLOBAL, NPC, DENIZEN, GLOBAL", arg))
type = Type.valueOf(arg.toUpperCase().replace("DENIZEN", "NPC"));
else if (arg.split(":", 2).length > 1)
{
String[] flagArgs = arg.split(":");
value = flagArgs[1].toUpperCase();
if (flagArgs[0].contains("["))
{
name = flagArgs[0].split("\\[", 2)[0].trim();
index = flagArgs[0].split("\\[", 2)[1].split("\\]", 2)[0].trim();
}
else
{
name = flagArgs[0].toUpperCase();
}
}
else
name = arg.toUpperCase();
}
FlagManager flagMng = DenizenAPI.getCurrentInstance().flagManager();
FlagManager.Flag flag = null;
String player = context.getPlayer().getName();
switch (type) {
case NPC:
flag = flagMng.getNPCFlag(context.getNPC().getId(), name);
break;
case PLAYER:
flag = flagMng.getPlayerFlag(player, name);
break;
case GLOBAL:
flag = flagMng.getGlobalFlag(name);
break;
}
if (index.length() == 0)
{
if (flag.getLast().asString().equalsIgnoreCase(value))
outcome = true;
else
dB.echoDebug(context.getScriptContainer(), "... does not match '" + flag.getLast().asString() + "'.");
}
else if (index.matches("\\d+"))
{
if (flag.get(Integer.parseInt(index)).asString().equalsIgnoreCase(value))
outcome = true;
else
dB.echoDebug(context.getScriptContainer(), "... does not match '" + flag.get(Integer.parseInt(index)).asString() + "'.");
}
return outcome;
}
}