Search in sources :

Example 1 with NPC

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

the class QuestAction method execute.

/**
 * Run this action on the specified user.
 *
 * @param user
 * @return The result of running this action.
 */
public QuestActionResult execute(User user) {
    if (quest.isLocked() && !PermissionUtil.verifyActivePermissionLevel(user, PermissionLevel.GM, false))
        return new QuestActionResult(false, false);
    if (action == QuestActionType.TELEPORT_PLAYER) {
        user.getPlayer().teleport(to);
    } else if (action == QuestActionType.SPAWN_NPC) {
        npcClassDeferredLoad();
        World world = user.getPlayer().getWorld();
        NPC npc = npcLoader.registerNew(world, to, npcClass);
        if (phased) {
            npc.phase(user.getPlayer());
        }
        if (!npc.getNPCType().isPersistent()) {
            user.markNPCForCleanup(quest, npc);
        }
        quest.registerNPCReference(user, npc, npcReferenceName);
    } else if (action == QuestActionType.BEGIN_DIALOGUE) {
        npcClassDeferredLoad();
        user.setDialogueBatch(quest, npcClass.getName(), dialogue);
        new BukkitRunnable() {

            @Override
            public void run() {
                if (!user.nextDialogue()) {
                    user.updateQuests(null);
                    cancel();
                }
            }
        }.runTaskTimer(dragons, 0L, 40L);
        return new QuestActionResult(false, true);
    } else if (action == QuestActionType.GIVE_XP) {
        user.getPlayer().sendMessage(ChatColor.GRAY + "+ " + ChatColor.LIGHT_PURPLE + xpAmount + " XP" + ChatColor.GRAY + " from quest " + quest.getQuestName());
        user.addXP(xpAmount);
    } else if (action == QuestActionType.GOTO_STAGE) {
        user.debug("    - going to stage " + stage + " (" + quest.getSteps().get(stage).getStepName() + ")");
        user.updateQuestProgress(quest, quest.getSteps().get(stage), notify);
        return new QuestActionResult(true, false);
    } else if (action == QuestActionType.TELEPORT_NPC) {
        npcClassDeferredLoad();
        NPC npc = quest.getNPCByReference(user, npcReferenceName);
        npc.getEntity().teleport(to);
    } else if (action == QuestActionType.PATHFIND_NPC) {
        npcClassDeferredLoad();
        NPC npc = quest.getNPCByReference(user, npcReferenceName);
        double speed = 0.25D;
        if (npc.getEntity() instanceof Attributable) {
            speed = ((Attributable) npc.getEntity()).getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).getValue();
        }
        boolean hasAI = dragons.getBridge().hasAI(npc.getEntity());
        if (hasAI) {
            dragons.getBridge().setEntityAI(npc.getEntity(), false);
        }
        PathfindingUtil.walkToLocation(npc.getEntity(), to, speed, e -> {
            if (stage != -1) {
                user.updateQuestProgress(quest, quest.getSteps().get(stage), false);
            }
            if (hasAI) {
                dragons.getBridge().setEntityAI(e, true);
            }
        });
        return new QuestActionResult(stage != -1, false);
    } else if (action == QuestActionType.TAKE_ITEM) {
        int remaining = quantity;
        for (ItemStack itemStack : user.getPlayer().getInventory().getContents()) {
            Item item = ItemLoader.fromBukkit(itemStack);
            if (item != null && item.getClassName().equals(itemClass.getClassName())) {
                int removeAmount = Math.min(remaining, item.getQuantity());
                user.takeItem(item, removeAmount, true, true, false);
                user.markItemForCleanup(quest, item, quantity);
                remaining -= item.getQuantity();
                if (remaining <= 0) {
                    user.markItemForCleanup(quest, item, quantity);
                    return new QuestActionResult(false, false);
                }
            }
        }
    } else if (action == QuestActionType.GIVE_ITEM) {
        Item item = itemLoader.registerNew(itemClass);
        item.setQuantity(quantity);
        user.giveItem(item);
        user.markItemForCleanup(quest, item, -quantity);
    } else if (action == QuestActionType.ADD_POTION_EFFECT) {
        user.getPlayer().addPotionEffect(new PotionEffect(effectType, duration, amplifier));
    } else if (action == QuestActionType.REMOVE_POTION_EFFECT) {
        user.getPlayer().removePotionEffect(effectType);
    } else if (action == QuestActionType.COMPLETION_HEADER) {
        new BukkitRunnable() {

            private float pitch = 1.0F;

            @Override
            public void run() {
                user.getPlayer().playSound(user.getPlayer().getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1.0F, pitch);
                pitch = (float) (pitch + 0.05D);
                if (pitch > 2.0F) {
                    cancel();
                }
            }
        }.runTaskTimer(dragons, 0L, 1L);
        user.getPlayer().sendMessage(" ");
        user.getPlayer().sendMessage(ChatColor.GREEN + "" + ChatColor.MAGIC + "ff" + ChatColor.DARK_GREEN + "  Quest Complete: " + quest.getQuestName() + ChatColor.GREEN + "  " + ChatColor.MAGIC + "ff");
        user.getPlayer().sendMessage(ChatColor.GRAY + "Rewards:");
    } else if (action == QuestActionType.WAIT) {
        user.debug("Waiting " + waitTime + "s");
        user.setQuestPaused(quest, true);
        sync(() -> {
            user.debug("Resuming quest actions");
            user.setQuestPaused(quest, false);
            user.updateQuests(null);
        }, 20 * waitTime);
        return new QuestActionResult(false, true);
    } else if (action == QuestActionType.CHOICES) {
        user.getPlayer().sendMessage(" ");
        for (Entry<String, Integer> choice : choices.entrySet()) {
            user.getPlayer().spigot().sendMessage(StringUtil.clickableHoverableText(ChatColor.YELLOW + " • " + ChatColor.GRAY + choice.getKey(), "/questchoice " + quest.getName() + " " + choice.getKey(), ChatColor.YELLOW + "Click to respond", ChatColor.GRAY + "Quest: " + ChatColor.RESET + quest.getQuestName(), ChatColor.GRAY + "Response: " + ChatColor.RESET + choice.getKey()));
        }
        user.getPlayer().sendMessage(" ");
        user.setQuestPaused(quest, true);
        return new QuestActionResult(false, true);
    } else if (action == QuestActionType.FAIL_QUEST) {
        user.getPlayer().sendMessage(ChatColor.RED + "Quest failed: " + quest.getQuestName());
        user.removeQuest(quest);
        return new QuestActionResult(true, false);
    } else if (action == QuestActionType.STASH_ITEMS) {
        user.stashItems(quest, materialType);
    } else if (action == QuestActionType.UNSTASH_ITEMS) {
        user.unstashItems(quest, materialType);
    } else if (action == QuestActionType.SPAWN_RELATIVE) {
        npcClassDeferredLoad();
        Location loc = user.getPlayer().getLocation();
        for (int i = 0; i < quantity; i++) {
            double angle = Math.random() * Math.PI * 2;
            double radius = Math.random() * this.radius;
            double dX = radius * Math.cos(angle);
            double dZ = radius * Math.sin(angle);
            Location spawn = BlockUtil.getAirAboveXZ(loc.clone().add(dX, 0, dZ));
            NPC npc = npcLoader.registerNew(spawn.getWorld(), spawn, npcClass);
            if (phased) {
                npc.phase(user.getPlayer());
            }
            if (!npc.getNPCType().isPersistent()) {
                user.markNPCForCleanup(quest, npc);
            }
        }
    }
    return new QuestActionResult(false, false);
}
Also used : NPC(mc.dragons.core.gameobject.npc.NPC) PotionEffect(org.bukkit.potion.PotionEffect) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) World(org.bukkit.World) Item(mc.dragons.core.gameobject.item.Item) Attributable(org.bukkit.attribute.Attributable) Entry(java.util.Map.Entry) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location)

Example 2 with NPC

use of mc.dragons.core.gameobject.npc.NPC 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)

Example 3 with NPC

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

the class EntityDamageListener method onEntityDamage.

/**
 * Handles damage where the player is not being damaged by another entity.
 *
 * @param event
 */
@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
    LOGGER.trace("Damage event on " + StringUtil.entityToString(event.getEntity()));
    Entity target = event.getEntity();
    if (target.isInvulnerable())
        event.setCancelled(true);
    NPC npc = NPCLoader.fromBukkit(target);
    if (npc != null && npc.isImmortal())
        event.setCancelled(true);
    if (target instanceof Player) {
        User user = UserLoader.fromPlayer((Player) target);
        if (user != null && user.isGodMode())
            event.setCancelled(true);
    }
}
Also used : NPC(mc.dragons.core.gameobject.npc.NPC) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) User(mc.dragons.core.gameobject.user.User) EventHandler(org.bukkit.event.EventHandler)

Example 4 with NPC

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

the class IWannaCompanionCommand method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!requirePlayer(sender) || !requirePermission(sender, PermissionLevel.TESTER))
        return true;
    Player player = player(sender);
    User user = user(sender);
    if (user.getStorageAccess().get("companion") != null) {
        sender.sendMessage("You already have a companion");
        return true;
    }
    NPC companion = GameObjectType.NPC.<NPC, NPCLoader>getLoader().registerNew(player.getLocation(), "Companion-Kitty");
    companion.getStorageAccess().set("companionOwner", user.getUUID());
    user.getStorageAccess().set("companion", companion.getUUID());
    sender.sendMessage("Spawned a companion (companion uuid: " + companion.getUUID() + ")");
    return true;
}
Also used : NPC(mc.dragons.core.gameobject.npc.NPC) Player(org.bukkit.entity.Player) User(mc.dragons.core.gameobject.user.User)

Example 5 with NPC

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

the class SlayCommand method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!requirePlayer(sender) || !requirePermission(sender, PermissionLevel.GM))
        return true;
    if (args.length == 0) {
        sender.sendMessage(ChatColor.RED + "Usage: //slay <radius>");
        return true;
    }
    Integer radius = parseInt(sender, args[0]);
    if (radius == null)
        return true;
    int slain = 0;
    for (Entity e : player(sender).getNearbyEntities(radius, radius, radius)) {
        NPC npc = NPCLoader.fromBukkit(e);
        if (npc == null)
            continue;
        if (npc.getNPCType().isPersistent() || npc.isImmortal())
            continue;
        npc.getEntity().remove();
        slain++;
    }
    sender.sendMessage(ChatColor.GREEN + "Slain " + slain + " entities in radius " + radius);
    return true;
}
Also used : NPC(mc.dragons.core.gameobject.npc.NPC) Entity(org.bukkit.entity.Entity)

Aggregations

NPC (mc.dragons.core.gameobject.npc.NPC)29 Entity (org.bukkit.entity.Entity)16 User (mc.dragons.core.gameobject.user.User)14 Player (org.bukkit.entity.Player)14 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)8 UUID (java.util.UUID)7 Item (mc.dragons.core.gameobject.item.Item)7 GameObject (mc.dragons.core.gameobject.GameObject)6 Location (org.bukkit.Location)6 ArrayList (java.util.ArrayList)5 World (org.bukkit.World)5 LivingEntity (org.bukkit.entity.LivingEntity)5 ItemStack (org.bukkit.inventory.ItemStack)5 List (java.util.List)4 Dragons (mc.dragons.core.Dragons)4 NPCLoader (mc.dragons.core.gameobject.npc.NPCLoader)4 Document (org.bson.Document)4 EventHandler (org.bukkit.event.EventHandler)4 PotionEffect (org.bukkit.potion.PotionEffect)4 Collectors (java.util.stream.Collectors)3