Skip to content

Commit

Permalink
Fix vehicle collision events. Some minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Sep 15, 2013
1 parent a137bb3 commit 230d73a
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/Duration.java
Expand Up @@ -400,7 +400,7 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));
}

return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dCuboid.java
Expand Up @@ -457,7 +457,7 @@ public String getAttribute(Attribute attribute) {
return loc_2.getAttribute(attribute.fulfill(1));
}

return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}

}
57 changes: 31 additions & 26 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -1000,6 +1000,21 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));


/////////////////////
// INVENTORY ATTRIBUTES
/////////////////

// <--[tag]
// @attribute <e@entity.equipment>
// @returns dInventory
// @description
// Returns the dInventory of the entity.
// -->
if (attribute.startsWith("equipment")) {
return new dInventory(getLivingEntity()).getAttribute(attribute.fulfill(1));
}


/////////////////////
// LOCATION ATTRIBUTES
/////////////////
Expand Down Expand Up @@ -1027,6 +1042,21 @@ public String getAttribute(Attribute attribute) {
return new dLocation(getEyeLocation())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <e@entity.get_eye_height>
// @returns Element(Boolean)
// @description
// Returns the height of the entity's eyes above its location.
// -->
if (attribute.startsWith("get_eye_height")) {
if (isLivingEntity())
return new Element(getLivingEntity().getEyeHeight())
.getAttribute(attribute.fulfill(1));
else
return new Element("null")
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <e@entity.location.cursor_on>
// @returns dLocation
Expand Down Expand Up @@ -1087,16 +1117,6 @@ public String getAttribute(Attribute attribute) {
return new Element(getLivingEntity().getCanPickupItems())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <e@entity.equipment>
// @returns dInventory
// @description
// Returns the dInventory of the entity.
// -->
if (attribute.startsWith("equipment")) {
return new dInventory(getLivingEntity()).getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <e@entity.fall_distance>
// @returns Element(Float)
Expand Down Expand Up @@ -1393,21 +1413,6 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
return new Element(entity_type.name()).getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <e@entity.get_eye_height>
// @returns Element(Boolean)
// @description
// Returns the height of the entity's eyes above its location.
// -->
if (attribute.startsWith("get_eye_height")) {
if (isLivingEntity())
return new Element(getLivingEntity().getEyeHeight())
.getAttribute(attribute.fulfill(1));
else
return new Element("null")
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <e@entity.is_living>
// @returns Element(Boolean)
Expand Down Expand Up @@ -1474,7 +1479,7 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
return new Element(entity instanceof Tameable)
.getAttribute(attribute.fulfill(1));

return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}

}
Expand Up @@ -838,7 +838,7 @@ else if (getInventory() instanceof HorseInventory) {
return new dList(items).getAttribute(attribute.fulfill(1));
}

return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}

}
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -801,7 +801,7 @@ public String getAttribute(Attribute attribute) {
}


return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}

}
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dList.java
Expand Up @@ -415,8 +415,8 @@ public String getAttribute(Attribute attribute) {
// with dList's identify() value.

return (flag != null
? new Element(flag.getLast().asString()).getAttribute(attribute.fulfill(0))
: new Element(identify()).getAttribute(attribute.fulfill(0)));
? new Element(flag.getLast().asString()).getAttribute(attribute)
: new Element(identify()).getAttribute(attribute));
}

}
Expand Up @@ -1057,7 +1057,7 @@ else if (dEntity.matches(attribute.getContext(1))) {
.getAttribute(attribute.fulfill(1));
}

return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}

}
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dNPC.java
Expand Up @@ -536,8 +536,8 @@ && getCitizen().getTrait(Anchors.class).getAnchor(attribute.getContext(1)) != nu
: "null");

return (getEntity() != null
? new dEntity(getEntity()).getAttribute(attribute.fulfill(0))
: new Element(identify()).getAttribute(attribute.fulfill(0)));
? new dEntity(getEntity()).getAttribute(attribute)
: new Element(identify()).getAttribute(attribute));

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dScript.java
Expand Up @@ -194,7 +194,7 @@ public String getAttribute(Attribute attribute) {

}

return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dWorld.java
Expand Up @@ -446,7 +446,7 @@ public String getAttribute(Attribute attribute) {
return Duration.valueOf(getWorld().getWeatherDuration() + "t")
.getAttribute(attribute.fulfill(1));

return new Element(identify()).getAttribute(attribute.fulfill(0));
return new Element(identify()).getAttribute(attribute);
}

}
Expand Up @@ -3705,98 +3705,84 @@ public void serverCommand(ServerCommandEvent event) {

// <--[event]
// @Events
// vehicle collides
// <vehicle> collides
// vehicle collides with block
// vehicle collides with <material>
// <vehicle> collides with block
// <vehicle> collides with <material>
//
// @Triggers when a vehicle collides.
// @Triggers when a vehicle collides with a block.
// @Context
// <context.vehicle> returns the dEntity of the vehicle.
// <context.location> returns the dLocation of the block.
//
// -->
@EventHandler
public void vehicleCollision(VehicleCollisionEvent event) {
public void vehicleBlockCollision(VehicleBlockCollisionEvent event) {

Player player = null;
dNPC npc = null;

dEntity vehicle = new dEntity(event.getVehicle());
String vehicleType = vehicle.getEntityType().name();
dMaterial material = new dMaterial(event.getBlock().getType());

Map<String, dObject> context = new HashMap<String, dObject>();
context.put("vehicle", vehicle);
context.put("location", new dLocation(event.getBlock().getLocation()));

List<String> events = new ArrayList<String>();
events.add("vehicle collides");
events.add(vehicleType + " collides");
events.add("vehicle collides with block");
events.add("vehicle collides with " + material.identify());
events.add(vehicleType + " collides with block");
events.add(vehicleType + " collides with " + material.identify());

// <--[event]
// @Events
// vehicle collides with block
// vehicle collides with <material>
// <vehicle> collides with block
// <vehicle> collides with <material>
//
// @Triggers when a vehicle collides with a block.
// @Context
// <context.vehicle> returns the dEntity of the vehicle.
// <context.location> returns the dLocation of the block.
//
// -->
if (event instanceof VehicleBlockCollisionEvent) {

VehicleBlockCollisionEvent subEvent = (VehicleBlockCollisionEvent) event;

dMaterial material = new dMaterial(subEvent.getBlock().getType());
context.put("location", new dLocation(subEvent.getBlock().getLocation()));

events.add("vehicle collides with block");
events.add("vehicle collides with " + material.identify());
events.add(vehicleType + " collides with block");
events.add(vehicleType + " collides with " + material.identify());
}
doEvents(events, npc, player, context);
}

// <--[event]
// @Events
// vehicle collides with entity
// vehicle collides with <entity>
// <vehicle> collides with entity
// <vehicle> collides with <entity>
//
// @Triggers when a vehicle collides with an entity.
// @Context
// <context.vehicle> returns the dEntity of the vehicle.
// <context.entity> returns the dEntity of the entity the vehicle has collided with.
//
// @Determine
// "CANCELLED" to stop the collision from happening.
// "NOPICKUP" to stop the vehicle from picking up the entity.
//
// -->
@EventHandler
public void vehicleEntityCollision(VehicleEntityCollisionEvent event) {

// <--[event]
// @Events
// vehicle collides with entity
// vehicle collides with <entity>
// <vehicle> collides with entity
// <vehicle> collides with <entity>
//
// @Triggers when a vehicle collides with an entity.
// @Context
// <context.vehicle> returns the dEntity of the vehicle.
// <context.entity> returns the dEntity of the entity the vehicle has collided with.
//
// @Determine
// "CANCELLED" to stop the collision from happening.
// "NOPICKUP" to stop the vehicle from picking up the entity.
//
// -->
if (event instanceof VehicleEntityCollisionEvent) {

VehicleEntityCollisionEvent subEvent = (VehicleEntityCollisionEvent) event;

dEntity entity = new dEntity(subEvent.getEntity());
String entityType = entity.getEntityType().name();

events.add("vehicle collides with entity");
events.add("vehicle collides with " + entityType);
events.add(vehicleType + " collides with entity");
events.add(vehicleType + " collides with " + entityType);
Player player = null;
dNPC npc = null;

dEntity vehicle = new dEntity(event.getVehicle());
String vehicleType = vehicle.getEntityType().name();
dEntity entity = new dEntity(event.getEntity());
String entityType = entity.getEntityType().name();

Map<String, dObject> context = new HashMap<String, dObject>();
context.put("vehicle", vehicle);
context.put("entity", entity);

List<String> events = new ArrayList<String>();
events.add("vehicle collides with entity");
events.add("vehicle collides with " + entityType);
events.add(vehicleType + " collides with entity");
events.add(vehicleType + " collides with " + entityType);

String determination = doEvents(events, npc, player, context);
String determination = doEvents(events, npc, player, context);

if (determination.toUpperCase().startsWith("CANCELLED"))
subEvent.setCancelled(true);
if (determination.toUpperCase().startsWith("NOPICKUP"))
subEvent.setPickupCancelled(true);
}
// Do these events separately because only VehicleEntityCollisionEvent
// can actually be cancelled
else {

doEvents(events, npc, player, context);
}
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
if (determination.toUpperCase().startsWith("NOPICKUP"))
event.setPickupCancelled(true);
}

// <--[event]
Expand Down

0 comments on commit 230d73a

Please sign in to comment.