Skip to content

Commit

Permalink
Add <e@entity.speed> tag and dEntity.speed mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Mar 1, 2015
1 parent 3daa253 commit d9701c0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -2030,6 +2030,17 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
return new Element(!EntityMovement.isAIDisabled(getBukkitEntity()))
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <e@entity.speed>
// @returns Element(Decimal)
// @group attributes
// @description
// Returns the entity's current speed when walking.
// -->
if (attribute.startsWith("speed"))
return new Element(EntityMovement.getSpeed(getBukkitEntity()))
.getAttribute(attribute.fulfill(1));


/////////////////////
// TYPE ATTRIBUTES
Expand Down Expand Up @@ -2476,6 +2487,19 @@ else if (isLivingEntity()) {
EntityMovement.toggleAI(getBukkitEntity(), value.asBoolean());
}

// <--[mechanism]
// @object dEntity
// @name speed
// @input Element(Decimal)
// @description
// Sets how fast the entity walks.
// @tags
// <e@entity.speed>
// -->
if (mechanism.matches("speed") && mechanism.requireDouble()) {
EntityMovement.setSpeed(getBukkitEntity(), value.asDouble());
}

// Iterate through this object's properties' mechanisms
for (Property property : PropertyParser.getProperties(this)) {
property.adjust(mechanism);
Expand Down
Expand Up @@ -42,6 +42,22 @@ public static boolean isAIDisabled(Entity entity) {
return nmsEntity.getDataWatcher().getByte(15) != 0;
}

public static double getSpeed(Entity entity) {
net.minecraft.server.v1_8_R1.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntityEntity instanceof EntityInsentient))
return 0.0;
EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
return nmsEntity.getAttributeInstance(GenericAttributes.d).getValue();
}

public static void setSpeed(Entity entity, double speed) {
net.minecraft.server.v1_8_R1.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntityEntity instanceof EntityInsentient))
return;
EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
nmsEntity.getAttributeInstance(GenericAttributes.d).setValue(speed);
}

public static void follow(final Entity target, final Entity follower, final double speed, final double lead,
final double maxRange, final boolean allowWander) {
if (target == null || follower == null)
Expand Down

0 comments on commit d9701c0

Please sign in to comment.