Search in sources :

Example 1 with Item

use of mc.dragons.core.gameobject.item.Item 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 Item

use of mc.dragons.core.gameobject.item.Item 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 Item

use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.

the class PlayerEventListeners method onItemHeldChange.

@EventHandler
public void onItemHeldChange(PlayerItemHeldEvent event) {
    LOGGER.trace("Item held change event on " + event.getPlayer().getName() + ": " + event.getPreviousSlot() + " -> " + event.getNewSlot());
    ItemStack held = event.getPlayer().getInventory().getItemInMainHand();
    Item heldItem = ItemLoader.fromBukkit(held);
    if (heldItem != null && !heldItem.hasCooldownRemaining()) {
        event.getPlayer().getInventory().setItemInMainHand(heldItem.localRename(heldItem.getDecoratedName()));
    }
}
Also used : Item(mc.dragons.core.gameobject.item.Item) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Item

use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.

the class PlayerEventListeners method onPickupItem.

@EventHandler
public void onPickupItem(EntityPickupItemEvent event) {
    if (event.getEntityType() != EntityType.PLAYER)
        return;
    final Player player = (Player) event.getEntity();
    ItemStack pickup = event.getItem().getItemStack();
    User user = UserLoader.fromPlayer(player);
    final Item item = ItemLoader.fromBukkit(pickup);
    if (item == null)
        return;
    LOGGER.trace("Pickup item event on " + player.getName() + " of " + (item == null ? "null" : item.getIdentifier()) + " (x" + pickup.getAmount() + ")");
    if (item.getItemClass().getClassName().equals(GOLD_CURRENCY_ITEM_CLASS_NAME)) {
        int amount = pickup.getAmount();
        user.giveGold(amount * 1.0D);
        sync(() -> Arrays.asList(player.getInventory().getContents()).stream().filter(i -> (i != null)).filter(i -> (i.getItemMeta() != null)).map(i -> ItemLoader.fromBukkit(i)).filter(Objects::nonNull).filter(i -> i.getClassName().equals(GOLD_CURRENCY_ITEM_CLASS_NAME)).forEach(i -> {
            player.getInventory().remove(i.getItemStack());
            plugin.getGameObjectRegistry().removeFromDatabase(item);
        }));
        player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_HARP, 1.0F, 1.3F);
        return;
    }
    item.setQuantity(pickup.getAmount());
    user.giveItem(item, true, false, false);
    player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1.0F, 1.0F);
    event.setCancelled(true);
    event.getItem().remove();
}
Also used : NPCLoader(mc.dragons.core.gameobject.npc.NPCLoader) Arrays(java.util.Arrays) UserHookRegistry(mc.dragons.core.gameobject.user.UserHookRegistry) PermissionUtil(mc.dragons.core.util.PermissionUtil) Event(org.bukkit.event.Event) User(mc.dragons.core.gameobject.user.User) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) HashBasedTable(com.google.common.collect.HashBasedTable) Player(org.bukkit.entity.Player) EventHandler(org.bukkit.event.EventHandler) ItemLoader(mc.dragons.core.gameobject.item.ItemLoader) Block(org.bukkit.block.Block) PermissionLevel(mc.dragons.core.gameobject.user.permission.PermissionLevel) Map(java.util.Map) Material(org.bukkit.Material) ItemClass(mc.dragons.core.gameobject.item.ItemClass) Sign(org.bukkit.block.Sign) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) DragonsLogger(mc.dragons.core.logging.DragonsLogger) StringUtil(mc.dragons.core.util.StringUtil) UserLoader(mc.dragons.core.gameobject.user.UserLoader) Entity(org.bukkit.entity.Entity) FloorLoader(mc.dragons.core.gameobject.floor.FloorLoader) ItemConstants(mc.dragons.core.gameobject.item.ItemConstants) UUID(java.util.UUID) Sound(org.bukkit.Sound) Item(mc.dragons.core.gameobject.item.Item) EntityType(org.bukkit.entity.EntityType) ItemClassLoader(mc.dragons.core.gameobject.item.ItemClassLoader) ItemStack(org.bukkit.inventory.ItemStack) Objects(java.util.Objects) Floor(mc.dragons.core.gameobject.floor.Floor) List(java.util.List) PlayerDeathEvent(org.bukkit.event.entity.PlayerDeathEvent) PlayerQuitEvent(org.bukkit.event.player.PlayerQuitEvent) BukkitUtil.sync(mc.dragons.core.util.BukkitUtil.sync) PlayerGameModeChangeEvent(org.bukkit.event.player.PlayerGameModeChangeEvent) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Action(org.bukkit.event.block.Action) EntityPickupItemEvent(org.bukkit.event.entity.EntityPickupItemEvent) UserHook(mc.dragons.core.gameobject.user.UserHook) ArrayList(java.util.ArrayList) GameMode(org.bukkit.GameMode) GameObjectType(mc.dragons.core.gameobject.GameObjectType) EntityShootBowEvent(org.bukkit.event.entity.EntityShootBowEvent) PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) PlayerPickupArrowEvent(org.bukkit.event.player.PlayerPickupArrowEvent) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Listener(org.bukkit.event.Listener) PlayerDropItemEvent(org.bukkit.event.player.PlayerDropItemEvent) NPCTrigger(mc.dragons.core.gameobject.npc.NPCConditionalActions.NPCTrigger) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent) PlayerItemHeldEvent(org.bukkit.event.player.PlayerItemHeldEvent) Dragons(mc.dragons.core.Dragons) Consumer(java.util.function.Consumer) FoodLevelChangeEvent(org.bukkit.event.entity.FoodLevelChangeEvent) ChatColor(org.bukkit.ChatColor) Table(com.google.common.collect.Table) NPC(mc.dragons.core.gameobject.npc.NPC) BukkitUtil.rollingAsync(mc.dragons.core.util.BukkitUtil.rollingAsync) Item(mc.dragons.core.gameobject.item.Item) Player(org.bukkit.entity.Player) User(mc.dragons.core.gameobject.user.User) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 5 with Item

use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.

the class PlayerEventListeners method onDropItem.

/**
 * FIXME Drop quantity sometimes calculated incorrectly when iterated
 *
 * @param event
 */
@EventHandler
public void onDropItem(PlayerDropItemEvent event) {
    ItemStack drop = event.getItemDrop().getItemStack();
    int amt = drop.getAmount();
    Item item = ItemLoader.fromBukkit(drop);
    LOGGER.trace("Drop item event on " + event.getPlayer().getName() + " of " + (item == null ? "null" : item.getIdentifier()) + " (x" + amt + ")");
    if (item == null) {
        return;
    }
    User user = UserLoader.fromPlayer(event.getPlayer());
    user.debug("held=" + item.getQuantity() + "=" + item.getItemStack().getAmount());
    if (item.isUndroppable() && event.getPlayer().getGameMode() != GameMode.CREATIVE) {
        user.sendActionBar(ChatColor.DARK_RED + "You can't drop this item!");
        event.setCancelled(true);
        return;
    }
    Item dropItem = itemLoader.registerNew(item);
    dropItem.setQuantity(amt);
    user.debug("drop=" + dropItem.getQuantity() + "=" + dropItem.getItemStack().getAmount());
    event.getItemDrop().setItemStack(dropItem.getItemStack());
    user.takeItem(item, amt, true, true, true);
}
Also used : Item(mc.dragons.core.gameobject.item.Item) User(mc.dragons.core.gameobject.user.User) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Item (mc.dragons.core.gameobject.item.Item)33 ItemStack (org.bukkit.inventory.ItemStack)19 User (mc.dragons.core.gameobject.user.User)15 Player (org.bukkit.entity.Player)11 EventHandler (org.bukkit.event.EventHandler)11 NPC (mc.dragons.core.gameobject.npc.NPC)9 Document (org.bson.Document)9 UUID (java.util.UUID)6 ArrayList (java.util.ArrayList)5 Entity (org.bukkit.entity.Entity)5 List (java.util.List)4 Dragons (mc.dragons.core.Dragons)4 GameObjectType (mc.dragons.core.gameobject.GameObjectType)4 ItemClass (mc.dragons.core.gameobject.item.ItemClass)4 ChatColor (org.bukkit.ChatColor)4 Location (org.bukkit.Location)4 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Floor (mc.dragons.core.gameobject.floor.Floor)3