Implemented Respawn Anchor (#6646)

PlayerRespawnAnchorUseEvent is also added with options SET_SPAWN and EXPLODE, which allows plugins to customise the outcome of using the anchor in PM, which currently doesn't support dimensions. The event is also cancellable.
This commit is contained in:
Adam
2025-05-28 02:57:28 +06:00
committed by GitHub
parent 059f4ee7bf
commit bf33a625c9
19 changed files with 711 additions and 14 deletions

View File

@ -43,13 +43,15 @@ class EntityExplodeEvent extends EntityEvent implements Cancellable{
/**
* @param Block[] $blocks
* @param float $yield 0-100
* @param float $yield 0-100
* @param Block[] $ignitions
*/
public function __construct(
Entity $entity,
protected Position $position,
protected array $blocks,
protected float $yield
protected float $yield,
private array $ignitions
){
$this->entity = $entity;
if($yield < 0.0 || $yield > 100.0){
@ -98,4 +100,23 @@ class EntityExplodeEvent extends EntityEvent implements Cancellable{
}
$this->yield = $yield;
}
/**
* Set the list of blocks that will be replaced by fire.
*
* @param Block[] $ignitions
*/
public function setIgnitions(array $ignitions) : void{
Utils::validateArrayValueType($ignitions, fn(Block $block) => null);
$this->ignitions = $ignitions;
}
/**
* Returns a list of affected blocks that will be replaced by fire.
*
* @return Block[]
*/
public function getIgnitions() : array{
return $this->ignitions;
}
}

View File

@ -26,6 +26,8 @@ namespace pocketmine\event\entity;
use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\utils\Utils;
use pocketmine\world\Explosion;
/**
* Called when an entity decides to explode, before the explosion's impact is calculated.
@ -42,11 +44,16 @@ class EntityPreExplodeEvent extends EntityEvent implements Cancellable{
public function __construct(
Entity $entity,
protected float $radius
protected float $radius,
private float $fireChance = 0.0,
){
if($radius <= 0){
throw new \InvalidArgumentException("Explosion radius must be positive");
}
Utils::checkFloatNotInfOrNaN("fireChance", $fireChance);
if($fireChance < 0.0 || $fireChance > 1.0){
throw new \InvalidArgumentException("Fire chance must be between 0 and 1.");
}
$this->entity = $entity;
}
@ -61,6 +68,47 @@ class EntityPreExplodeEvent extends EntityEvent implements Cancellable{
$this->radius = $radius;
}
/**
* Returns whether the explosion will create a fire.
*/
public function isIncendiary() : bool{
return $this->fireChance > 0;
}
/**
* Sets whether the explosion will create a fire by filling fireChance with default values.
*
* If $incendiary is true, the fire chance will be filled only if explosion isn't currently creating a fire (if fire chance is 0).
*/
public function setIncendiary(bool $incendiary) : void{
if(!$incendiary){
$this->fireChance = 0;
}elseif($this->fireChance <= 0){
$this->fireChance = Explosion::DEFAULT_FIRE_CHANCE;
}
}
/**
* Returns a chance between 0 and 1 of creating a fire.
*/
public function getFireChance() : float{
return $this->fireChance;
}
/**
* Sets a chance between 0 and 1 of creating a fire.
* For example, if the chance is 1/3, then that amount of affected blocks will be ignited.
*
* @param float $fireChance 0 ... 1
*/
public function setFireChance(float $fireChance) : void{
Utils::checkFloatNotInfOrNaN("fireChance", $fireChance);
if($fireChance < 0.0 || $fireChance > 1.0){
throw new \InvalidArgumentException("Fire chance must be between 0 and 1.");
}
$this->fireChance = $fireChance;
}
public function isBlockBreaking() : bool{
return $this->blockBreaking;
}