Search in sources :

Example 1 with LookClose

use of net.citizensnpcs.trait.LookClose 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));
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) ArrayList(java.util.ArrayList) FlagManager(net.aufdemrand.denizen.flags.FlagManager) Pose(net.citizensnpcs.util.Pose) Property(net.aufdemrand.denizencore.objects.properties.Property) Pattern(java.util.regex.Pattern) Anchors(net.citizensnpcs.trait.Anchors) Anchor(net.citizensnpcs.util.Anchor) Poses(net.citizensnpcs.trait.Poses) LookClose(net.citizensnpcs.trait.LookClose) Trait(net.citizensnpcs.api.trait.Trait)

Example 2 with LookClose

use of net.citizensnpcs.trait.LookClose in project Denizen-For-Bukkit by DenizenScript.

the class LookcloseCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    dB.report(scriptEntry, getName(), ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().debug() + aH.debugObj("realistic", scriptEntry.getObject("realistic")) + aH.debugObj("range", scriptEntry.getObject("range")) + aH.debugObj("toggle", scriptEntry.getObject("toggle")));
    // Get the instance of the trait that belongs to the target NPC
    LookClose trait = ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getCitizen().getTrait(LookClose.class);
    // Handle toggle
    if (scriptEntry.hasObject("toggle")) {
        trait.lookClose(scriptEntry.getElement("toggle").asBoolean());
    }
    // Handle realistic
    if (scriptEntry.hasObject("realistic")) {
        trait.setRealisticLooking(true);
    } else {
        trait.setRealisticLooking(false);
    }
    // Handle range
    if (scriptEntry.hasObject("range")) {
        trait.setRange(scriptEntry.getElement("range").asInt());
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) LookClose(net.citizensnpcs.trait.LookClose)

Aggregations

LookClose (net.citizensnpcs.trait.LookClose)2 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)1 FlagManager (net.aufdemrand.denizen.flags.FlagManager)1 Property (net.aufdemrand.denizencore.objects.properties.Property)1 NPC (net.citizensnpcs.api.npc.NPC)1 Trait (net.citizensnpcs.api.trait.Trait)1 Anchors (net.citizensnpcs.trait.Anchors)1 Poses (net.citizensnpcs.trait.Poses)1 Anchor (net.citizensnpcs.util.Anchor)1 Pose (net.citizensnpcs.util.Pose)1