mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
this fixes TNT spawning multiple entities when lit by flaming arrows. The problem here is a bit more complex (entities aren't immediately notified when local block updates happen, so they cache stuff that becomes unusable). The simplest option would be to just lose the cache, but that would have some impacts on performance. Barring a rethink of the block updating mechanism, this solution seems usable for now.
180 lines
4.6 KiB
PHP
180 lines
4.6 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\block;
|
|
|
|
use pocketmine\block\utils\BlockDataSerializer;
|
|
use pocketmine\entity\Entity;
|
|
use pocketmine\entity\projectile\Arrow;
|
|
use pocketmine\event\block\BlockBurnEvent;
|
|
use pocketmine\event\entity\EntityCombustByBlockEvent;
|
|
use pocketmine\event\entity\EntityDamageByBlockEvent;
|
|
use pocketmine\event\entity\EntityDamageEvent;
|
|
use pocketmine\item\Item;
|
|
use pocketmine\math\Facing;
|
|
use function min;
|
|
use function mt_rand;
|
|
|
|
class Fire extends Flowable{
|
|
|
|
/** @var int */
|
|
protected $age = 0;
|
|
|
|
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
|
|
parent::__construct($idInfo, $name, $breakInfo ?? BlockBreakInfo::instant());
|
|
}
|
|
|
|
protected function writeStateToMeta() : int{
|
|
return $this->age;
|
|
}
|
|
|
|
public function readStateFromData(int $id, int $stateMeta) : void{
|
|
$this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 15);
|
|
}
|
|
|
|
public function getStateBitmask() : int{
|
|
return 0b1111;
|
|
}
|
|
|
|
public function hasEntityCollision() : bool{
|
|
return true;
|
|
}
|
|
|
|
public function getLightLevel() : int{
|
|
return 15;
|
|
}
|
|
|
|
public function canBeReplaced() : bool{
|
|
return true;
|
|
}
|
|
|
|
public function onEntityInside(Entity $entity) : bool{
|
|
$ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1);
|
|
$entity->attack($ev);
|
|
|
|
$ev = new EntityCombustByBlockEvent($this, $entity, 8);
|
|
if($entity instanceof Arrow){
|
|
$ev->setCancelled();
|
|
}
|
|
$ev->call();
|
|
if(!$ev->isCancelled()){
|
|
$entity->setOnFire($ev->getDuration());
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function getDropsForCompatibleTool(Item $item) : array{
|
|
return [];
|
|
}
|
|
|
|
public function onNearbyBlockChange() : void{
|
|
if(!$this->getSide(Facing::DOWN)->isSolid() and !$this->hasAdjacentFlammableBlocks()){
|
|
$this->pos->getWorld()->setBlock($this->pos, VanillaBlocks::AIR());
|
|
}else{
|
|
$this->pos->getWorld()->scheduleDelayedBlockUpdate($this->pos, mt_rand(30, 40));
|
|
}
|
|
}
|
|
|
|
public function ticksRandomly() : bool{
|
|
return true;
|
|
}
|
|
|
|
public function onRandomTick() : void{
|
|
$down = $this->getSide(Facing::DOWN);
|
|
|
|
$result = null;
|
|
if($this->age < 15 and mt_rand(0, 2) === 0){
|
|
$this->age++;
|
|
$result = $this;
|
|
}
|
|
$canSpread = true;
|
|
|
|
if(!$down->burnsForever()){
|
|
//TODO: check rain
|
|
if($this->age === 15){
|
|
if(!$down->isFlammable() and mt_rand(0, 3) === 3){ //1/4 chance to extinguish
|
|
$canSpread = false;
|
|
$result = VanillaBlocks::AIR();
|
|
}
|
|
}elseif(!$this->hasAdjacentFlammableBlocks()){
|
|
$canSpread = false;
|
|
if(!$down->isSolid() or $this->age > 3){
|
|
$result = VanillaBlocks::AIR();
|
|
}
|
|
}
|
|
}
|
|
|
|
if($result !== null){
|
|
$this->pos->getWorld()->setBlock($this->pos, $result);
|
|
}
|
|
|
|
$this->pos->getWorld()->scheduleDelayedBlockUpdate($this->pos, mt_rand(30, 40));
|
|
|
|
if($canSpread){
|
|
//TODO: raise upper bound for chance in humid biomes
|
|
|
|
foreach($this->getHorizontalSides() as $side){
|
|
$this->burnBlock($side, 300);
|
|
}
|
|
|
|
//vanilla uses a 250 upper bound here, but I don't think they intended to increase the chance of incineration
|
|
$this->burnBlock($this->getSide(Facing::UP), 350);
|
|
$this->burnBlock($this->getSide(Facing::DOWN), 350);
|
|
|
|
//TODO: fire spread
|
|
}
|
|
}
|
|
|
|
public function onScheduledUpdate() : void{
|
|
$this->onRandomTick();
|
|
}
|
|
|
|
private function hasAdjacentFlammableBlocks() : bool{
|
|
foreach(Facing::ALL as $face){
|
|
if($this->getSide($face)->isFlammable()){
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function burnBlock(Block $block, int $chanceBound) : void{
|
|
if(mt_rand(0, $chanceBound) < $block->getFlammability()){
|
|
$ev = new BlockBurnEvent($block, $this);
|
|
$ev->call();
|
|
if(!$ev->isCancelled()){
|
|
$block->onIncinerate();
|
|
|
|
if(mt_rand(0, $this->age + 9) < 5){ //TODO: check rain
|
|
$fire = clone $this;
|
|
$fire->age = min(15, $fire->age + (mt_rand(0, 4) >> 2));
|
|
$this->pos->getWorld()->setBlock($block->pos, $fire);
|
|
}else{
|
|
$this->pos->getWorld()->setBlock($block->pos, VanillaBlocks::AIR());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|