/
LeashCommand.java
133 lines (125 loc) · 5.42 KB
/
LeashCommand.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
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.denizen.objects.LocationTag;
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 org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LeashHitch;
import java.util.List;
public class LeashCommand extends AbstractCommand {
public LeashCommand() {
setName("leash");
setSyntax("leash (cancel) [<entity>|...] (holder:<entity>/<location>)");
setRequiredArguments(1, 3);
isProcedural = false;
}
// <--[command]
// @Name Leash
// @Syntax leash (cancel) [<entity>|...] (holder:<entity>/<location>)
// @Required 1
// @Maximum 3
// @Short Sticks a leash on target entity, held by a fence or another entity.
// @Group entity
//
// @Description
// Attaches a leash to the specified entity.
// The leash may be attached to a fence, or another entity.
// Players and Player NPCs may not be leashed.
// Note that releasing a mob from a fence post may leave the leash attached to that fence post.
//
// Non-player NPCs can be leashed if '/npc leashable' is enabled.
//
// @Tags
// <EntityTag.is_leashed>
// <EntityTag.leash_holder>
//
// @Usage
// Use to attach a leash to the player's target.
// - leash <player.target> holder:<player>
//
// @Usage
// Use to attach the closest cow in 10 blocks to the fence the player is looking at.
// - leash <player.location.find_entities[cow].within[10].first> holder:<player.cursor_on>
//
// @Usage
// Use to release the target entity.
// - leash cancel <player.target>
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("cancel")
&& arg.matches("cancel", "stop")) {
scriptEntry.addObject("cancel", "");
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("holder")
&& arg.matchesPrefix("holder", "h")) {
if (arg.matchesArgumentType(EntityTag.class)) {
scriptEntry.addObject("holder", arg.asType(EntityTag.class));
}
else if (arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("holder", arg.asType(LocationTag.class));
}
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}
if (!scriptEntry.hasObject("cancel")) {
scriptEntry.defaultObject("holder", Utilities.entryDefaultEntity(scriptEntry, false));
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
EntityTag holder = null;
LocationTag holderLoc = null;
Entity Holder = null;
Object holderObject = scriptEntry.getObject("holder");
if (holderObject instanceof EntityTag) {
holder = scriptEntry.getObjectTag("holder");
Holder = holder.getBukkitEntity();
}
else if (holderObject instanceof LocationTag) {
holderLoc = scriptEntry.getObjectTag("holder");
Material material = holderLoc.getBlock().getType();
if (material.name().endsWith("_FENCE")) {
Holder = holderLoc.getWorld().spawn(holderLoc, LeashHitch.class);
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "Bad holder location specified - only fences are permitted!");
return;
}
}
boolean cancel = scriptEntry.hasObject("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? ArgumentHelper.debugObj("cancel", cancel) : "") +
ArgumentHelper.debugObj("entities", entities.toString()) +
(holder != null ? ArgumentHelper.debugObj("holder", holder) : ArgumentHelper.debugObj("holder", holderLoc)));
}
for (EntityTag entity : entities) {
if (entity.isSpawned() && entity.isLivingEntity()) {
if (cancel) {
entity.getLivingEntity().setLeashHolder(null);
}
else {
entity.getLivingEntity().setLeashHolder(Holder);
}
}
}
}
}