mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-13 09:19:42 +00:00
EnumTrait: add a numeric runtimeID field for arrays and switches
This commit is contained in:
parent
69e23998f4
commit
a7d51a273d
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\utils;
|
namespace pocketmine\utils;
|
||||||
|
|
||||||
|
use function getmypid;
|
||||||
use function preg_match;
|
use function preg_match;
|
||||||
|
|
||||||
trait EnumTrait{
|
trait EnumTrait{
|
||||||
@ -85,8 +86,13 @@ trait EnumTrait{
|
|||||||
return self::_registryFromString($name);
|
return self::_registryFromString($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var int|null */
|
||||||
|
private static $nextId = null;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $enumName;
|
private $enumName;
|
||||||
|
/** @var int */
|
||||||
|
private $runtimeId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $enumName
|
* @param string $enumName
|
||||||
@ -98,6 +104,10 @@ trait EnumTrait{
|
|||||||
throw new \InvalidArgumentException("Invalid enum member name \"$enumName\", should only contain letters, numbers and underscores, and must not start with a number");
|
throw new \InvalidArgumentException("Invalid enum member name \"$enumName\", should only contain letters, numbers and underscores, and must not start with a number");
|
||||||
}
|
}
|
||||||
$this->enumName = $enumName;
|
$this->enumName = $enumName;
|
||||||
|
if(self::$nextId === null){
|
||||||
|
self::$nextId = getmypid(); //this provides enough base entropy to prevent hardcoding
|
||||||
|
}
|
||||||
|
$this->runtimeId = self::$nextId++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,6 +117,17 @@ trait EnumTrait{
|
|||||||
return $this->enumName;
|
return $this->enumName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a runtime-only identifier for this enum member. This will be different with each run, so don't try to
|
||||||
|
* hardcode it.
|
||||||
|
* This can be useful for switches or array indexing.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function id() : int{
|
||||||
|
return $this->runtimeId;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the two objects are equivalent.
|
* Returns whether the two objects are equivalent.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user