/
FailCommand.java
113 lines (93 loc) · 4.36 KB
/
FailCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.bukkit.ScriptFailEvent;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizencore.objects.dScript;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
/**
* Sets a Script as 'FAILED'. Scripts can be failed multiple times and Denizen will keep track
* of the total amount. This can also be checked against with the SCRIPT requirement.
*
* @author aufdemrand
*/
public class FailCommand extends AbstractCommand {
/* FAIL ('SCRIPT:[Script Name]') (PLAYER:[Player Name])
/* Arguments: [] - Required, () - Optional
* ('SCRIPT:[Script Name]') Changes the script from the triggering script to the one specified.
* Defaults to the script that the Script Entry was sent from.
* (PLAYER:[Player Name]) Changes the Player affected.
* Defaults to the Player who triggered the script.
*
* Example Usage:
* FAIL PLAYER:aufdemrand
* FAIL 'SCRIPT:A different script'
*/
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Parse the arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesArgumentType(dScript.class))
scriptEntry.addObject("script", arg.asType(dScript.class));
else if (arg.matchesArgumentType(dPlayer.class))
scriptEntry.addObject("player", arg.asType(dPlayer.class));
else arg.reportUnhandled();
}
// Check for required args
scriptEntry.defaultObject("player", ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer());
scriptEntry.defaultObject("script", scriptEntry.getScript());
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Grab objects from scriptEntry
dScript script = (dScript) scriptEntry.getObject("script");
dPlayer player = (dPlayer) scriptEntry.getObject("player");
// Report to dB
dB.report(scriptEntry, getName(),
player.debug() + script.debug());
dB.echoError(scriptEntry.getResidingQueue(), "The Fail command is outdated, use flags intead!");
failScript(player.getName(), script.getName());
}
public static void resetFails(String playerName, String scriptName) {
scriptName = scriptName.toUpperCase();
DenizenAPI._saves().set("Players." + playerName + "." + scriptName + "." + "Failed", null);
}
/**
* Increases a scripts 'failed' counter for a specified Player.
*
* @param playerName
* name of the Player
* @param scriptName
* name of the Script
*/
public static void failScript(String playerName, String scriptName) {
scriptName = scriptName.toUpperCase();
int fails = DenizenAPI._saves().getInt("Players." + playerName + "." + scriptName + "." + "Failed", 0);
// Increase fails by one and set.
fails++;
DenizenAPI._saves().set("Players." + playerName + "." + scriptName + "." + "Failed", fails);
// Call ScriptFailEvent
ScriptFailEvent event = new ScriptFailEvent(playerName, scriptName, fails);
Bukkit.getServer().getPluginManager().callEvent(event);
}
/**
* Gets the number of times a Player has 'failed' a script.
*
* @param playerName
* name of the Player
* @param scriptName
* name of the Script
* @return
* number of times the Player has failed the specified script
*/
public static int getScriptFails(String playerName, String scriptName) {
return DenizenAPI.getCurrentInstance().getSaves()
.getInt("Players." + playerName + "." + scriptName.toUpperCase() + "." + "Failed", 0);
}
}