/
MountCommand.java
126 lines (119 loc) · 5.04 KB
/
MountCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Conversion;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.entity.Position;
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 java.util.List;
public class MountCommand extends AbstractCommand {
public MountCommand() {
setName("mount");
setSyntax("mount (cancel) [<entity>|...] (<location>)");
setRequiredArguments(0, 3);
isProcedural = false;
}
// <--[command]
// @Name Mount
// @Syntax mount (cancel) [<entity>|...] (<location>)
// @Required 0
// @Maximum 3
// @Short Mounts one entity onto another.
// @Group entity
//
// @Description
// Mounts an entity onto another as though in a vehicle. Can be used to force a player into a vehicle or to
// mount an entity onto another entity. e.g. a player onto an npc. If the entity(s) don't exist they will be
// spawned. Accepts a location, which the entities will be teleported to on mounting.
//
// @Tags
// <EntityTag.vehicle>
// <EntityTag.is_inside_vehicle>
// <entry[saveName].mounted_entities> returns a list of entities that were mounted.
//
// @Usage
// Use to mount an NPC on top of a player.
// - mount <npc>|<player>
//
// @Usage
// Use to spawn a mutant pile of mobs.
// - mount cow|pig|sheep|chicken
//
// @Usage
// Use to place a diamond block above a player's head.
// - mount falling_block,diamond_block|<player>
//
// @Usage
// Use to force an entity in a vehicle.
// - mount <player>|boat
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
List<EntityTag> entities = null;
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("cancel")
&& arg.matches("cancel")) {
scriptEntry.addObject("cancel", "");
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
entities = arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry);
scriptEntry.addObject("entities", entities);
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}
if (!scriptEntry.hasObject("location")) {
if (entities != null) {
for (int i = entities.size() - 1; i >= 0; i--) {
if (entities.get(i).isSpawned()) {
scriptEntry.defaultObject("location", entities.get(i).getLocation());
break;
}
}
}
scriptEntry.defaultObject("location", Utilities.entryDefaultLocation(scriptEntry, true));
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a location!");
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
boolean cancel = scriptEntry.hasObject("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? ArgumentHelper.debugObj("cancel", cancel) : "") +
ArgumentHelper.debugObj("location", location) +
ArgumentHelper.debugObj("entities", entities.toString()));
}
if (!cancel) {
for (EntityTag entity : entities) {
entity.spawnAt(location);
}
Position.mount(Conversion.convertEntities(entities));
}
else {
Position.dismount(Conversion.convertEntities(entities));
}
ListTag entityList = new ListTag();
entityList.addObjects((List) entities);
scriptEntry.addObject("mounted_entities", entityList);
}
}