/
AttackCommand.java
149 lines (130 loc) · 5.94 KB
/
AttackCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.ai.Navigator;
import net.citizensnpcs.api.ai.TargetType;
import java.util.Arrays;
import java.util.List;
public class AttackCommand extends AbstractCommand {
public AttackCommand() {
setName("attack");
setSyntax("attack [<entity>|...] (target:<entity>/cancel)");
setRequiredArguments(0, 2);
isProcedural = false;
}
// <--[command]
// @Name Attack
// @Syntax attack [<entity>|...] (target:<entity>/cancel)
// @Required 0
// @Maximum 2
// @Short Makes an entity, or list of entities, attack a target.
// @Group entity
//
// @Description
// The attack command causes a mob entity to attack a target mob entity or player.
//
// This technically can be used on an NPC, but it will trigger the Citizens internal punching-pathfinder.
// This attack mode doesn't work well. If you want NPC combat, consider using Sentinel instead: <@link url https://github.com/mcmonkeyprojects/Sentinel/blob/master/README.md>.
//
// To cancel an attack, use the 'cancel' argument instead of specifying a target.
//
// @Tags
// <NPCTag.is_fighting>
// <NPCTag.attack_strategy>
// <NPCTag.target_entity>
//
// @Usage
// Use to make the player's target entity attack a nearby entity.
// - attack <player.target> target:<npc.location.find.living_entities.within[10].random>
//
// @Usage
// Use to make a random nearby entity attack a player.
// - attack <player.location.find.living_entities.within[10].random> target:<player>
//
// @Usage
// Use to stop an entity from attacking.
// - attack <[entity]> stop
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("cancel")
&& arg.matches("cancel", "stop")) {
scriptEntry.addObject("cancel", "true");
}
else if (!scriptEntry.hasObject("target")
&& arg.matchesArgumentType(EntityTag.class)
&& arg.matchesPrefix("target", "t")) {
// Single entity arg
scriptEntry.addObject("target", arg.asType(EntityTag.class));
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)
&& !arg.matchesPrefix("target", "t")) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
// Use the player as the target if one is not specified
if (!scriptEntry.hasObject("target")) {
scriptEntry.addObject("target", Utilities.entryHasPlayer(scriptEntry) ? Utilities.getEntryPlayer(scriptEntry).getDenizenEntity() : null);
}
// Use the NPC as the attacking entity if one is not specified
scriptEntry.defaultObject("entities",
Utilities.entryHasNPC(scriptEntry) ? Arrays.asList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()) : null);
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}
if (!scriptEntry.hasObject("target") && !scriptEntry.hasObject("cancel")) {
throw new InvalidArgumentsException("Must specify a target!");
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
EntityTag target = scriptEntry.getObjectTag("target");
boolean cancel = scriptEntry.hasObject("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? ArgumentHelper.debugObj("cancel", "true") : "") +
ArgumentHelper.debugObj("entities", entities.toString()) +
(target != null ? ArgumentHelper.debugObj("target", target) : ""));
}
// Go through all the entities and make them either attack
// the target or stop attacking
for (EntityTag entity : entities) {
if (entity.isCitizensNPC()) {
Navigator nav = entity.getDenizenNPC().getCitizen().getNavigator();
if (!cancel) {
nav.setTarget(target.getBukkitEntity(), true);
}
else {
// Only cancel navigation if the NPC is attacking something
if (nav.isNavigating()
&& nav.getTargetType().equals(TargetType.ENTITY)
&& nav.getEntityTarget().isAggressive()) {
nav.cancelNavigation();
}
}
}
else {
if (!cancel) {
entity.target(target.getLivingEntity());
}
else {
entity.target(null);
}
}
}
}
}