Search in sources :

Example 1 with Property

use of net.aufdemrand.denizencore.objects.properties.Property in project Denizen-For-Bukkit by DenizenScript.

the class dEntity method adjust.

@Override
public void adjust(Mechanism mechanism) {
    if (isGeneric()) {
        mechanisms.add(mechanism);
        return;
    }
    Element value = mechanism.getValue();
    // -->
    if (mechanism.matches("item_in_hand")) {
        NMSHandler.getInstance().getEntityHelper().setItemInHand(getLivingEntity(), value.asType(dItem.class).getItemStack());
    }
    // -->
    if (mechanism.matches("item_in_offhand")) {
        NMSHandler.getInstance().getEntityHelper().setItemInOffHand(getLivingEntity(), value.asType(dItem.class).getItemStack());
    }
    // -->
    if (mechanism.matches("shooter")) {
        setShooter(value.asType(dEntity.class));
    }
    // -->
    if (mechanism.matches("can_pickup_items") && mechanism.requireBoolean()) {
        getLivingEntity().setCanPickupItems(value.asBoolean());
    }
    // -->
    if (mechanism.matches("fall_distance") && mechanism.requireFloat()) {
        entity.setFallDistance(value.asFloat());
    }
    // -->
    if (mechanism.matches("fire_time") && mechanism.requireObject(Duration.class)) {
        entity.setFireTicks(value.asType(Duration.class).getTicksAsInt());
    }
    // -->
    if (mechanism.matches("leash_holder") && mechanism.requireObject(dEntity.class)) {
        getLivingEntity().setLeashHolder(value.asType(dEntity.class).getBukkitEntity());
    }
    // -->
    if (mechanism.matches("can_breed") && mechanism.requireBoolean()) {
        ((Ageable) getLivingEntity()).setBreed(true);
    }
    // -->
    if (mechanism.matches("breed") && mechanism.requireBoolean()) {
        NMSHandler.getInstance().getEntityHelper().setBreeding((Animals) getLivingEntity(), value.asBoolean());
    }
    // -->
    if (mechanism.matches("passenger") && mechanism.requireObject(dEntity.class)) {
        entity.setPassenger(value.asType(dEntity.class).getBukkitEntity());
    }
    // -->
    if (mechanism.matches("time_lived") && mechanism.requireObject(Duration.class)) {
        entity.setTicksLived(value.asType(Duration.class).getTicksAsInt());
    }
    // -->
    if (mechanism.matches("remaining_air") && mechanism.requireInteger()) {
        getLivingEntity().setRemainingAir(value.asInt());
    }
    // -->
    if (mechanism.matches("remove_effects")) {
        for (PotionEffect potionEffect : this.getLivingEntity().getActivePotionEffects()) {
            getLivingEntity().removePotionEffect(potionEffect.getType());
        }
    }
    // -->
    if (mechanism.matches("remove_when_far_away") && mechanism.requireBoolean()) {
        getLivingEntity().setRemoveWhenFarAway(value.asBoolean());
    }
    // -->
    if (mechanism.matches("velocity") && mechanism.requireObject(dLocation.class)) {
        setVelocity(value.asType(dLocation.class).toVector());
    }
    // -->
    if (mechanism.matches("move") && mechanism.requireObject(dLocation.class)) {
        NMSHandler.getInstance().getEntityHelper().move(getBukkitEntity(), value.asType(dLocation.class).toVector());
    }
    // -->
    if (mechanism.matches("interact_with") && mechanism.requireObject(dLocation.class)) {
        dLocation interactLocation = value.asType(dLocation.class);
        NMSHandler.getInstance().getEntityHelper().forceInteraction(getPlayer(), interactLocation);
    }
    // -->
    if (mechanism.matches("play_death")) {
        getLivingEntity().playEffect(EntityEffect.DEATH);
    }
    // -->
    if ((mechanism.matches("pickup_delay") || mechanism.matches("pickupdelay")) && getBukkitEntity() instanceof Item && mechanism.requireObject(Duration.class)) {
        ((Item) getBukkitEntity()).setPickupDelay(value.asType(Duration.class).getTicksAsInt());
    }
    // -->
    if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && mechanism.matches("gliding") && mechanism.requireBoolean()) {
        getLivingEntity().setGliding(value.asBoolean());
    }
    // -->
    if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && mechanism.matches("glowing") && mechanism.requireBoolean()) {
        getLivingEntity().setGlowing(value.asBoolean());
        if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(getLivingEntity())) {
            CitizensAPI.getNPCRegistry().getNPC(getLivingEntity()).data().setPersistent(NPC.GLOWING_METADATA, value.asBoolean());
        }
    }
    // -->
    if (mechanism.matches("dragon_phase")) {
        EnderDragon ed = (EnderDragon) getLivingEntity();
        ed.setPhase(EnderDragon.Phase.valueOf(value.asString().toUpperCase()));
    }
    // -->
    if (mechanism.matches("experience") && getBukkitEntity() instanceof ExperienceOrb && mechanism.requireInteger()) {
        ((ExperienceOrb) getBukkitEntity()).setExperience(value.asInt());
    }
    // Iterate through this object's properties' mechanisms
    for (Property property : PropertyParser.getProperties(this)) {
        property.adjust(mechanism);
        if (mechanism.fulfilled()) {
            break;
        }
    }
    if (!mechanism.fulfilled()) {
        mechanism.reportInvalid();
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) Property(net.aufdemrand.denizencore.objects.properties.Property)

Example 2 with Property

use of net.aufdemrand.denizencore.objects.properties.Property in project Denizen-For-Bukkit by DenizenScript.

the class dEntity method getAttribute.

@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    if (entity == null && entity_type == null) {
        if (npc != null) {
            return new Element(identify()).getAttribute(attribute);
        }
        dB.echoError("dEntity has returned null.");
        return null;
    }
    // -->
    if (attribute.startsWith("debug.log")) {
        dB.log(debug());
        return new Element(Boolean.TRUE.toString()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("debug.no_color")) {
        return new Element(ChatColor.stripColor(debug())).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("debug")) {
        return new Element(debug()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("prefix")) {
        return new Element(prefix).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("type")) {
        return new Element("Entity").getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("entity_type")) {
        return new Element(entity_type.getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_spawned")) {
        return new Element(isSpawned()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("eid")) {
        return new Element(entity.getEntityId()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("uuid")) {
        return new Element(getUUID().toString()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("script")) {
        // TODO: Maybe return legit null?
        return new Element(entityScript == null ? "null" : entityScript).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_flag")) {
        String flag_name;
        if (attribute.hasContext(1)) {
            flag_name = attribute.getContext(1);
        } else {
            return null;
        }
        if (isPlayer() || isCitizensNPC()) {
            dB.echoError("Reading flag for PLAYER or NPC as if it were an ENTITY!");
            return null;
        }
        return new Element(FlagManager.entityHasFlag(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 (isPlayer() || isCitizensNPC()) {
            dB.echoError("Reading flag for PLAYER or NPC as if it were an ENTITY!");
            return null;
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("is_expired") || attribute.startsWith("isexpired")) {
            return new Element(!FlagManager.entityHasFlag(this, flag_name)).getAttribute(attribute.fulfill(2));
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("size") && !FlagManager.entityHasFlag(this, flag_name)) {
            return new Element(0).getAttribute(attribute.fulfill(2));
        }
        if (FlagManager.entityHasFlag(this, flag_name)) {
            FlagManager.Flag flag = DenizenAPI.getCurrentInstance().flagManager().getEntityFlag(this, 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().listEntityFlags(this));
        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 (entity == null) {
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("custom_id")) {
        if (CustomNBT.hasCustomNBT(getLivingEntity(), "denizen-script-id")) {
            return new dScript(CustomNBT.getCustomNBT(getLivingEntity(), "denizen-script-id")).getAttribute(attribute.fulfill(1));
        } else {
            return new Element(entity.getType().name()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("name")) {
        return new Element(getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("saddle")) {
        if (getLivingEntity().getType() == EntityType.HORSE) {
            return new dItem(((Horse) getLivingEntity()).getInventory().getSaddle()).getAttribute(attribute.fulfill(1));
        } else if (getLivingEntity().getType() == EntityType.PIG) {
            return new dItem(((Pig) getLivingEntity()).hasSaddle() ? Material.SADDLE : Material.AIR).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("horse_armor") || attribute.startsWith("horse_armour")) {
        if (getLivingEntity().getType() == EntityType.HORSE) {
            return new dItem(((Horse) getLivingEntity()).getInventory().getArmor()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("has_saddle")) {
        if (getLivingEntity().getType() == EntityType.HORSE) {
            return new Element(((Horse) getLivingEntity()).getInventory().getSaddle().getType() == Material.SADDLE).getAttribute(attribute.fulfill(1));
        } else if (getLivingEntity().getType() == EntityType.PIG) {
            return new Element(((Pig) getLivingEntity()).hasSaddle()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("item_in_hand") || attribute.startsWith("iteminhand")) {
        return new dItem(NMSHandler.getInstance().getEntityHelper().getItemInHand(getLivingEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("item_in_offhand") || attribute.startsWith("iteminoffhand")) {
        return new dItem(NMSHandler.getInstance().getEntityHelper().getItemInOffHand(getLivingEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("map_trace")) {
        EntityHelper.MapTraceResult mtr = NMSHandler.getInstance().getEntityHelper().mapTrace(getLivingEntity(), 200);
        if (mtr != null) {
            double x = 0;
            double y = 0;
            double basex = mtr.hitLocation.getX() - Math.floor(mtr.hitLocation.getX());
            double basey = mtr.hitLocation.getY() - Math.floor(mtr.hitLocation.getY());
            double basez = mtr.hitLocation.getZ() - Math.floor(mtr.hitLocation.getZ());
            if (mtr.angle == BlockFace.NORTH) {
                x = 128f - (basex * 128f);
            } else if (mtr.angle == BlockFace.SOUTH) {
                x = basex * 128f;
            } else if (mtr.angle == BlockFace.WEST) {
                x = basez * 128f;
            } else if (mtr.angle == BlockFace.EAST) {
                x = 128f - (basez * 128f);
            }
            y = 128f - (basey * 128f);
            return new dLocation(null, Math.round(x), Math.round(y)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("can_see")) {
        if (isLivingEntity() && attribute.hasContext(1) && dEntity.matches(attribute.getContext(1))) {
            dEntity toEntity = dEntity.valueOf(attribute.getContext(1));
            if (toEntity != null && toEntity.isSpawned()) {
                return new Element(getLivingEntity().hasLineOfSight(toEntity.getBukkitEntity())).getAttribute(attribute.fulfill(1));
            }
        }
    }
    // -->
    if (attribute.startsWith("eye_location")) {
        return new dLocation(getEyeLocation()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("eye_height")) {
        if (isLivingEntity()) {
            return new Element(getLivingEntity().getEyeHeight()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("location.cursor_on")) {
        int range = attribute.getIntContext(2);
        if (range < 1) {
            range = 50;
        }
        Set<Material> set = new HashSet<Material>();
        set.add(Material.AIR);
        attribute = attribute.fulfill(2);
        if (attribute.startsWith("ignore") && attribute.hasContext(1)) {
            List<dMaterial> ignoreList = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);
            for (dMaterial material : ignoreList) {
                set.add(material.getMaterial());
            }
            attribute = attribute.fulfill(1);
        }
        return new dLocation(getLivingEntity().getTargetBlock(set, range).getLocation()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("location.standing_on")) {
        return new dLocation(entity.getLocation().clone().add(0, -0.5f, 0)).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("location")) {
        return new dLocation(entity.getLocation()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("velocity")) {
        return new dLocation(entity.getVelocity().toLocation(entity.getWorld())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("world")) {
        return new dWorld(entity.getWorld()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("can_pickup_items")) {
        if (isLivingEntity()) {
            return new Element(getLivingEntity().getCanPickupItems()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("fallingblock_material")) {
        return dMaterial.getMaterialFrom(((FallingBlock) entity).getMaterial()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("fall_distance")) {
        return new Element(entity.getFallDistance()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("fire_time")) {
        return new Duration(entity.getFireTicks() / 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("on_fire")) {
        return new Element(entity.getFireTicks() > 0).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("leash_holder") || attribute.startsWith("get_leash_holder")) {
        if (isLivingEntity() && getLivingEntity().isLeashed()) {
            return new dEntity(getLivingEntity().getLeashHolder()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("passenger") || attribute.startsWith("get_passenger")) {
        if (!entity.isEmpty()) {
            return new dEntity(entity.getPassenger()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("shooter") || attribute.startsWith("get_shooter")) {
        if (isProjectile() && hasShooter()) {
            return getShooter().getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("vehicle") || attribute.startsWith("get_vehicle")) {
        if (entity.isInsideVehicle()) {
            return new dEntity(entity.getVehicle()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("has_effect")) {
        boolean returnElement = false;
        if (attribute.hasContext(1)) {
            PotionEffectType effectType = PotionEffectType.getByName(attribute.getContext(1));
            for (org.bukkit.potion.PotionEffect effect : getLivingEntity().getActivePotionEffects()) {
                if (effect.getType().equals(effectType)) {
                    returnElement = true;
                }
            }
        } else if (!getLivingEntity().getActivePotionEffects().isEmpty()) {
            returnElement = true;
        }
        return new Element(returnElement).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("can_breed")) {
        return new Element(((Ageable) getLivingEntity()).canBreed()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("breeding") || attribute.startsWith("is_breeding")) {
        return new Element(NMSHandler.getInstance().getEntityHelper().isBreeding((Animals) getLivingEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_passenger")) {
        return new Element(!entity.isEmpty()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("empty") || attribute.startsWith("is_empty")) {
        return new Element(entity.isEmpty()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("inside_vehicle") || attribute.startsWith("is_inside_vehicle")) {
        return new Element(entity.isInsideVehicle()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("leashed") || attribute.startsWith("is_leashed")) {
        if (isLivingEntity()) {
            return new Element(getLivingEntity().isLeashed()).getAttribute(attribute.fulfill(1));
        } else {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("on_ground") || attribute.startsWith("is_on_ground")) {
        return new Element(entity.isOnGround()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("persistent") || attribute.startsWith("is_persistent")) {
        if (isLivingEntity()) {
            return new Element(!getLivingEntity().getRemoveWhenFarAway()).getAttribute(attribute.fulfill(1));
        } else {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("killer")) {
        return getPlayerFrom(getLivingEntity().getKiller()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("last_damage.amount")) {
        return new Element(getLivingEntity().getLastDamage()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("last_damage.cause") && entity.getLastDamageCause() != null) {
        return new Element(entity.getLastDamageCause().getCause().name()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("last_damage.duration")) {
        return new Duration((long) getLivingEntity().getNoDamageTicks()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("oxygen.max")) {
        return new Duration((long) getLivingEntity().getMaximumAir()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("oxygen")) {
        return new Duration((long) getLivingEntity().getRemainingAir()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("remove_when_far")) {
        return new Element(getLivingEntity().getRemoveWhenFarAway()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("target")) {
        if (getBukkitEntity() instanceof Creature) {
            Entity target = ((Creature) getLivingEntity()).getTarget();
            if (target != null) {
                return new dEntity(target).getAttribute(attribute.fulfill(1));
            }
        }
        return null;
    }
    // -->
    if (attribute.startsWith("time_lived")) {
        return new Duration(entity.getTicksLived() / 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if ((attribute.startsWith("pickup_delay") || attribute.startsWith("pickupdelay")) && getBukkitEntity() instanceof Item) {
        return new Duration(((Item) getBukkitEntity()).getPickupDelay() * 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && attribute.startsWith("gliding")) {
        return new Element(getLivingEntity().isGliding()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && attribute.startsWith("glowing")) {
        return new Element(getLivingEntity().isGlowing()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_living")) {
        return new Element(isLivingEntity()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_mob")) {
        if (!isPlayer() && !isNPC()) {
            return Element.TRUE.getAttribute(attribute.fulfill(1));
        } else {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("is_npc")) {
        return new Element(isCitizensNPC()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_player")) {
        return new Element(isPlayer()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_projectile")) {
        return new Element(isProjectile()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("tameable") || attribute.startsWith("is_tameable")) {
        return new Element(EntityTame.describes(this)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("ageable") || attribute.startsWith("is_ageable")) {
        return new Element(EntityAge.describes(this)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("colorable") || attribute.startsWith("is_colorable")) {
        return new Element(EntityColor.describes(this)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("experience") && getBukkitEntity() instanceof ExperienceOrb) {
        return new Element(((ExperienceOrb) getBukkitEntity()).getExperience()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("dragon_phase") && getBukkitEntity() instanceof EnderDragon) {
        return new Element(((EnderDragon) getLivingEntity()).getPhase().name()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("describe")) {
        String escript = getEntityScript();
        return new Element("e@" + (escript != null && escript.length() > 0 ? escript : getEntityType().getLowercaseName()) + PropertyParser.getPropertiesString(this)).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 new Element(identify()).getAttribute(attribute);
}
Also used : FlagManager(net.aufdemrand.denizen.flags.FlagManager) Property(net.aufdemrand.denizencore.objects.properties.Property) Pattern(java.util.regex.Pattern) PotionEffectType(org.bukkit.potion.PotionEffectType) EntityHelper(net.aufdemrand.denizen.nms.interfaces.EntityHelper) Material(org.bukkit.Material) PotionEffect(org.bukkit.potion.PotionEffect)

Example 3 with Property

use of net.aufdemrand.denizencore.objects.properties.Property in project Denizen-For-Bukkit by DenizenScript.

the class dMaterial method getAttribute.

@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // TODO: Scrap getAttribute, make this functionality a core system
    String attrLow = CoreUtilities.toLowerCase(attribute.getAttributeWithoutContext(1));
    TagRunnable tr = registeredTags.get(attrLow);
    if (tr != null) {
        if (!tr.name.equals(attrLow)) {
            net.aufdemrand.denizencore.utilities.debugging.dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null, "Using deprecated form of tag '" + tr.name + "': '" + attrLow + "'.");
        }
        return tr.run(attribute, this);
    }
    // Iterate through this object's properties' attributes
    for (Property property : PropertyParser.getProperties(this)) {
        String returned = property.getAttribute(attribute);
        if (returned != null) {
            return returned;
        }
    }
    return new Element(identify()).getAttribute(attribute.fulfill(0));
}
Also used : TagRunnable(net.aufdemrand.denizencore.objects.TagRunnable) Element(net.aufdemrand.denizencore.objects.Element) Property(net.aufdemrand.denizencore.objects.properties.Property)

Example 4 with Property

use of net.aufdemrand.denizencore.objects.properties.Property 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 5 with Property

use of net.aufdemrand.denizencore.objects.properties.Property in project Denizen-For-Bukkit by DenizenScript.

the class dPlugin method getAttribute.

/////////////////
// Attributes
/////////
@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // TODO: Scrap getAttribute, make this functionality a core system
    String attrLow = CoreUtilities.toLowerCase(attribute.getAttributeWithoutContext(1));
    TagRunnable tr = registeredTags.get(attrLow);
    if (tr != null) {
        if (!tr.name.equals(attrLow)) {
            net.aufdemrand.denizencore.utilities.debugging.dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null, "Using deprecated form of tag '" + tr.name + "': '" + attrLow + "'.");
        }
        return tr.run(attribute, this);
    }
    // Iterate through this object's properties' attributes
    for (Property property : PropertyParser.getProperties(this)) {
        String returned = property.getAttribute(attribute);
        if (returned != null) {
            return returned;
        }
    }
    return new Element(identify()).getAttribute(attribute);
}
Also used : Property(net.aufdemrand.denizencore.objects.properties.Property)

Aggregations

Property (net.aufdemrand.denizencore.objects.properties.Property)20 ArrayList (java.util.ArrayList)4 Pattern (java.util.regex.Pattern)4 FlagManager (net.aufdemrand.denizen.flags.FlagManager)3 Material (org.bukkit.Material)3 Player (org.bukkit.entity.Player)3 ItemStack (org.bukkit.inventory.ItemStack)3 PotionEffect (org.bukkit.potion.PotionEffect)3 ImprovedOfflinePlayer (net.aufdemrand.denizen.nms.abstracts.ImprovedOfflinePlayer)2 EntityHelper (net.aufdemrand.denizen.nms.interfaces.EntityHelper)2 Entity (org.bukkit.entity.Entity)2 LivingEntity (org.bukkit.entity.LivingEntity)2 CraftingInventory (org.bukkit.inventory.CraftingInventory)2 BlockIterator (org.bukkit.util.BlockIterator)2 File (java.io.File)1 java.util (java.util)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 List (java.util.List)1 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)1