Move potion types to enum

This commit is contained in:
Dylan K. Taylor
2021-06-19 21:39:23 +01:00
parent 908fa5f901
commit 5387456e44
8 changed files with 398 additions and 253 deletions

View File

@ -0,0 +1,99 @@
<?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\item\PotionType;
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;
private function __construct(){
$this->register(PotionTypeIds::WATER, PotionType::WATER());
$this->register(PotionTypeIds::MUNDANE, PotionType::MUNDANE());
$this->register(PotionTypeIds::LONG_MUNDANE, PotionType::LONG_MUNDANE());
$this->register(PotionTypeIds::THICK, PotionType::THICK());
$this->register(PotionTypeIds::AWKWARD, PotionType::AWKWARD());
$this->register(PotionTypeIds::NIGHT_VISION, PotionType::NIGHT_VISION());
$this->register(PotionTypeIds::LONG_NIGHT_VISION, PotionType::LONG_NIGHT_VISION());
$this->register(PotionTypeIds::INVISIBILITY, PotionType::INVISIBILITY());
$this->register(PotionTypeIds::LONG_INVISIBILITY, PotionType::LONG_INVISIBILITY());
$this->register(PotionTypeIds::LEAPING, PotionType::LEAPING());
$this->register(PotionTypeIds::LONG_LEAPING, PotionType::LONG_LEAPING());
$this->register(PotionTypeIds::STRONG_LEAPING, PotionType::STRONG_LEAPING());
$this->register(PotionTypeIds::FIRE_RESISTANCE, PotionType::FIRE_RESISTANCE());
$this->register(PotionTypeIds::LONG_FIRE_RESISTANCE, PotionType::LONG_FIRE_RESISTANCE());
$this->register(PotionTypeIds::SWIFTNESS, PotionType::SWIFTNESS());
$this->register(PotionTypeIds::LONG_SWIFTNESS, PotionType::LONG_SWIFTNESS());
$this->register(PotionTypeIds::STRONG_SWIFTNESS, PotionType::STRONG_SWIFTNESS());
$this->register(PotionTypeIds::SLOWNESS, PotionType::SLOWNESS());
$this->register(PotionTypeIds::LONG_SLOWNESS, PotionType::LONG_SLOWNESS());
$this->register(PotionTypeIds::WATER_BREATHING, PotionType::WATER_BREATHING());
$this->register(PotionTypeIds::LONG_WATER_BREATHING, PotionType::LONG_WATER_BREATHING());
$this->register(PotionTypeIds::HEALING, PotionType::HEALING());
$this->register(PotionTypeIds::STRONG_HEALING, PotionType::STRONG_HEALING());
$this->register(PotionTypeIds::HARMING, PotionType::HARMING());
$this->register(PotionTypeIds::STRONG_HARMING, PotionType::STRONG_HARMING());
$this->register(PotionTypeIds::POISON, PotionType::POISON());
$this->register(PotionTypeIds::LONG_POISON, PotionType::LONG_POISON());
$this->register(PotionTypeIds::STRONG_POISON, PotionType::STRONG_POISON());
$this->register(PotionTypeIds::REGENERATION, PotionType::REGENERATION());
$this->register(PotionTypeIds::LONG_REGENERATION, PotionType::LONG_REGENERATION());
$this->register(PotionTypeIds::STRONG_REGENERATION, PotionType::STRONG_REGENERATION());
$this->register(PotionTypeIds::STRENGTH, PotionType::STRENGTH());
$this->register(PotionTypeIds::LONG_STRENGTH, PotionType::LONG_STRENGTH());
$this->register(PotionTypeIds::STRONG_STRENGTH, PotionType::STRONG_STRENGTH());
$this->register(PotionTypeIds::WEAKNESS, PotionType::WEAKNESS());
$this->register(PotionTypeIds::LONG_WEAKNESS, PotionType::LONG_WEAKNESS());
$this->register(PotionTypeIds::WITHER, PotionType::WITHER());
}
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()];
}
}

View File

@ -0,0 +1,64 @@
<?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 PotionTypeIds{
public const WATER = 0;
public const MUNDANE = 1;
public const LONG_MUNDANE = 2;
public const THICK = 3;
public const AWKWARD = 4;
public const NIGHT_VISION = 5;
public const LONG_NIGHT_VISION = 6;
public const INVISIBILITY = 7;
public const LONG_INVISIBILITY = 8;
public const LEAPING = 9;
public const LONG_LEAPING = 10;
public const STRONG_LEAPING = 11;
public const FIRE_RESISTANCE = 12;
public const LONG_FIRE_RESISTANCE = 13;
public const SWIFTNESS = 14;
public const LONG_SWIFTNESS = 15;
public const STRONG_SWIFTNESS = 16;
public const SLOWNESS = 17;
public const LONG_SLOWNESS = 18;
public const WATER_BREATHING = 19;
public const LONG_WATER_BREATHING = 20;
public const HEALING = 21;
public const STRONG_HEALING = 22;
public const HARMING = 23;
public const STRONG_HARMING = 24;
public const POISON = 25;
public const LONG_POISON = 26;
public const STRONG_POISON = 27;
public const REGENERATION = 28;
public const LONG_REGENERATION = 29;
public const STRONG_REGENERATION = 30;
public const STRENGTH = 31;
public const LONG_STRENGTH = 32;
public const STRONG_STRENGTH = 33;
public const WEAKNESS = 34;
public const LONG_WEAKNESS = 35;
public const WITHER = 36;
}