Files
PocketMine-MP/src/pocketmine/item/enchantment/ProtectionEnchantment.php
Dylan K. Taylor 4b9a142a5d Import global functions and constants for enhanced performance
This is better for performance because these then don't need to be reevaluated every time they are called.

When encountering an unqualified function or constant reference, PHP will first try to locate a symbol in the current namespace by that name, and then fall back to the global namespace.
This short-circuits the check, which has substantial performance effects in some cases - in particular, ord(), chr() and strlen() show ~1500x faster calls when they are fully qualified.

However, this doesn't mean that PM is getting a massive amount faster. In real world terms, this translates to about 10-15% performance improvement.
But before anyone gets excited, you should know that the CodeOptimizer in the PreProcessor repo has been applying fully-qualified symbol optimizations to Jenkins builds for years, which is one of the reasons why Jenkins builds have better performance than home-built or source installations.
We're choosing to do this for the sake of future SafePHP integration and also to be able to get rid of the buggy CodeOptimizer, so that phar and source are more consistent.
2019-01-04 20:43:15 +00:00

87 lines
2.7 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 function array_flip;
use function floor;
class ProtectionEnchantment extends Enchantment{
/** @var float */
protected $typeModifier;
/** @var int[]|null */
protected $applicableDamageTypes = null;
/**
* ProtectionEnchantment constructor.
*
* @param int $id
* @param string $name
* @param int $rarity
* @param int $primaryItemFlags
* @param int $secondaryItemFlags
* @param int $maxLevel
* @param float $typeModifier
* @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(int $id, string $name, int $rarity, int $primaryItemFlags, int $secondaryItemFlags, int $maxLevel, float $typeModifier, ?array $applicableDamageTypes){
parent::__construct($id, $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.
* @return float
*/
public function getTypeModifier() : float{
return $this->typeModifier;
}
/**
* Returns the base EPF this enchantment type offers for the given enchantment level.
*
* @param int $level
*
* @return int
*/
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.
*
* @param EntityDamageEvent $event
*
* @return bool
*/
public function isApplicable(EntityDamageEvent $event) : bool{
return $this->applicableDamageTypes === null or isset($this->applicableDamageTypes[$event->getCause()]);
}
}