Recognize underwater TNT

This commit is contained in:
Dylan K. Taylor 2021-02-06 23:37:05 +00:00
parent 609b21679f
commit fd2ebd84b4
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 25 additions and 6 deletions

View File

@ -253,10 +253,8 @@ final class BlockLegacyMetadata{
public const TALLGRASS_NORMAL = 1;
public const TALLGRASS_FERN = 2;
public const TNT_NORMAL = 0;
public const TNT_UNDERWATER = 2;
public const TNT_FLAG_UNSTABLE = 0x01;
public const TNT_FLAG_UNDERWATER = 0x02;
public const TRAPDOOR_FLAG_UPPER = 0x04;
public const TRAPDOOR_FLAG_OPEN = 0x08;

View File

@ -43,6 +43,7 @@ class TNT extends Opaque{
/** @var bool */
protected $unstable = false; //TODO: Usage unclear, seems to be a weird hack in vanilla
protected bool $worksUnderwater = false;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? BlockBreakInfo::instant());
@ -50,14 +51,25 @@ class TNT extends Opaque{
public function readStateFromData(int $id, int $stateMeta) : void{
$this->unstable = ($stateMeta & BlockLegacyMetadata::TNT_FLAG_UNSTABLE) !== 0;
$this->worksUnderwater = ($stateMeta & BlockLegacyMetadata::TNT_FLAG_UNDERWATER) !== 0;
}
protected function writeStateToMeta() : int{
return $this->unstable ? BlockLegacyMetadata::TNT_FLAG_UNSTABLE : 0;
return ($this->unstable ? BlockLegacyMetadata::TNT_FLAG_UNSTABLE : 0) | ($this->worksUnderwater ? BlockLegacyMetadata::TNT_FLAG_UNDERWATER : 0);
}
public function getStateBitmask() : int{
return 0b1;
return 0b11;
}
public function getNonPersistentStateBitmask() : int{ return 0b1; }
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{
@ -99,6 +111,7 @@ class TNT extends Opaque{
$tnt = new PrimedTNT(Location::fromObject($this->pos->add(0.5, 0, 0.5), $this->pos->getWorld()));
$tnt->setFuse($fuse);
$tnt->setWorksUnderwater($this->worksUnderwater);
$tnt->setMotion(new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02));
$tnt->spawnToAll();

View File

@ -47,6 +47,8 @@ class PrimedTNT extends Entity implements Explosive{
/** @var int */
protected $fuse;
protected bool $worksUnderwater = false;
public $canCollide = false;
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.98, 0.98); }
@ -62,6 +64,10 @@ class PrimedTNT extends Entity implements Explosive{
$this->fuse = $fuse;
}
public function worksUnderwater() : bool{ return $this->worksUnderwater; }
public function setWorksUnderwater(bool $worksUnderwater) : void{ $this->worksUnderwater = $worksUnderwater; }
public function attack(EntityDamageEvent $source) : void{
if($source->getCause() === EntityDamageEvent::CAUSE_VOID){
parent::attack($source);
@ -108,6 +114,7 @@ class PrimedTNT extends Entity implements Explosive{
$ev = new ExplosionPrimeEvent($this, 4);
$ev->call();
if(!$ev->isCancelled()){
//TODO: deal with underwater TNT (underwater TNT treats water as if it has a blast resistance of 0)
$explosion = new Explosion(Position::fromObject($this->location->add(0, $this->size->getHeight() / 2, 0), $this->getWorld()), $ev->getForce(), $this);
if($ev->isBlockBreaking()){
$explosion->explodeA();
@ -120,6 +127,7 @@ class PrimedTNT extends Entity implements Explosive{
parent::syncNetworkData($properties);
$properties->setGenericFlag(EntityMetadataFlags::IGNITED, true);
$properties->setInt(EntityMetadataProperties::VARIANT, $this->worksUnderwater ? 1 : 0);
$properties->setInt(EntityMetadataProperties::FUSE_LENGTH, $this->fuse);
}

File diff suppressed because one or more lines are too long