Search in sources :

Example 1 with MemoryDataKey

use of net.citizensnpcs.api.util.MemoryDataKey in project CitizensAPI by CitizensDev.

the class AbstractNPC method clone.

@Override
public NPC clone() {
    NPC copy = registry.createNPC(getTrait(MobType.class).getType(), getFullName());
    DataKey key = new MemoryDataKey();
    save(key);
    copy.load(key);
    for (Trait trait : copy.getTraits()) {
        trait.onCopy();
    }
    return copy;
}
Also used : DataKey(net.citizensnpcs.api.util.DataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) Trait(net.citizensnpcs.api.trait.Trait)

Example 2 with MemoryDataKey

use of net.citizensnpcs.api.util.MemoryDataKey in project CitizensAPI by CitizensDev.

the class AbstractNPC method copy.

@Override
public NPC copy() {
    NPC copy = registry.createNPC(getOrAddTrait(MobType.class).getType(), getFullName());
    DataKey key = new MemoryDataKey();
    save(key);
    copy.load(key);
    for (Trait trait : copy.getTraits()) {
        trait.onCopy();
    }
    Bukkit.getPluginManager().callEvent(new NPCCloneEvent(this, copy));
    return copy;
}
Also used : NPCCloneEvent(net.citizensnpcs.api.event.NPCCloneEvent) DataKey(net.citizensnpcs.api.util.DataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) Trait(net.citizensnpcs.api.trait.Trait)

Example 3 with MemoryDataKey

use of net.citizensnpcs.api.util.MemoryDataKey in project Citizens2 by CitizensDev.

the class Template method apply.

@SuppressWarnings("unchecked")
public void apply(NPC npc) {
    MemoryDataKey memoryKey = new MemoryDataKey();
    boolean wasSpawned = npc.isSpawned();
    if (wasSpawned) {
        npc.despawn(DespawnReason.PENDING_RESPAWN);
    }
    npc.save(memoryKey);
    List<Node> queue = Lists.newArrayList(new Node("", replacements));
    for (int i = 0; i < queue.size(); i++) {
        Node node = queue.get(i);
        for (Entry<String, Object> entry : node.map.entrySet()) {
            String fullKey = node.headKey.isEmpty() ? entry.getKey() : node.headKey + '.' + entry.getKey();
            if (entry.getValue() instanceof Map<?, ?>) {
                queue.add(new Node(fullKey, (Map<String, Object>) entry.getValue()));
                continue;
            }
            boolean overwrite = memoryKey.keyExists(fullKey) | override;
            if (!overwrite || fullKey.equals("uuid"))
                continue;
            memoryKey.setRaw(fullKey, entry.getValue());
        }
    }
    npc.load(memoryKey);
    if (wasSpawned && npc.getStoredLocation() != null) {
        npc.spawn(npc.getStoredLocation());
    }
}
Also used : MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) Map(java.util.Map)

Example 4 with MemoryDataKey

use of net.citizensnpcs.api.util.MemoryDataKey in project Denizen-For-Bukkit by DenizenScript.

the class NPCTag method registerTags.

public static void registerTags() {
    AbstractFlagTracker.registerFlagHandlers(tagProcessor);
    // Defined in EntityTag
    tagProcessor.registerTag(ElementTag.class, "is_npc", (attribute, object) -> {
        return new ElementTag(true);
    });
    // Defined in EntityTag
    tagProcessor.registerTag(ObjectTag.class, "location", (attribute, object) -> {
        if (attribute.startsWith("previous_location", 2)) {
            attribute.fulfill(1);
            Deprecations.npcPreviousLocationTag.warn(attribute.context);
            return NPCTagBase.previousLocations.get(object.getId());
        }
        if (object.isSpawned()) {
            return new EntityTag(object).doLocationTag(attribute);
        }
        return object.getLocation();
    });
    // <--[tag]
    // @attribute <NPCTag.previous_location>
    // @returns LocationTag
    // @description
    // Returns the NPC's previous navigated location.
    // -->
    tagProcessor.registerTag(LocationTag.class, "previous_location", (attribute, object) -> {
        return NPCTagBase.previousLocations.get(object.getId());
    });
    // Defined in EntityTag
    tagProcessor.registerTag(LocationTag.class, "eye_location", (attribute, object) -> {
        return object.getEyeLocation();
    });
    // <--[tag]
    // @attribute <NPCTag.has_nickname>
    // @returns ElementTag(Boolean)
    // @description
    // Returns true if the NPC has a nickname.
    // -->
    tagProcessor.registerTag(ElementTag.class, "has_nickname", (attribute, object) -> {
        NPC citizen = object.getCitizen();
        return new ElementTag(citizen.hasTrait(NicknameTrait.class) && citizen.getOrAddTrait(NicknameTrait.class).hasNickname());
    });
    // <--[tag]
    // @attribute <NPCTag.is_sitting>
    // @returns ElementTag(Boolean)
    // @description
    // Returns true if the NPC is sitting. Relates to <@link command sit>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_sitting", (attribute, object) -> {
        NPC citizen = object.getCitizen();
        return new ElementTag(citizen.hasTrait(SittingTrait.class) && citizen.getOrAddTrait(SittingTrait.class).isSitting());
    });
    // <--[tag]
    // @attribute <NPCTag.is_sleeping>
    // @returns ElementTag(Boolean)
    // @description
    // Returns true if the NPC is sleeping. Relates to <@link command sleep>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_sleeping", (attribute, object) -> {
        NPC citizen = object.getCitizen();
        return new ElementTag(citizen.hasTrait(SleepingTrait.class) && citizen.getOrAddTrait(SleepingTrait.class).isSleeping());
    });
    // <--[tag]
    // @attribute <NPCTag.nickname>
    // @returns ElementTag
    // @description
    // Returns the NPC's display name, as set by the Nickname trait (or the default NPC name).
    // -->
    tagProcessor.registerTag(ElementTag.class, "nickname", (attribute, object) -> {
        return new ElementTag(object.getCitizen().hasTrait(NicknameTrait.class) ? object.getCitizen().getOrAddTrait(NicknameTrait.class).getNickname() : object.getName());
    });
    // Documented in EntityTag
    tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> {
        if (attribute.startsWith("nickname", 2)) {
            Deprecations.npcNicknameTag.warn(attribute.context);
            attribute.fulfill(1);
            return new ElementTag(object.getCitizen().hasTrait(NicknameTrait.class) ? object.getCitizen().getOrAddTrait(NicknameTrait.class).getNickname() : object.getName());
        }
        return new ElementTag(object.getName());
    });
    // <--[tag]
    // @attribute <NPCTag.traits>
    // @returns ListTag
    // @description
    // Returns a list of all of the NPC's traits.
    // -->
    tagProcessor.registerTag(ListTag.class, "traits", (attribute, object) -> {
        List<String> list = new ArrayList<>();
        for (Trait trait : object.getCitizen().getTraits()) {
            list.add(trait.getName());
        }
        return new ListTag(list);
    }, "list_traits");
    // <--[tag]
    // @attribute <NPCTag.has_trait[<trait>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC has a specified trait.
    // -->
    tagProcessor.registerTag(ElementTag.class, "has_trait", (attribute, object) -> {
        if (attribute.hasParam()) {
            Class<? extends Trait> trait = CitizensAPI.getTraitFactory().getTraitClass(attribute.getParam());
            if (trait != null) {
                return new ElementTag(object.getCitizen().hasTrait(trait));
            }
        }
        return null;
    });
    // <--[tag]
    // @attribute <NPCTag.pushable>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is pushable.
    // -->
    tagProcessor.registerTag(ElementTag.class, "pushable", (attribute, object) -> {
        return new ElementTag(object.getPushableTrait().isPushable());
    }, "is_pushable");
    // <--[tag]
    // @attribute <NPCTag.has_trigger[<trigger>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC has a specified trigger.
    // -->
    tagProcessor.registerTag(ElementTag.class, "has_trigger", (attribute, object) -> {
        if (!attribute.hasParam()) {
            return null;
        }
        if (!object.getCitizen().hasTrait(TriggerTrait.class)) {
            return new ElementTag(false);
        }
        TriggerTrait trait = object.getCitizen().getOrAddTrait(TriggerTrait.class);
        return new ElementTag(trait.hasTrigger(attribute.getParam()));
    });
    // <--[tag]
    // @attribute <NPCTag.has_anchors>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC has anchors assigned.
    // -->
    tagProcessor.registerTag(ElementTag.class, "has_anchors", (attribute, object) -> {
        return (new ElementTag(object.getCitizen().getOrAddTrait(Anchors.class).getAnchors().size() > 0));
    });
    // <--[tag]
    // @attribute <NPCTag.list_anchors>
    // @returns ListTag
    // @description
    // Returns a list of anchor names currently assigned to the NPC.
    // -->
    tagProcessor.registerTag(ListTag.class, "list_anchors", (attribute, object) -> {
        ListTag list = new ListTag();
        for (Anchor anchor : object.getCitizen().getOrAddTrait(Anchors.class).getAnchors()) {
            list.add(anchor.getName());
        }
        return list;
    });
    // <--[tag]
    // @attribute <NPCTag.anchor[<name>]>
    // @returns LocationTag
    // @description
    // Returns the location associated with the specified anchor, or null if it doesn't exist.
    // -->
    tagProcessor.registerTag(ObjectTag.class, "anchor", (attribute, object) -> {
        Anchors trait = object.getCitizen().getOrAddTrait(Anchors.class);
        if (attribute.hasParam()) {
            Anchor anchor = trait.getAnchor(attribute.getParam());
            if (anchor != null) {
                return new LocationTag(anchor.getLocation());
            } else {
                attribute.echoError("NPC Anchor '" + attribute.getParam() + "' is not defined.");
                return null;
            }
        } else if (attribute.startsWith("list", 2)) {
            attribute.fulfill(1);
            Deprecations.npcAnchorListTag.warn(attribute.context);
            ListTag list = new ListTag();
            for (Anchor anchor : trait.getAnchors()) {
                list.add(anchor.getName());
            }
            return list;
        } else {
            attribute.echoError("npc.anchor[...] tag must have an input.");
        }
        return null;
    }, "anchors");
    // <--[tag]
    // @attribute <NPCTag.constant[<constant_name>]>
    // @returns ElementTag
    // @description
    // Returns the specified constant from the NPC.
    // -->
    tagProcessor.registerTag(ElementTag.class, "constant", (attribute, object) -> {
        if (attribute.hasParam()) {
            if (object.getCitizen().hasTrait(ConstantsTrait.class) && object.getCitizen().getOrAddTrait(ConstantsTrait.class).getConstant(attribute.getParam()) != null) {
                return new ElementTag(object.getCitizen().getOrAddTrait(ConstantsTrait.class).getConstant(attribute.getParam()));
            } else {
                return null;
            }
        }
        return null;
    });
    // <--[tag]
    // @attribute <NPCTag.has_pose[<name>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns true if the NPC has the specified pose, otherwise returns false.
    // -->
    tagProcessor.registerTag(ElementTag.class, "has_pose", (attribute, object) -> {
        if (attribute.hasParam()) {
            return new ElementTag(object.getCitizen().getOrAddTrait(Poses.class).hasPose(attribute.getParam()));
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <NPCTag.pose[<name>]>
    // @returns LocationTag
    // @description
    // Returns the pose as a LocationTag with x, y, and z set to 0, and the world set to the first
    // possible available world Bukkit knows about.
    // -->
    tagProcessor.registerTag(LocationTag.class, "pose", (attribute, object) -> {
        if (attribute.hasParam()) {
            Pose pose = object.getCitizen().getOrAddTrait(Poses.class).getPose(attribute.getParam());
            return new LocationTag(org.bukkit.Bukkit.getWorlds().get(0), 0, 0, 0, pose.getYaw(), pose.getPitch());
        } else {
            return null;
        }
    }, "get_pose");
    // <--[tag]
    // @attribute <NPCTag.name_hologram_npc>
    // @returns NPCTag
    // @description
    // Returns the NPCTag of a hologram attached to this NPC as its nameplate (if any).
    // Note that this can regenerate at any time.
    // -->
    tagProcessor.registerTag(ObjectTag.class, "name_hologram_npc", (attribute, object) -> {
        if (!object.getCitizen().hasTrait(HologramTrait.class)) {
            return null;
        }
        HologramTrait hologram = object.getCitizen().getTraitNullable(HologramTrait.class);
        Entity entity = hologram.getNameEntity();
        if (entity == null) {
            return null;
        }
        return new EntityTag(entity).getDenizenObject();
    });
    // <--[tag]
    // @attribute <NPCTag.hologram_npcs>
    // @returns ListTag(NPCTag)
    // @description
    // Returns the list of hologram NPCs attached to an NPC (if any).
    // Note that these can regenerate at any time.
    // -->
    tagProcessor.registerTag(ListTag.class, "hologram_npcs", (attribute, object) -> {
        if (!object.getCitizen().hasTrait(HologramTrait.class)) {
            return null;
        }
        HologramTrait hologram = object.getCitizen().getTraitNullable(HologramTrait.class);
        Collection<ArmorStand> stands = hologram.getHologramEntities();
        if (stands == null || stands.isEmpty()) {
            return null;
        }
        ListTag output = new ListTag();
        for (ArmorStand stand : stands) {
            output.addObject(new EntityTag(stand).getDenizenObject());
        }
        return output;
    });
    // <--[tag]
    // @attribute <NPCTag.hologram_lines>
    // @returns ListTag
    // @mechanism NPCTag.hologram_lines
    // @description
    // Returns the list of hologram lines attached to an NPC.
    // -->
    tagProcessor.registerTag(ListTag.class, "hologram_lines", (attribute, object) -> {
        if (!object.getCitizen().hasTrait(HologramTrait.class)) {
            return null;
        }
        HologramTrait hologram = object.getCitizen().getTraitNullable(HologramTrait.class);
        return new ListTag(hologram.getLines());
    });
    // <--[tag]
    // @attribute <NPCTag.hologram_direction>
    // @returns ElementTag
    // @mechanism NPCTag.hologram_direction
    // @description
    // Returns the direction of an NPC's hologram as "BOTTOM_UP" or "TOP_DOWN".
    // -->
    tagProcessor.registerTag(ElementTag.class, "hologram_direction", (attribute, object) -> {
        if (!object.getCitizen().hasTrait(HologramTrait.class)) {
            return null;
        }
        HologramTrait hologram = object.getCitizen().getTraitNullable(HologramTrait.class);
        return new ElementTag(hologram.getDirection().name());
    });
    // <--[tag]
    // @attribute <NPCTag.hologram_line_height>
    // @returns ElementTag(Decimal)
    // @mechanism NPCTag.hologram_line_height
    // @description
    // Returns the line height for an NPC's hologram. Can be -1, indicating a default value should be used.
    // -->
    tagProcessor.registerTag(ElementTag.class, "hologram_line_height", (attribute, object) -> {
        if (!object.getCitizen().hasTrait(HologramTrait.class)) {
            return null;
        }
        HologramTrait hologram = object.getCitizen().getTraitNullable(HologramTrait.class);
        return new ElementTag(hologram.getLineHeight());
    });
    // <--[tag]
    // @attribute <NPCTag.is_sneaking>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is currently sneaking. Only works for player-type NPCs.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_sneaking", (attribute, object) -> {
        if (!object.isSpawned() && object.getEntity() instanceof Player) {
            return null;
        }
        return new ElementTag(((Player) object.getEntity()).isSneaking());
    });
    // <--[tag]
    // @attribute <NPCTag.engaged[(<player>)]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is currently engaged.
    // See <@link command engage>
    // -->
    tagProcessor.registerTag(ElementTag.class, "engaged", (attribute, object) -> {
        return new ElementTag(EngageCommand.getEngaged(object.getCitizen(), attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : null));
    }, "is_engaged");
    // <--[tag]
    // @attribute <NPCTag.invulnerable>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is currently invulnerable.
    // See <@link command vulnerable>
    // -->
    tagProcessor.registerTag(ElementTag.class, "invulnerable", (attribute, object) -> {
        return new ElementTag(object.getCitizen().data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
    }, "vulnerable");
    // <--[tag]
    // @attribute <NPCTag.id>
    // @returns ElementTag(Number)
    // @description
    // Returns the NPC's ID number.
    // -->
    tagProcessor.registerTag(ElementTag.class, "id", (attribute, object) -> {
        return new ElementTag(object.getId());
    });
    // <--[tag]
    // @attribute <NPCTag.owner>
    // @returns PlayerTag
    // @mechanism NPCTag.owner
    // @description
    // Returns the owner of the NPC as a PlayerTag, if any.
    // -->
    tagProcessor.registerTag(ObjectTag.class, "owner", (attribute, object) -> {
        UUID owner = object.getOwner();
        if (owner == null) {
            return null;
        }
        OfflinePlayer player = Bukkit.getOfflinePlayer(owner);
        if (player.isOnline() || player.hasPlayedBefore()) {
            return new PlayerTag(player);
        }
        return null;
    });
    // <--[tag]
    // @attribute <NPCTag.has_skin>
    // @returns ElementTag(Boolean)
    // @mechanism NPCTag.skin
    // @description
    // Returns whether the NPC has a custom skin.
    // -->
    tagProcessor.registerTag(ElementTag.class, "has_skin", (attribute, object) -> {
        return new ElementTag(object.getCitizen().hasTrait(SkinTrait.class) && object.getCitizen().getOrAddTrait(SkinTrait.class).getSkinName() != null);
    });
    // <--[tag]
    // @attribute <NPCTag.skin_blob>
    // @returns ElementTag
    // @mechanism NPCTag.skin_blob
    // @description
    // Returns the NPC's custom skin blob, if any.
    // In the format: "texture;signature" (two values separated by a semicolon).
    // See also <@link language Player Entity Skins (Skin Blobs)>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "skin_blob", (attribute, object) -> {
        if (object.getCitizen().hasTrait(SkinTrait.class)) {
            SkinTrait skin = object.getCitizen().getOrAddTrait(SkinTrait.class);
            String tex = skin.getTexture();
            String sign = "";
            if (skin.getSignature() != null) {
                sign = ";" + skin.getSignature();
            }
            return new ElementTag(tex + sign);
        }
        return null;
    });
    // <--[tag]
    // @attribute <NPCTag.skull_skin>
    // @returns ElementTag
    // @description
    // Returns the NPC's current skin blob, formatted for input to a Player Skull item.
    // In the format: "UUID|Texture" (two values separated by pipes).
    // See also <@link language Player Entity Skins (Skin Blobs)>.
    // -->
    tagProcessor.registerTag(ElementTag.class, "skull_skin", (attribute, object) -> {
        if (!object.getCitizen().hasTrait(SkinTrait.class)) {
            return null;
        }
        SkinTrait skin = object.getCitizen().getOrAddTrait(SkinTrait.class);
        return new ElementTag(skin.getSkinName() + "|" + skin.getTexture());
    });
    // <--[tag]
    // @attribute <NPCTag.skin>
    // @returns ElementTag
    // @mechanism NPCTag.skin
    // @description
    // Returns the NPC's custom skin, if any.
    // -->
    tagProcessor.registerTag(ElementTag.class, "skin", (attribute, object) -> {
        if (object.getCitizen().hasTrait(SkinTrait.class)) {
            return new ElementTag(object.getCitizen().getOrAddTrait(SkinTrait.class).getSkinName());
        }
        return null;
    });
    // <--[tag]
    // @attribute <NPCTag.auto_update_skin>
    // @returns ElementTag(Boolean)
    // @mechanism NPCTag.auto_update_skin
    // @description
    // Returns whether the NPC is set to automatically update skins from name.
    // -->
    tagProcessor.registerTag(ElementTag.class, "auto_update_skin", (attribute, object) -> {
        if (object.getCitizen().hasTrait(SkinTrait.class)) {
            return new ElementTag(object.getCitizen().getOrAddTrait(SkinTrait.class).shouldUpdateSkins());
        }
        return null;
    });
    // <--[tag]
    // @attribute <NPCTag.inventory>
    // @returns InventoryTag
    // @description
    // Returns the InventoryTag of the NPC.
    // -->
    tagProcessor.registerTag(InventoryTag.class, "inventory", (attribute, object) -> {
        return object.getDenizenInventory();
    });
    // <--[tag]
    // @attribute <NPCTag.is_spawned>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is spawned.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_spawned", (attribute, object) -> {
        return new ElementTag(object.isSpawned());
    });
    // <--[tag]
    // @attribute <NPCTag.is_protected>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is protected.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_protected", (attribute, object) -> {
        return new ElementTag(object.getCitizen().isProtected());
    });
    // <--[tag]
    // @attribute <NPCTag.lookclose>
    // @returns ElementTag(Boolean)
    // @mechanism NPCTag.lookclose
    // @description
    // Returns whether the NPC has lookclose enabled.
    // -->
    tagProcessor.registerTag(ElementTag.class, "lookclose", (attribute, object) -> {
        NPC citizen = object.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.getOrAddTrait(LookClose.class).toString();
            lookclose = lookclose.substring(10, lookclose.length() - 1);
            return new ElementTag(Boolean.valueOf(lookclose));
        }
        return new ElementTag(false);
    });
    // <--[tag]
    // @attribute <NPCTag.controllable>
    // @returns ElementTag(Boolean)
    // @mechanism NPCTag.controllable
    // @description
    // Returns whether the NPC has controllable enabled.
    // -->
    tagProcessor.registerTag(ElementTag.class, "controllable", (attribute, object) -> {
        if (object.getCitizen().hasTrait(Controllable.class)) {
            return new ElementTag(object.getCitizen().getOrAddTrait(Controllable.class).isEnabled());
        }
        return new ElementTag(false);
    });
    // <--[tag]
    // @attribute <NPCTag.targetable>
    // @returns ElementTag(Boolean)
    // @mechanism NPCTag.targetable
    // @description
    // Returns whether the NPC is targetable.
    // -->
    tagProcessor.registerTag(ElementTag.class, "targetable", (attribute, object) -> {
        boolean targetable = object.getCitizen().data().get(NPC.TARGETABLE_METADATA, object.getCitizen().data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
        return new ElementTag(targetable);
    });
    // <--[tag]
    // @attribute <NPCTag.teleport_on_stuck>
    // @returns ElementTag(Boolean)
    // @mechanism NPCTag.teleport_on_stuck
    // @description
    // Returns whether the NPC teleports when it is stuck.
    // -->
    tagProcessor.registerTag(ElementTag.class, "teleport_on_stuck", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getDefaultParameters().stuckAction() == TeleportStuckAction.INSTANCE);
    });
    tagProcessor.registerTag(ElementTag.class, "has_script", (attribute, object) -> {
        Deprecations.hasScriptTags.warn(attribute.context);
        NPC citizen = object.getCitizen();
        return new ElementTag(citizen.hasTrait(AssignmentTrait.class));
    });
    // <--[tag]
    // @attribute <NPCTag.script>
    // @returns ScriptTag
    // @deprecated Use 'NPCTag.scripts' (plural) instead.
    // @description
    // Deprecated variant of <@link tag NPCTag.scripts>.
    // -->
    tagProcessor.registerTag(ScriptTag.class, "script", (attribute, object) -> {
        Deprecations.npcScriptSingle.warn(attribute.context);
        NPC citizen = object.getCitizen();
        if (!citizen.hasTrait(AssignmentTrait.class)) {
            return null;
        } else {
            for (AssignmentScriptContainer container : citizen.getOrAddTrait(AssignmentTrait.class).containerCache) {
                if (container != null) {
                    return new ScriptTag(container);
                }
            }
            return null;
        }
    });
    // <--[tag]
    // @attribute <NPCTag.scripts>
    // @returns ListTag(ScriptTag)
    // @description
    // Returns a list of all assignment scripts on the NPC. Returns null if none.
    // -->
    tagProcessor.registerTag(ListTag.class, "scripts", (attribute, object) -> {
        NPC citizen = object.getCitizen();
        if (!citizen.hasTrait(AssignmentTrait.class)) {
            return null;
        } else {
            ListTag result = new ListTag();
            for (AssignmentScriptContainer container : citizen.getOrAddTrait(AssignmentTrait.class).containerCache) {
                if (container != null) {
                    result.addObject(new ScriptTag(container));
                }
            }
            return result;
        }
    });
    // <--[tag]
    // @attribute <NPCTag.distance_margin>
    // @returns ElementTag(Decimal)
    // @mechanism NPCTag.distance_margin
    // @description
    // Returns the NPC's current pathfinding distance margin. That is, how close it needs to get to its destination (in block-lengths).
    // -->
    tagProcessor.registerTag(ElementTag.class, "distance_margin", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getDefaultParameters().distanceMargin());
    });
    // <--[tag]
    // @attribute <NPCTag.path_distance_margin>
    // @returns ElementTag(Decimal)
    // @mechanism NPCTag.path_distance_margin
    // @description
    // Returns the NPC's current pathfinding distance margin. That is, how close it needs to get to individual points along its path.
    // -->
    tagProcessor.registerTag(ElementTag.class, "path_distance_margin", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getDefaultParameters().pathDistanceMargin());
    });
    // <--[tag]
    // @attribute <NPCTag.is_navigating>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is currently navigating.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_navigating", (attribute, object) -> {
        return new ElementTag(object.getNavigator().isNavigating());
    });
    // <--[tag]
    // @attribute <NPCTag.speed>
    // @returns ElementTag(Decimal)
    // @mechanism NPCTag.speed
    // @description
    // Returns the current speed of the NPC.
    // -->
    tagProcessor.registerTag(ElementTag.class, "speed", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getLocalParameters().speed());
    });
    // <--[tag]
    // @attribute <NPCTag.range>
    // @returns ElementTag(Decimal)
    // @mechanism NPCTag.range
    // @description
    // Returns the NPC's current maximum pathfinding range.
    // -->
    tagProcessor.registerTag(ElementTag.class, "range", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getLocalParameters().range());
    });
    // <--[tag]
    // @attribute <NPCTag.attack_range>
    // @returns ElementTag(Decimal)
    // @mechanism NPCTag.attack_range
    // @description
    // Returns the NPC's current navigator attack range limit.
    // -->
    tagProcessor.registerTag(ElementTag.class, "attack_range", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getLocalParameters().attackRange());
    });
    // <--[tag]
    // @attribute <NPCTag.attack_strategy>
    // @returns ElementTag
    // @description
    // Returns the NPC's current navigator attack strategy.
    // Not related to Sentinel combat.
    // -->
    tagProcessor.registerTag(ElementTag.class, "attack_strategy", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getLocalParameters().attackStrategy().toString());
    });
    // <--[tag]
    // @attribute <NPCTag.speed_modifier>
    // @returns ElementTag(Decimal)
    // @description
    // Returns the NPC's current movement speed modifier (a multiplier applied over their base speed).
    // -->
    tagProcessor.registerTag(ElementTag.class, "speed_modifier", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getLocalParameters().speedModifier());
    });
    // <--[tag]
    // @attribute <NPCTag.base_speed>
    // @returns ElementTag(Decimal)
    // @description
    // Returns the NPC's base navigation speed.
    // -->
    tagProcessor.registerTag(ElementTag.class, "base_speed", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getLocalParameters().baseSpeed());
    });
    // <--[tag]
    // @attribute <NPCTag.avoid_water>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC will avoid water.
    // -->
    tagProcessor.registerTag(ElementTag.class, "avoid_water", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getLocalParameters().avoidWater());
    });
    // <--[tag]
    // @attribute <NPCTag.target_location>
    // @returns LocationTag
    // @description
    // Returns the location the NPC is currently navigating towards (if any).
    // -->
    tagProcessor.registerTag(LocationTag.class, "target_location", (attribute, object) -> {
        if (object.getNavigator().getTargetAsLocation() == null) {
            return null;
        }
        return new LocationTag(object.getNavigator().getTargetAsLocation());
    });
    // <--[tag]
    // @attribute <NPCTag.navigator_look_at>
    // @returns LocationTag
    // @mechanism NPCTag.navigator_look_at
    // @description
    // Returns the location the NPC will currently look at while moving, if any.
    // -->
    tagProcessor.registerTag(LocationTag.class, "navigator_look_at", (attribute, object) -> {
        if (object.getNavigator().getLocalParameters().lookAtFunction() == null) {
            return null;
        }
        Location res = object.getNavigator().getLocalParameters().lookAtFunction().apply(object.getNavigator());
        if (res == null) {
            return null;
        }
        return new LocationTag(res);
    });
    // <--[tag]
    // @attribute <NPCTag.is_fighting>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the NPC is currently targeting an entity for the Citizens internal punching pathfinder.
    // Not compatible with Sentinel.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_fighting", (attribute, object) -> {
        return new ElementTag(object.getNavigator().getEntityTarget() != null && object.getNavigator().getEntityTarget().isAggressive());
    });
    // <--[tag]
    // @attribute <NPCTag.target_type>
    // @returns ElementTag
    // @description
    // Returns the entity type of the NPC's current navigation target (if any).
    // -->
    tagProcessor.registerTag(ElementTag.class, "target_type", (attribute, object) -> {
        if (object.getNavigator().getTargetType() == null) {
            return null;
        }
        return new ElementTag(object.getNavigator().getTargetType().toString());
    });
    // <--[tag]
    // @attribute <NPCTag.target_entity>
    // @returns EntityTag
    // @description
    // Returns the entity being targeted by the NPC's current navigation (if any).
    // -->
    tagProcessor.registerTag(EntityTag.class, "target_entity", (attribute, object) -> {
        if (object.getNavigator().getEntityTarget() == null || object.getNavigator().getEntityTarget().getTarget() == null) {
            return null;
        }
        return new EntityTag(object.getNavigator().getEntityTarget().getTarget());
    });
    // <--[tag]
    // @attribute <NPCTag.registry_name>
    // @returns ElementTag
    // @description
    // Returns the name of the registry this NPC came from.
    // -->
    tagProcessor.registerTag(ElementTag.class, "registry_name", (attribute, object) -> {
        return new ElementTag(object.getCitizen().getOwningRegistry().getName());
    });
    // <--[tag]
    // @attribute <NPCTag.citizens_data[<key>]>
    // @returns ElementTag
    // @description
    // Returns the value of a Citizens NPC metadata key.
    // -->
    tagProcessor.registerTag(ElementTag.class, "citizens_data", (attribute, object) -> {
        if (!attribute.hasParam()) {
            return null;
        }
        Object val = object.getCitizen().data().get(attribute.getParam());
        if (val == null) {
            return null;
        }
        return new ElementTag(val.toString());
    });
    // <--[tag]
    // @attribute <NPCTag.citizens_data_keys>
    // @returns ListTag
    // @description
    // Returns a list of Citizens NPC metadata keys.
    // -->
    tagProcessor.registerTag(ListTag.class, "citizens_data_keys", (attribute, object) -> {
        DataKey holder = new MemoryDataKey();
        object.getCitizen().data().saveTo(holder);
        ListTag result = new ListTag();
        for (DataKey key : holder.getSubKeys()) {
            result.addObject(new ElementTag(key.name(), true));
        }
        return result;
    });
    tagProcessor.registerTag(NPCTag.class, "navigator", (attribute, object) -> {
        Deprecations.oldNPCNavigator.warn(attribute.context);
        return object;
    });
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) SkinnableEntity(net.citizensnpcs.npc.skin.SkinnableEntity) DataKey(net.citizensnpcs.api.util.DataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer) Pose(net.citizensnpcs.util.Pose) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Anchor(net.citizensnpcs.util.Anchor) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) FlaggableObject(com.denizenscript.denizencore.flags.FlaggableObject) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Trait(net.citizensnpcs.api.trait.Trait)

Example 5 with MemoryDataKey

use of net.citizensnpcs.api.util.MemoryDataKey in project CitizensAPI by CitizensDev.

the class PersistenceLoaderTest method testSuperClass.

@Test
public void testSuperClass() {
    root.setInt("integer", 5);
    root.setString("superclass", "test");
    SuperclassTest test = PersistenceLoader.load(SuperclassTest.class, root);
    assertEquals(test.superclass, "test");
    assertEquals(test.integer, 5);
    root = new MemoryDataKey();
    PersistenceLoader.save(test, root);
    assertEquals(root.getString("superclass"), "test");
    assertEquals(root.getInt("integer"), 5);
}
Also used : MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) Test(org.junit.Test)

Aggregations

MemoryDataKey (net.citizensnpcs.api.util.MemoryDataKey)5 Trait (net.citizensnpcs.api.trait.Trait)3 DataKey (net.citizensnpcs.api.util.DataKey)3 AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)1 FlaggableObject (com.denizenscript.denizencore.flags.FlaggableObject)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 Map (java.util.Map)1 NPCCloneEvent (net.citizensnpcs.api.event.NPCCloneEvent)1 NPC (net.citizensnpcs.api.npc.NPC)1 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)1 Anchor (net.citizensnpcs.util.Anchor)1 Pose (net.citizensnpcs.util.Pose)1 Test (org.junit.Test)1