Search in sources :

Example 1 with Talkable

use of net.citizensnpcs.api.ai.speech.Talkable in project Citizens2 by CitizensDev.

the class Chat method talk.

@Override
public void talk(SpeechContext context) {
    if (context.getTalker() == null)
        return;
    NPC npc = CitizensAPI.getNPCRegistry().getNPC(context.getTalker().getEntity());
    if (npc == null)
        return;
    // chat to the world with CHAT_FORMAT and CHAT_RANGE settings
    if (!context.hasRecipients()) {
        String text = Setting.CHAT_FORMAT.asString().replace("<npc>", npc.getName()).replace("<text>", context.getMessage());
        talkToBystanders(npc, text, context);
        return;
    } else // Assumed recipients at this point
    if (context.size() <= 1) {
        String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName()).replace("<text>", context.getMessage());
        String targetName = "";
        // For each recipient
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
            targetName = entity.getName();
        }
        // Check if bystanders hear targeted chat
        if (!Setting.CHAT_BYSTANDERS_HEAR_TARGETED_CHAT.asBoolean())
            return;
        // Format message with config setting and send to bystanders
        String bystanderText = Setting.CHAT_FORMAT_TO_BYSTANDERS.asString().replace("<npc>", npc.getName()).replace("<target>", targetName).replace("<text>", context.getMessage());
        talkToBystanders(npc, bystanderText, context);
        return;
    } else {
        // Multiple recipients
        String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName()).replace("<text>", context.getMessage());
        List<String> targetNames = new ArrayList<String>();
        // Talk to each recipient
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
            targetNames.add(entity.getName());
        }
        if (!Setting.CHAT_BYSTANDERS_HEAR_TARGETED_CHAT.asBoolean())
            return;
        String targets = "";
        int max = Setting.CHAT_MAX_NUMBER_OF_TARGETS.asInt();
        String[] format = Setting.CHAT_MULTIPLE_TARGETS_FORMAT.asString().split("\\|");
        if (format.length != 4)
            Messaging.severe("npc.chat.options.multiple-targets-format invalid!");
        if (max == 1) {
            targets = format[0].replace("<target>", targetNames.get(0)) + format[3];
        } else if (max == 2 || targetNames.size() == 2) {
            if (targetNames.size() == 2) {
                targets = format[0].replace("<target>", targetNames.get(0)) + format[2].replace("<target>", targetNames.get(1));
            } else
                targets = format[0].replace("<target>", targetNames.get(0)) + format[1].replace("<target>", targetNames.get(1)) + format[3];
        } else if (max >= 3) {
            targets = format[0].replace("<target>", targetNames.get(0));
            int x = 1;
            for (x = 1; x < max - 1; x++) {
                if (targetNames.size() - 1 == x)
                    break;
                targets = targets + format[1].replace("<npc>", targetNames.get(x));
            }
            if (targetNames.size() == max) {
                targets = targets + format[2].replace("<npc>", targetNames.get(x));
            } else
                targets = targets + format[3];
        }
        String bystanderText = Setting.CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS.asString().replace("<npc>", npc.getName()).replace("<targets>", targets).replace("<text>", context.getMessage());
        talkToBystanders(npc, bystanderText, context);
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) List(java.util.List) ArrayList(java.util.ArrayList) Talkable(net.citizensnpcs.api.ai.speech.Talkable)

Example 2 with Talkable

use of net.citizensnpcs.api.ai.speech.Talkable in project Denizen-For-Bukkit by DenizenScript.

the class DenizenChat method talk.

@Override
public void talk(SpeechContext speechContext) {
    if (!(speechContext instanceof DenizenSpeechContext)) {
        return;
    }
    DenizenSpeechContext context = (DenizenSpeechContext) speechContext;
    Talkable talker = context.getTalker();
    if (talker == null) {
        return;
    }
    ScriptEntry entry = context.getScriptEntry();
    ScriptQueue queue = entry.getResidingQueue();
    String defTalker = null;
    if (queue.hasDefinition("talker")) {
        defTalker = queue.getDefinition("talker");
    }
    queue.addDefinition("talker", new EntityTag(talker.getEntity()).identify());
    String defMessage = null;
    if (queue.hasDefinition("message")) {
        defMessage = queue.getDefinition("message");
    }
    queue.addDefinition("message", context.getMessage());
    // Chat to the world using Denizen chat settings
    if (!context.hasRecipients()) {
        String text = TagManager.tag(Settings.chatNoTargetFormat(), new BukkitTagContext(entry));
        talkToBystanders(talker, text, context);
    } else // Single recipient
    if (context.size() <= 1) {
        // Send chat to target
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        // Check if bystanders hear targeted chat
        if (context.isBystandersEnabled()) {
            String defTarget = null;
            if (queue.hasDefinition("target")) {
                defTarget = queue.getDefinition("target");
            }
            queue.addDefinition("target", new EntityTag(context.iterator().next().getEntity()).identify());
            String bystanderText = TagManager.tag(Settings.chatWithTargetToBystandersFormat(), new BukkitTagContext(entry));
            talkToBystanders(talker, bystanderText, context);
            if (defTarget != null) {
                queue.addDefinition("target", defTarget);
            }
        }
    } else // Multiple recipients
    {
        // Send chat to targets
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        if (context.isBystandersEnabled()) {
            String[] format = Settings.chatMultipleTargetsFormat().split("%target%");
            if (format.length <= 1) {
                Debug.echoError("Invalid 'Commands.Chat.Options.Multiple targets format' in config.yml! Must have at least 1 %target%");
            }
            StringBuilder parsed = new StringBuilder();
            Iterator<Talkable> iter = context.iterator();
            int i = 0;
            while (iter.hasNext()) {
                if (i == format.length - 1) {
                    parsed.append(format[i]);
                    break;
                }
                parsed.append(format[i]).append(new EntityTag(iter.next().getEntity()).getName());
                i++;
            }
            String targets = TagManager.tag(parsed.toString(), new BukkitTagContext(entry));
            String defTargets = null;
            if (queue.hasDefinition("targets")) {
                defTargets = queue.getDefinition("targets");
            }
            queue.addDefinition("targets", targets);
            String bystanderText = TagManager.tag(Settings.chatWithTargetsToBystandersFormat(), new BukkitTagContext(entry));
            talkToBystanders(talker, bystanderText, context);
            if (defTargets != null) {
                queue.addDefinition("targets", defTargets);
            }
        }
    }
    if (defMessage != null) {
        queue.addDefinition("message", defMessage);
    }
    if (defTalker != null) {
        queue.addDefinition("talker", defTalker);
    }
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) Iterator(java.util.Iterator) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) EntityTag(com.denizenscript.denizen.objects.EntityTag) Talkable(net.citizensnpcs.api.ai.speech.Talkable) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 3 with Talkable

use of net.citizensnpcs.api.ai.speech.Talkable in project Denizen-For-Bukkit by DenizenScript.

the class DenizenChat method talk.

@Override
public void talk(SpeechContext speechContext) {
    if (!(speechContext instanceof DenizenSpeechContext)) {
        return;
    }
    DenizenSpeechContext context = (DenizenSpeechContext) speechContext;
    Talkable talker = context.getTalker();
    if (talker == null) {
        return;
    }
    ScriptEntry entry = context.getScriptEntry();
    ScriptQueue queue = entry.getResidingQueue();
    String defTalker = null;
    if (queue.hasDefinition("talker")) {
        defTalker = queue.getDefinition("talker");
    }
    queue.addDefinition("talker", new dEntity(talker.getEntity()).identify());
    String defMessage = null;
    if (queue.hasDefinition("message")) {
        defMessage = queue.getDefinition("message");
    }
    queue.addDefinition("message", context.getMessage());
    // Chat to the world using Denizen chat settings
    if (!context.hasRecipients()) {
        String text = TagManager.tag(Settings.chatNoTargetFormat(), new BukkitTagContext(entry, false));
        talkToBystanders(talker, text, context);
    } else // Single recipient
    if (context.size() <= 1) {
        // Send chat to target
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry, false));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        // Check if bystanders hear targeted chat
        if (context.isBystandersEnabled()) {
            String defTarget = null;
            if (queue.hasDefinition("target")) {
                defTarget = queue.getDefinition("target");
            }
            queue.addDefinition("target", new dEntity(context.iterator().next().getEntity()).identify());
            String bystanderText = TagManager.tag(Settings.chatWithTargetToBystandersFormat(), new BukkitTagContext(entry, false));
            talkToBystanders(talker, bystanderText, context);
            if (defTarget != null) {
                queue.addDefinition("target", defTarget);
            }
        }
    } else // Multiple recipients
    {
        // Send chat to targets
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry, false));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        if (context.isBystandersEnabled()) {
            String[] format = Settings.chatMultipleTargetsFormat().split("%target%");
            if (format.length <= 1) {
                dB.echoError("Invalid 'Commands.Chat.Options.Multiple targets format' in config.yml! Must have at least 1 %target%");
            }
            StringBuilder parsed = new StringBuilder();
            Iterator<Talkable> iter = context.iterator();
            int i = 0;
            while (iter.hasNext()) {
                if (i == format.length) {
                    parsed.append(format[i]);
                    break;
                }
                parsed.append(format[i]).append(new dEntity(iter.next().getEntity()).getName());
                i++;
            }
            String targets = TagManager.tag(parsed.toString(), new BukkitTagContext(entry, false));
            String defTargets = null;
            if (queue.hasDefinition("targets")) {
                defTargets = queue.getDefinition("targets");
            }
            queue.addDefinition("targets", targets);
            String bystanderText = TagManager.tag(Settings.chatWithTargetsToBystandersFormat(), new BukkitTagContext(entry, false));
            talkToBystanders(talker, bystanderText, context);
            if (defTargets != null) {
                queue.addDefinition("targets", defTargets);
            }
        }
    }
    if (defMessage != null) {
        queue.addDefinition("message", defMessage);
    }
    if (defTalker != null) {
        queue.addDefinition("talker", defTalker);
    }
}
Also used : BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Iterator(java.util.Iterator) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) Talkable(net.citizensnpcs.api.ai.speech.Talkable) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Example 4 with Talkable

use of net.citizensnpcs.api.ai.speech.Talkable in project Denizen-For-Bukkit by DenizenScript.

the class DenizenChat method talkToBystanders.

private void talkToBystanders(Talkable talkable, String text, DenizenSpeechContext context) {
    double range = context.getChatRange();
    List<Entity> bystanderEntities = new ArrayList<Entity>();
    if (range == 0D) {
        for (Player player : Bukkit.getServer().getOnlinePlayers()) {
            bystanderEntities.add(player);
        }
    } else {
        bystanderEntities = talkable.getEntity().getNearbyEntities(range, range, range);
    }
    for (Entity bystander : bystanderEntities) {
        boolean shouldTalk = true;
        // Exclude targeted recipients
        if (context.hasRecipients()) {
            for (Talkable target : context) {
                if (target.getEntity().equals(bystander)) {
                    shouldTalk = false;
                    break;
                }
            }
        }
        // talkNear it if 'should_talk'
        if (shouldTalk) {
            new TalkableEntity(bystander).talkNear(context, text, this);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) TalkableEntity(net.citizensnpcs.npc.ai.speech.TalkableEntity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) TalkableEntity(net.citizensnpcs.npc.ai.speech.TalkableEntity) Talkable(net.citizensnpcs.api.ai.speech.Talkable)

Example 5 with Talkable

use of net.citizensnpcs.api.ai.speech.Talkable in project Denizen-For-Bukkit by DenizenScript.

the class DenizenChat method talkToBystanders.

private void talkToBystanders(Talkable talkable, String text, DenizenSpeechContext context) {
    double range = context.getChatRange();
    List<Entity> bystanderEntities;
    if (range == 0D) {
        bystanderEntities = new ArrayList<>(Bukkit.getServer().getOnlinePlayers());
    } else {
        bystanderEntities = talkable.getEntity().getNearbyEntities(range, range, range);
    }
    for (Entity bystander : bystanderEntities) {
        boolean shouldTalk = true;
        // Exclude targeted recipients
        if (context.hasRecipients()) {
            for (Talkable target : context) {
                if (target.getEntity().equals(bystander)) {
                    shouldTalk = false;
                    break;
                }
            }
        }
        // talkNear it if 'should_talk'
        if (shouldTalk) {
            new TalkableEntity(bystander).talkNear(context, text, this);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) TalkableEntity(net.citizensnpcs.npc.ai.speech.TalkableEntity) TalkableEntity(net.citizensnpcs.npc.ai.speech.TalkableEntity) Talkable(net.citizensnpcs.api.ai.speech.Talkable)

Aggregations

Talkable (net.citizensnpcs.api.ai.speech.Talkable)5 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)2 TalkableEntity (net.citizensnpcs.npc.ai.speech.TalkableEntity)2 Entity (org.bukkit.entity.Entity)2 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)1 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)1 ScriptQueue (com.denizenscript.denizencore.scripts.queues.ScriptQueue)1 List (java.util.List)1 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)1 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)1 ScriptQueue (net.aufdemrand.denizencore.scripts.queues.ScriptQueue)1 NPC (net.citizensnpcs.api.npc.NPC)1 Player (org.bukkit.entity.Player)1