mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
Deduplicate a bunch of repeated type ID map code
This commit is contained in:
parent
c7a311c17a
commit
b56f1b679e
@ -1 +1 @@
|
||||
Subproject commit a053f65e1897e432478229071383fe1ba16032c3
|
||||
Subproject commit 9f984a1dc154603a27543ecdcebc6d6a5871f425
|
@ -29,18 +29,10 @@ use pocketmine\utils\SingletonTrait;
|
||||
|
||||
final class DyeColorIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var DyeColor[]
|
||||
* @phpstan-var array<int, DyeColor>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<DyeColor> */
|
||||
use IntSaveIdMapTrait {
|
||||
register as registerInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var DyeColor[]
|
||||
@ -74,16 +66,11 @@ final class DyeColorIdMap{
|
||||
}
|
||||
|
||||
private function register(int $id, string $itemId, DyeColor $color) : void{
|
||||
$this->idToEnum[$id] = $color;
|
||||
$this->enumToId[$color->id()] = $id;
|
||||
$this->registerInt($id, $color);
|
||||
$this->itemIdToEnum[$itemId] = $color;
|
||||
$this->enumToItemId[$color->id()] = $itemId;
|
||||
}
|
||||
|
||||
public function toId(DyeColor $color) : int{
|
||||
return $this->enumToId[$color->id()]; //TODO: is it possible for this to be missing?
|
||||
}
|
||||
|
||||
public function toInvertedId(DyeColor $color) : int{
|
||||
return ~$this->toId($color) & 0xf;
|
||||
}
|
||||
@ -92,10 +79,6 @@ final class DyeColorIdMap{
|
||||
return $this->enumToItemId[$color->id()];
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?DyeColor{
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
public function fromInvertedId(int $id) : ?DyeColor{
|
||||
return $this->fromId(~$id & 0xf);
|
||||
}
|
||||
|
@ -31,18 +31,8 @@ use function spl_object_id;
|
||||
|
||||
final class EffectIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var Effect[]
|
||||
* @phpstan-var array<int, Effect>
|
||||
*/
|
||||
private array $idToEffect = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $effectToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<Effect> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
private function __construct(){
|
||||
$this->register(EffectIds::SPEED, VanillaEffects::SPEED());
|
||||
@ -76,24 +66,4 @@ final class EffectIdMap{
|
||||
//TODO: VILLAGE_HERO
|
||||
$this->register(EffectIds::DARKNESS, VanillaEffects::DARKNESS());
|
||||
}
|
||||
|
||||
//TODO: not a big fan of the code duplication here :(
|
||||
|
||||
public function register(int $mcpeId, Effect $effect) : void{
|
||||
$this->idToEffect[$mcpeId] = $effect;
|
||||
$this->effectToId[spl_object_id($effect)] = $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(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[spl_object_id($effect)];
|
||||
}
|
||||
}
|
||||
|
@ -34,17 +34,8 @@ use function spl_object_id;
|
||||
*/
|
||||
final class EnchantmentIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var Enchantment[]
|
||||
* @phpstan-var array<int, Enchantment>
|
||||
*/
|
||||
private array $idToEnch = [];
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enchToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<Enchantment> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
private function __construct(){
|
||||
$this->register(EnchantmentIds::PROTECTION, VanillaEnchantments::PROTECTION());
|
||||
@ -77,22 +68,4 @@ final class EnchantmentIdMap{
|
||||
|
||||
$this->register(EnchantmentIds::SWIFT_SNEAK, VanillaEnchantments::SWIFT_SNEAK());
|
||||
}
|
||||
|
||||
public function register(int $mcpeId, Enchantment $enchantment) : void{
|
||||
$this->idToEnch[$mcpeId] = $enchantment;
|
||||
$this->enchToId[spl_object_id($enchantment)] = $mcpeId;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?Enchantment{
|
||||
//we might not have all the enchantment IDs registered
|
||||
return $this->idToEnch[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(Enchantment $enchantment) : int{
|
||||
if(!array_key_exists(spl_object_id($enchantment), $this->enchToId)){
|
||||
//this should never happen, so we treat it as an exceptional condition
|
||||
throw new \InvalidArgumentException("Enchantment does not have a mapped ID");
|
||||
}
|
||||
return $this->enchToId[spl_object_id($enchantment)];
|
||||
}
|
||||
}
|
||||
|
82
src/data/bedrock/IntSaveIdMapTrait.php
Normal file
82
src/data/bedrock/IntSaveIdMapTrait.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?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 function abs;
|
||||
use function array_key_exists;
|
||||
use function spl_object_id;
|
||||
|
||||
/**
|
||||
* @phpstan-template TObject of object
|
||||
*/
|
||||
trait IntSaveIdMapTrait{
|
||||
|
||||
/**
|
||||
* @var object[]
|
||||
* @phpstan-var array<int, TObject>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
|
||||
/**
|
||||
* @phpstan-param TObject $enum
|
||||
*/
|
||||
protected function getRuntimeId(object $enum) : int{
|
||||
//this is fine for enums and non-cloning object registries
|
||||
return spl_object_id($enum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-param TObject $enum
|
||||
*/
|
||||
public function register(int $saveId, object $enum) : void{
|
||||
$this->idToEnum[$saveId] = $enum;
|
||||
$this->enumToId[$this->getRuntimeId($enum)] = $saveId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-return TObject|null
|
||||
*/
|
||||
public function fromId(int $id) : ?object{
|
||||
//we might not have all the effect IDs registered
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-param TObject $enum
|
||||
*/
|
||||
public function toId(object $enum) : int{
|
||||
$runtimeId = $this->getRuntimeId($enum);
|
||||
if(!array_key_exists($runtimeId, $this->enumToId)){
|
||||
//this should never happen, so we treat it as an exceptional condition
|
||||
throw new \InvalidArgumentException("Object does not have a mapped save ID");
|
||||
}
|
||||
return $this->enumToId[$runtimeId];
|
||||
}
|
||||
}
|
@ -28,18 +28,8 @@ use pocketmine\utils\SingletonTrait;
|
||||
|
||||
final class MedicineTypeIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var MedicineType[]
|
||||
* @phpstan-var array<int, MedicineType>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<MedicineType> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
private function __construct(){
|
||||
$this->register(MedicineTypeIds::ANTIDOTE, MedicineType::ANTIDOTE());
|
||||
@ -47,20 +37,4 @@ final class MedicineTypeIdMap{
|
||||
$this->register(MedicineTypeIds::EYE_DROPS, MedicineType::EYE_DROPS());
|
||||
$this->register(MedicineTypeIds::TONIC, MedicineType::TONIC());
|
||||
}
|
||||
|
||||
private function register(int $id, MedicineType $type) : void{
|
||||
$this->idToEnum[$id] = $type;
|
||||
$this->enumToId[$type->id()] = $id;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?MedicineType{
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(MedicineType $type) : int{
|
||||
if(!isset($this->enumToId[$type->id()])){
|
||||
throw new \InvalidArgumentException("Type does not have a mapped ID");
|
||||
}
|
||||
return $this->enumToId[$type->id()];
|
||||
}
|
||||
}
|
||||
|
@ -28,18 +28,8 @@ use pocketmine\utils\SingletonTrait;
|
||||
|
||||
final class MobHeadTypeIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var MobHeadType[]
|
||||
* @phpstan-var array<int, MobHeadType>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<MobHeadType> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
private function __construct(){
|
||||
$this->register(0, MobHeadType::SKELETON());
|
||||
@ -50,20 +40,4 @@ final class MobHeadTypeIdMap{
|
||||
$this->register(5, MobHeadType::DRAGON());
|
||||
$this->register(6, MobHeadType::PIGLIN());
|
||||
}
|
||||
|
||||
private function register(int $id, MobHeadType $type) : void{
|
||||
$this->idToEnum[$id] = $type;
|
||||
$this->enumToId[$type->id()] = $id;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?MobHeadType{
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(MobHeadType $type) : int{
|
||||
if(!isset($this->enumToId[$type->id()])){
|
||||
throw new \InvalidArgumentException("Type does not have a mapped ID");
|
||||
}
|
||||
return $this->enumToId[$type->id()];
|
||||
}
|
||||
}
|
||||
|
@ -30,17 +30,8 @@ use function array_key_exists;
|
||||
|
||||
final class MushroomBlockTypeIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var MushroomBlockType[]
|
||||
* @phpstan-var array<int, MushroomBlockType>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<MushroomBlockType> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
public function __construct(){
|
||||
$this->register(LegacyMeta::MUSHROOM_BLOCK_ALL_PORES, MushroomBlockType::PORES());
|
||||
@ -55,20 +46,4 @@ final class MushroomBlockTypeIdMap{
|
||||
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_SOUTHEAST_CORNER, MushroomBlockType::CAP_SOUTHEAST());
|
||||
$this->register(LegacyMeta::MUSHROOM_BLOCK_ALL_CAP, MushroomBlockType::ALL_CAP());
|
||||
}
|
||||
|
||||
public function register(int $id, MushroomBlockType $type) : void{
|
||||
$this->idToEnum[$id] = $type;
|
||||
$this->enumToId[$type->id()] = $id;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?MushroomBlockType{
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(MushroomBlockType $type) : int{
|
||||
if(!array_key_exists($type->id(), $this->enumToId)){
|
||||
throw new \InvalidArgumentException("Mushroom block type does not have a mapped ID"); //this should never happen
|
||||
}
|
||||
return $this->enumToId[$type->id()];
|
||||
}
|
||||
}
|
||||
|
@ -28,18 +28,8 @@ use pocketmine\world\sound\NoteInstrument;
|
||||
|
||||
final class NoteInstrumentIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var NoteInstrument[]
|
||||
* @phpstan-var array<int, NoteInstrument>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<NoteInstrument> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
private function __construct(){
|
||||
$this->register(0, NoteInstrument::PIANO());
|
||||
@ -59,20 +49,4 @@ final class NoteInstrumentIdMap{
|
||||
$this->register(14, NoteInstrument::BANJO());
|
||||
$this->register(15, NoteInstrument::PLING());
|
||||
}
|
||||
|
||||
private function register(int $id, NoteInstrument $instrument) : void{
|
||||
$this->idToEnum[$id] = $instrument;
|
||||
$this->enumToId[$instrument->id()] = $id;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?NoteInstrument{
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(NoteInstrument $instrument) : int{
|
||||
if(!isset($this->enumToId[$instrument->id()])){
|
||||
throw new \InvalidArgumentException("Type does not have a mapped ID");
|
||||
}
|
||||
return $this->enumToId[$instrument->id()];
|
||||
}
|
||||
}
|
||||
|
@ -28,18 +28,8 @@ use pocketmine\utils\SingletonTrait;
|
||||
|
||||
final class PotionTypeIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var PotionType[]
|
||||
* @phpstan-var array<int, PotionType>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<PotionType> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
private function __construct(){
|
||||
$this->register(PotionTypeIds::WATER, PotionType::WATER());
|
||||
@ -86,20 +76,4 @@ final class PotionTypeIdMap{
|
||||
$this->register(PotionTypeIds::LONG_SLOW_FALLING, PotionType::LONG_SLOW_FALLING());
|
||||
$this->register(PotionTypeIds::STRONG_SLOWNESS, PotionType::STRONG_SLOWNESS());
|
||||
}
|
||||
|
||||
private function register(int $id, PotionType $type) : void{
|
||||
$this->idToEnum[$id] = $type;
|
||||
$this->enumToId[$type->id()] = $id;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?PotionType{
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(PotionType $type) : int{
|
||||
if(!isset($this->enumToId[$type->id()])){
|
||||
throw new \InvalidArgumentException("Type does not have a mapped ID");
|
||||
}
|
||||
return $this->enumToId[$type->id()];
|
||||
}
|
||||
}
|
||||
|
@ -28,18 +28,8 @@ use pocketmine\utils\SingletonTrait;
|
||||
|
||||
final class SuspiciousStewTypeIdMap{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var SuspiciousStewType[]
|
||||
* @phpstan-var array<int, SuspiciousStewType>
|
||||
*/
|
||||
private array $idToEnum = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<int, int>
|
||||
*/
|
||||
private array $enumToId = [];
|
||||
/** @phpstan-use IntSaveIdMapTrait<SuspiciousStewType> */
|
||||
use IntSaveIdMapTrait;
|
||||
|
||||
private function __construct(){
|
||||
$this->register(SuspiciousStewTypeIds::POPPY, SuspiciousStewType::POPPY());
|
||||
@ -53,20 +43,4 @@ final class SuspiciousStewTypeIdMap{
|
||||
$this->register(SuspiciousStewTypeIds::OXEYE_DAISY, SuspiciousStewType::OXEYE_DAISY());
|
||||
$this->register(SuspiciousStewTypeIds::WITHER_ROSE, SuspiciousStewType::WITHER_ROSE());
|
||||
}
|
||||
|
||||
private function register(int $id, SuspiciousStewType $type) : void{
|
||||
$this->idToEnum[$id] = $type;
|
||||
$this->enumToId[$type->id()] = $id;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : ?SuspiciousStewType{
|
||||
return $this->idToEnum[$id] ?? null;
|
||||
}
|
||||
|
||||
public function toId(SuspiciousStewType $type) : int{
|
||||
if(!isset($this->enumToId[$type->id()])){
|
||||
throw new \InvalidArgumentException("Type does not have a mapped ID");
|
||||
}
|
||||
return $this->enumToId[$type->id()];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user