Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add armor_pose mech and armor_pose tags
- Loading branch information
Showing
2 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
233 changes: 233 additions & 0 deletions
233
src/main/java/net/aufdemrand/denizen/objects/properties/entity/EntityArmorPose.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
package net.aufdemrand.denizen.objects.properties.entity; | ||
|
||
import net.aufdemrand.denizen.objects.dEntity; | ||
import net.aufdemrand.denizen.objects.dLocation; | ||
import net.aufdemrand.denizen.utilities.debugging.dB; | ||
import net.aufdemrand.denizencore.objects.Mechanism; | ||
import net.aufdemrand.denizencore.objects.dList; | ||
import net.aufdemrand.denizencore.objects.dObject; | ||
import net.aufdemrand.denizencore.objects.properties.Property; | ||
import net.aufdemrand.denizencore.tags.Attribute; | ||
import net.aufdemrand.denizencore.utilities.CoreUtilities; | ||
import org.bukkit.entity.ArmorStand; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.util.EulerAngle; | ||
|
||
import java.util.Iterator; | ||
|
||
public class EntityArmorPose implements Property { | ||
|
||
public static boolean describes(dObject entity) { | ||
return entity instanceof dEntity | ||
&& ((dEntity) entity).getBukkitEntityType() == EntityType.ARMOR_STAND; | ||
} | ||
|
||
public static EntityArmorPose getFrom(dObject entity) { | ||
if (!describes(entity)) return null; | ||
|
||
else return new EntityArmorPose((dEntity) entity); | ||
} | ||
|
||
|
||
/////////////////// | ||
// Instance Fields and Methods | ||
///////////// | ||
|
||
private EntityArmorPose(dEntity ent) { | ||
entity = ent; | ||
} | ||
|
||
dEntity entity; | ||
|
||
///////// | ||
// Property Methods | ||
/////// | ||
|
||
@Override | ||
public String getPropertyString() { | ||
return getPoseList().identify(); | ||
} | ||
|
||
@Override | ||
public String getPropertyId() { | ||
return "armor_pose"; | ||
} | ||
|
||
private dList getPoseList() { | ||
ArmorStand armorStand = (ArmorStand) entity.getBukkitEntity(); | ||
dList list = new dList(); | ||
for (PosePart posePart : PosePart.values()) { | ||
list.add(CoreUtilities.toLowerCase(posePart.name())); | ||
list.add(fromEulerAngle(posePart.getAngle(armorStand)).identify()); | ||
} | ||
return list; | ||
} | ||
|
||
/////////// | ||
// dObject Attributes | ||
//////// | ||
|
||
@Override | ||
public String getAttribute(Attribute attribute) { | ||
|
||
if (attribute == null) return "null"; | ||
|
||
// <--[tag] | ||
// @attribute <e@entity.armor_pose_list> | ||
// @returns dList | ||
// @group attributes | ||
// @description | ||
// Returns a list of all poses and angles for the armor stand in the | ||
// format: PART|ANGLE|... | ||
// For example, HEAD|l@12,3,45|BODY|l@54,32,1|etc... | ||
// --> | ||
if (attribute.startsWith("armor_pose_list")) { | ||
return getPoseList().getAttribute(attribute.fulfill(1)); | ||
} | ||
|
||
// <--[tag] | ||
// @attribute <e@entity.armor_pose[<part>]> | ||
// @returns dLocation | ||
// @group attributes | ||
// @description | ||
// Returns the current angle pose for the specified part. | ||
// Valid parts: HEAD, BODY, LEFT_ARM, RIGHT_ARM, LEFT_LEG, RIGHT_LEG | ||
// --> | ||
else if (attribute.startsWith("armor_pose") && attribute.hasContext(1)) { | ||
String name = attribute.getContext(1); | ||
PosePart posePart = PosePart.fromName(name); | ||
if (posePart == null) { | ||
dB.echoError("Invalid pose part specified: " + name); | ||
return null; | ||
} | ||
else { | ||
return fromEulerAngle(posePart.getAngle((ArmorStand) entity.getBukkitEntity())) | ||
.getAttribute(attribute.fulfill(1)); | ||
} | ||
} | ||
|
||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public void adjust(Mechanism mechanism) { | ||
|
||
// <--[mechanism] | ||
// @object dEntity | ||
// @name armor_pose | ||
// @input dList | ||
// @description | ||
// Sets the angle for various parts of the armor stand in the | ||
// format: PART|ANGLE|... | ||
// For example, head|12,3,45|body|54,32,1 | ||
// Valid parts: HEAD, BODY, LEFT_ARM, RIGHT_ARM, LEFT_LEG, RIGHT_LEG | ||
// @tags | ||
// <e@entity.has_ai> | ||
// --> | ||
if (mechanism.matches("armor_pose")) { | ||
ArmorStand armorStand = (ArmorStand) entity.getBukkitEntity(); | ||
dList list = mechanism.getValue().asType(dList.class); | ||
Iterator<String> iterator = list.iterator(); | ||
while (iterator.hasNext()) { | ||
String name = iterator.next(); | ||
PosePart posePart = PosePart.fromName(name); | ||
if (posePart == null) { | ||
dB.echoError("Invalid pose part specified: " + name); | ||
} | ||
else { | ||
posePart.setAngle(armorStand, toEulerAngle(dLocation.valueOf(iterator.next()))); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static dLocation fromEulerAngle(EulerAngle eulerAngle) { | ||
return new dLocation(null, eulerAngle.getX(), eulerAngle.getY(), eulerAngle.getZ()); | ||
} | ||
|
||
private static EulerAngle toEulerAngle(dLocation location) { | ||
return new EulerAngle(location.getX(), location.getY(), location.getZ()); | ||
} | ||
|
||
private enum PosePart { | ||
HEAD { | ||
@Override | ||
EulerAngle getAngle(ArmorStand armorStand) { | ||
return armorStand.getHeadPose(); | ||
} | ||
|
||
@Override | ||
void setAngle(ArmorStand armorStand, EulerAngle eulerAngle) { | ||
armorStand.setHeadPose(eulerAngle); | ||
} | ||
}, | ||
BODY { | ||
@Override | ||
EulerAngle getAngle(ArmorStand armorStand) { | ||
return armorStand.getBodyPose(); | ||
} | ||
|
||
@Override | ||
void setAngle(ArmorStand armorStand, EulerAngle eulerAngle) { | ||
armorStand.setBodyPose(eulerAngle); | ||
} | ||
}, | ||
LEFT_ARM { | ||
@Override | ||
EulerAngle getAngle(ArmorStand armorStand) { | ||
return armorStand.getLeftArmPose(); | ||
} | ||
|
||
@Override | ||
void setAngle(ArmorStand armorStand, EulerAngle eulerAngle) { | ||
armorStand.setLeftArmPose(eulerAngle); | ||
} | ||
}, | ||
RIGHT_ARM { | ||
@Override | ||
EulerAngle getAngle(ArmorStand armorStand) { | ||
return armorStand.getRightArmPose(); | ||
} | ||
|
||
@Override | ||
void setAngle(ArmorStand armorStand, EulerAngle eulerAngle) { | ||
armorStand.setRightArmPose(eulerAngle); | ||
} | ||
}, | ||
LEFT_LEG { | ||
@Override | ||
EulerAngle getAngle(ArmorStand armorStand) { | ||
return armorStand.getLeftLegPose(); | ||
} | ||
|
||
@Override | ||
void setAngle(ArmorStand armorStand, EulerAngle eulerAngle) { | ||
armorStand.setLeftLegPose(eulerAngle); | ||
} | ||
}, | ||
RIGHT_LEG { | ||
@Override | ||
EulerAngle getAngle(ArmorStand armorStand) { | ||
return armorStand.getRightLegPose(); | ||
} | ||
|
||
@Override | ||
void setAngle(ArmorStand armorStand, EulerAngle eulerAngle) { | ||
armorStand.setRightLegPose(eulerAngle); | ||
} | ||
}; | ||
|
||
abstract EulerAngle getAngle(ArmorStand armorStand); | ||
abstract void setAngle(ArmorStand armorStand, EulerAngle eulerAngle); | ||
|
||
static PosePart fromName(String name) { | ||
for (PosePart posePart : PosePart.values()) { | ||
if (posePart.name().equalsIgnoreCase(name)) { | ||
return posePart; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |