use of net.citizensnpcs.api.npc.NPC in project Denizen-For-Bukkit by DenizenScript.
the class dNPC method getAttribute.
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// Defined in dEntity
if (attribute.startsWith("is_npc")) {
return Element.TRUE.getAttribute(attribute.fulfill(1));
}
// Defined in dEntity
if (attribute.startsWith("location") && !isSpawned()) {
return getLocation().getAttribute(attribute.fulfill(1));
}
// Defined in dEntity
if (attribute.startsWith("eye_location")) {
return getEyeLocation().getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("has_nickname")) {
NPC citizen = getCitizen();
return new Element(citizen.hasTrait(NicknameTrait.class) && citizen.getTrait(NicknameTrait.class).hasNickname()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("name.nickname")) {
return new Element(getCitizen().hasTrait(NicknameTrait.class) ? getCitizen().getTrait(NicknameTrait.class).getNickname() : getName()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("name")) {
return new Element(getName()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("list_traits")) {
List<String> list = new ArrayList<String>();
for (Trait trait : getCitizen().getTraits()) {
list.add(trait.getName());
}
return new dList(list).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("has_trait")) {
if (attribute.hasContext(1)) {
Class<? extends Trait> trait = CitizensAPI.getTraitFactory().getTraitClass(attribute.getContext(1));
if (trait != null) {
return new Element(getCitizen().hasTrait(trait)).getAttribute(attribute.fulfill(1));
}
}
}
// -->
if (attribute.startsWith("pushable") || attribute.startsWith("is_pushable")) {
return new Element(getPushableTrait().isPushable()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("has_trigger") && attribute.hasContext(1)) {
if (!getCitizen().hasTrait(TriggerTrait.class)) {
return Element.FALSE.getAttribute(attribute.fulfill(1));
}
TriggerTrait trait = getCitizen().getTrait(TriggerTrait.class);
return new Element(trait.hasTrigger(attribute.getContext(1))).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("anchor.list") || attribute.startsWith("anchors.list")) {
List<String> list = new ArrayList<String>();
for (Anchor anchor : getCitizen().getTrait(Anchors.class).getAnchors()) {
list.add(anchor.getName());
}
return new dList(list).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("has_anchors")) {
return (new Element(getCitizen().getTrait(Anchors.class).getAnchors().size() > 0)).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("anchor")) {
if (attribute.hasContext(1) && getCitizen().getTrait(Anchors.class).getAnchor(attribute.getContext(1)) != null) {
return new dLocation(getCitizen().getTrait(Anchors.class).getAnchor(attribute.getContext(1)).getLocation()).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("has_flag")) {
String flag_name;
if (attribute.hasContext(1)) {
flag_name = attribute.getContext(1);
} else {
return null;
}
return new Element(FlagManager.npcHasFlag(this, flag_name)).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("flag")) {
String flag_name;
if (attribute.hasContext(1)) {
flag_name = attribute.getContext(1);
} else {
return null;
}
if (attribute.getAttribute(2).equalsIgnoreCase("is_expired") || attribute.startsWith("isexpired")) {
return new Element(!FlagManager.npcHasFlag(this, flag_name)).getAttribute(attribute.fulfill(2));
}
if (attribute.getAttribute(2).equalsIgnoreCase("size") && !FlagManager.npcHasFlag(this, flag_name)) {
return new Element(0).getAttribute(attribute.fulfill(2));
}
if (FlagManager.npcHasFlag(this, flag_name)) {
FlagManager.Flag flag = DenizenAPI.getCurrentInstance().flagManager().getNPCFlag(getId(), flag_name);
return new dList(flag.toString(), true, flag.values()).getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute);
}
// -->
if (attribute.startsWith("list_flags")) {
dList allFlags = new dList(DenizenAPI.getCurrentInstance().flagManager().listNPCFlags(getId()));
dList searchFlags = null;
if (!allFlags.isEmpty() && attribute.hasContext(1)) {
searchFlags = new dList();
String search = attribute.getContext(1);
if (search.startsWith("regex:")) {
try {
Pattern pattern = Pattern.compile(search.substring(6), Pattern.CASE_INSENSITIVE);
for (String flag : allFlags) {
if (pattern.matcher(flag).matches()) {
searchFlags.add(flag);
}
}
} catch (Exception e) {
dB.echoError(e);
}
} else {
search = CoreUtilities.toLowerCase(search);
for (String flag : allFlags) {
if (CoreUtilities.toLowerCase(flag).contains(search)) {
searchFlags.add(flag);
}
}
}
}
return searchFlags == null ? allFlags.getAttribute(attribute.fulfill(1)) : searchFlags.getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("constant")) {
if (attribute.hasContext(1)) {
if (getCitizen().hasTrait(ConstantsTrait.class) && getCitizen().getTrait(ConstantsTrait.class).getConstant(attribute.getContext(1)) != null) {
return new Element(getCitizen().getTrait(ConstantsTrait.class).getConstant(attribute.getContext(1))).getAttribute(attribute.fulfill(1));
} else {
return null;
}
}
}
// -->
if (attribute.startsWith("has_pose")) {
if (attribute.hasContext(1)) {
return new Element(getCitizen().getTrait(Poses.class).hasPose(attribute.getContext(1))).getAttribute(attribute.fulfill(1));
} else {
return null;
}
}
// -->
if (attribute.startsWith("pose") || attribute.startsWith("get_pose")) {
if (attribute.hasContext(1)) {
Pose pose = getCitizen().getTrait(Poses.class).getPose(attribute.getContext(1));
return new dLocation(org.bukkit.Bukkit.getWorlds().get(0), 0, 0, 0, pose.getYaw(), pose.getPitch()).getAttribute(attribute.fulfill(1));
} else {
return null;
}
}
// -->
if (attribute.startsWith("engaged") || attribute.startsWith("is_engaged")) {
return new Element(isEngaged()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("invulnerable") || attribute.startsWith("vulnerable")) {
return new Element(getCitizen().data().get(NPC.DEFAULT_PROTECTED_METADATA, true)).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("id")) {
return new Element(getId()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("owner")) {
String owner = getOwner();
dPlayer player = null;
if (!owner.equalsIgnoreCase("server")) {
player = dPlayer.valueOfInternal(owner, false);
}
if (player != null) {
return player.getAttribute(attribute.fulfill(1));
} else {
return new Element(owner).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("has_skin")) {
return new Element(getCitizen().data().has(NPC.PLAYER_SKIN_UUID_METADATA)).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("skin_blob")) {
if (getCitizen().data().has(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_METADATA)) {
String tex = getCitizen().data().get(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_METADATA).toString();
String sign = "";
if (getCitizen().data().has(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_SIGN_METADATA)) {
sign = ";" + getCitizen().data().get(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_SIGN_METADATA).toString();
}
return new Element(tex + sign).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("skin")) {
if (getCitizen().data().has(NPC.PLAYER_SKIN_UUID_METADATA)) {
return new Element(getCitizen().data().get(NPC.PLAYER_SKIN_UUID_METADATA).toString()).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("inventory")) {
return getDenizenInventory().getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("is_spawned")) {
return new Element(isSpawned()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("is_protected")) {
return new Element(getCitizen().isProtected()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("lookclose")) {
NPC citizen = getCitizen();
if (citizen.hasTrait(LookClose.class)) {
// There is no method to check if the NPC has LookClose enabled...
// LookClose.toString() returns "LookClose{" + enabled + "}"
String lookclose = citizen.getTrait(LookClose.class).toString();
lookclose = lookclose.substring(10, lookclose.length() - 1);
return new Element(Boolean.valueOf(lookclose)).getAttribute(attribute.fulfill(1));
}
return Element.FALSE.getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("location.previous_location")) {
return (NPCTags.previousLocations.containsKey(getId()) ? NPCTags.previousLocations.get(getId()).getAttribute(attribute.fulfill(2)) : null);
}
// -->
if (attribute.startsWith("teleport_on_stuck")) {
return new Element(getNavigator().getDefaultParameters().stuckAction() == TeleportStuckAction.INSTANCE).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("has_script")) {
NPC citizen = getCitizen();
return new Element(citizen.hasTrait(AssignmentTrait.class) && citizen.getTrait(AssignmentTrait.class).hasAssignment()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("script")) {
NPC citizen = getCitizen();
if (!citizen.hasTrait(AssignmentTrait.class) || !citizen.getTrait(AssignmentTrait.class).hasAssignment()) {
return null;
} else {
return new dScript(citizen.getTrait(AssignmentTrait.class).getAssignment().getName()).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("navigator.is_navigating")) {
return new Element(getNavigator().isNavigating()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.speed")) {
return new Element(getNavigator().getLocalParameters().speed()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.range")) {
return new Element(getNavigator().getLocalParameters().range()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.attack_range")) {
return new Element(getNavigator().getLocalParameters().attackRange()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.attack_strategy")) {
return new Element(getNavigator().getLocalParameters().attackStrategy().toString()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.speed_modifier")) {
return new Element(getNavigator().getLocalParameters().speedModifier()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.base_speed")) {
return new Element(getNavigator().getLocalParameters().baseSpeed()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.avoid_water")) {
return new Element(getNavigator().getLocalParameters().avoidWater()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.target_location")) {
return (getNavigator().getTargetAsLocation() != null ? new dLocation(getNavigator().getTargetAsLocation()).getAttribute(attribute.fulfill(2)) : null);
}
// -->
if (attribute.startsWith("navigator.is_fighting")) {
return new Element(getNavigator().getEntityTarget() != null && getNavigator().getEntityTarget().isAggressive()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.target_type")) // TODO: IMPROVE
{
return new Element(getNavigator().getTargetType() == null ? "null" : getNavigator().getTargetType().toString()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("navigator.target_entity")) {
return (getNavigator().getEntityTarget() != null && getNavigator().getEntityTarget().getTarget() != null ? new dEntity(getNavigator().getEntityTarget().getTarget()).getAttribute(attribute.fulfill(2)) : null);
}
// -->
if (attribute.startsWith("type")) {
return new Element("NPC").getAttribute(attribute.fulfill(1));
}
// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
if (returned != null) {
return returned;
}
}
return (getEntity() != null ? new dEntity(this).getAttribute(attribute) : new Element(identify()).getAttribute(attribute));
}
use of net.citizensnpcs.api.npc.NPC in project Denizen-For-Bukkit by DenizenScript.
the class dNPC method valueOf.
@Fetchable("n")
public static dNPC valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
////////
// Match NPC id
string = string.toUpperCase().replace("N@", "");
NPC npc;
if (aH.matchesInteger(string)) {
int id = aH.getIntegerFrom(string);
if (dNPCRegistry._isRegistered(id)) {
return dNPCRegistry.getDenizen(id);
}
npc = CitizensAPI.getNPCRegistry().getById(id);
if (npc != null) {
return new dNPC(npc);
}
} else ////////
// Match NPC name
{
for (NPC test : CitizensAPI.getNPCRegistry()) {
if (test.getName().equalsIgnoreCase(string)) {
return new dNPC(test);
}
}
}
return null;
}
use of net.citizensnpcs.api.npc.NPC in project Denizen-For-Bukkit by DenizenScript.
the class EntityInfected method setInfected.
public void setInfected(boolean bool) {
if (bool) {
if (infected.isCitizensNPC()) {
NPC infected_npc = infected.getDenizenNPC().getCitizen();
infected_npc.setBukkitEntityType(EntityType.ZOMBIE_VILLAGER);
} else // TODO: Improve upon.
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_11_R1)) {
LivingEntity entity = infected.getLivingEntity();
// Make a new entity
ZombieVillager infect = (ZombieVillager) entity.getLocation().getWorld().spawnEntity(infected.getLocation(), EntityType.ZOMBIE_VILLAGER);
// Set health
infect.setHealth(entity.getHealth());
// Set equipment
infect.getEquipment().setArmorContents(entity.getEquipment().getArmorContents());
// Remove the Villager
entity.remove();
// Set the dEntity to the new entity
infected.setEntity(infect);
} else // TODO: Improve upon.
if (infected.getBukkitEntity() instanceof Villager) {
Villager villager = (Villager) infected.getBukkitEntity();
// Make a new entity
Zombie infect = (Zombie) villager.getLocation().getWorld().spawnEntity(infected.getLocation(), EntityType.ZOMBIE);
infect.setVillager(true);
// Set health
infect.setHealth(villager.getHealth());
// Set equipment
infect.getEquipment().setArmorContents(villager.getEquipment().getArmorContents());
// Remove the Villager
villager.remove();
// Set the dEntity to the new entity
infected.setEntity(infect);
} else // Much much easier
if (infected.getBukkitEntity() instanceof Zombie) {
((Zombie) infected).setVillager(true);
}
}
}
use of net.citizensnpcs.api.npc.NPC in project Denizen-For-Bukkit by DenizenScript.
the class EntityAge method setAge.
public void setAge(int val) {
if (ageable.isCitizensNPC()) {
NPC ageable_npc = ageable.getDenizenNPC().getCitizen();
ageable_npc.getTrait(Age.class).setAge(val);
} else {
if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE) {
((Zombie) ageable.getBukkitEntity()).setBaby(val >= 0);
} else {
((Ageable) ageable.getBukkitEntity()).setAge(val);
}
}
}
use of net.citizensnpcs.api.npc.NPC in project Denizen-For-Bukkit by DenizenScript.
the class EntityAge method setBaby.
public void setBaby(boolean bool) {
if (ageable.isCitizensNPC()) {
NPC ageable_npc = ageable.getDenizenNPC().getCitizen();
if (!ageable_npc.hasTrait(Age.class)) {
ageable_npc.addTrait(Age.class);
}
ageable_npc.getTrait(Age.class).setAge(bool ? -24000 : 0);
} else {
if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE) {
((Zombie) ageable.getBukkitEntity()).setBaby(bool);
} else if (bool) {
((Ageable) ageable.getBukkitEntity()).setBaby();
} else {
((Ageable) ageable.getBukkitEntity()).setAdult();
}
}
}
Aggregations