Search in sources :

Example 1 with IndirectBreakEvent

use of com.easterlyn.effect.event.IndirectBreakEvent in project Easterlyn by Easterlyn.

the class EffectTunnelBore method applyEffect.

@Override
public void applyEffect(@NotNull LivingEntity entity, int level, @Nullable Event event) {
    if (!(event instanceof BlockBreakEvent) || event instanceof IndirectBreakEvent) {
        return;
    }
    BlockBreakEvent breakEvent = (BlockBreakEvent) event;
    Player player = breakEvent.getPlayer();
    if (player.isSneaking()) {
        // Sneak to mine single blocks
        return;
    }
    Block block = breakEvent.getBlock();
    if (!BlockUtil.isCorrectTool(player.getInventory().getItemInMainHand(), breakEvent.getBlock().getType())) {
        // Require correct tool used
        return;
    }
    RegisteredServiceProvider<EasterlynCore> registration = getPlugin().getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (registration == null) {
        return;
    }
    BlockUpdateManager budManager = registration.getProvider().getBlockUpdateManager();
    /*
     * Forgiveness of .25 hardness allows indirect breaking of most common related materials
     * while not excessively increasing risk of accidents inside builds. Allows for mining gravel
     * via sand and dirt, etc.
     */
    float hardness = block.getType().getHardness() + 0.25F;
    for (BlockFace yLevel : levels) {
        if (block.getY() == 0 && yLevel == BlockFace.DOWN) {
            continue;
        }
        Block relativeCenter = block.getRelative(yLevel);
        if (yLevel != BlockFace.SELF) {
            if (handleBlock(hardness, relativeCenter, player, budManager)) {
                return;
            }
        }
        for (BlockFace face : faces) {
            if (handleBlock(hardness, relativeCenter.getRelative(face), player, budManager)) {
                return;
            }
        }
    }
    player.updateInventory();
}
Also used : Player(org.bukkit.entity.Player) EasterlynCore(com.easterlyn.EasterlynCore) IndirectBreakEvent(com.easterlyn.effect.event.IndirectBreakEvent) BlockFace(org.bukkit.block.BlockFace) BlockUpdateManager(com.easterlyn.util.BlockUpdateManager) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) Block(org.bukkit.block.Block)

Example 2 with IndirectBreakEvent

use of com.easterlyn.effect.event.IndirectBreakEvent in project Easterlyn by Easterlyn.

the class EffectPullNearbyItems method applyEffect.

@Override
public void applyEffect(@NotNull LivingEntity entity, int level, Event event) {
    final double radius = level * 1.5;
    final UUID uuid = entity.getUniqueId();
    new BukkitRunnable() {

        @Override
        public void run() {
            Player player = Bukkit.getPlayer(uuid);
            if (player == null) {
                // Logged out
                return;
            }
            for (Entity near : player.getNearbyEntities(radius, radius, radius)) {
                if (!(near instanceof Item)) {
                    continue;
                }
                near.getWorld().spawnParticle(Particle.ENCHANTMENT_TABLE, near.getLocation(), 1);
                near.teleport(player);
                if (!(event instanceof IndirectBreakEvent)) {
                    player.playSound(near.getLocation(), Sound.ENTITY_ENDER_DRAGON_FLAP, SoundCategory.BLOCKS, 0.1F, 1.5F);
                }
                Item item = (Item) near;
                if (item.getPickupDelay() < 1000) {
                    item.setPickupDelay(0);
                }
            }
        }
    }.runTask(getPlugin());
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Item(org.bukkit.entity.Item) Player(org.bukkit.entity.Player) IndirectBreakEvent(com.easterlyn.effect.event.IndirectBreakEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) UUID(java.util.UUID)

Example 3 with IndirectBreakEvent

use of com.easterlyn.effect.event.IndirectBreakEvent in project Easterlyn by Easterlyn.

the class EffectTunnelBore method handleBlock.

private boolean handleBlock(float requiredHardness, Block block, Player player, BlockUpdateManager budManager) {
    float blockHardness = block.getType().getHardness();
    if (blockHardness > requiredHardness || blockHardness < 0 || block.getState() instanceof InventoryHolder || !BlockUtil.isCorrectTool(player.getInventory().getItemInMainHand(), block.getType())) {
        return false;
    }
    IndirectBreakEvent event = new IndirectBreakEvent(block, player);
    Bukkit.getServer().getPluginManager().callEvent(event);
    if (event.isCancelled() || block.isLiquid()) {
        return false;
    }
    if (player.getGameMode() == GameMode.CREATIVE) {
        block.setType(Material.AIR, false);
        budManager.queueBlock(block);
        return false;
    }
    // Item breaking blocks has to be in main hand
    ItemStack hand = player.getInventory().getItemInMainHand();
    Collection<ItemStack> drops = BlockUtil.getDrops(hand, block);
    int exp = BlockUtil.getExp(player, hand, block);
    block.setType(Material.AIR, false);
    for (ItemStack is : drops) {
        player.getWorld().dropItem(player.getLocation(), is).setPickupDelay(0);
    }
    if (exp > 0) {
        ExperienceUtil.changeExp(player, exp);
    }
    budManager.queueBlock(block);
    ItemMeta handMeta = hand.getItemMeta();
    if (handMeta != null && !handMeta.isUnbreakable() && handMeta instanceof Damageable && (!hand.containsEnchantment(Enchantment.DURABILITY) || ThreadLocalRandom.current().nextDouble() < 1.0 / (hand.getEnchantmentLevel(Enchantment.DURABILITY) + 2))) {
        Damageable damageable = (Damageable) handMeta;
        damageable.setDamage(damageable.getDamage() + 1);
        hand.setItemMeta(handMeta);
        return damageable.getDamage() > hand.getType().getMaxDurability();
    }
    return false;
}
Also used : Damageable(org.bukkit.inventory.meta.Damageable) IndirectBreakEvent(com.easterlyn.effect.event.IndirectBreakEvent) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

IndirectBreakEvent (com.easterlyn.effect.event.IndirectBreakEvent)3 Player (org.bukkit.entity.Player)2 EasterlynCore (com.easterlyn.EasterlynCore)1 BlockUpdateManager (com.easterlyn.util.BlockUpdateManager)1 UUID (java.util.UUID)1 Block (org.bukkit.block.Block)1 BlockFace (org.bukkit.block.BlockFace)1 Entity (org.bukkit.entity.Entity)1 Item (org.bukkit.entity.Item)1 LivingEntity (org.bukkit.entity.LivingEntity)1 BlockBreakEvent (org.bukkit.event.block.BlockBreakEvent)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 ItemStack (org.bukkit.inventory.ItemStack)1 Damageable (org.bukkit.inventory.meta.Damageable)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1