PocketMine-MP/src/item/enchantment/ProtectionEnchantment.php
2021-09-03 21:20:43 +01:00

72 lines
2.4 KiB
PHP

<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\item\enchantment;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\lang\Translatable;
use function array_flip;
use function floor;
class ProtectionEnchantment extends Enchantment{
/** @var float */
protected $typeModifier;
/** @var int[]|null */
protected $applicableDamageTypes = null;
/**
* ProtectionEnchantment constructor.
*
* @param int[]|null $applicableDamageTypes EntityDamageEvent::CAUSE_* constants which this enchantment type applies to, or null if it applies to all types of damage.
*/
public function __construct(Translatable|string $name, int $rarity, int $primaryItemFlags, int $secondaryItemFlags, int $maxLevel, float $typeModifier, ?array $applicableDamageTypes){
parent::__construct($name, $rarity, $primaryItemFlags, $secondaryItemFlags, $maxLevel);
$this->typeModifier = $typeModifier;
if($applicableDamageTypes !== null){
$this->applicableDamageTypes = array_flip($applicableDamageTypes);
}
}
/**
* Returns the multiplier by which this enchantment type's EPF increases with each enchantment level.
*/
public function getTypeModifier() : float{
return $this->typeModifier;
}
/**
* Returns the base EPF this enchantment type offers for the given enchantment level.
*/
public function getProtectionFactor(int $level) : int{
return (int) floor((6 + $level ** 2) * $this->typeModifier / 3);
}
/**
* Returns whether this enchantment type offers protection from the specified damage source's cause.
*/
public function isApplicable(EntityDamageEvent $event) : bool{
return $this->applicableDamageTypes === null or isset($this->applicableDamageTypes[$event->getCause()]);
}
}