Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Using a Integer From a config.yml file as a variable in a mt_rand() function (pocketmine plugin)

I'm trying to use a config file to store a minimum and maximum value that can be changed by users for easier changing of a randomizing code. This is being used for a PocketMine-MP plugin that will spawn a player at a random position and have their spawn at the random location.

My config file looks like this:

##Change These Listed Numbers To Change Your Spawn Randomizers min and Maxes
-Coords:
Xvalues:
Xmin: -10000
Xmax: 10000
Yvalues:
Ymin: -10000
Ymax: 10000
Zvalues:
Zmin: -10000
Zmax: 10000
...

I know this works but when I use these values and make them a variable and use that variable in a mt_rand() function I get a error saying:

[21:26:18] [Server thread/CRITICAL]: TypeError: "mt_rand() expects parameter 1 to be int, bool given" (EXCEPTION) in "plugins/TPRandomOnFirstJoinAndDeath/src/JviguyGamesYT/TPRandomOnFirstJoinAndDeath/Main" at line 27

I don't really know what to do to fix this. If anyone could help my code for the plugin, here's the code:

$player = $e->getPlayer();
$Xmin = $this->getConfig()->get("Xmin");
$Xmax = $this->getConfig()->get("Xmax");
$Ymin = $this->getConfig()->get("Ymin");
$Ymax = $this->getConfig()->get("Ymax");
$Zmin = $this->getConfig()->get("Zmin");
$Zmax = $this->getConfig()->get("Zmax");
$x = mt_rand($Xmin , $Xmax);
$y = mt_rand($Ymin , $Ymax);
$z = mt_rand($Zmin , $Zmax);
$player->teleport(new vector3($x, $y, $z));
like image 681
Jviguy Avatar asked Apr 13 '26 02:04

Jviguy


1 Answers

You need to cast it to an int.

e.g. $x = mt_rand((int) $Xmin, (int) $Xmax);

like image 197
Himbeer Avatar answered Apr 15 '26 15:04

Himbeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!