下面列出了org.bukkit.entity.EnderPearl#org.bukkit.entity.ThrownPotion 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static double getScore(ThrownPotion potion) {
double score = 0;
for (PotionEffect effect :
Iterables.concat(
potion.getEffects(),
((PotionMeta) potion.getItem().getItemMeta()).getCustomEffects())) {
score += getScore(effect);
}
return score;
}
@EventHandler(ignoreCancelled = true)
public void onPotionSplash(final PotionSplashEvent event) {
ThrownPotion potion = event.getPotion();
if (!PotionClassifier.isHarmful(potion)) return;
for (LivingEntity entity : event.getAffectedEntities()) {
ParticipantState victim = match.getParticipantState(entity);
DamageInfo damageInfo =
tracker().resolveDamage(EntityDamageEvent.DamageCause.MAGIC, entity, potion);
if (victim != null && queryDamage(event, victim, damageInfo).isDenied()) {
event.setIntensity(entity, 0);
}
}
}
public PhysicalInfo createEntity(Entity entity, @Nullable ParticipantState owner) {
if (entity instanceof ThrownPotion) {
return new ThrownPotionInfo((ThrownPotion) entity, owner);
} else if (entity instanceof FallingBlock) {
return new FallingBlockInfo((FallingBlock) entity, owner);
} else if (entity instanceof LivingEntity) {
return new MobInfo((LivingEntity) entity, owner);
} else {
return new EntityInfo(entity, owner);
}
}
@EventHandler(ignoreCancelled = true)
public void onPotionSplash(final PotionSplashEvent event) {
final ThrownPotion potion = event.getPotion();
if(PotionClassification.classify(potion) != PotionClassification.HARMFUL) return;
for(LivingEntity victim : event.getAffectedEntities()) {
final ParticipantState victimState = getMatch().getParticipantState(victim);
final DamageInfo damageInfo = damageResolver.resolveDamage(EntityDamageEvent.DamageCause.MAGIC, victim, potion);
if(victimState != null && queryDamage(event, victimState, damageInfo).isDenied()) {
event.setIntensity(victim, 0);
}
}
}
public PhysicalInfo createEntity(Entity entity, @Nullable ParticipantState owner) {
if(entity instanceof ThrownPotion) {
return new ThrownPotionInfo((ThrownPotion) entity, owner);
} else if(entity instanceof FallingBlock) {
return new FallingBlockInfo((FallingBlock) entity, owner);
} else if(entity instanceof LivingEntity) {
return new MobInfo((LivingEntity) entity, owner);
} else {
return new EntityInfo(entity, owner);
}
}
/**
* Handles splash potions+
*
* @param event
*/
@EventHandler
public void onSplash(PotionSplashEvent event) {
boolean isHarming = false;
for (PotionEffect effect : event.getPotion().getEffects()) {
if (bad.contains(effect.getType())) {
isHarming = true;
break;
}
}
if (!isHarming)
return;
ThrownPotion potion = event.getPotion();
if (!(potion.getShooter() instanceof Player))
return;
Player shooter = (Player) potion.getShooter();
for (LivingEntity entity : event.getAffectedEntities()) {
if (entity instanceof Player) {
Player player = (Player) entity;
if (guildHandler.isSameGuild(shooter, player) && potion.getShooter() != player) {
event.setCancelled(!settingsManager.getProperty(GuildSettings.GUILD_DAMAGE));
return;
}
if (guildHandler.isAlly(shooter, player)) {
event.setCancelled(!settingsManager.getProperty(GuildSettings.ALLY_DAMAGE));
}
}
}
}
@Override
protected boolean init(final @Nullable Class<? extends ThrownPotion> c, final @Nullable ThrownPotion e) {
if (e != null) {
final ItemStack i = e.getItem();
if (i == null)
return false;
types = new ItemType[] {new ItemType(i)};
}
return true;
}
@Override
protected boolean match(final ThrownPotion entity) {
if (types != null) {
for (final ItemType t : types) {
if (t.isOfType(entity.getItem()))
return true;
}
return false;
}
return true;
}
@Override
public void set(final ThrownPotion entity) {
if (types != null) {
final ItemType t = CollectionUtils.getRandom(types);
assert t != null;
ItemStack i = t.getRandom();
if (i == null)
return; // Missing item, can't make thrown potion of it
entity.setItem(i);
}
}
public static short guessData(final ThrownPotion p) {
if (p.getEffects().size() == 1) {
final PotionEffect e = p.getEffects().iterator().next();
PotionType type = PotionType.getByEffect(e.getType());
assert type != null;
final Potion d = new Potion(type).splash();
return d.toDamageValue();
}
return 0;
}
/**
* PotionSplashEvent
*/
public static PotionSplashEvent callPotionSplashEvent(net.minecraft.entity.projectile.EntityPotion potion, Map<LivingEntity, Double> affectedEntities) {
ThrownPotion thrownPotion = (ThrownPotion) potion.getBukkitEntity();
PotionSplashEvent event = new PotionSplashEvent(thrownPotion, affectedEntities);
Bukkit.getPluginManager().callEvent(event);
return event;
}
@Override
public boolean effect(Event event, Player player) {
int slot = player.getInventory().getHeldItemSlot();
ItemStack potion = player.getInventory().getItem(slot + 1);
Location loc = player.getLocation();
if (potion != null && potion.getType().toString().contains("POTION")) {
ThrownPotion tp = player.launchProjectile(ThrownPotion.class);
EffectManager.playSound(loc, "ENTITY_GENERIC_EXPLODE", 0.5f, 2f);
try {
tp.setItem(potion);
} catch (IllegalArgumentException ex) {
ItemStack pt = potion.clone();
if (potion.getType().equals(Material.POTION) || potion.getType().equals(Material.LINGERING_POTION))
pt.setType(Material.SPLASH_POTION);
tp.setItem(pt);
}
tp.setBounce(false);
tp.setVelocity(loc.getDirection().multiply(ProjectileSpeedMultiplier));
if (!player.getGameMode().equals(GameMode.CREATIVE)) {
potion.setAmount(potion.getAmount() - 1);
player.getInventory().setItem(slot + 1, potion);
player.updateInventory();
}
return true;
} else {
player.sendMessage(ChatColor.RED + "You need a Potion in the slot to the right of the Potion Launcher!");
player.getWorld().playEffect(loc, Effect.CLICK1, 5);
}
return false;
}
public static boolean isHarmful(ThrownPotion potion) {
return getScore(potion) <= HARMFUL;
}
public ThrownPotionInfo(ThrownPotion entity, @Nullable ParticipantState owner) {
super(entity, owner);
this.effectType = InventoryUtils.getPrimaryEffectType(entity.getItem());
}
public ThrownPotionInfo(ThrownPotion entity) {
this(entity, null);
}
public PotionSplashEvent(final ThrownPotion potion, final Map<LivingEntity, Double> affectedEntities) {
super(potion);
this.affectedEntities = affectedEntities;
}
@Override
public ThrownPotion getEntity() {
return (ThrownPotion) entity;
}
public LingeringPotionSplashEvent(final ThrownPotion potion, final AreaEffectCloud entity) {
super(potion);
this.entity = entity;
}
public ThrownPotionInfo(ThrownPotion entity, @Nullable ParticipantState owner) {
super(entity, owner);
this.effectType = PotionUtils.primaryEffectType(entity.getItem());
}
public ThrownPotionInfo(ThrownPotion entity) {
this(entity, null);
}
@Override
public Class<? extends ThrownPotion> getType() {
return ThrownPotion.class;
}
@SuppressWarnings("unchecked")
public <T extends Projectile> T launchProjectile(Class<? extends T> projectile, Vector velocity) {
net.minecraft.world.World world = ((CraftWorld) getWorld()).getHandle();
net.minecraft.entity.Entity launch = null;
if (Snowball.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.projectile.EntitySnowball(world, getHandle());
} else if (Egg.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.projectile.EntityEgg(world, getHandle());
} else if (EnderPearl.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.item.EntityEnderPearl(world, getHandle());
} else if (Arrow.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.projectile.EntityArrow(world, getHandle(), 1);
} else if (ThrownPotion.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.projectile.EntityPotion(world, getHandle(), CraftItemStack.asNMSCopy(new ItemStack(Material.POTION, 1)));
} else if (ThrownExpBottle.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.item.EntityExpBottle(world, getHandle());
} else if (Fish.class.isAssignableFrom(projectile) && getHandle() instanceof net.minecraft.entity.player.EntityPlayer) {
launch = new net.minecraft.entity.projectile.EntityFishHook(world, (net.minecraft.entity.player.EntityPlayer) getHandle());
} else if (Fireball.class.isAssignableFrom(projectile)) {
Location location = getEyeLocation();
Vector direction = location.getDirection().multiply(10);
if (SmallFireball.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.projectile.EntitySmallFireball(world, getHandle(), direction.getX(), direction.getY(), direction.getZ());
} else if (WitherSkull.class.isAssignableFrom(projectile)) {
launch = new net.minecraft.entity.projectile.EntityWitherSkull(world, getHandle(), direction.getX(), direction.getY(), direction.getZ());
} else {
launch = new net.minecraft.entity.projectile.EntityLargeFireball(world, getHandle(), direction.getX(), direction.getY(), direction.getZ());
}
((net.minecraft.entity.projectile.EntityFireball) launch).projectileSource = this;
launch.setLocationAndAngles(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
}
Validate.notNull(launch, "Projectile not supported");
if (velocity != null) {
((T) launch.getBukkitEntity()).setVelocity(velocity);
}
world.spawnEntityInWorld(launch);
return (T) launch.getBukkitEntity();
}
@Override
public boolean damageEntity(DamageSource source, float damage) {
if(godmode || noDamageTicks > 0) {
return false;
}
DamageCause cause = null;
org.bukkit.entity.Entity bEntity = null;
if(source instanceof EntityDamageSource) {
Entity damager = source.getEntity();
cause = DamageCause.ENTITY_ATTACK;
if(source instanceof EntityDamageSourceIndirect) {
damager = ((EntityDamageSourceIndirect) source).getProximateDamageSource();
if(damager.getBukkitEntity() instanceof ThrownPotion) {
cause = DamageCause.MAGIC;
} else if(damager.getBukkitEntity() instanceof Projectile) {
cause = DamageCause.PROJECTILE;
}
}
bEntity = damager.getBukkitEntity();
} else if(source == DamageSource.FIRE)
cause = DamageCause.FIRE;
else if(source == DamageSource.STARVE)
cause = DamageCause.STARVATION;
else if(source == DamageSource.WITHER)
cause = DamageCause.WITHER;
else if(source == DamageSource.STUCK)
cause = DamageCause.SUFFOCATION;
else if(source == DamageSource.DROWN)
cause = DamageCause.DROWNING;
else if(source == DamageSource.BURN)
cause = DamageCause.FIRE_TICK;
else if(source == CraftEventFactory.MELTING)
cause = DamageCause.MELTING;
else if(source == CraftEventFactory.POISON)
cause = DamageCause.POISON;
else if(source == DamageSource.MAGIC) {
cause = DamageCause.MAGIC;
} else if(source == DamageSource.OUT_OF_WORLD) {
cause = DamageCause.VOID;
}
if(cause != null) {
NPCDamageEvent event = new NPCDamageEvent(this, bEntity, cause, (double) damage);
Bukkit.getPluginManager().callEvent(event);
if(!event.isCancelled()) {
return super.damageEntity(source, (float) event.getDamage());
} else {
return false;
}
}
if(super.damageEntity(source, damage)) {
if(bEntity != null) {
Entity e = ((CraftEntity) bEntity).getHandle();
double d0 = e.locX - this.locX;
double d1;
for(d1 = e.locZ - this.locZ; d0 * d0 + d1 * d1 < 0.0001D; d1 = (Math.random() - Math.random()) * 0.01D) {
d0 = (Math.random() - Math.random()) * 0.01D;
}
a(e, damage, d0, d1);
}
return true;
} else {
return false;
}
}
/**
* Gets the potion which caused this event
*
* @return The thrown potion entity
*/
public ThrownPotion getPotion() {
return (ThrownPotion) getEntity();
}