Search in sources :

Example 1 with QuestActionResult

use of mc.dragons.core.gameobject.quest.QuestAction.QuestActionResult in project DragonsOnline by UniverseCraft.

the class QuestTrigger method test.

/**
 * @param user
 * @param event
 * @return Whether to execute the actions associated with this trigger
 */
public boolean test(User user, Event event) {
    if (type == TriggerType.INSTANT) {
        return true;
    } else if (type == TriggerType.NEVER) {
        return false;
    } else if (type == TriggerType.HAS_ITEM || type == TriggerType.HAS_NO_ITEM || type == TriggerType.HAS_LESS_ITEM) {
        int has = 0;
        for (ItemStack itemStack : user.getPlayer().getInventory().getContents()) {
            Item item = ItemLoader.fromBukkit(itemStack);
            if (item != null && item.getClassName().equals(itemClass.getClassName())) {
                has += itemStack.getAmount();
            }
        }
        if (type == TriggerType.HAS_ITEM) {
            return has >= quantity;
        } else if (type == TriggerType.HAS_NO_ITEM) {
            return has == 0;
        } else if (type == TriggerType.HAS_LESS_ITEM) {
            return has < quantity;
        }
    } else if (type == TriggerType.ENTER_REGION) {
        user.updateState(false, false);
        if (user.getRegions().contains(region)) {
            return true;
        }
    } else if (type == TriggerType.WALK_REGION) {
        user.updateState(false, false);
        if (user.getRegions().contains(region) && user.getContinuousWalkDistance(region) >= minDistance) {
            return true;
        }
    } else if (type == TriggerType.EXIT_REGION) {
        user.updateState(false, false);
        if (!user.getRegions().contains(region)) {
            return true;
        }
    } else if (type == TriggerType.CLICK_NPC) {
        npcClassDeferredLoad();
        if (event == null) {
            return false;
        }
        if (event instanceof PlayerInteractEntityEvent) {
            PlayerInteractEntityEvent interactEvent = (PlayerInteractEntityEvent) event;
            NPC npc = NPCLoader.fromBukkit(interactEvent.getRightClicked());
            if (npc == null) {
                return false;
            }
            if (npc.getNPCClass().equals(npcClass)) {
                return true;
            }
        }
    } else if (type == TriggerType.KILL_NPC) {
        npcClassDeferredLoad();
        if (event == null) {
            return false;
        }
        if (event instanceof EntityDeathEvent) {
            EntityDeathEvent deathEvent = (EntityDeathEvent) event;
            NPC npc = NPCLoader.fromBukkit(deathEvent.getEntity());
            if (npc == null) {
                return false;
            }
            if (npc.getNPCClass().equals(npcClass)) {
                killQuantity.put(user, killQuantity.getOrDefault(user, 0) + 1);
                if (killQuantity.getOrDefault(user, 0) >= quantity) {
                    killQuantity.remove(user);
                    return true;
                }
            }
        }
    } else if (type == TriggerType.BRANCH_CONDITIONAL) {
        for (Entry<QuestTrigger, QuestAction> conditional : branchPoints.entrySet()) {
            if (conditional.getKey().test(user, event)) {
                QuestActionResult result = conditional.getValue().execute(user);
                user.debug("   [ - ran branch conditional actions on trigger " + conditional.getKey().getTriggerType() + ", stageModified=" + result.wasStageModified() + ", shouldPause=" + result.shouldPause());
                if (result.shouldPause()) {
                    user.setQuestPaused(quest, true);
                }
                return !result.wasStageModified() && !result.shouldPause();
            }
        }
    }
    return false;
}
Also used : NPC(mc.dragons.core.gameobject.npc.NPC) Item(mc.dragons.core.gameobject.item.Item) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) QuestActionResult(mc.dragons.core.gameobject.quest.QuestAction.QuestActionResult) ItemStack(org.bukkit.inventory.ItemStack) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent)

Aggregations

Item (mc.dragons.core.gameobject.item.Item)1 NPC (mc.dragons.core.gameobject.npc.NPC)1 QuestActionResult (mc.dragons.core.gameobject.quest.QuestAction.QuestActionResult)1 EntityDeathEvent (org.bukkit.event.entity.EntityDeathEvent)1 PlayerInteractEntityEvent (org.bukkit.event.player.PlayerInteractEntityEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1