/
ExplodeCommand.java
116 lines (100 loc) · 4.79 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
package com.denizenscript.denizen.scripts.commands.world;
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.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
public class ExplodeCommand extends AbstractCommand {
// <--[command]
// @Name Explode
// @Syntax explode (power:<#.#>) (<location>) (fire) (breakblocks)
// @Required 0
// @Short Causes an explosion at the location.
// @Group world
//
// @Description
// This command causes an explosion at the location specified (or the npc / player location) which does not
// destroy blocks or set fire to blocks within the explosion. It accepts a 'fire' option which will set blocks
// on fire within the explosion radius. It also accepts a 'breakblocks' option which will cause the explosion to
// break blocks within the power radius as well as creating an animation and sounds.
// Default power: 1
// Default location: npc.location, or if no NPC link, player.location.
// It is highly recommended you specify a location to be safe.
//
// @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 parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Iterate through arguments
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.matchesPrimitive(ArgumentHelper.PrimitiveType.Float)
&& arg.matchesPrefix("power", "p")) {
scriptEntry.addObject("power", arg.asElement());
}
else if (!scriptEntry.hasObject("breakblocks")
&& arg.matches("breakblocks")) {
scriptEntry.addObject("breakblocks", "");
}
else if (!scriptEntry.hasObject("fire")
&& arg.matches("fire")) {
scriptEntry.addObject("fire", "");
}
else {
arg.reportUnhandled();
}
}
// Use default values if necessary
scriptEntry.defaultObject("power", new ElementTag(1.0));
scriptEntry.defaultObject("location",
Utilities.entryHasNPC(scriptEntry) ? Utilities.getEntryNPC(scriptEntry).getLocation() : null,
Utilities.entryHasPlayer(scriptEntry) ? Utilities.getEntryPlayer(scriptEntry).getLocation() : null);
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Missing location argument!");
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
// Get objects
final LocationTag location = (LocationTag) scriptEntry.getObject("location");
ElementTag power = (ElementTag) scriptEntry.getObject("power");
boolean breakblocks = scriptEntry.hasObject("breakblocks");
boolean fire = scriptEntry.hasObject("fire");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(ArgumentHelper.debugObj("location", location.toString()) +
ArgumentHelper.debugObj("power", power) +
ArgumentHelper.debugObj("breakblocks", breakblocks) +
ArgumentHelper.debugObj("fire", fire)));
}
location.getWorld().createExplosion
(location.getX(), location.getY(), location.getZ(),
power.asFloat(), fire, breakblocks);
}
}