Search in sources :

Example 11 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class AssignmentTrait method removeAssignmentScript.

// <--[action]
// @Actions
// remove assignment
// 
// @Triggers when the assignment script is removed from an NPC.
// 
// @Context
// None
// 
// -->
public boolean removeAssignmentScript(String name, PlayerTag player) {
    int index = assignments.indexOf(CoreUtilities.toLowerCase(name));
    if (index == -1) {
        return false;
    }
    assignments.remove(index);
    AssignmentScriptContainer container = containerCache.remove(index);
    if (container != null) {
        Denizen.getInstance().npcHelper.getActionHandler().doAction("remove assignment", new NPCTag(npc), player, container, null);
    }
    return true;
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)

Example 12 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class MobproxTrait method onSpawn.

@Override
public void onSpawn() {
    liveEnt = (LivingEntity) getNPC().getEntity();
    dnpc = new NPCTag(getNPC());
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag)

Example 13 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class PushableTrait method NPCPush.

// <--[action]
// @Actions
// push
// 
// @Triggers when the NPC is pushed by a player
// 
// @Context
// None
// 
// -->
/**
 * Fires an 'On Push:' action upon being pushed.
 */
@EventHandler
public void NPCPush(NPCPushEvent event) {
    if (event.getNPC() == npc && pushable) {
        event.setCancelled(false);
        // On Push action / Push Trigger
        if (System.currentTimeMillis() > pushedTimer) {
            // Get pusher
            Player pusher = null;
            for (Entity le : event.getNPC().getEntity().getNearbyEntities(1, 1, 1)) {
                if (le instanceof Player) {
                    pusher = (Player) le;
                }
            }
            if (pusher != null) {
                new NPCTag(npc).action("push", PlayerTag.mirrorBukkitPlayer(pusher));
                pushedTimer = System.currentTimeMillis() + (delay * 1000);
            }
        }
        // End push action
        if (!pushed && returnable) {
            pushed = true;
            returnLocation = npc.getStoredLocation().clone();
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
                if (npc.isSpawned()) {
                    navigateBack();
                }
            }, delay * 20);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) NPCTag(com.denizenscript.denizen.objects.NPCTag) EventHandler(org.bukkit.event.EventHandler)

Example 14 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class RenameCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final ElementTag name = scriptEntry.getElement("name");
    ElementTag perPlayer = scriptEntry.getElement("per_player");
    ElementTag listNameOnly = scriptEntry.getElement("list_name_only");
    ListTag targets = scriptEntry.getObjectTag("targets");
    List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
    if (perPlayer != null && perPlayer.asBoolean()) {
        NetworkInterceptHelper.enable();
        if (scriptEntry.dbCallShouldDebug()) {
            Debug.report(scriptEntry, getName(), name, targets, perPlayer, listNameOnly, db("for", players));
        }
        for (ObjectTag target : targets.objectForms) {
            EntityTag entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
            if (entity != null) {
                Entity bukkitEntity = entity.getBukkitEntity();
                if (bukkitEntity == null) {
                    Debug.echoError("Invalid entity in rename command.");
                    continue;
                }
                if (name.asString().equals("cancel")) {
                    customNames.remove(bukkitEntity.getUniqueId());
                    if (bukkitEntity.isCustomNameVisible()) {
                        if (players == null) {
                            for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
                                NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, bukkitEntity.getCustomName(), false);
                            }
                        } else {
                            for (PlayerTag player : players) {
                                NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, bukkitEntity.getCustomName(), false);
                            }
                        }
                    } else {
                        bukkitEntity.setCustomNameVisible(true);
                        // Force a metadata update
                        bukkitEntity.setCustomNameVisible(false);
                    }
                } else {
                    final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
                    HashMap<UUID, RenameData> playerToFuncMap = customNames.computeIfAbsent(bukkitEntity.getUniqueId(), k -> new HashMap<>());
                    Function<Player, String> nameGetter = p -> {
                        originalContext.player = new PlayerTag(p);
                        return TagManager.tag(name.asString(), originalContext);
                    };
                    RenameData renamer = new RenameData();
                    renamer.nameFunction = nameGetter;
                    renamer.listOnly = listNameOnly != null && listNameOnly.asBoolean();
                    if (players == null) {
                        playerToFuncMap.put(null, renamer);
                    } else {
                        for (PlayerTag player : players) {
                            playerToFuncMap.put(player.getUUID(), renamer);
                        }
                    }
                    if (players == null) {
                        for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
                            NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, "", renamer.listOnly);
                        }
                    } else {
                        for (PlayerTag player : players) {
                            NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, "", renamer.listOnly);
                        }
                    }
                }
            }
        }
        return;
    }
    String nameString = TagManager.tag(name.asString(), scriptEntry.context);
    if (nameString.length() > 256) {
        nameString = nameString.substring(0, 256);
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("name", nameString), listNameOnly, targets);
    }
    for (ObjectTag target : targets.objectForms) {
        EntityFormObject entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
        if (entity == null) {
            entity = target.asType(NPCTag.class, scriptEntry.context);
        } else {
            entity = ((EntityTag) entity).getDenizenObject();
        }
        if (entity == null) {
            Debug.echoError("Invalid entity in rename command.");
            continue;
        }
        if (entity instanceof NPCTag) {
            NPC npc = ((NPCTag) entity).getCitizen();
            if (npc.isSpawned()) {
                Location prev = npc.getStoredLocation().clone();
                npc.despawn(DespawnReason.PENDING_RESPAWN);
                npc.setName(nameString);
                npc.spawn(prev);
            } else {
                npc.setName(nameString);
            }
        } else if (entity instanceof PlayerTag) {
            if (listNameOnly != null && listNameOnly.asBoolean()) {
                AdvancedTextImpl.instance.setPlayerListName(((PlayerTag) entity).getPlayerEntity(), nameString);
            } else {
                String limitedName = nameString.length() > 16 ? nameString.substring(0, 16) : nameString;
                NMSHandler.getInstance().getProfileEditor().setPlayerName(((PlayerTag) entity).getPlayerEntity(), limitedName);
            }
        } else {
            Entity bukkitEntity = entity.getDenizenEntity().getBukkitEntity();
            customNames.remove(bukkitEntity.getUniqueId());
            bukkitEntity.setCustomName(nameString);
            bukkitEntity.setCustomNameVisible(true);
        }
    }
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) TagManager(com.denizenscript.denizencore.tags.TagManager) AdvancedTextImpl(com.denizenscript.denizen.utilities.AdvancedTextImpl) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Function(java.util.function.Function) Argument(com.denizenscript.denizencore.objects.Argument) NPC(net.citizensnpcs.api.npc.NPC) Location(org.bukkit.Location) EntityFormObject(com.denizenscript.denizen.objects.EntityFormObject) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) NetworkInterceptHelper(com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper) Entity(org.bukkit.entity.Entity) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) UUID(java.util.UUID) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) DespawnReason(net.citizensnpcs.api.event.DespawnReason) NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityFormObject(com.denizenscript.denizen.objects.EntityFormObject) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID) Location(org.bukkit.Location)

Example 15 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class ActionCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ListTag actions = scriptEntry.getObjectTag("actions");
    ListTag context = scriptEntry.getObjectTag("context");
    List<NPCTag> npcs = (List<NPCTag>) scriptEntry.getObject("npcs");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), actions, context, db("npcs", npcs));
    }
    if (context.size() % 2 == 1) {
        // Size is uneven!
        context.add("null");
    }
    // Change the context input to a list of objects
    Map<String, ObjectTag> context_map = new HashMap<>();
    for (int i = 0; i < context.size(); i += 2) {
        context_map.put(context.get(i), ObjectFetcher.pickObjectFor(context.get(i + 1), scriptEntry.getContext()));
    }
    for (NPCTag npc : npcs) {
        for (String action : actions) {
            npc.action(action, Utilities.getEntryPlayer(scriptEntry), context_map);
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Aggregations

NPCTag (com.denizenscript.denizen.objects.NPCTag)72 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)24 EventHandler (org.bukkit.event.EventHandler)22 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)21 Player (org.bukkit.entity.Player)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)12 EntityTag (com.denizenscript.denizen.objects.EntityTag)10 ListTag (com.denizenscript.denizencore.objects.core.ListTag)9 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 HashMap (java.util.HashMap)8 NPC (net.citizensnpcs.api.npc.NPC)8 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)6 Location (org.bukkit.Location)6 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)5 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)5 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Entity (org.bukkit.entity.Entity)5