Move enchanting seed generation to EnchantmentHelper

This commit is contained in:
Dylan K. Taylor 2023-08-23 15:52:49 +01:00
parent 29fdc8b08d
commit d942748203
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 14 additions and 6 deletions

View File

@ -37,6 +37,7 @@ use pocketmine\inventory\InventoryHolder;
use pocketmine\inventory\PlayerEnderInventory; use pocketmine\inventory\PlayerEnderInventory;
use pocketmine\inventory\PlayerInventory; use pocketmine\inventory\PlayerInventory;
use pocketmine\inventory\PlayerOffHandInventory; use pocketmine\inventory\PlayerOffHandInventory;
use pocketmine\item\enchantment\EnchantmentHelper;
use pocketmine\item\enchantment\VanillaEnchantments; use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\Totem; use pocketmine\item\Totem;
@ -66,7 +67,6 @@ use pocketmine\network\mcpe\protocol\types\PlayerListEntry;
use pocketmine\network\mcpe\protocol\types\PlayerPermissions; use pocketmine\network\mcpe\protocol\types\PlayerPermissions;
use pocketmine\network\mcpe\protocol\UpdateAbilitiesPacket; use pocketmine\network\mcpe\protocol\UpdateAbilitiesPacket;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\utils\Limits;
use pocketmine\world\sound\TotemUseSound; use pocketmine\world\sound\TotemUseSound;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface; use Ramsey\Uuid\UuidInterface;
@ -76,7 +76,6 @@ use function array_key_exists;
use function array_merge; use function array_merge;
use function array_values; use function array_values;
use function min; use function min;
use function mt_rand;
class Human extends Living implements ProjectileSource, InventoryHolder{ class Human extends Living implements ProjectileSource, InventoryHolder{
@ -219,8 +218,8 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
$this->xpSeed = $seed; $this->xpSeed = $seed;
} }
public function generateEnchantmentSeed() : int{ public function regenerateEnchantmentSeed() : void{
return mt_rand(Limits::INT32_MIN, Limits::INT32_MAX); $this->xpSeed = EnchantmentHelper::generateSeed();
} }
public function getXpDropAmount() : int{ public function getXpDropAmount() : int{
@ -346,7 +345,7 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
if(($xpSeedTag = $nbt->getTag(self::TAG_XP_SEED)) instanceof IntTag){ if(($xpSeedTag = $nbt->getTag(self::TAG_XP_SEED)) instanceof IntTag){
$this->xpSeed = $xpSeedTag->getValue(); $this->xpSeed = $xpSeedTag->getValue();
}else{ }else{
$this->xpSeed = $this->generateEnchantmentSeed(); $this->xpSeed = EnchantmentHelper::generateSeed();
} }
} }

View File

@ -119,7 +119,7 @@ class EnchantTransaction extends InventoryTransaction{
//In this case, as much XP as possible will be taken. //In this case, as much XP as possible will be taken.
$this->source->getXpManager()->subtractXpLevels(min($this->cost, $this->source->getXpManager()->getXpLevel())); $this->source->getXpManager()->subtractXpLevels(min($this->cost, $this->source->getXpManager()->getXpLevel()));
} }
$this->source->setEnchantmentSeed($this->source->generateEnchantmentSeed()); $this->source->regenerateEnchantmentSeed();
} }
protected function callExecuteEvent() : bool{ protected function callExecuteEvent() : bool{

View File

@ -28,6 +28,7 @@ use pocketmine\item\enchantment\AvailableEnchantmentRegistry as EnchantmentRegis
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemTypeIds; use pocketmine\item\ItemTypeIds;
use pocketmine\item\VanillaItems as Items; use pocketmine\item\VanillaItems as Items;
use pocketmine\utils\Limits;
use pocketmine\utils\Random; use pocketmine\utils\Random;
use pocketmine\world\Position; use pocketmine\world\Position;
use function abs; use function abs;
@ -37,6 +38,7 @@ use function count;
use function floor; use function floor;
use function max; use function max;
use function min; use function min;
use function mt_rand;
use function ord; use function ord;
use function round; use function round;
@ -47,6 +49,13 @@ final class EnchantmentHelper{
//NOOP //NOOP
} }
/**
* Generates a new random seed for enchant option randomization.
*/
public static function generateSeed() : int{
return mt_rand(Limits::INT32_MIN, Limits::INT32_MAX);
}
/** /**
* @param EnchantmentInstance[] $enchantments * @param EnchantmentInstance[] $enchantments
*/ */