/
AnimateChestCommand.java
166 lines (157 loc) · 7.15 KB
/
AnimateChestCommand.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
161
162
163
164
165
166
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.objects.notable.NotableManager;
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.PacketHelper;
import com.denizenscript.denizen.objects.LocationTag;
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.objects.notable.Notable;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class AnimateChestCommand extends AbstractCommand {
public AnimateChestCommand() {
setName("animatechest");
setSyntax("animatechest [<location>] ({open}/close) (sound:{true}/false) (<player>|...)");
setRequiredArguments(1, 4);
isProcedural = false;
}
// <--[command]
// @Name AnimateChest
// @Syntax animatechest [<location>] ({open}/close) (sound:{true}/false) (<player>|...)
// @Required 1
// @Maximum 4
// @Short Makes a chest appear to open or close.
// @Group world
//
// @Description
// This command animates a chest at a specified location in the world opening or closing.
// By default, the chest will animate opening.
// Optionally, specify whether to play a sound with the animation. By default this will play.
// Optionally, specify a player or list of players that the animation should be visible to.
// By default, only the linked player can see the animation.
//
// Note that this uses a generic 'block action' packet internally,
// which means other block types may also react to this command.
//
// @Tags
// None
//
// @Usage
// Use to animate a chest opening, which only the linked player will see.
// - animatechest <context.location>
//
// @Usage
// Use to then animate a chest closing, which only the linked player will see.
// - animatechest <context.location> close
//
// @Usage
// Use to animate a chest opening with no sound, which only the linked player will see.
// - animatechest <context.location> sound:false
//
// @Usage
// Use to animate a chest opening that only a single specific player will see.
// - animatechest <context.location> sound:false <[someplayer]>
//
// @Usage
// Use to animate a chest opening that only a list of specific players will see.
// - animatechest <context.location> sound:false <[someplayer]>|<[player]>|<[thatplayer]>
// -->
enum ChestAction {OPEN, CLOSE}
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Notable note : NotableManager.notesByType.get(LocationTag.class)) {
addOne.accept(NotableManager.getSavedId(note));
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(ChestAction.values())) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("sound")
&& arg.matchesPrefix("sound")
&& arg.matchesBoolean()) {
scriptEntry.addObject("sound", arg.asElement());
}
else if (!scriptEntry.hasObject("players")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("players", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a location!");
}
if (!scriptEntry.hasObject("action")) {
scriptEntry.addObject("action", new ElementTag("OPEN"));
}
if (!scriptEntry.hasObject("sound")) {
scriptEntry.addObject("sound", new ElementTag(true));
}
if (!scriptEntry.hasObject("players")) {
if (Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("players", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)));
}
else // TODO: Perhaps instead add all players in sight range?
{
throw new InvalidArgumentsException("Missing 'players' argument!");
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
ElementTag action = scriptEntry.getElement("action");
ElementTag sound = scriptEntry.getElement("sound");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location.debug()
+ ArgumentHelper.debugObj("block type", location.getBlock().getType().name())
+ action.debug()
+ sound.debug()
+ ArgumentHelper.debugObj("players", players.toString()));
}
PacketHelper packetHelper = NMSHandler.getPacketHelper();
switch (ChestAction.valueOf(action.asString().toUpperCase())) {
case OPEN:
for (PlayerTag player : players) {
Player ent = player.getPlayerEntity();
if (sound.asBoolean()) {
NMSHandler.getSoundHelper().playSound(ent, location,
Sound.BLOCK_CHEST_OPEN, 1, 1, "BLOCKS");
}
packetHelper.showBlockAction(ent, location, 1, 1);
}
break;
case CLOSE:
for (PlayerTag player : players) {
Player ent = player.getPlayerEntity();
if (sound.asBoolean()) {
NMSHandler.getSoundHelper().playSound(ent, location,
Sound.BLOCK_CHEST_CLOSE, 1, 1, "BLOCKS");
}
packetHelper.showBlockAction(ent, location, 1, 0);
}
break;
}
}
}