2022-07-02 17:01:10 +01:00

147 lines
4.0 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\data\bedrock\block\BlockLegacyMetadata;
use pocketmine\data\runtime\block\BlockDataReader;
use pocketmine\data\runtime\block\BlockDataWriter;
use pocketmine\entity\Entity;
use pocketmine\entity\Location;
use pocketmine\entity\object\PrimedTNT;
use pocketmine\entity\projectile\Arrow;
use pocketmine\item\Durable;
use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\item\FlintSteel;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\utils\Random;
use pocketmine\world\sound\IgniteSound;
use function cos;
use function sin;
use const M_PI;
class TNT extends Opaque{
protected bool $unstable = false; //TODO: Usage unclear, seems to be a weird hack in vanilla
protected bool $worksUnderwater = false;
protected function writeStateToItemMeta() : int{
return $this->worksUnderwater ? BlockLegacyMetadata::TNT_FLAG_UNDERWATER : 0;
}
public function getRequiredTypeDataBits() : int{ return 1; }
protected function decodeType(BlockDataReader $r) : void{
$this->worksUnderwater = $r->readBool();
}
protected function encodeType(BlockDataWriter $w) : void{
$w->writeBool($this->worksUnderwater);
}
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{
$this->unstable = $r->readBool();
}
protected function encodeState(BlockDataWriter $w) : void{
$w->writeBool($this->unstable);
}
public function isUnstable() : bool{ return $this->unstable; }
/** @return $this */
public function setUnstable(bool $unstable) : self{
$this->unstable = $unstable;
return $this;
}
public function worksUnderwater() : bool{ return $this->worksUnderwater; }
/** @return $this */
public function setWorksUnderwater(bool $worksUnderwater) : self{
$this->worksUnderwater = $worksUnderwater;
return $this;
}
public function onBreak(Item $item, ?Player $player = null) : bool{
if($this->unstable){
$this->ignite();
return true;
}
return parent::onBreak($item, $player);
}
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($item instanceof FlintSteel || $item->hasEnchantment(VanillaEnchantments::FIRE_ASPECT())){
if($item instanceof Durable){
$item->applyDamage(1);
}
$this->ignite();
return true;
}
return false;
}
public function hasEntityCollision() : bool{
return true;
}
public function onEntityInside(Entity $entity) : bool{
if($entity instanceof Arrow && $entity->isOnFire()){
$this->ignite();
return false;
}
return true;
}
public function ignite(int $fuse = 80) : void{
$this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR());
$mot = (new Random())->nextSignedFloat() * M_PI * 2;
$tnt = new PrimedTNT(Location::fromObject($this->position->add(0.5, 0, 0.5), $this->position->getWorld()));
$tnt->setFuse($fuse);
$tnt->setWorksUnderwater($this->worksUnderwater);
$tnt->setMotion(new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02));
$tnt->spawnToAll();
$tnt->broadcastSound(new IgniteSound());
}
public function getFlameEncouragement() : int{
return 15;
}
public function getFlammability() : int{
return 100;
}
public function onIncinerate() : void{
$this->ignite();
}
}