Example #1
0
 public function onConsume(Entity $human)
 {
     $pk = new EntityEventPacket();
     $pk->eid = $human->getId();
     $pk->event = EntityEventPacket::USE_ITEM;
     if ($human instanceof Player) {
         $human->dataPacket($pk);
     }
     Server::broadcastPacket($human->getViewers(), $pk);
     $ev = new EntityEatItemEvent($human, $this);
     $human->addSaturation($ev->getSaturationRestore());
     $human->addFood($ev->getFoodRestore());
     foreach ($ev->getAdditionalEffects() as $effect) {
         $human->addEffect($effect);
     }
     $human->getInventory()->setItemInHand($ev->getResidue());
 }
Example #2
0
 public function onConsume(Entity $human)
 {
     $pk = new EntityEventPacket();
     $pk->eid = $human->getId();
     $pk->event = EntityEventPacket::USE_ITEM;
     if ($human instanceof Player) {
         $human->dataPacket($pk);
     }
     Server::broadcastPacket($human->getViewers(), $pk);
     Server::getInstance()->getPluginManager()->callEvent($ev = new EntityDrinkPotionEvent($human, $this));
     if (!$ev->isCancelled()) {
         foreach ($ev->getEffects() as $effect) {
             $human->addEffect($effect);
         }
         //Don't set the held item to glass bottle if we're in creative
         if ($human instanceof Player) {
             if ($human->getGamemode() === 1) {
                 return;
             }
         }
         $human->getInventory()->setItemInHand(Item::get(self::GLASS_BOTTLE));
     }
 }
Example #3
0
 /**
  * @param Entity    $entity
  * @param int       $cause
  * @param int|int[] $damage
  *
  * @throws \Exception
  */
 public function __construct(Entity $entity, $cause, $damage)
 {
     $this->entity = $entity;
     $this->cause = $cause;
     if (is_array($damage)) {
         $this->modifiers = $damage;
     } else {
         $this->modifiers = [self::MODIFIER_BASE => $damage];
     }
     $this->originals = $this->modifiers;
     if (!isset($this->modifiers[self::MODIFIER_BASE])) {
         throw new \InvalidArgumentException("BASE Damage modifier missing");
     }
     //For DAMAGE_RESISTANCE
     if ($cause !== self::CAUSE_VOID and $cause !== self::CAUSE_SUICIDE) {
         if ($entity->hasEffect(Effect::DAMAGE_RESISTANCE)) {
             $RES_level = 1 - 0.2 * ($entity->getEffect(Effect::DAMAGE_RESISTANCE)->getAmplifier() + 1);
             if ($RES_level < 0) {
                 $RES_level = 0;
             }
             $this->setRateDamage($RES_level, self::MODIFIER_RESISTANCE);
         }
     }
     //TODO: add zombie
     if ($entity instanceof Player and $entity->getInventory() instanceof PlayerInventory) {
         switch ($cause) {
             case self::CAUSE_CONTACT:
             case self::CAUSE_ENTITY_ATTACK:
             case self::CAUSE_PROJECTILE:
             case self::CAUSE_FIRE:
             case self::CAUSE_LAVA:
             case self::CAUSE_BLOCK_EXPLOSION:
             case self::CAUSE_ENTITY_EXPLOSION:
             case self::CAUSE_LIGHTNING:
                 $points = 0;
                 foreach ($entity->getInventory()->getArmorContents() as $index => $i) {
                     if ($i->isArmor()) {
                         $points += $i->getArmorValue();
                         $this->usedArmors[$index] = 1;
                     }
                 }
                 if ($points !== 0) {
                     $this->setRateDamage(1 - 0.04 * $points, self::MODIFIER_ARMOR);
                 }
                 //For Protection
                 $spe_Prote = null;
                 switch ($cause) {
                     case self::CAUSE_ENTITY_EXPLOSION:
                     case self::CAUSE_BLOCK_EXPLOSION:
                         $spe_Prote = Enchantment::TYPE_ARMOR_EXPLOSION_PROTECTION;
                         break;
                     case self::CAUSE_FIRE:
                     case self::CAUSE_LAVA:
                         $spe_Prote = Enchantment::TYPE_ARMOR_FIRE_PROTECTION;
                         break;
                     case self::CAUSE_PROJECTILE:
                         $spe_Prote = Enchantment::TYPE_ARMOR_PROJECTILE_PROTECTION;
                         break;
                     default:
                         break;
                 }
                 foreach ($this->usedArmors as $index => $cost) {
                     $i = $entity->getInventory()->getArmorItem($index);
                     if ($i->isArmor()) {
                         $this->EPF += $i->getEnchantmentLevel(Enchantment::TYPE_ARMOR_PROTECTION);
                         $this->fireProtectL = max($this->fireProtectL, $i->getEnchantmentLevel(Enchantment::TYPE_ARMOR_FIRE_PROTECTION));
                         if ($i->getEnchantmentLevel(Enchantment::TYPE_ARMOR_THORNS) > 0) {
                             $this->thornsLevel[$index] = $i->getEnchantmentLevel(Enchantment::TYPE_ARMOR_THORNS);
                         }
                         if ($spe_Prote !== null) {
                             $this->EPF += 2 * $i->getEnchantmentLevel($spe_Prote);
                         }
                     }
                 }
                 break;
             case self::CAUSE_FALL:
                 //Feather Falling
                 $i = $entity->getInventory()->getBoots();
                 if ($i->isArmor()) {
                     $this->EPF += $i->getEnchantmentLevel(Enchantment::TYPE_ARMOR_PROTECTION);
                     $this->EPF += 3 * $i->getEnchantmentLevel(Enchantment::TYPE_ARMOR_FALL_PROTECTION);
                 }
                 break;
             case self::CAUSE_FIRE_TICK:
             case self::CAUSE_SUFFOCATION:
             case self::CAUSE_DROWNING:
             case self::CAUSE_VOID:
             case self::CAUSE_SUICIDE:
             case self::CAUSE_MAGIC:
             case self::CAUSE_CUSTOM:
             case self::CAUSE_STARVATION:
                 break;
             default:
                 break;
         }
         if ($this->EPF !== 0) {
             $this->EPF = min(20, ceil($this->EPF * mt_rand(50, 100) / 100));
             $this->setRateDamage(1 - 0.04 * $this->EPF, self::MODIFIER_PROTECTION);
         }
     }
 }