mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 10:22:56 +00:00
Implement armor damage protection enchantments (#1839)
This commit is contained in:
@ -23,6 +23,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\item\enchantment;
|
||||
|
||||
use pocketmine\event\entity\EntityDamageEvent;
|
||||
|
||||
/**
|
||||
* Manages enchantment type data.
|
||||
*/
|
||||
@ -88,9 +90,23 @@ class Enchantment{
|
||||
public static function init(){
|
||||
self::$enchantments = new \SplFixedArray(256);
|
||||
|
||||
self::registerEnchantment(new Enchantment(self::PROTECTION, "%enchantment.protect.all", self::RARITY_COMMON, self::SLOT_ARMOR, 4));
|
||||
self::registerEnchantment(new Enchantment(self::FIRE_PROTECTION, "%enchantment.protect.fire", self::RARITY_UNCOMMON, self::SLOT_ARMOR, 4));
|
||||
self::registerEnchantment(new Enchantment(self::FEATHER_FALLING, "%enchantment.protect.fall", self::RARITY_UNCOMMON, self::SLOT_FEET, 4));
|
||||
self::registerEnchantment(new ProtectionEnchantment(self::PROTECTION, "%enchant.protect.all", self::RARITY_COMMON, self::SLOT_ARMOR, 4, 0.75, null));
|
||||
self::registerEnchantment(new ProtectionEnchantment(self::FIRE_PROTECTION, "%enchantment.protect.fire", self::RARITY_UNCOMMON, self::SLOT_ARMOR, 4, 1.25, [
|
||||
EntityDamageEvent::CAUSE_FIRE,
|
||||
EntityDamageEvent::CAUSE_FIRE_TICK,
|
||||
EntityDamageEvent::CAUSE_LAVA
|
||||
//TODO: check fireballs
|
||||
]));
|
||||
self::registerEnchantment(new ProtectionEnchantment(self::FEATHER_FALLING, "%enchantment.protect.fall", self::RARITY_UNCOMMON, self::SLOT_FEET, 4, 2.5, [
|
||||
EntityDamageEvent::CAUSE_FALL
|
||||
]));
|
||||
self::registerEnchantment(new ProtectionEnchantment(self::BLAST_PROTECTION, "%enchantment.protect.explosion", self::RARITY_RARE, self::SLOT_ARMOR, 4, 1.5, [
|
||||
EntityDamageEvent::CAUSE_BLOCK_EXPLOSION,
|
||||
EntityDamageEvent::CAUSE_ENTITY_EXPLOSION
|
||||
]));
|
||||
self::registerEnchantment(new ProtectionEnchantment(self::PROJECTILE_PROTECTION, "%enchantment.protect.projectile", self::RARITY_UNCOMMON, self::SLOT_ARMOR, 4, 1.5, [
|
||||
EntityDamageEvent::CAUSE_PROJECTILE
|
||||
]));
|
||||
|
||||
self::registerEnchantment(new Enchantment(self::RESPIRATION, "%enchantment.oxygen", self::RARITY_RARE, self::SLOT_HEAD, 3));
|
||||
|
||||
|
81
src/pocketmine/item/enchantment/ProtectionEnchantment.php
Normal file
81
src/pocketmine/item/enchantment/ProtectionEnchantment.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?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;
|
||||
|
||||
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 $slot
|
||||
* @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 $slot, int $maxLevel, float $typeModifier, ?array $applicableDamageTypes){
|
||||
parent::__construct($id, $name, $rarity, $slot, $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()]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user