-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
TitleCommand.java
120 lines (101 loc) · 5.06 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
package com.denizenscript.denizen.scripts.commands.player;
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 java.util.Arrays;
import java.util.List;
public class TitleCommand extends AbstractCommand {
// <--[command]
// @Name Title
// @Syntax title (title:<text>) (subtitle:<text>) (fade_in:<duration>/{1s}) (stay:<duration>/{3s}) (fade_out:<duration>/{1s}) (targets:<player>|...)
// @Required 1
// @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.
//
// @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 : scriptEntry.getProcessedArgs()) {
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)) {
scriptEntry.addObject("fade_in", arg.asType(DurationTag.class));
}
else if (arg.matchesPrefix("stay")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("stay", arg.asType(DurationTag.class));
}
else if (arg.matchesPrefix("fade_out")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("fade_out", arg.asType(DurationTag.class));
}
else if (arg.matchesPrefix("targets", "target")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
}
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)));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag title = scriptEntry.getElement("title");
ElementTag subtitle = scriptEntry.getElement("subtitle");
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");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(title != null ? title.debug() : "") +
(subtitle != null ? subtitle.debug() : "") +
fade_in.debug() +
stay.debug() +
fade_out.debug() +
ArgumentHelper.debugObj("targets", targets));
}
for (PlayerTag player : targets) {
if (player.isValid() && player.isOnline()) {
NMSHandler.getInstance().getPacketHelper().showTitle(player.getPlayerEntity(),
title != null ? title.asString() : "",
subtitle != null ? subtitle.asString() : "",
fade_in.getTicksAsInt(),
stay.getTicksAsInt(),
fade_out.getTicksAsInt());
}
}
}
}