-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ToastCommand.java
128 lines (121 loc) · 5.47 KB
/
ToastCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.AdvancementHelper;
import com.denizenscript.denizen.nms.util.Advancement;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
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 org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
public class ToastCommand extends AbstractCommand {
public ToastCommand() {
setName("toast");
setSyntax("toast [<text>] (targets:<player>|...) (icon:<item>) (frame:{task}/challenge/goal)");
setRequiredArguments(1, 4);
isProcedural = false;
}
// <--[command]
// @Name Toast
// @Syntax toast [<text>] (targets:<player>|...) (icon:<item>) (frame:{task}/challenge/goal)
// @Required 1
// @Maximum 4
// @Short Shows the player a custom advancement toast.
// @Group player
//
// @Description
// Displays a client-side custom advancement "toast" notification popup to the player(s).
// If no target is specified it will default to the attached player.
// The icon argument changes the icon displayed in the toast pop-up notification.
// The frame argument changes the type of advancement.
//
// @Tags
// None
//
// @Usage
// Welcomes the player with an advancement toast.
// - toast "Welcome <player.name>!"
//
// @Usage
// Sends the player an advancement toast with a custom icon.
// - toast "Diggy Diggy Hole" icon:iron_spade
//
// @Usage
// Sends the player a "Challenge Complete!" type advancement toast.
// - toast "You finished a challenge!" frame:challenge icon:diamond
//
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("targets")
&& arg.matchesPrefix("target", "targets", "t")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("icon")
&& arg.matchesPrefix("icon", "i")
&& arg.matchesArgumentType(ItemTag.class)) {
scriptEntry.addObject("icon", arg.asType(ItemTag.class));
}
else if (!scriptEntry.hasObject("frame")
&& arg.matchesPrefix("frame", "f")
&& arg.matchesEnum(Advancement.Frame.values())) {
scriptEntry.addObject("frame", arg.asElement());
}
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", new ElementTag(arg.getRawValue()));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify a message!");
}
if (!scriptEntry.hasObject("targets")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must specify valid player targets!");
}
else {
scriptEntry.addObject("targets", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)));
}
}
scriptEntry.defaultObject("icon", new ItemTag(Material.AIR));
scriptEntry.defaultObject("frame", new ElementTag("TASK"));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag text = scriptEntry.getElement("text");
ElementTag frame = scriptEntry.getElement("frame");
ItemTag icon = scriptEntry.getObjectTag("icon");
final List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, name, text, frame, icon, db("targets", targets));
}
final Advancement advancement = new Advancement(true,
new NamespacedKey(Denizen.getInstance(), UUID.randomUUID().toString()), null,
icon.getItemStack(), text.asString(), "", null,
Advancement.Frame.valueOf(frame.asString().toUpperCase()), true, false, true, 0, 0, 1);
final AdvancementHelper advancementHelper = NMSHandler.getAdvancementHelper();
for (PlayerTag target : targets) {
Player player = target.getPlayerEntity();
if (player != null) {
advancementHelper.grant(advancement, player);
advancementHelper.revoke(advancement, player);
}
}
}
}