Skip to content

Commit

Permalink
mount command: support multiple mounts like boats
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 6, 2020
1 parent c327fa3 commit 89027d6
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 46 deletions.
Expand Up @@ -61,36 +61,28 @@ public MountCommand() {

@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)) {
// Location arg
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
// Entity arg
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--) {
Expand All @@ -104,7 +96,6 @@ else if (!scriptEntry.hasObject("entities")
Utilities.entryHasPlayer(scriptEntry) ? Utilities.getEntryPlayer(scriptEntry).getLocation() : null,
Utilities.entryHasNPC(scriptEntry) ? Utilities.getEntryNPC(scriptEntry).getLocation() : null);
}

if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a location!");
}
Expand All @@ -113,31 +104,23 @@ else if (!scriptEntry.hasObject("entities")
@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()));
}

// Mount or dismount all of the entities
if (!cancel) {

// Go through all the entities, spawning/teleporting them
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);
Expand Down
Expand Up @@ -55,10 +55,6 @@ public FakeItemCommand() {
// @Usage
// Use to show a clientside-only pumpkin on the player's head.
// - fakeitem pumpkin slot:head
//
// @Usage
// Use to show a fake book in the player's hand for 1 tick.
// - fakeitem "written_book[book=author|Bob|title|My Book|pages|This is my book!]" slot:<player.held_item_slot> duration:1t
// -->

@Override
Expand Down
Expand Up @@ -6,46 +6,23 @@

public class Position {

/**
* Mounts a list of entities on top of each other.
*
* @param entities The list of entities
*/

public static void mount(List<Entity> entities) {

Entity lastEntity = null;

for (Entity entity : entities) {

if (entity != null) {

if (lastEntity != null && entity != lastEntity) {

// Because setPassenger() is a toggle, only use it if the new passenger
// is not already the current passenger, and also make sure we're not
// mounting the entity on itself

if (entity.getPassenger() != lastEntity) {
if (entity.getPassengers().contains(lastEntity)) {
lastEntity.teleport(entity.getLocation());
entity.setPassenger(lastEntity);
entity.addPassenger(lastEntity);
}
}

lastEntity = entity;
}
}
}

/**
* Dismounts a list of entities.
*
* @param entities The list of entities
*/
public static void dismount(List<Entity> entities) {

for (Entity entity : entities) {

if (entity != null) {
entity.leaveVehicle();
}
Expand Down

0 comments on commit 89027d6

Please sign in to comment.