Permalink
Browse files

Add get_entity_riders() and clarify behavior of rider functions.

Also fix core error when horse and rider are identical in set_entity_rider().
  • Loading branch information...
PseudoKnight committed Oct 28, 2018
1 parent e1fabec commit 27124a4613aff57451d7e8efa417a34caed3568c
@@ -25,7 +25,7 @@
List<MCEntity> getNearbyEntities(double x, double y, double z);
MCEntity getPassenger();
List<MCEntity> getPassengers();
MCServer getServer();
@@ -105,12 +105,12 @@ public int getMaxFireTicks() {
}
@Override
public MCEntity getPassenger() {
List<Entity> passengers = e.getPassengers();
if(passengers.isEmpty()) {
return null;
public List<MCEntity> getPassengers() {
List<MCEntity> passengers = new ArrayList<>();
for(Entity passenger : e.getPassengers()) {
passengers.add(BukkitConvertor.BukkitGetCorrectEntity(passenger));
}
return BukkitConvertor.BukkitGetCorrectEntity(passengers.get(0));
return passengers;
}
@Override
@@ -45,6 +45,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@@ -465,9 +466,9 @@ public boolean matches(Map<String, Construct> prefilter, BindableEvent e) throws
}
Prefilters.match(prefilter, "vehicletype", event.getVehicle().getType().name(), PrefilterType.MACRO);
MCEntity passenger = event.getVehicle().getPassenger();
if(passenger != null) {
Prefilters.match(prefilter, "passengertype", passenger.getType().name(), PrefilterType.MACRO);
List<MCEntity> passengers = event.getVehicle().getPassengers();
if(!passengers.isEmpty()) {
Prefilters.match(prefilter, "passengertype", passengers.get(0).getType().name(), PrefilterType.MACRO);
}
return true;
@@ -500,21 +501,21 @@ public BindableEvent convert(CArray manualObject, Target t) {
ret.put("vehicletype", new CString(e.getVehicle().getType().name(), t));
ret.put("id", new CString(e.getVehicle().getUniqueId().toString(), t));
MCEntity passenger = e.getVehicle().getPassenger();
List<MCEntity> passengers = e.getVehicle().getPassengers();
if(passenger == null) {
if(passengers.isEmpty()) {
ret.put("passenger", CNull.NULL);
ret.put("passengertype", CNull.NULL);
ret.put("player", CNull.NULL);
} else {
MCEntityType<?> passengertype = e.getVehicle().getPassenger().getType();
MCEntity passenger = passengers.get(0);
MCEntityType<?> passengertype = passenger.getType();
ret.put("passengertype", new CString(passengertype.name(), t));
ret.put("passenger", new CString(passenger.getUniqueId().toString(), t));
if(passengertype.getAbstracted() == MCEntityType.MCVanillaEntityType.PLAYER) {
ret.put("player", new CString(((MCPlayer) e.getVehicle().getPassenger()).getName(), t));
ret.put("player", new CString(((MCPlayer) passenger).getName(), t));
} else {
ret.put("player", CNull.NULL);
}
@@ -1267,7 +1267,7 @@ public Construct exec(Target t, Environment environment, Construct... args) thro
} else {
rider = Static.getEntity(args[1], t);
}
if((horse == null && rider == null) || horse == rider) {
if((horse == null && rider == null) || args[0].val().equals(args[1].val())) {
throw new CREFormatException("Horse and rider cannot be the same entity", t);
} else if(horse == null) {
success = rider.leaveVehicle();
@@ -1295,7 +1295,8 @@ public String docs() {
+ " If rider is null, horse will eject its current rider, if it has one. If horse is null,"
+ " rider will leave whatever it is riding. If horse and rider are both valid entities,"
+ " rider will ride horse. The function returns the success of whatever operation is done."
+ " If horse and rider are both null, or otherwise the same, a FormatException is thrown.";
+ " If horse and rider are both null, or otherwise the same, a FormatException is thrown."
+ " If a horse already has a rider, this will add the new rider without ejecting the existing one.";
}
@Override
@@ -1310,8 +1311,9 @@ public CHVersion since() {
@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCEntity ent = Static.getEntity(args[0], t);
if(ent.getPassenger() != null) {
return new CString(ent.getPassenger().getUniqueId().toString(), t);
List<MCEntity> passengers = ent.getPassengers();
if(!passengers.isEmpty()) {
return new CString(passengers.get(0).getUniqueId().toString(), t);
}
return CNull.NULL;
}
@@ -1323,7 +1325,8 @@ public String getName() {
@Override
public String docs() {
return "string {entityUUID} Returns the UUID of the given entity's rider, or null if it doesn't have one.";
return "string {entityUUID} Returns the UUID of the given entity's rider, or null if it doesn't have one."
+ " If there are multiple riders, only the first is returned.";
}
@Override
@@ -1332,6 +1335,36 @@ public CHVersion since() {
}
}
@api
public static class get_entity_riders extends EntityGetterFunction {
@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCEntity ent = Static.getEntity(args[0], t);
List<MCEntity> riders = ent.getPassengers();
CArray ret = new CArray(t);
for(MCEntity rider : riders) {
ret.push(new CString(rider.getUniqueId().toString(), t), t);
}
return ret;
}
@Override
public String getName() {
return "get_entity_riders";
}
@Override
public String docs() {
return "array {entityUUID} Returns an array of UUIDs for the given entity's riders.";
}
@Override
public CHVersion since() {
return CHVersion.V3_3_3;
}
}
@api
public static class get_entity_vehicle extends EntityGetterFunction {

0 comments on commit 27124a4

Please sign in to comment.