/
ItemCooldownCommand.java
110 lines (92 loc) · 3.71 KB
/
ItemCooldownCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Material;
import java.util.ArrayList;
import java.util.function.Consumer;
public class ItemCooldownCommand extends AbstractCommand {
public ItemCooldownCommand() {
setName("itemcooldown");
setSyntax("itemcooldown [<material>|...] (duration:<duration>)");
setRequiredArguments(1, 2);
isProcedural = false;
}
// <--[command]
// @Name ItemCooldown
// @Syntax itemcooldown [<material>|...] (duration:<duration>)
// @Required 1
// @Maximum 2
// @Short Places a cooldown on a material in a player's inventory.
// @Group player
//
// @Description
// Places a cooldown on a material in a player's inventory.
//
// @Tags
// <PlayerTag.item_cooldown[<material>]>
//
// @Usage
// Places a 1 second cooldown on using an ender pearl.
// - itemcooldown ender_pearl
//
// @Usage
// Places a 10 minute cooldown on using golden apples.
// - itemcooldown golden_apple d:10m
//
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Material material : Material.values()) {
if (material.isItem()) {
addOne.accept(material.name());
}
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("materials")
&& (arg.matchesArgumentType(MaterialTag.class)
|| arg.matchesArgumentType(ListTag.class))) {
scriptEntry.addObject("materials", arg.asType(ListTag.class).filter(MaterialTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("duration")
&& arg.matchesPrefix("d", "duration")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("materials")) {
throw new InvalidArgumentsException("Missing materials argument!");
}
scriptEntry.defaultObject("duration", new DurationTag(1));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ArrayList<MaterialTag> materials = (ArrayList<MaterialTag>) scriptEntry.getObject("materials");
DurationTag duration = scriptEntry.getObjectTag("duration");
PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
if (player == null) {
Debug.echoError("Invalid linked player.");
return;
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("materials", materials) + duration.debug());
}
for (MaterialTag mat : materials) {
player.getPlayerEntity().setCooldown(mat.getMaterial(), duration.getTicksAsInt());
}
}
}