/
ExplodeCommand.java
132 lines (124 loc) · 5.67 KB
/
ExplodeCommand.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
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.function.Consumer;
public class ExplodeCommand extends AbstractCommand {
public ExplodeCommand() {
setName("explode");
setSyntax("explode (power:<#.#>) (<location>) (fire) (breakblocks) (source:<entity>)");
setRequiredArguments(0, 5);
isProcedural = false;
}
// <--[command]
// @Name Explode
// @Syntax explode (power:<#.#>) (<location>) (fire) (breakblocks) (source:<entity>)
// @Required 0
// @Maximum 5
// @Short Causes an explosion at the location.
// @Synonyms Detonate,Bomb,TNT
// @Group world
//
// @Description
// This command causes an explosion at the location specified (or the npc / player location).
// By default, this will not destroy blocks or set fire to blocks within the explosion.
//
// Specify the 'fire' argument to set blocks on fire within the explosion radius.
//
// Specify the 'breakblocks' argument to cause the explosion to break blocks within the power radius.
//
// If no power is specified, the default power will be 1.
//
// If no location is given, the default will be the linked NPC or player's location.
// It is highly recommended you specify a location to be safe.
//
// Optionally specify a source entity that will be tracked as the damage cause.
//
// @Tags
// None
//
// @Usage
// Use to create an explosion at a player's location.
// - explode <player.location>
//
// @Usage
// Use to create an explosion at a player, which breaks blocks and causes fire with a power of 5.
// - explode power:5 <player.location> fire breakblocks
//
// @Usage
// Use to create an explosion with a power radius of 3 at an NPC's location.
// - explode power:3 <npc.location>
//
// @Usage
// Use to create an explosion with a power radius of 3 at a related location which breaks blocks.
// - explode power:3 <context.location> breakblocks
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Notable note : NoteManager.notesByType.get(LocationTag.class)) {
addOne.accept(NoteManager.getSavedId(note));
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("power")
&& arg.matchesFloat()
&& arg.matchesPrefix("power", "p")) {
scriptEntry.addObject("power", arg.asElement());
}
else if (!scriptEntry.hasObject("source")
&& arg.matchesArgumentType(EntityTag.class)
&& arg.matchesPrefix("source")) {
scriptEntry.addObject("source", arg.asType(EntityTag.class));
}
else if (!scriptEntry.hasObject("breakblocks")
&& arg.matches("breakblocks")) {
scriptEntry.addObject("breakblocks", new ElementTag(true));
}
else if (!scriptEntry.hasObject("fire")
&& arg.matches("fire")) {
scriptEntry.addObject("fire", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("power", new ElementTag(1.0));
scriptEntry.defaultObject("fire", new ElementTag(false));
scriptEntry.defaultObject("breakblocks", new ElementTag(false));
scriptEntry.defaultObject("location", Utilities.entryDefaultLocation(scriptEntry, false));
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Missing location argument!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
ElementTag power = scriptEntry.getElement("power");
ElementTag breakblocks = scriptEntry.getElement("breakblocks");
ElementTag fire = scriptEntry.getElement("fire");
EntityTag source = scriptEntry.getObjectTag("source");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
location.debug() +
(source == null ? "" : source.debug()) +
power.debug() +
breakblocks.debug() +
fire.debug());
}
location.getWorld().createExplosion(location, power.asFloat(), fire.asBoolean(), breakblocks.asBoolean(), source == null ? null : source.getBukkitEntity());
}
}