-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
BreakCommand.java
85 lines (65 loc) · 3.46 KB
/
BreakCommand.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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.citizensnpcs.npc.ai.BlockBreaker;
/**
* Breaks a block using Citizens' BlockBreaker
*
* @author Jeremy Schroeder
*/
public class BreakCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("location", arg.asType(dLocation.class));
else if (!scriptEntry.hasObject("entity")
&& arg.matchesArgumentType(dEntity.class))
scriptEntry.addObject("entity", arg.asType(dEntity.class));
else if (!scriptEntry.hasObject("radius")
&& arg.matchesPrimitive(aH.PrimitiveType.Double))
scriptEntry.addObject("radius", arg.asElement());
}
// Make sure location and entity were fulfilled
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException("Must specify a location!");
if (!scriptEntry.hasObject("entity")) {
if (scriptEntry.getPlayer() != null && scriptEntry.getPlayer().isOnline())
scriptEntry.addObject("entity", new dEntity(scriptEntry.getPlayer().getPlayerEntity()));
else if (scriptEntry.getNPC() != null && scriptEntry.getNPC().isSpawned())
scriptEntry.addObject("entity", new dEntity(scriptEntry.getNPC().getEntity()));
else throw new InvalidArgumentsException("Must specify an entity!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
final dLocation location = (dLocation) scriptEntry.getObject("location");
final dEntity entity = (dEntity) scriptEntry.getObject("entity");
Element radius = (scriptEntry.hasObject("radius")
? (Element) scriptEntry.getObject("radius")
: null);
dB.report(getName(), location.debug() + entity.debug()
+ (radius != null ? radius.debug() : ""));
BlockBreaker.Configuration config = new BlockBreaker.Configuration()
.item(entity.getLivingEntity().getEquipment().getItemInHand())
.radius(radius != null ? radius.asDouble() : 1)
.callback(new Runnable() {
@Override
public void run() {
dB.echoDebug(entity.debug() + " dug " + location.debug());
}
});
BlockBreaker breaker = BlockBreaker.createWithConfiguration(entity.getLivingEntity(), location.getBlock(), config);
breaker.run();
}
}