-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
TitleCommand.java
160 lines (142 loc) · 7.71 KB
/
TitleCommand.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.tags.TagManager;
import java.util.Arrays;
import java.util.List;
public class TitleCommand extends AbstractCommand {
public TitleCommand() {
setName("title");
setSyntax("title (title:<text>) (subtitle:<text>) (fade_in:<duration>/{1s}) (stay:<duration>/{3s}) (fade_out:<duration>/{1s}) (targets:<player>|...) (per_player)");
setRequiredArguments(1, 7);
setParseArgs(false);
}
// <--[command]
// @Name Title
// @Syntax title (title:<text>) (subtitle:<text>) (fade_in:<duration>/{1s}) (stay:<duration>/{3s}) (fade_out:<duration>/{1s}) (targets:<player>|...) (per_player)
// @Required 1
// @Maximum 7
// @Short Displays a title to specified players.
// @Group player
//
// @Description
// Shows the players a large, noticeable wall of text in the center of the screen.
// You can also show a "subtitle" below that title.
// You may add timings for fading in, staying there, and fading out.
// The defaults for these are: 1 second, 3 seconds, and 1 second, respectively.
//
// Optionally use 'per_player' with a list of player targets, to have the tags in the text input be reparsed for each and every player.
// So, for example, "- title 'title:hello <player.name>' targets:<server.list_online_players>"
// would normally say "hello bob" to every player (every player sees the exact same name in the text, ie bob sees "hello bob", steve also sees "hello bob", etc)
// but if you use "per_player", each player online would see their own name (so bob sees "hello bob", steve sees "hello steve", etc).
//
// @Tags
// None
//
// @Usage
// Use to alert players of impending server restart.
// - title "title:<red>Server Restarting" "subtitle:<red>In 1 minute!" stay:1m targets:<server.list_online_players>
//
// @Usage
// Use to inform the player about the area they have just entered.
// - title "title:<green>Tatooine" "subtitle:<gold>What a desolate place this is."
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpret(scriptEntry, scriptEntry.getOriginalArguments())) {
if (arg.matchesPrefix("title")) {
scriptEntry.addObject("title", arg.asElement());
}
else if (arg.matchesPrefix("subtitle")) {
scriptEntry.addObject("subtitle", arg.asElement());
}
else if (arg.matchesPrefix("fade_in")
&& arg.matchesArgumentType(DurationTag.class)) {
String argStr = TagManager.tag(arg.getValue(), scriptEntry.getContext());
scriptEntry.addObject("fade_in", DurationTag.valueOf(argStr, scriptEntry.context));
}
else if (arg.matchesPrefix("stay")
&& arg.matchesArgumentType(DurationTag.class)) {
String argStr = TagManager.tag(arg.getValue(), scriptEntry.getContext());
scriptEntry.addObject("stay", DurationTag.valueOf(argStr, scriptEntry.context));
}
else if (arg.matchesPrefix("fade_out")
&& arg.matchesArgumentType(DurationTag.class)) {
String argStr = TagManager.tag(arg.getValue(), scriptEntry.getContext());
scriptEntry.addObject("fade_out", DurationTag.valueOf(argStr, scriptEntry.context));
}
else if (arg.matchesPrefix("targets", "target")) {
scriptEntry.addObject("targets", ListTag.getListFor(TagManager.tagObject(arg.getValue(), scriptEntry.getContext()), scriptEntry.getContext()).filter(PlayerTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("per_player")
&& arg.matches("per_player")) {
scriptEntry.addObject("per_player", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("title") && !scriptEntry.hasObject("subtitle")) {
throw new InvalidArgumentsException("Must have a title or subtitle!");
}
scriptEntry.defaultObject("fade_in", new DurationTag(1)).defaultObject("stay", new DurationTag(3))
.defaultObject("fade_out", new DurationTag(1))
.defaultObject("targets", Arrays.asList(Utilities.getEntryPlayer(scriptEntry)))
.defaultObject("subtitle", new ElementTag("")).defaultObject("title", new ElementTag(""));
}
@Override
public void execute(ScriptEntry scriptEntry) {
String title = scriptEntry.getElement("title").asString();
String subtitle = scriptEntry.getElement("subtitle").asString();
DurationTag fade_in = scriptEntry.getObjectTag("fade_in");
DurationTag stay = scriptEntry.getObjectTag("stay");
DurationTag fade_out = scriptEntry.getObjectTag("fade_out");
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
ElementTag perPlayerObj = scriptEntry.getElement("per_player");
boolean perPlayer = perPlayerObj != null && perPlayerObj.asBoolean();
BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
if (!perPlayer) {
title = TagManager.tag(title, context);
subtitle = TagManager.tag(subtitle, context);
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
ArgumentHelper.debugObj("title", title)
+ ArgumentHelper.debugObj("subtitle", subtitle)
+ fade_in.debug()
+ stay.debug()
+ fade_out.debug()
+ ArgumentHelper.debugList("targets", targets)
+ (perPlayerObj != null ? perPlayerObj.debug() : ""));
}
for (PlayerTag player : targets) {
if (player != null) {
if (!player.isOnline()) {
Debug.echoDebug(scriptEntry, "Player is offline, can't send title to them. Skipping.");
continue;
}
String personalTitle = title;
String personalSubtitle = subtitle;
if (perPlayer) {
context.player = player;
personalTitle = TagManager.tag(personalTitle, context);
personalSubtitle = TagManager.tag(personalSubtitle, context);
}
NMSHandler.getPacketHelper().showTitle(player.getPlayerEntity(), personalTitle, personalSubtitle, fade_in.getTicksAsInt(), stay.getTicksAsInt(), fade_out.getTicksAsInt());
}
else {
Debug.echoError("Sent title to non-existent player!?");
}
}
}
}