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);
}
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
}
Aggregations