Search in sources :

Example 1 with NPCCondition

use of mc.dragons.core.gameobject.npc.NPCCondition in project DragonsOnline by UniverseCraft.

the class NPCCommand method displayBehavior.

private void displayBehavior(CommandSender sender, String[] args) {
    NPCClass npcClass = lookupNPCClass(sender, args[0]);
    if (npcClass == null)
        return;
    sender.sendMessage(ChatColor.GREEN + "NPC Behaviors:");
    for (NPCTrigger trigger : NPCTrigger.values()) {
        sender.sendMessage(ChatColor.GRAY + "- Trigger: " + trigger);
        int i = 0;
        for (Entry<List<NPCCondition>, List<NPCAction>> entry : npcClass.getConditionalActions(trigger).getConditionals().entrySet()) {
            sender.sendMessage(ChatColor.GRAY + "  - Behavior " + ChatColor.DARK_GREEN + "#" + i + ChatColor.GRAY + ":");
            sender.sendMessage(ChatColor.GRAY + "    - Conditions:");
            int j = 0;
            for (NPCCondition condition : entry.getKey()) {
                sender.sendMessage(ChatColor.DARK_GREEN + "      #" + j + ": " + ChatColor.GRAY + displayNPCCondition(condition));
                j++;
            }
            sender.sendMessage(ChatColor.GRAY + "    - Actions:");
            j = 0;
            for (NPCAction action : entry.getValue()) {
                sender.sendMessage(ChatColor.DARK_GREEN + "      #" + j + ": " + ChatColor.GRAY + displayNPCAction(action));
                j++;
            }
            i++;
        }
    }
}
Also used : NPCClass(mc.dragons.core.gameobject.npc.NPCClass) NPCCondition(mc.dragons.core.gameobject.npc.NPCCondition) NPCTrigger(mc.dragons.core.gameobject.npc.NPCConditionalActions.NPCTrigger) ArrayList(java.util.ArrayList) List(java.util.List) NPCAction(mc.dragons.core.gameobject.npc.NPCAction)

Example 2 with NPCCondition

use of mc.dragons.core.gameobject.npc.NPCCondition in project DragonsOnline by UniverseCraft.

the class NPCCommand method manageBehavior.

private boolean manageBehavior(CommandSender sender, String[] args, List<Document> behaviors, Document conditionals, NPCTrigger trigger, NPCClass npcClass, NPCConditionalActions behaviorsLocal) {
    User user = user(sender);
    Player player = player(sender);
    if (args.length < 6) {
        sender.sendMessage(ChatColor.RED + "Insufficient arguments! For usage info, do /npc");
        return false;
    }
    Integer behaviorNo = parseInt(sender, args[3]);
    if (behaviorNo == null)
        return false;
    Document behavior = behaviors.get(behaviorNo);
    if (args[4].equalsIgnoreCase("condition") || args[4].equalsIgnoreCase("c")) {
        List<Document> conditions = behavior.getList("conditions", Document.class);
        if (args[5].equalsIgnoreCase("add")) {
            if (args.length < 8) {
                sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> condition add <ConditionType> <ConditionParams...>");
                return false;
            }
            boolean inverted = args[6].startsWith("!");
            if (inverted)
                args[6] = args[6].replace("!", "");
            NPCConditionType condType = StringUtil.parseEnum(sender, NPCConditionType.class, args[6]);
            if (condType == null)
                return false;
            NPCCondition cond = null;
            switch(condType) {
                case HAS_COMPLETED_QUEST:
                    cond = NPCCondition.hasCompletedQuest(questLoader.getQuestByName(args[7]), inverted);
                    break;
                case HAS_QUEST_STAGE:
                    Integer stage = parseInt(sender, args[8]);
                    if (stage == null)
                        return false;
                    cond = NPCCondition.hasQuestStage(questLoader.getQuestByName(args[7]), stage, inverted);
                    break;
                case HAS_GOLD:
                    Double qty = parseDouble(sender, args[7]);
                    if (qty == null)
                        return false;
                    cond = NPCCondition.hasGold(qty, inverted);
                    break;
                case HAS_LEVEL:
                    Integer lv = parseInt(sender, args[7]);
                    if (lv == null)
                        return false;
                    cond = NPCCondition.hasLevel(lv, inverted);
                    break;
                default:
                    break;
            }
            conditions.add(cond.toDocument());
            behaviorsLocal.getConditional(behaviorNo).getKey().add(cond);
        } else if (args[6].equalsIgnoreCase("del")) {
            if (args.length < 7) {
                sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> condition <#> del");
                return false;
            }
            Integer index = parseInt(sender, args[5]);
            if (index == null)
                return false;
            conditions.remove((int) index);
            behaviorsLocal.getConditional(behaviorNo).getKey().remove((int) index);
        } else {
            sender.sendMessage(ChatColor.RED + "Invalid behavioral arguments! For usage info, do /npc");
            return false;
        }
        behavior.append("conditions", conditions);
    } else if (args[4].equalsIgnoreCase("action") || args[4].equalsIgnoreCase("a")) {
        List<Document> actions = behavior.getList("actions", Document.class);
        if (args[5].equalsIgnoreCase("shop")) {
            if (args[6].equalsIgnoreCase("add")) {
                if (args.length < 11) {
                    sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action shop add <#> <ItemClass> <Quantity> <CostPer>");
                    return false;
                }
                Integer actionNo = parseInt(sender, args[7]);
                Integer qty = parseInt(sender, args[9]);
                Double cost = parseDouble(sender, args[10]);
                if (actionNo == null || qty == null || cost == null)
                    return false;
                NPCAction action = behaviorsLocal.getConditional(behaviorNo).getValue().get(actionNo);
                action.getShopItems().add(new ShopItem(args[8], qty, cost));
                actions.get(actionNo).putAll(action.toDocument());
                behavior.append("actions", actions);
                npcClass.getStorageAccess().update(new Document("conditionals", conditionals));
                sender.sendMessage(ChatColor.GREEN + "Added item to shop successfully.");
                propagateRevisions(npcClass);
                return true;
            } else if (args[6].equalsIgnoreCase("remove")) {
                if (args.length < 9) {
                    sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action shop remove <Action#> <Item#>");
                    return false;
                }
                Integer actionNo = parseInt(sender, args[7]);
                Integer slotNo = parseInt(sender, args[8]);
                if (actionNo == null || slotNo == null)
                    return false;
                NPCAction action = behaviorsLocal.getConditional(behaviorNo).getValue().get(actionNo);
                if (slotNo >= action.getShopItems().size()) {
                    sender.sendMessage(ChatColor.RED + "Index " + slotNo + " is out of bounds: must be between 0 and " + (action.getShopItems().size() - 1));
                    return true;
                }
                action.getShopItems().remove((int) slotNo);
                actions.get(actionNo).clear();
                actions.get(actionNo).putAll(action.toDocument());
                behavior.append("actions", actions);
                npcClass.getStorageAccess().update(new Document("conditionals", conditionals));
                sender.sendMessage(ChatColor.GREEN + "Removed item from shop successfully.");
                propagateRevisions(npcClass);
                return true;
            }
        } else if (args[5].equalsIgnoreCase("dialogue")) {
            if (args[6].equalsIgnoreCase("add")) {
                if (args.length < 9) {
                    sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action dialogue add <#> <Line|Line|...>");
                    return false;
                }
                Integer actionNo = parseInt(sender, args[7]);
                if (actionNo == null)
                    return false;
                List<String> lines = Arrays.asList(StringUtil.concatArgs(args, 8).replaceAll(Pattern.quote("%PH%"), user.getLocalData().get("placeholder", "(Empty placeholder)")).split(Pattern.quote("|")));
                NPCAction action = behaviorsLocal.getConditional(behaviorNo).getValue().get(actionNo);
                action.getDialogue().addAll(lines);
                actions.get(actionNo).putAll(action.toDocument());
                behavior.append("actions", actions);
                npcClass.getStorageAccess().update(new Document("conditionals", conditionals));
                sender.sendMessage(ChatColor.GREEN + "Added dialogue successfully.");
                propagateRevisions(npcClass);
                return true;
            } else if (args[6].equalsIgnoreCase("remove")) {
                if (args.length < 9) {
                    sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action dialogue remove <Action#> <Line#>");
                    return false;
                }
                Integer actionNo = parseInt(sender, args[7]);
                Integer slotNo = parseInt(sender, args[8]);
                if (actionNo == null || slotNo == null)
                    return false;
                NPCAction action = behaviorsLocal.getConditional(behaviorNo).getValue().get(actionNo);
                if (slotNo >= action.getDialogue().size()) {
                    sender.sendMessage(ChatColor.RED + "Index " + slotNo + " is out of bounds: must be between 0 and " + (action.getDialogue().size() - 1));
                    return true;
                }
                action.getDialogue().remove((int) slotNo);
                actions.get(actionNo).clear();
                actions.get(actionNo).putAll(action.toDocument());
                behavior.append("actions", actions);
                npcClass.getStorageAccess().update(new Document("conditionals", conditionals));
                sender.sendMessage(ChatColor.GREEN + "Removed line from dialogue successfully.");
                propagateRevisions(npcClass);
                return true;
            }
        } else if (args[5].equalsIgnoreCase("add")) {
            NPCActionType actionType = StringUtil.parseEnum(sender, NPCActionType.class, args[6]);
            if (actionType == null)
                return false;
            NPCAction action = null;
            switch(actionType) {
                case BEGIN_DIALOGUE:
                    if (args.length < 8) {
                        sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action add BEGIN_DIALOGUE <DialogueLine1|DialogueLine2|...>");
                        return false;
                    }
                    action = NPCAction.beginDialogue(npcClass, Arrays.asList(StringUtil.concatArgs(args, 7).replaceAll(Pattern.quote("%PH%"), user.getLocalData().get("placeholder", "(Empty placeholder)")).split(Pattern.quote("|"))));
                    break;
                case BEGIN_QUEST:
                    if (args.length < 8) {
                        sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action add BEGIN_QUEST <QuestShortName>");
                        return false;
                    }
                    Quest quest = lookupQuest(sender, args[7]);
                    if (quest == null)
                        return false;
                    action = NPCAction.beginQuest(npcClass, questLoader.getQuestByName(args[7]));
                    break;
                case OPEN_SHOP:
                    if (args.length < 8) {
                        sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action add OPEN_SHOP <Shop Title>");
                        return false;
                    }
                    action = NPCAction.openShop(npcClass, StringUtil.concatArgs(args, 7), new ArrayList<>());
                    break;
                case TELEPORT_NPC:
                    if (!requirePlayer(sender))
                        return false;
                    action = NPCAction.teleportNPC(npcClass, player.getLocation());
                    break;
                case PATHFIND_NPC:
                    if (!requirePlayer(sender))
                        return false;
                    action = NPCAction.pathfindNPC(npcClass, player.getLocation());
                    break;
                default:
                    break;
            }
            actions.add(action.toDocument());
            behaviorsLocal.getConditional(behaviorNo).getValue().add(action);
        } else if (args[6].equalsIgnoreCase("del")) {
            if (args.length < 7) {
                sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc <ClassName> behavior <CLICK|HIT> <#> action <#> del");
                return false;
            }
            Integer index = parseInt(sender, args[5]);
            if (index == null)
                return false;
            actions.remove((int) index);
            behaviorsLocal.getConditional(behaviorNo).getValue().remove((int) index);
        } else {
            sender.sendMessage(ChatColor.RED + "Invalid behavioral arguments! For usage info, do /npc");
            return false;
        }
        behavior.append("actions", actions);
    } else {
        sender.sendMessage(ChatColor.RED + "Invalid behavioral arguments! For usage info, do /npc");
        return false;
    }
    npcClass.getStorageAccess().update(new Document("conditionals", conditionals));
    sender.sendMessage(ChatColor.GREEN + "Updated behavior successfully.");
    propagateRevisions(npcClass);
    return true;
}
Also used : Player(org.bukkit.entity.Player) User(mc.dragons.core.gameobject.user.User) NPCConditionType(mc.dragons.core.gameobject.npc.NPCCondition.NPCConditionType) ShopItem(mc.dragons.core.gameobject.npc.NPCAction.ShopItem) Document(org.bson.Document) NPCActionType(mc.dragons.core.gameobject.npc.NPCAction.NPCActionType) Quest(mc.dragons.core.gameobject.quest.Quest) NPCCondition(mc.dragons.core.gameobject.npc.NPCCondition) ArrayList(java.util.ArrayList) List(java.util.List) NPCAction(mc.dragons.core.gameobject.npc.NPCAction)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 NPCAction (mc.dragons.core.gameobject.npc.NPCAction)2 NPCCondition (mc.dragons.core.gameobject.npc.NPCCondition)2 NPCActionType (mc.dragons.core.gameobject.npc.NPCAction.NPCActionType)1 ShopItem (mc.dragons.core.gameobject.npc.NPCAction.ShopItem)1 NPCClass (mc.dragons.core.gameobject.npc.NPCClass)1 NPCConditionType (mc.dragons.core.gameobject.npc.NPCCondition.NPCConditionType)1 NPCTrigger (mc.dragons.core.gameobject.npc.NPCConditionalActions.NPCTrigger)1 Quest (mc.dragons.core.gameobject.quest.Quest)1 User (mc.dragons.core.gameobject.user.User)1 Document (org.bson.Document)1 Player (org.bukkit.entity.Player)1