use of com.denizenscript.denizen.npc.traits.HealthTrait in project Denizen-For-Bukkit by DenizenScript.
the class HealthCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag quantity = scriptEntry.getElement("quantity");
ElementTag action = scriptEntry.getElement("action");
ElementTag heal = scriptEntry.getElement("heal");
List<EntityTag> targets = (List<EntityTag>) scriptEntry.getObject("target");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), quantity, action, heal, db("target", targets));
}
if (quantity == null && action == null) {
Debug.echoError(scriptEntry, "Null quantity!");
}
if (action == null) {
action = new ElementTag(true);
}
for (EntityTag target : targets) {
if (target.isCitizensNPC()) {
if (action.asString().equalsIgnoreCase("true")) {
target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
} else if (action.asString().equalsIgnoreCase("false")) {
target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
} else if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
} else {
target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
}
}
if (quantity != null) {
if (target.isCitizensNPC()) {
if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
HealthTrait trait = target.getDenizenNPC().getCitizen().getOrAddTrait(HealthTrait.class);
trait.setMaxhealth(quantity.asInt());
if (heal.asBoolean()) {
trait.setHealth(quantity.asDouble());
}
} else {
Debug.echoError(scriptEntry, "NPC doesn't have health trait!");
}
} else if (target.isLivingEntity()) {
target.getLivingEntity().setMaxHealth(quantity.asDouble());
if (heal.asBoolean()) {
target.getLivingEntity().setHealth(quantity.asDouble());
}
} else {
Debug.echoError(scriptEntry, "Entity '" + target.identify() + "'is not alive!");
}
}
}
}
Aggregations