Search in sources :

Example 21 with Item

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

the class PlayerEventListeners method onPickupArrow.

@SuppressWarnings("deprecation")
@EventHandler
public void onPickupArrow(PlayerPickupArrowEvent event) {
    Item arrow = arrowsByEntity.get(event.getArrow().getEntityId());
    if (arrow == null)
        return;
    LOGGER.trace("Pickup arrow event on " + event.getPlayer().getName() + " of " + arrow.getIdentifier());
    event.setCancelled(true);
    event.getItem().remove();
    UserLoader.fromPlayer(event.getPlayer()).giveItem(arrow);
}
Also used : Item(mc.dragons.core.gameobject.item.Item) EventHandler(org.bukkit.event.EventHandler)

Example 22 with Item

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

the class PlayerEventListeners method onShootBow.

@EventHandler
public void onShootBow(EntityShootBowEvent event) {
    if (event.getEntityType() != EntityType.PLAYER)
        return;
    Player player = (Player) event.getEntity();
    User user = UserLoader.fromPlayer(player);
    Item bow = ItemLoader.fromBukkit(event.getBow());
    if (bow == null)
        return;
    LOGGER.debug("Shoot bow event on " + player.getName() + " with " + bow.getIdentifier());
    if (!bow.getItemClass().checkCanUse(user, true)) {
        event.setCancelled(true);
        event.setConsumeItem(false);
        user.getPlayer().updateInventory();
        return;
    }
    Item arrow = ItemLoader.fromBukkit(event.getConsumable());
    Item link = itemLoader.registerNew(arrow);
    arrow.setQuantity(arrow.getQuantity() - 1);
    link.setQuantity(1);
    link.updateItemStackData();
    user.getPlayer().updateInventory();
    if (arrow != null) {
        arrowsByEntity.put(event.getProjectile().getEntityId(), link);
        event.getProjectile().setMetadata("allow", new FixedMetadataValue(plugin, true));
    }
}
Also used : Item(mc.dragons.core.gameobject.item.Item) Player(org.bukkit.entity.Player) User(mc.dragons.core.gameobject.user.User) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) EventHandler(org.bukkit.event.EventHandler)

Example 23 with Item

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

the class PlayerEventListeners method onClick.

@EventHandler
public void onClick(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    User user = UserLoader.fromPlayer(player);
    Action action = event.getAction();
    if ((action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) && event.getPlayer().getInventory().getItemInMainHand().getType() == Material.MAP) {
        // TODO do we need this? (changing from EMPTY_MAP [1.12] to MAP [1.16]
        user.debug("Right click with map, denying");
        event.setUseItemInHand(Event.Result.DENY);
        event.setCancelled(true);
    }
    if (action == Action.RIGHT_CLICK_BLOCK) {
        user.debug("Right click block");
        Block clicked = event.getClickedBlock();
        if (clicked.getType().toString().toUpperCase().contains("SIGN")) {
            Sign sign = (Sign) clicked.getState();
            if (sign.getLine(0).equals("[RIGHT CLICK]")) {
                if (sign.getLine(2).equals("Join as player")) {
                    if (user.getSystemProfile() != null) {
                        user.setSystemProfile(null);
                    }
                    player.sendMessage(ChatColor.GREEN + "Joining as a player. You can always sign in to your system profile later.");
                    player.teleport(user.getSavedLocation());
                    user.handleJoin(false);
                } else if (sign.getLine(2).equals("Join as staff")) {
                    if (user.getSystemProfile() == null) {
                        player.sendMessage(ChatColor.RED + "You must be logged in to your system profile to join as staff!");
                        return;
                    }
                    if (sign.getLine(3).equals("Vanished (Mod+)")) {
                        if (!PermissionUtil.verifyActivePermissionLevel(user, PermissionLevel.MODERATOR, true)) {
                            return;
                        }
                        user.setVanished(true);
                    }
                    player.teleport(user.getSavedStaffLocation());
                    user.handleJoin(false);
                } else {
                    player.sendMessage(ChatColor.RED + "I don't know what to do with this!");
                }
            }
        }
        return;
    }
    ItemStack heldItem = player.getInventory().getItemInMainHand();
    if (heldItem == null) {
        return;
    }
    Item item = ItemLoader.fromBukkit(heldItem);
    if (item == null) {
        return;
    }
    if (action == Action.LEFT_CLICK_AIR) {
        user.debug("Left click with " + item.getName());
        item.getItemClass().handleLeftClick(user);
    } else if (action == Action.RIGHT_CLICK_AIR) {
        user.debug("Right click with " + item.getName());
        item.getItemClass().handleRightClick(user);
    }
}
Also used : Item(mc.dragons.core.gameobject.item.Item) Player(org.bukkit.entity.Player) Action(org.bukkit.event.block.Action) User(mc.dragons.core.gameobject.user.User) Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 24 with Item

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

the class EntityDamageListener method onEntityDamage.

/**
 * Handles player-vs-player, player-vs-entity, and entity-vs-player interactions.
 * Calculates damage amounts and applies any special damage effects.
 *
 * @param event
 */
@EventHandler
public void onEntityDamage(EntityDamageByEntityEvent event) {
    LOGGER.trace("Damage event on " + StringUtil.entityToString(event.getEntity()) + " by " + StringUtil.entityToString(event.getDamager()));
    Entity damager = event.getDamager();
    User userDamager = null;
    NPC npcDamager = null;
    Item attackerHeldItem = null;
    if (damager instanceof Player) {
        if (!dragons.getEntityHider().canSee((Player) damager, event.getEntity())) {
            event.setCancelled(true);
            return;
        }
        userDamager = UserLoader.fromPlayer((Player) damager);
        attackerHeldItem = ItemLoader.fromBukkit(userDamager.getPlayer().getInventory().getItemInMainHand());
        if (attackerHeldItem != null && !attackerHeldItem.getItemClass().checkCanUse(userDamager, true)) {
            // Don't cancel the event, just don't modify the damage from the default
            return;
        }
    } else if (damager instanceof Arrow) {
        Arrow arrow = (Arrow) damager;
        if (arrow.getShooter() instanceof Entity) {
            npcDamager = NPCLoader.fromBukkit((Entity) arrow.getShooter());
        }
    } else {
        npcDamager = NPCLoader.fromBukkit(damager);
    }
    if (damager.hasMetadata("partOf")) {
        npcDamager = (NPC) damager.getMetadata("partOf").get(0).value();
    }
    if (npcDamager != null && npcDamager.getNPCType() != NPC.NPCType.HOSTILE) {
        event.setCancelled(true);
        return;
    }
    boolean external = false;
    Entity target = event.getEntity();
    User userTarget = null;
    NPC npcTarget = null;
    if (target.getPersistentDataContainer().has(Dragons.FIXED_ENTITY_KEY, PersistentDataType.SHORT)) {
        if (userDamager != null) {
            userDamager.debug("- Fixed entity, cancelling");
            damager.sendMessage(ChatColor.RED + "You cannot damage or destroy a fixed entity! Please un-fix this entity (/fixed) if you wish to remove it.");
        }
        event.setCancelled(true);
        return;
    }
    if (target.isInvulnerable()) {
        event.setCancelled(true);
        return;
    }
    if (target instanceof Player) {
        userTarget = UserLoader.fromPlayer((Player) target);
        if (userTarget == null) {
            npcTarget = NPCLoader.fromBukkit(target);
            if (npcTarget == null) {
                event.setCancelled(true);
                return;
            }
        } else if (userTarget.isGodMode()) {
            immortalTarget(target, userDamager);
            userTarget.debug("- God mode, cancelling");
            event.setCancelled(true);
            return;
        }
    /*
		 * Complex entities are rendered as composites of simpler entities.
		 * When a player attacks a complex entity, one of those component
		 * entities actually receives the damage. We route it to the appropriate
		 * actual entity by checking for the appropriate metadata.
		 */
    } else if (target.hasMetadata("partOf")) {
        LOGGER.trace("-Target is part of a complex entity!");
        npcTarget = (NPC) target.getMetadata("partOf").get(0).value();
        external = true;
    } else {
        npcTarget = NPCLoader.fromBukkit(target);
        if (npcTarget != null) {
            if (npcTarget.isImmortal() || target.isInvulnerable()) {
                event.setCancelled(true);
                if (userDamager != null) {
                    Item item = ItemLoader.fromBukkit(userDamager.getPlayer().getInventory().getItemInMainHand());
                    /*
						 * This specific item class allows removal of immortal entities,
						 * intended for use by the content team.
						 */
                    if (item != null && item.getClassName().equals(ItemConstants.IMMORTAL_OVERRIDE_ITEM_CLASS)) {
                        npcTarget.getEntity().remove();
                        dragons.getGameObjectRegistry().removeFromDatabase(npcTarget);
                        userDamager.getPlayer().sendMessage(ChatColor.GREEN + "Removed NPC successfully.");
                        return;
                    }
                    immortalTarget(target, userDamager);
                }
                npcTarget.updateHealthBar();
                return;
            }
        } else {
            /*
				 * Sometimes vanilla entities will sneak their way into a production world.
				 */
            LOGGER.trace("- Target is an entity but not an NPC! HasHandle=" + target.hasMetadata("handle"));
            LOGGER.warning("- Unauthorized vanilla interaction from " + StringUtil.entityToString(damager) + " to " + StringUtil.entityToString(target));
            if (userDamager != null && PermissionUtil.verifyActivePermissionLevel(userDamager, PermissionLevel.TESTER, false)) {
                HologramUtil.temporaryHologram(target, ChatColor.RED + "Error: Unbound Entity Target", 20 * 5, true);
            }
        }
    }
    if (userDamager != null && npcTarget != null) {
        Item item = ItemLoader.fromBukkit(((Player) damager).getInventory().getItemInMainHand());
        if (item != null) {
            // TODO verify this
            item.getItemClass().handleLeftClick(userDamager);
        }
    }
    /*
		 * When a player is talking to a quest NPC, they cannot interact with other entities.
		 */
    if (userDamager != null && npcTarget != null && userDamager.hasActiveDialogue()) {
        userDamager.sendActionBar(ChatColor.GRAY + "PVE is disabled during quest dialogue!");
        event.setCancelled(true);
        return;
    }
    if (npcDamager != null && userTarget != null && userTarget.hasActiveDialogue()) {
        event.setCancelled(true);
        return;
    }
    double distance = damager.getLocation().distance(target.getLocation());
    double damage = event.getDamage();
    if (userDamager == null && npcDamager == null || userTarget == null && npcTarget == null || npcDamager != null && npcTarget != null) {
        return;
    }
    if (userDamager != null && userDamager.isGodMode()) {
        if (npcTarget != null) {
            npcTarget.remove();
        } else if (!(target instanceof Player)) {
            target.remove();
        } else {
            ((Player) target).setHealth(0.0D);
        }
        return;
    }
    Set<Region> regions = regionLoader.getRegionsByLocation(target.getLocation());
    if (userTarget != null) {
        userTarget.debug("user target");
        for (Region region : regions) {
            if (!Boolean.valueOf(region.getFlags().getString("pve"))) {
                userTarget.debug("- Cancelled damage due to region " + region.getName() + ": PVE flag = false");
                event.setCancelled(true);
                return;
            }
        }
    }
    if (npcDamager != null) {
        /*
			 * Adjust damage amount based on the level difference. A high level entity will damage a
			 * low level player more than it would damage a high level player.
			 */
        double weightedLevelDiscrepancy = Math.max(0.0D, npcDamager.getLevel() - TARGET_LEVEL_DISCOUNTING * userTarget.getLevel());
        damage += LEVEL_DISCREPANCY_FACTOR * weightedLevelDiscrepancy;
    } else {
        /*
			 * Apply item based damage modifiers and cooldowns
			 */
        double itemDamage = 0.5D;
        if (attackerHeldItem != null) {
            if (attackerHeldItem.hasCooldownRemaining()) {
                userDamager.sendActionBar(ChatColor.RED + "- WAIT - " + MathUtil.round(attackerHeldItem.getCooldownRemaining()) + "s -");
                event.setCancelled(true);
                return;
            }
            if (!external) {
                attackerHeldItem.registerUse();
            }
            final User fUserDamager = userDamager;
            final Item fAttackerHeldItem = attackerHeldItem;
            new BukkitRunnable() {

                @Override
                public void run() {
                    Item currentHeldItem = ItemLoader.fromBukkit(fUserDamager.getPlayer().getInventory().getItemInMainHand());
                    if (currentHeldItem == null || !currentHeldItem.equals(fAttackerHeldItem)) {
                        return;
                    }
                    double percentRemaining = fAttackerHeldItem.getCooldownRemaining() / fAttackerHeldItem.getCooldown();
                    String cooldownName = String.valueOf(fAttackerHeldItem.getDecoratedName()) + ChatColor.DARK_GRAY + " [" + ChatColor.RESET + "WAIT " + ProgressBarUtil.getCountdownBar(percentRemaining) + ChatColor.DARK_GRAY + "]";
                    fUserDamager.getPlayer().getInventory().setItemInMainHand(fAttackerHeldItem.localRename(cooldownName));
                    if (!fAttackerHeldItem.hasCooldownRemaining()) {
                        sync(() -> {
                            Item currentHeldItem2 = ItemLoader.fromBukkit(fUserDamager.getPlayer().getInventory().getItemInMainHand());
                            if (currentHeldItem2 == null || !currentHeldItem2.equals(fAttackerHeldItem)) {
                                return;
                            }
                            fUserDamager.getPlayer().getInventory().setItemInMainHand(fAttackerHeldItem.localRename(fAttackerHeldItem.getDecoratedName()));
                        }, 10);
                        cancel();
                    }
                }
            }.runTaskTimer(dragons, 0L, 5L);
            itemDamage = attackerHeldItem.getDamage();
            damage += itemDamage;
        }
        if (userTarget == null) {
            for (Region region : regions) {
                if (!Boolean.valueOf(region.getFlags().getString("pve"))) {
                    event.setCancelled(true);
                    userDamager.sendActionBar(ChatColor.GRAY + "PVE is disabled in this region.");
                    return;
                }
            }
        } else {
            for (Region region : regions) {
                if (!Boolean.valueOf(region.getFlags().getString("pvp"))) {
                    event.setCancelled(true);
                    userDamager.sendActionBar(ChatColor.GRAY + "PVP is disabled in this region.");
                    return;
                }
            }
        }
        /* Melee skill increases inversely proportionally with distance from target */
        userDamager.incrementSkillProgress(SkillType.MELEE, Math.min(0.5D, 1.0D / distance));
        double randomMelee = Math.random() * userDamager.getSkillLevel(SkillType.MELEE) / distance;
        damage += randomMelee;
    }
    if (userTarget != null) {
        double randomDefense = Math.random() * Math.random() * userTarget.getSkillLevel(SkillType.DEFENSE);
        damage -= randomDefense;
        Item targetHeldItem = ItemLoader.fromBukkit(userTarget.getPlayer().getInventory().getItemInMainHand());
        double itemDefense = 0.0D;
        if (targetHeldItem != null && targetHeldItem.getItemClass().checkCanUse(userTarget, false)) {
            itemDefense = targetHeldItem.getArmor();
        }
        for (ItemStack itemStack : userTarget.getPlayer().getInventory().getArmorContents()) {
            Item armorItem = ItemLoader.fromBukkit(itemStack);
            if (armorItem != null && armorItem.getItemClass().checkCanUse(userTarget, false)) {
                itemDefense += armorItem.getArmor();
            }
        }
        double actualItemDefense = Math.min(damage, Math.random() * itemDefense);
        damage -= actualItemDefense;
        userTarget.incrementSkillProgress(SkillType.DEFENSE, Math.random() * actualItemDefense);
    }
    damage = Math.max(0.0D, damage);
    if (userDamager != null) {
        userDamager.debug("DMG OUTGOING: " + damage + " to " + StringUtil.entityToString(target));
    }
    if (userTarget != null) {
        userTarget.debug("DMG INCOMING: " + damage + " from " + StringUtil.entityToString(damager));
    }
    if (npcTarget != null) {
        npcTarget.setDamageExternalized(external);
    }
    if (external) {
        npcTarget.damage(damage, damager);
        event.setDamage(0.0D);
        LOGGER.trace("-Damage event external from " + StringUtil.entityToString(target) + " to " + StringUtil.entityToString(npcTarget.getEntity()));
    } else {
        event.setDamage(damage);
        if (userDamager != null) {
            String tag = ChatColor.RED + "-" + Math.round(damage) + "❤";
            if (target.getNearbyEntities(10.0D, 10.0D, 10.0D).stream().filter(e -> (e.getType() == EntityType.PLAYER)).count() > 1L) {
                tag = String.valueOf(tag) + ChatColor.GRAY + " from " + userDamager.getName();
            }
            HologramUtil.temporaryHologram(target, tag, 20, false);
        }
    }
    if (npcTarget != null) {
        npcTarget.getNPCClass().handleTakeDamage(npcTarget, npcDamager != null ? (GameObject) npcDamager : (GameObject) userDamager, damage);
        npcTarget.updateHealthBar(damage);
        if (userDamager != null) {
            npcTarget.getNPCClass().executeConditionals(NPCTrigger.HIT, userDamager, npcTarget);
        }
    }
    if (npcDamager != null) {
        if (npcDamager.getEntityType() == EntityType.PLAYER) {
            npcDamager.getPlayerNPC().playAnimation(NPCAnimation.SWING_MAIN_HAND);
        }
        npcDamager.getNPCClass().handleDealDamage(npcDamager, npcTarget != null ? (GameObject) npcTarget : (GameObject) userTarget, damage);
    }
}
Also used : NPC(mc.dragons.core.gameobject.npc.NPC) Arrow(org.bukkit.entity.Arrow) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) User(mc.dragons.core.gameobject.user.User) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Item(mc.dragons.core.gameobject.item.Item) GameObject(mc.dragons.core.gameobject.GameObject) Region(mc.dragons.core.gameobject.region.Region) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 25 with Item

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

the class EntityDeathListener method onEntityDeath.

@EventHandler
public void onEntityDeath(EntityDeathEvent event) {
    LOGGER.trace("Death event on " + StringUtil.entityToString(event.getEntity()));
    if (event.getEntity().getPersistentDataContainer().has(Dragons.FIXED_ENTITY_KEY, PersistentDataType.SHORT)) {
        UUID cid = LOGGER.newCID();
        LOGGER.severe(cid, "A fixed entity (" + StringUtil.entityToString(event.getEntity()) + ") has died! Location: " + StringUtil.locToString(event.getEntity().getLocation()) + " [" + event.getEntity().getWorld().getName() + "]");
        Dragons.getInstance().getStaffAlertHandler().sendGenericMessage(PermissionLevel.BUILDER, "A fixed entity died! Correlation ID: " + cid);
        if (CRASH_ON_FIXED_DEATH) {
            LOGGER.severe(cid, "Crashing server to prevent saving");
            for (Player p : Bukkit.getOnlinePlayers()) {
                p.kickPlayer(ChatColor.RED + "The server entered an unexpected error state and was terminated to prevent data loss.\n\n" + "Server: " + Dragons.getInstance().getServerName() + "\n" + "Log Token: " + Dragons.getInstance().getCustomLoggingProvider().getCustomLogFilter().getLogEntryUUID());
            }
            System.exit(-1);
        }
        return;
    }
    Player player = event.getEntity().getKiller();
    User user = UserLoader.fromPlayer(player);
    LivingEntity livingEntity = event.getEntity();
    for (Entity passenger : new ArrayList<>(livingEntity.getPassengers())) {
        livingEntity.removePassenger(passenger);
        passenger.remove();
    }
    NPC npc = NPCLoader.fromBukkit(livingEntity);
    if (npc == null) {
        return;
    }
    if (npc.getEntityType() == EntityType.PLAYER) {
        npc.getPlayerNPC().setStatus(NPCStatus.DIE);
    }
    if (npc.isImmortal() || npc.getNPCType().canRespawnOnDeath()) {
        npc.regenerate(livingEntity.getLocation());
    } else {
        npc.getNPCClass().handleDeath(npc);
        sync(() -> npc.remove(), 5);
    }
    npc.updateHealthBar();
    if (player == null) {
        return;
    }
    Location loc = user.getPlayer().getLocation();
    World world = loc.getWorld();
    for (Item item : npc.getNPCClass().getLootTable().getDrops(loc)) {
        world.dropItem(loc, item.getItemStack());
    }
    if (npc.getNPCType() == NPC.NPCType.HOSTILE) {
        int xpReward = getXPReward(user.getLevel(), npc.getLevel());
        user.sendActionBar("+ " + ChatColor.GREEN + xpReward + " XP");
        String tag = "+ " + ChatColor.GREEN + ChatColor.BOLD + xpReward + " XP";
        if (livingEntity.getNearbyEntities(10.0D, 10.0D, 10.0D).stream().filter(e -> (e.getType() == EntityType.PLAYER)).count() > 1L) {
            tag = String.valueOf(tag) + ChatColor.GRAY + " to " + user.getName();
        }
        HologramUtil.temporaryHologram(livingEntity, tag, 20, false);
        user.addXP(xpReward);
    }
    user.updateQuests(event);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) NPC(mc.dragons.core.gameobject.npc.NPC) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Item(mc.dragons.core.gameobject.item.Item) Player(org.bukkit.entity.Player) User(mc.dragons.core.gameobject.user.User) ArrayList(java.util.ArrayList) UUID(java.util.UUID) World(org.bukkit.World) Location(org.bukkit.Location) 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