Search in sources :

Example 1 with GlowTippedArrow

use of net.glowstone.entity.projectile.GlowTippedArrow in project Glowstone by GlowstoneMC.

the class ItemBow method endUse.

@Override
public void endUse(GlowPlayer player, ItemStack bow) {
    // Check arrows again, since plugins may have changed the inventory while the bow was drawn
    Optional<GlowInventorySlot> maybeArrow = findArrow(player);
    GlowInventorySlot slot = null;
    ItemStack arrow = null;
    final Material arrowType;
    Projectile launchedProjectile = null;
    boolean consumeArrow = false;
    if (maybeArrow.isPresent()) {
        slot = maybeArrow.get();
        arrow = slot.getItem();
        arrowType = arrow.getType();
        consumeArrow = (player.getGameMode() != GameMode.CREATIVE);
    } else if (player.getGameMode() == GameMode.CREATIVE) {
        // Can fire without arrows in Creative
        arrowType = Material.ARROW;
        consumeArrow = false;
    } else {
        arrowType = Material.AIR;
    }
    switch(arrowType) {
        case ARROW:
            if (bow.containsEnchantment(Enchantment.ARROW_INFINITE)) {
                consumeArrow = false;
            }
            launchedProjectile = player.launchProjectile(Arrow.class);
            break;
        case TIPPED_ARROW:
            launchedProjectile = player.launchProjectile(TippedArrow.class);
            GlowTippedArrow launchedTippedArrow = (GlowTippedArrow) launchedProjectile;
            launchedTippedArrow.copyFrom((PotionMeta) arrow.getItemMeta());
            break;
        case SPECTRAL_ARROW:
            launchedProjectile = player.launchProjectile(SpectralArrow.class);
            break;
        case AIR:
            // Not in creative mode and have no arrow
            break;
        default:
            GlowServer.logger.log(Level.SEVERE, () -> String.format("Attempt to fire %s from a bow", arrowType));
    }
    if (launchedProjectile != null) {
        float chargeFraction = (TICKS_TO_FULLY_CHARGE - Floats.constrainToRange(player.getUsageTime(), 0.0f, TICKS_TO_FULLY_CHARGE)) / TICKS_TO_FULLY_CHARGE;
        EntityShootBowEvent event = new EntityShootBowEvent(player, bow, arrow, launchedProjectile, chargeFraction);
        event.setConsumeArrow(consumeArrow);
        event = EventFactory.getInstance().callEvent(event);
        consumeArrow = event.getConsumeArrow();
        // TODO: Call for Skeleton firing too when implemented
        if (event.isCancelled()) {
            launchedProjectile.remove();
        } else {
            chargeFraction = event.getForce();
            launchedProjectile = (Projectile) event.getProjectile();
            if (consumeArrow) {
                int amount = arrow.getAmount();
                if (amount <= 1) {
                    arrow = InventoryUtil.createEmptyStack();
                } else {
                    arrow.setAmount(amount - 1);
                }
                slot.setItem(arrow);
            }
            double damage = Math.max(1.0, MAX_BASE_DAMAGE + (chargeFraction == 1.0 && ThreadLocalRandom.current().nextFloat() >= 0.8 ? 1 : 0) * chargeFraction * (1 + 0.25 * bow.getEnchantmentLevel(Enchantment.ARROW_DAMAGE)));
            launchedProjectile.setVelocity(player.getEyeLocation().getDirection().multiply(Math.max(5, chargeFraction * MAX_SPEED)));
            if (bow.containsEnchantment(Enchantment.ARROW_FIRE)) {
                // Arrow will burn as long as it's in flight, unless extinguished by water
                launchedProjectile.setFireTicks(Integer.MAX_VALUE);
            }
            // Plugin may change projectile to non arrow.
            if (launchedProjectile instanceof Arrow) {
                Arrow launchedArrow = (Arrow) launchedProjectile;
                launchedArrow.setDamage(damage);
                launchedArrow.setKnockbackStrength(bow.getEnchantmentLevel(Enchantment.ARROW_KNOCKBACK));
                // 20% crit chance
                if (ThreadLocalRandom.current().nextDouble() < 0.2) {
                    launchedArrow.setCritical(true);
                }
            }
            if (player.getInventory().getItemInMainHand().getType() == Material.BOW) {
                player.getInventory().setItemInMainHand(InventoryUtil.damageItem(player, bow));
            } else {
                player.getInventory().setItemInOffHand(InventoryUtil.damageItem(player, bow));
            }
        }
    }
    player.setUsageItem(null);
    player.setUsageTime(0);
}
Also used : Arrow(org.bukkit.entity.Arrow) SpectralArrow(org.bukkit.entity.SpectralArrow) TippedArrow(org.bukkit.entity.TippedArrow) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) EntityShootBowEvent(org.bukkit.event.entity.EntityShootBowEvent) Material(org.bukkit.Material) SpectralArrow(org.bukkit.entity.SpectralArrow) Projectile(org.bukkit.entity.Projectile) TippedArrow(org.bukkit.entity.TippedArrow) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) GlowInventorySlot(net.glowstone.inventory.GlowInventorySlot) ItemStack(org.bukkit.inventory.ItemStack)

Example 2 with GlowTippedArrow

use of net.glowstone.entity.projectile.GlowTippedArrow in project Glowstone by GlowstoneMC.

the class GlowDispenser method register.

/**
 * Registers all vanilla dispense behaviors.
 */
public static void register() {
    // register all dispense behaviors
    DefaultDispenseBehavior bucketDispenseBehavior = new BucketDispenseBehavior();
    DispenseBehaviorRegistry registry = getDispenseBehaviorRegistry();
    registry.putBehavior(Material.WATER_BUCKET, bucketDispenseBehavior);
    registry.putBehavior(Material.LAVA_BUCKET, bucketDispenseBehavior);
    registry.putBehavior(Material.BUCKET, new EmptyBucketDispenseBehavior());
    registry.putBehavior(Material.FLINT_AND_STEEL, new FlintAndSteelDispenseBehavior());
    registry.putBehavior(Material.TNT, new TntDispenseBehavior());
    ArmorDispenseBehavior armorDispenseBehavior = new ArmorDispenseBehavior();
    registry.putBehavior(Material.LEATHER_BOOTS, armorDispenseBehavior);
    registry.putBehavior(Material.LEATHER_LEGGINGS, armorDispenseBehavior);
    registry.putBehavior(Material.LEATHER_CHESTPLATE, armorDispenseBehavior);
    registry.putBehavior(Material.LEATHER_HELMET, armorDispenseBehavior);
    registry.putBehavior(Material.GOLDEN_BOOTS, armorDispenseBehavior);
    registry.putBehavior(Material.GOLDEN_LEGGINGS, armorDispenseBehavior);
    registry.putBehavior(Material.GOLDEN_CHESTPLATE, armorDispenseBehavior);
    registry.putBehavior(Material.GOLDEN_HELMET, armorDispenseBehavior);
    registry.putBehavior(Material.IRON_BOOTS, armorDispenseBehavior);
    registry.putBehavior(Material.IRON_LEGGINGS, armorDispenseBehavior);
    registry.putBehavior(Material.IRON_CHESTPLATE, armorDispenseBehavior);
    registry.putBehavior(Material.IRON_HELMET, armorDispenseBehavior);
    registry.putBehavior(Material.CHAINMAIL_BOOTS, armorDispenseBehavior);
    registry.putBehavior(Material.CHAINMAIL_LEGGINGS, armorDispenseBehavior);
    registry.putBehavior(Material.CHAINMAIL_CHESTPLATE, armorDispenseBehavior);
    registry.putBehavior(Material.CHAINMAIL_HELMET, armorDispenseBehavior);
    registry.putBehavior(Material.DIAMOND_BOOTS, armorDispenseBehavior);
    registry.putBehavior(Material.DIAMOND_LEGGINGS, armorDispenseBehavior);
    registry.putBehavior(Material.DIAMOND_CHESTPLATE, armorDispenseBehavior);
    registry.putBehavior(Material.DIAMOND_HELMET, armorDispenseBehavior);
    for (Material headType : MaterialTags.SKULLS.getValues()) {
        registry.putBehavior(headType, armorDispenseBehavior);
    }
    registry.putBehavior(Material.PUMPKIN, armorDispenseBehavior);
    registry.putBehavior(Material.EGG, new ProjectileDispenseBehavior(GlowEgg::new));
    registry.putBehavior(Material.SNOWBALL, new ProjectileDispenseBehavior(GlowSnowball::new));
    registry.putBehavior(Material.ARROW, new ProjectileDispenseBehavior(GlowArrow::new));
    registry.putBehavior(Material.EXPERIENCE_BOTTLE, new ProjectileDispenseBehavior(GlowThrownExpBottle::new));
    registry.putBehavior(Material.SPECTRAL_ARROW, new ProjectileDispenseBehavior(GlowSpectralArrow::new));
    registry.putBehavior(Material.TIPPED_ARROW, new ProjectileDispenseBehavior(((location, itemStack) -> {
        GlowTippedArrow tippedArrow = new GlowTippedArrow(location);
        tippedArrow.copyFrom((PotionMeta) itemStack.getItemMeta());
        return tippedArrow;
    })));
    registry.putBehavior(Material.FIRE_CHARGE, new ProjectileDispenseBehavior(location -> {
        Fireball fireball = new GlowFireball(location);
        fireball.setYield(0);
        fireball.setIsIncendiary(true);
        return fireball;
    }));
    registry.putBehavior(Material.SPLASH_POTION, new ProjectileDispenseBehavior((location, itemStack) -> {
        SplashPotion potion = new GlowSplashPotion(location);
        potion.setItem(itemStack);
        return potion;
    }));
    registry.putBehavior(Material.LINGERING_POTION, new ProjectileDispenseBehavior((location, itemStack) -> {
        SplashPotion potion = new GlowLingeringPotion(location);
        potion.setItem(itemStack);
        return potion;
    }));
// TODO: Firework rockets
}
Also used : GlowEgg(net.glowstone.entity.projectile.GlowEgg) GlowSplashPotion(net.glowstone.entity.projectile.GlowSplashPotion) GlowBlock(net.glowstone.block.GlowBlock) Getter(lombok.Getter) BucketDispenseBehavior(net.glowstone.dispenser.BucketDispenseBehavior) DispenseBehaviorRegistry(net.glowstone.dispenser.DispenseBehaviorRegistry) SplashPotion(org.bukkit.entity.SplashPotion) FlintAndSteelDispenseBehavior(net.glowstone.dispenser.FlintAndSteelDispenseBehavior) Projectile(org.bukkit.entity.Projectile) InventoryUtil(net.glowstone.util.InventoryUtil) Inventory(org.bukkit.inventory.Inventory) Effect(org.bukkit.Effect) EmptyBucketDispenseBehavior(net.glowstone.dispenser.EmptyBucketDispenseBehavior) GlowFireball(net.glowstone.entity.projectile.GlowFireball) GlowThrownExpBottle(net.glowstone.entity.projectile.GlowThrownExpBottle) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) PotionMeta(org.bukkit.inventory.meta.PotionMeta) Dispenser(org.bukkit.block.Dispenser) Material(org.bukkit.Material) BlockProjectileSource(org.bukkit.projectiles.BlockProjectileSource) GlowSpectralArrow(net.glowstone.entity.projectile.GlowSpectralArrow) MaterialTags(com.destroystokyo.paper.MaterialTags) GlowArrow(net.glowstone.entity.projectile.GlowArrow) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) DefaultDispenseBehavior(net.glowstone.dispenser.DefaultDispenseBehavior) GlowSnowball(net.glowstone.entity.projectile.GlowSnowball) DispenseBehavior(net.glowstone.dispenser.DispenseBehavior) ProjectileDispenseBehavior(net.glowstone.dispenser.ProjectileDispenseBehavior) GlowLingeringPotion(net.glowstone.entity.projectile.GlowLingeringPotion) ArmorDispenseBehavior(net.glowstone.dispenser.ArmorDispenseBehavior) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) DispenserEntity(net.glowstone.block.entity.DispenserEntity) Fireball(org.bukkit.entity.Fireball) TntDispenseBehavior(net.glowstone.dispenser.TntDispenseBehavior) FlintAndSteelDispenseBehavior(net.glowstone.dispenser.FlintAndSteelDispenseBehavior) ArmorDispenseBehavior(net.glowstone.dispenser.ArmorDispenseBehavior) TntDispenseBehavior(net.glowstone.dispenser.TntDispenseBehavior) Material(org.bukkit.Material) GlowSplashPotion(net.glowstone.entity.projectile.GlowSplashPotion) GlowFireball(net.glowstone.entity.projectile.GlowFireball) GlowSplashPotion(net.glowstone.entity.projectile.GlowSplashPotion) SplashPotion(org.bukkit.entity.SplashPotion) BucketDispenseBehavior(net.glowstone.dispenser.BucketDispenseBehavior) EmptyBucketDispenseBehavior(net.glowstone.dispenser.EmptyBucketDispenseBehavior) DispenseBehaviorRegistry(net.glowstone.dispenser.DispenseBehaviorRegistry) GlowFireball(net.glowstone.entity.projectile.GlowFireball) Fireball(org.bukkit.entity.Fireball) EmptyBucketDispenseBehavior(net.glowstone.dispenser.EmptyBucketDispenseBehavior) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) GlowLingeringPotion(net.glowstone.entity.projectile.GlowLingeringPotion) DefaultDispenseBehavior(net.glowstone.dispenser.DefaultDispenseBehavior) ProjectileDispenseBehavior(net.glowstone.dispenser.ProjectileDispenseBehavior)

Aggregations

GlowTippedArrow (net.glowstone.entity.projectile.GlowTippedArrow)2 Material (org.bukkit.Material)2 Projectile (org.bukkit.entity.Projectile)2 ItemStack (org.bukkit.inventory.ItemStack)2 MaterialTags (com.destroystokyo.paper.MaterialTags)1 Map (java.util.Map)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 Getter (lombok.Getter)1 GlowBlock (net.glowstone.block.GlowBlock)1 DispenserEntity (net.glowstone.block.entity.DispenserEntity)1 ArmorDispenseBehavior (net.glowstone.dispenser.ArmorDispenseBehavior)1 BucketDispenseBehavior (net.glowstone.dispenser.BucketDispenseBehavior)1 DefaultDispenseBehavior (net.glowstone.dispenser.DefaultDispenseBehavior)1 DispenseBehavior (net.glowstone.dispenser.DispenseBehavior)1 DispenseBehaviorRegistry (net.glowstone.dispenser.DispenseBehaviorRegistry)1 EmptyBucketDispenseBehavior (net.glowstone.dispenser.EmptyBucketDispenseBehavior)1 FlintAndSteelDispenseBehavior (net.glowstone.dispenser.FlintAndSteelDispenseBehavior)1 ProjectileDispenseBehavior (net.glowstone.dispenser.ProjectileDispenseBehavior)1 TntDispenseBehavior (net.glowstone.dispenser.TntDispenseBehavior)1 GlowArrow (net.glowstone.entity.projectile.GlowArrow)1