Skip to content

Instantly share code, notes, and snippets.

@NgLam2911
Last active March 23, 2022 16:35
Show Gist options
  • Save NgLam2911/3efacec0a0e2df0d1dce5f0739e8530a to your computer and use it in GitHub Desktop.
Save NgLam2911/3efacec0a0e2df0d1dce5f0739e8530a to your computer and use it in GitHub Desktop.
RandomBlock template
<?php
declare(strict_types=1);
namespace ShutTheFuckUp;
use InvalidArgumentException;
use pocketmine\block\Block;
use pocketmine\block\VanillaBlocks;
class RandomBlock{
/** @var Block[] */
protected array $array = [];
/** @var Block[] */
protected array $block_list;
/** @var int[] */
protected array $percent_list;
public function __construct(){
$this->block_list = [
VanillaBlocks::DIRT(),
VanillaBlocks::STONE(),
VanillaBlocks::COAL_ORE()
];
$this->percent_list = [
20, //20% DIRT
70, //70% STONE
10, //10% COAL ORE
];
if (array_sum($this->percent_list) !== 100){
throw new InvalidArgumentException("Total of percent must be = 100");
}
$this->generate();
}
public function generate() : void{
foreach($this->block_list as $id => $block){
for ($i = 0; $i < $this->percent_list[$id]; $i++){
$this->array[] = $block;
}
}
shuffle($this->array);
}
public function get() : Block{
return $this->array[array_rand($this->array)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment