mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 16:24:05 +00:00
Effect: get rid of runtimeIDs
This commit is contained in:
parent
c062282954
commit
0404298c74
@ -27,6 +27,7 @@ use pocketmine\entity\effect\Effect;
|
||||
use pocketmine\entity\effect\VanillaEffects;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use function array_key_exists;
|
||||
use function spl_object_id;
|
||||
|
||||
final class EffectIdMap{
|
||||
use SingletonTrait;
|
||||
@ -79,7 +80,7 @@ final class EffectIdMap{
|
||||
|
||||
public function register(int $mcpeId, Effect $effect) : void{
|
||||
$this->idToEffect[$mcpeId] = $effect;
|
||||
$this->effectToId[$effect->getRuntimeId()] = $mcpeId;
|
||||
$this->effectToId[spl_object_id($effect)] = $mcpeId;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?Effect{
|
||||
@ -88,10 +89,10 @@ final class EffectIdMap{
|
||||
}
|
||||
|
||||
public function toId(Effect $effect) : int{
|
||||
if(!array_key_exists($effect->getRuntimeId(), $this->effectToId)){
|
||||
if(!array_key_exists(spl_object_id($effect), $this->effectToId)){
|
||||
//this should never happen, so we treat it as an exceptional condition
|
||||
throw new \InvalidArgumentException("Effect does not have a mapped ID");
|
||||
}
|
||||
return $this->effectToId[$effect->getRuntimeId()];
|
||||
return $this->effectToId[spl_object_id($effect)];
|
||||
}
|
||||
}
|
||||
|
@ -27,32 +27,26 @@ use pocketmine\color\Color;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\entity\Living;
|
||||
use pocketmine\lang\Translatable;
|
||||
use pocketmine\utils\NotCloneable;
|
||||
use pocketmine\utils\NotSerializable;
|
||||
|
||||
class Effect{
|
||||
use NotCloneable;
|
||||
use NotSerializable;
|
||||
|
||||
/**
|
||||
* @param int $internalRuntimeId Internal runtime ID, unique to this effect type. Used for comparisons.
|
||||
* @param Translatable|string $name Translation key used for effect name
|
||||
* @param Color $color Color of bubbles given by this effect
|
||||
* @param bool $bad Whether the effect is harmful
|
||||
* @param bool $hasBubbles Whether the effect has potion bubbles. Some do not (e.g. Instant Damage has its own particles instead of bubbles)
|
||||
*/
|
||||
public function __construct(
|
||||
protected int $internalRuntimeId,
|
||||
protected Translatable|string $name,
|
||||
protected Color $color,
|
||||
protected bool $bad = false,
|
||||
protected bool $hasBubbles = true
|
||||
){}
|
||||
|
||||
/**
|
||||
* Returns a unique identifier for this effect type
|
||||
* WARNING: DO NOT STORE THIS - IT MAY CHANGE BETWEEN RESTARTS
|
||||
*/
|
||||
public function getRuntimeId() : int{
|
||||
return $this->internalRuntimeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the translation key used to translate this effect's name.
|
||||
*/
|
||||
|
@ -30,6 +30,7 @@ use pocketmine\event\entity\EntityEffectRemoveEvent;
|
||||
use pocketmine\utils\ObjectSet;
|
||||
use function abs;
|
||||
use function count;
|
||||
use function spl_object_id;
|
||||
|
||||
class EffectManager{
|
||||
|
||||
@ -83,7 +84,7 @@ class EffectManager{
|
||||
* Removes the effect with the specified ID from the mob.
|
||||
*/
|
||||
public function remove(Effect $effectType) : void{
|
||||
$index = $effectType->getRuntimeId();
|
||||
$index = spl_object_id($effectType);
|
||||
if(isset($this->effects[$index])){
|
||||
$effect = $this->effects[$index];
|
||||
$hasExpired = $effect->hasExpired();
|
||||
@ -113,14 +114,14 @@ class EffectManager{
|
||||
* effect.
|
||||
*/
|
||||
public function get(Effect $effect) : ?EffectInstance{
|
||||
return $this->effects[$effect->getRuntimeId()] ?? null;
|
||||
return $this->effects[spl_object_id($effect)] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified effect is active on the mob.
|
||||
*/
|
||||
public function has(Effect $effect) : bool{
|
||||
return isset($this->effects[$effect->getRuntimeId()]);
|
||||
return isset($this->effects[spl_object_id($effect)]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +135,7 @@ class EffectManager{
|
||||
$oldEffect = null;
|
||||
$cancelled = false;
|
||||
|
||||
$index = $effect->getType()->getRuntimeId();
|
||||
$index = spl_object_id($effect->getType());
|
||||
if(isset($this->effects[$index])){
|
||||
$oldEffect = $this->effects[$index];
|
||||
if(
|
||||
|
@ -34,8 +34,8 @@ class PoisonEffect extends Effect{
|
||||
/** @var bool */
|
||||
private $fatal;
|
||||
|
||||
public function __construct(int $internalRuntimeId, Translatable $name, Color $color, bool $isBad = false, bool $hasBubbles = true, bool $fatal = false){
|
||||
parent::__construct($internalRuntimeId, $name, $color, $isBad, $hasBubbles);
|
||||
public function __construct(Translatable $name, Color $color, bool $isBad = false, bool $hasBubbles = true, bool $fatal = false){
|
||||
parent::__construct($name, $color, $isBad, $hasBubbles);
|
||||
$this->fatal = $fatal;
|
||||
}
|
||||
|
||||
|
@ -65,35 +65,35 @@ final class VanillaEffects{
|
||||
use RegistryTrait;
|
||||
|
||||
protected static function setup() : void{
|
||||
self::register("absorption", new AbsorptionEffect(22, KnownTranslationFactory::potion_absorption(), new Color(0x25, 0x52, 0xa5)));
|
||||
self::register("absorption", new AbsorptionEffect(KnownTranslationFactory::potion_absorption(), new Color(0x25, 0x52, 0xa5)));
|
||||
//TODO: bad_omen
|
||||
self::register("blindness", new Effect(15, KnownTranslationFactory::potion_blindness(), new Color(0x1f, 0x1f, 0x23), true));
|
||||
self::register("conduit_power", new Effect(26, KnownTranslationFactory::potion_conduitPower(), new Color(0x1d, 0xc2, 0xd1)));
|
||||
self::register("fatal_poison", new PoisonEffect(25, KnownTranslationFactory::potion_poison(), new Color(0x4e, 0x93, 0x31), true, true, true));
|
||||
self::register("fire_resistance", new Effect(12, KnownTranslationFactory::potion_fireResistance(), new Color(0xe4, 0x9a, 0x3a)));
|
||||
self::register("haste", new Effect(3, KnownTranslationFactory::potion_digSpeed(), new Color(0xd9, 0xc0, 0x43)));
|
||||
self::register("health_boost", new HealthBoostEffect(21, KnownTranslationFactory::potion_healthBoost(), new Color(0xf8, 0x7d, 0x23)));
|
||||
self::register("hunger", new HungerEffect(17, KnownTranslationFactory::potion_hunger(), new Color(0x58, 0x76, 0x53), true));
|
||||
self::register("instant_damage", new InstantDamageEffect(7, KnownTranslationFactory::potion_harm(), new Color(0x43, 0x0a, 0x09), true, false));
|
||||
self::register("instant_health", new InstantHealthEffect(6, KnownTranslationFactory::potion_heal(), new Color(0xf8, 0x24, 0x23), false, false));
|
||||
self::register("invisibility", new InvisibilityEffect(14, KnownTranslationFactory::potion_invisibility(), new Color(0x7f, 0x83, 0x92)));
|
||||
self::register("jump_boost", new Effect(8, KnownTranslationFactory::potion_jump(), new Color(0x22, 0xff, 0x4c)));
|
||||
self::register("levitation", new LevitationEffect(24, KnownTranslationFactory::potion_levitation(), new Color(0xce, 0xff, 0xff)));
|
||||
self::register("mining_fatigue", new Effect(4, KnownTranslationFactory::potion_digSlowDown(), new Color(0x4a, 0x42, 0x17), true));
|
||||
self::register("nausea", new Effect(9, KnownTranslationFactory::potion_confusion(), new Color(0x55, 0x1d, 0x4a), true));
|
||||
self::register("night_vision", new Effect(16, KnownTranslationFactory::potion_nightVision(), new Color(0x1f, 0x1f, 0xa1)));
|
||||
self::register("poison", new PoisonEffect(19, KnownTranslationFactory::potion_poison(), new Color(0x4e, 0x93, 0x31), true));
|
||||
self::register("regeneration", new RegenerationEffect(10, KnownTranslationFactory::potion_regeneration(), new Color(0xcd, 0x5c, 0xab)));
|
||||
self::register("resistance", new Effect(11, KnownTranslationFactory::potion_resistance(), new Color(0x99, 0x45, 0x3a)));
|
||||
self::register("saturation", new SaturationEffect(23, KnownTranslationFactory::potion_saturation(), new Color(0xf8, 0x24, 0x23), false));
|
||||
self::register("blindness", new Effect(KnownTranslationFactory::potion_blindness(), new Color(0x1f, 0x1f, 0x23), true));
|
||||
self::register("conduit_power", new Effect(KnownTranslationFactory::potion_conduitPower(), new Color(0x1d, 0xc2, 0xd1)));
|
||||
self::register("fatal_poison", new PoisonEffect(KnownTranslationFactory::potion_poison(), new Color(0x4e, 0x93, 0x31), true, true, true));
|
||||
self::register("fire_resistance", new Effect(KnownTranslationFactory::potion_fireResistance(), new Color(0xe4, 0x9a, 0x3a)));
|
||||
self::register("haste", new Effect(KnownTranslationFactory::potion_digSpeed(), new Color(0xd9, 0xc0, 0x43)));
|
||||
self::register("health_boost", new HealthBoostEffect(KnownTranslationFactory::potion_healthBoost(), new Color(0xf8, 0x7d, 0x23)));
|
||||
self::register("hunger", new HungerEffect(KnownTranslationFactory::potion_hunger(), new Color(0x58, 0x76, 0x53), true));
|
||||
self::register("instant_damage", new InstantDamageEffect(KnownTranslationFactory::potion_harm(), new Color(0x43, 0x0a, 0x09), true, false));
|
||||
self::register("instant_health", new InstantHealthEffect(KnownTranslationFactory::potion_heal(), new Color(0xf8, 0x24, 0x23), false, false));
|
||||
self::register("invisibility", new InvisibilityEffect(KnownTranslationFactory::potion_invisibility(), new Color(0x7f, 0x83, 0x92)));
|
||||
self::register("jump_boost", new Effect(KnownTranslationFactory::potion_jump(), new Color(0x22, 0xff, 0x4c)));
|
||||
self::register("levitation", new LevitationEffect(KnownTranslationFactory::potion_levitation(), new Color(0xce, 0xff, 0xff)));
|
||||
self::register("mining_fatigue", new Effect(KnownTranslationFactory::potion_digSlowDown(), new Color(0x4a, 0x42, 0x17), true));
|
||||
self::register("nausea", new Effect(KnownTranslationFactory::potion_confusion(), new Color(0x55, 0x1d, 0x4a), true));
|
||||
self::register("night_vision", new Effect(KnownTranslationFactory::potion_nightVision(), new Color(0x1f, 0x1f, 0xa1)));
|
||||
self::register("poison", new PoisonEffect(KnownTranslationFactory::potion_poison(), new Color(0x4e, 0x93, 0x31), true));
|
||||
self::register("regeneration", new RegenerationEffect(KnownTranslationFactory::potion_regeneration(), new Color(0xcd, 0x5c, 0xab)));
|
||||
self::register("resistance", new Effect(KnownTranslationFactory::potion_resistance(), new Color(0x99, 0x45, 0x3a)));
|
||||
self::register("saturation", new SaturationEffect(KnownTranslationFactory::potion_saturation(), new Color(0xf8, 0x24, 0x23), false));
|
||||
//TODO: slow_falling
|
||||
self::register("slowness", new SlownessEffect(2, KnownTranslationFactory::potion_moveSlowdown(), new Color(0x5a, 0x6c, 0x81), true));
|
||||
self::register("speed", new SpeedEffect(1, KnownTranslationFactory::potion_moveSpeed(), new Color(0x7c, 0xaf, 0xc6)));
|
||||
self::register("strength", new Effect(5, KnownTranslationFactory::potion_damageBoost(), new Color(0x93, 0x24, 0x23)));
|
||||
self::register("slowness", new SlownessEffect(KnownTranslationFactory::potion_moveSlowdown(), new Color(0x5a, 0x6c, 0x81), true));
|
||||
self::register("speed", new SpeedEffect(KnownTranslationFactory::potion_moveSpeed(), new Color(0x7c, 0xaf, 0xc6)));
|
||||
self::register("strength", new Effect(KnownTranslationFactory::potion_damageBoost(), new Color(0x93, 0x24, 0x23)));
|
||||
//TODO: village_hero
|
||||
self::register("water_breathing", new Effect(13, KnownTranslationFactory::potion_waterBreathing(), new Color(0x2e, 0x52, 0x99)));
|
||||
self::register("weakness", new Effect(18, KnownTranslationFactory::potion_weakness(), new Color(0x48, 0x4d, 0x48), true));
|
||||
self::register("wither", new WitherEffect(20, KnownTranslationFactory::potion_wither(), new Color(0x35, 0x2a, 0x27), true));
|
||||
self::register("water_breathing", new Effect(KnownTranslationFactory::potion_waterBreathing(), new Color(0x2e, 0x52, 0x99)));
|
||||
self::register("weakness", new Effect(KnownTranslationFactory::potion_weakness(), new Color(0x48, 0x4d, 0x48), true));
|
||||
self::register("wither", new WitherEffect(KnownTranslationFactory::potion_wither(), new Color(0x35, 0x2a, 0x27), true));
|
||||
}
|
||||
|
||||
protected static function register(string $name, Effect $member) : void{
|
||||
|
Loading…
x
Reference in New Issue
Block a user