mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
Separated effects' MCPE ID registration from VanillaEffects
This commit is contained in:
parent
cf7f50af06
commit
3f254bd49c
97
src/data/bedrock/EffectIdMap.php
Normal file
97
src/data/bedrock/EffectIdMap.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?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\data\bedrock;
|
||||
|
||||
use pocketmine\entity\effect\Effect;
|
||||
use pocketmine\entity\effect\VanillaEffects;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use function array_key_exists;
|
||||
|
||||
final class EffectIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var Effect[]
|
||||
* @phpstan-var array<int, Effect>
|
||||
*/
|
||||
private $idToEffect = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private $effectToId = [];
|
||||
|
||||
private function __construct(){
|
||||
$this->register(EffectIds::SPEED, VanillaEffects::SPEED());
|
||||
$this->register(EffectIds::SLOWNESS, VanillaEffects::SLOWNESS());
|
||||
$this->register(EffectIds::HASTE, VanillaEffects::HASTE());
|
||||
$this->register(EffectIds::MINING_FATIGUE, VanillaEffects::MINING_FATIGUE());
|
||||
$this->register(EffectIds::STRENGTH, VanillaEffects::STRENGTH());
|
||||
$this->register(EffectIds::INSTANT_HEALTH, VanillaEffects::INSTANT_HEALTH());
|
||||
$this->register(EffectIds::INSTANT_DAMAGE, VanillaEffects::INSTANT_DAMAGE());
|
||||
$this->register(EffectIds::JUMP_BOOST, VanillaEffects::JUMP_BOOST());
|
||||
$this->register(EffectIds::NAUSEA, VanillaEffects::NAUSEA());
|
||||
$this->register(EffectIds::REGENERATION, VanillaEffects::REGENERATION());
|
||||
$this->register(EffectIds::RESISTANCE, VanillaEffects::RESISTANCE());
|
||||
$this->register(EffectIds::FIRE_RESISTANCE, VanillaEffects::FIRE_RESISTANCE());
|
||||
$this->register(EffectIds::WATER_BREATHING, VanillaEffects::WATER_BREATHING());
|
||||
$this->register(EffectIds::INVISIBILITY, VanillaEffects::INVISIBILITY());
|
||||
$this->register(EffectIds::BLINDNESS, VanillaEffects::BLINDNESS());
|
||||
$this->register(EffectIds::NIGHT_VISION, VanillaEffects::NIGHT_VISION());
|
||||
$this->register(EffectIds::HUNGER, VanillaEffects::HUNGER());
|
||||
$this->register(EffectIds::WEAKNESS, VanillaEffects::WEAKNESS());
|
||||
$this->register(EffectIds::POISON, VanillaEffects::POISON());
|
||||
$this->register(EffectIds::WITHER, VanillaEffects::WITHER());
|
||||
$this->register(EffectIds::HEALTH_BOOST, VanillaEffects::HEALTH_BOOST());
|
||||
$this->register(EffectIds::ABSORPTION, VanillaEffects::ABSORPTION());
|
||||
$this->register(EffectIds::SATURATION, VanillaEffects::SATURATION());
|
||||
$this->register(EffectIds::LEVITATION, VanillaEffects::LEVITATION());
|
||||
$this->register(EffectIds::FATAL_POISON, VanillaEffects::FATAL_POISON());
|
||||
$this->register(EffectIds::CONDUIT_POWER, VanillaEffects::CONDUIT_POWER());
|
||||
//TODO: SLOW_FALLING
|
||||
//TODO: BAD_OMEN
|
||||
//TODO: VILLAGE_HERO
|
||||
}
|
||||
|
||||
//TODO: not a big fan of the code duplication here :(
|
||||
|
||||
public function register(int $mcpeId, Effect $effect) : void{
|
||||
$this->idToEffect[$mcpeId] = $effect;
|
||||
$this->effectToId[$effect->getRuntimeId()] = $mcpeId;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?Effect{
|
||||
//we might not have all the effect IDs registered
|
||||
return $this->idToEffect[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(Effect $effect) : int{
|
||||
if(!array_key_exists($effect->getRuntimeId(), $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()];
|
||||
}
|
||||
}
|
61
src/data/bedrock/EffectIds.php
Normal file
61
src/data/bedrock/EffectIds.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?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\data\bedrock;
|
||||
|
||||
final class EffectIds{
|
||||
|
||||
private function __construct(){
|
||||
//NOOP
|
||||
}
|
||||
|
||||
public const SPEED = 1;
|
||||
public const SLOWNESS = 2;
|
||||
public const HASTE = 3;
|
||||
public const MINING_FATIGUE = 4;
|
||||
public const STRENGTH = 5;
|
||||
public const INSTANT_HEALTH = 6;
|
||||
public const INSTANT_DAMAGE = 7;
|
||||
public const JUMP_BOOST = 8;
|
||||
public const NAUSEA = 9;
|
||||
public const REGENERATION = 10;
|
||||
public const RESISTANCE = 11;
|
||||
public const FIRE_RESISTANCE = 12;
|
||||
public const WATER_BREATHING = 13;
|
||||
public const INVISIBILITY = 14;
|
||||
public const BLINDNESS = 15;
|
||||
public const NIGHT_VISION = 16;
|
||||
public const HUNGER = 17;
|
||||
public const WEAKNESS = 18;
|
||||
public const POISON = 19;
|
||||
public const WITHER = 20;
|
||||
public const HEALTH_BOOST = 21;
|
||||
public const ABSORPTION = 22;
|
||||
public const SATURATION = 23;
|
||||
public const LEVITATION = 24;
|
||||
public const FATAL_POISON = 25;
|
||||
public const CONDUIT_POWER = 26;
|
||||
public const SLOW_FALLING = 27;
|
||||
public const BAD_OMEN = 28;
|
||||
public const VILLAGE_HERO = 29;
|
||||
}
|
@ -25,6 +25,7 @@ namespace pocketmine\entity;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockLegacyIds;
|
||||
use pocketmine\data\bedrock\EffectIdMap;
|
||||
use pocketmine\entity\animation\DeathAnimation;
|
||||
use pocketmine\entity\animation\HurtAnimation;
|
||||
use pocketmine\entity\animation\RespawnAnimation;
|
||||
@ -151,7 +152,7 @@ abstract class Living extends Entity{
|
||||
$activeEffectsTag = $nbt->getListTag("ActiveEffects");
|
||||
if($activeEffectsTag !== null){
|
||||
foreach($activeEffectsTag as $e){
|
||||
$effect = VanillaEffects::byMcpeId($e->getByte("Id"));
|
||||
$effect = EffectIdMap::getInstance()->fromId($e->getByte("Id"));
|
||||
if($effect === null){
|
||||
continue;
|
||||
}
|
||||
@ -240,7 +241,7 @@ abstract class Living extends Entity{
|
||||
$effects = [];
|
||||
foreach($this->effectManager->all() as $effect){
|
||||
$effects[] = CompoundTag::create()
|
||||
->setByte("Id", $effect->getId())
|
||||
->setByte("Id", EffectIdMap::getInstance()->toId($effect->getType()))
|
||||
->setByte("Amplifier", Binary::signByte($effect->getAmplifier()))
|
||||
->setInt("Duration", $effect->getDuration())
|
||||
->setByte("Ambient", $effect->isAmbient() ? 1 : 0)
|
||||
|
@ -30,7 +30,7 @@ use pocketmine\entity\Living;
|
||||
class Effect{
|
||||
|
||||
/** @var int */
|
||||
protected $id;
|
||||
protected $internalRuntimeId;
|
||||
/** @var string */
|
||||
protected $name;
|
||||
/** @var Color */
|
||||
@ -41,14 +41,14 @@ class Effect{
|
||||
protected $hasBubbles;
|
||||
|
||||
/**
|
||||
* @param int $id Effect ID as per Minecraft PE
|
||||
* @param int $internalRuntimeId Internal runtime ID, unique to this effect type. Used for comparisons.
|
||||
* @param string $name Translation key used for effect name
|
||||
* @param Color $color Color of bubbles given by this effect
|
||||
* @param bool $isBad 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(int $id, string $name, Color $color, bool $isBad = false, bool $hasBubbles = true){
|
||||
$this->id = $id;
|
||||
public function __construct(int $internalRuntimeId, string $name, Color $color, bool $isBad = false, bool $hasBubbles = true){
|
||||
$this->internalRuntimeId = $internalRuntimeId;
|
||||
$this->name = $name;
|
||||
$this->color = $color;
|
||||
$this->bad = $isBad;
|
||||
@ -56,10 +56,11 @@ class Effect{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the effect ID as per Minecraft PE
|
||||
* Returns a unique identifier for this effect type
|
||||
* WARNING: DO NOT STORE THIS - IT MAY CHANGE BETWEEN RESTARTS
|
||||
*/
|
||||
public function getId() : int{
|
||||
return $this->id;
|
||||
public function getRuntimeId() : int{
|
||||
return $this->internalRuntimeId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,10 +58,6 @@ class EffectInstance{
|
||||
$this->color = $overrideColor ?? $effectType->getColor();
|
||||
}
|
||||
|
||||
public function getId() : int{
|
||||
return $this->effectType->getId();
|
||||
}
|
||||
|
||||
public function getType() : Effect{
|
||||
return $this->effectType;
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ class EffectManager{
|
||||
* Removes the effect with the specified ID from the mob.
|
||||
*/
|
||||
public function remove(Effect $effectType) : void{
|
||||
$index = $effectType->getId();
|
||||
$index = $effectType->getRuntimeId();
|
||||
if(isset($this->effects[$index])){
|
||||
$effect = $this->effects[$index];
|
||||
$hasExpired = $effect->hasExpired();
|
||||
@ -113,14 +113,14 @@ class EffectManager{
|
||||
* effect.
|
||||
*/
|
||||
public function get(Effect $effect) : ?EffectInstance{
|
||||
return $this->effects[$effect->getId()] ?? null;
|
||||
return $this->effects[$effect->getRuntimeId()] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified effect is active on the mob.
|
||||
*/
|
||||
public function has(Effect $effect) : bool{
|
||||
return isset($this->effects[$effect->getId()]);
|
||||
return isset($this->effects[$effect->getRuntimeId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +134,7 @@ class EffectManager{
|
||||
$oldEffect = null;
|
||||
$cancelled = false;
|
||||
|
||||
$index = $effect->getType()->getId();
|
||||
$index = $effect->getType()->getRuntimeId();
|
||||
if(isset($this->effects[$index])){
|
||||
$oldEffect = $this->effects[$index];
|
||||
if(
|
||||
|
@ -33,8 +33,8 @@ class PoisonEffect extends Effect{
|
||||
/** @var bool */
|
||||
private $fatal;
|
||||
|
||||
public function __construct(int $id, string $name, Color $color, bool $isBad = false, bool $hasBubbles = true, bool $fatal = false){
|
||||
parent::__construct($id, $name, $color, $isBad, $hasBubbles);
|
||||
public function __construct(int $internalRuntimeId, string $name, Color $color, bool $isBad = false, bool $hasBubbles = true, bool $fatal = false){
|
||||
parent::__construct($internalRuntimeId, $name, $color, $isBad, $hasBubbles);
|
||||
$this->fatal = $fatal;
|
||||
}
|
||||
|
||||
|
@ -62,9 +62,6 @@ use function assert;
|
||||
final class VanillaEffects{
|
||||
use RegistryTrait;
|
||||
|
||||
/** @var Effect[] */
|
||||
private static $mcpeIdMap = [];
|
||||
|
||||
protected static function setup() : void{
|
||||
self::register("absorption", new AbsorptionEffect(22, "%potion.absorption", new Color(0x25, 0x52, 0xa5)));
|
||||
//TODO: bad_omen
|
||||
@ -99,13 +96,6 @@ final class VanillaEffects{
|
||||
|
||||
protected static function register(string $name, Effect $member) : void{
|
||||
self::_registryRegister($name, $member);
|
||||
assert(!isset(self::$mcpeIdMap[$member->getId()]));
|
||||
self::$mcpeIdMap[$member->getId()] = $member;
|
||||
}
|
||||
|
||||
public static function byMcpeId(int $id) : ?Effect{
|
||||
self::checkInit();
|
||||
return self::$mcpeIdMap[$id] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,6 +25,7 @@ namespace pocketmine\network\mcpe;
|
||||
|
||||
use Ds\Set;
|
||||
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
|
||||
use pocketmine\data\bedrock\EffectIdMap;
|
||||
use pocketmine\entity\Attribute;
|
||||
use pocketmine\entity\effect\EffectInstance;
|
||||
use pocketmine\entity\Entity;
|
||||
@ -742,11 +743,12 @@ class NetworkSession{
|
||||
}
|
||||
|
||||
public function onEntityEffectAdded(Living $entity, EffectInstance $effect, bool $replacesOldEffect) : void{
|
||||
$this->sendDataPacket(MobEffectPacket::add($entity->getId(), $replacesOldEffect, $effect->getId(), $effect->getAmplifier(), $effect->isVisible(), $effect->getDuration()));
|
||||
//TODO: we may need yet another effect <=> ID map in the future depending on protocol changes
|
||||
$this->sendDataPacket(MobEffectPacket::add($entity->getId(), $replacesOldEffect, EffectIdMap::getInstance()->toId($effect->getType()), $effect->getAmplifier(), $effect->isVisible(), $effect->getDuration()));
|
||||
}
|
||||
|
||||
public function onEntityEffectRemoved(Living $entity, EffectInstance $effect) : void{
|
||||
$this->sendDataPacket(MobEffectPacket::remove($entity->getId(), $effect->getId()));
|
||||
$this->sendDataPacket(MobEffectPacket::remove($entity->getId(), EffectIdMap::getInstance()->toId($effect->getType())));
|
||||
}
|
||||
|
||||
public function onEntityRemoved(Entity $entity) : void{
|
||||
|
38
tests/phpunit/data/bedrock/EffectIdMapTest.php
Normal file
38
tests/phpunit/data/bedrock/EffectIdMapTest.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?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\data\bedrock;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use pocketmine\entity\effect\VanillaEffects;
|
||||
|
||||
class EffectIdMapTest extends TestCase{
|
||||
|
||||
public function testAllEffectsMapped() : void{
|
||||
foreach(VanillaEffects::getAll() as $e){
|
||||
$id = EffectIdMap::getInstance()->toId($e);
|
||||
$e2 = EffectIdMap::getInstance()->fromId($id);
|
||||
self::assertTrue($e === $e2);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user