46 public const TAG_ITEM =
"Trident";
47 protected const TAG_SPAWNED_IN_CREATIVE =
"isCreative";
49 public function getNetworkTypeId() :
string{
return EntityIds::THROWN_TRIDENT; }
53 protected float $damage = 8.0;
55 protected bool $canCollide =
true;
57 protected bool $spawnedInCreative =
false;
59 public function __construct(
66 throw new \InvalidArgumentException(
"Trident must have a count of at least 1");
68 $this->item = clone $item;
69 parent::__construct($location, $shootingEntity, $nbt);
78 protected function initEntity(
CompoundTag $nbt) : void{
79 parent::initEntity($nbt);
81 $this->spawnedInCreative = $nbt->getByte(self::TAG_SPAWNED_IN_CREATIVE, 0) === 1;
85 $nbt = parent::saveNBT();
86 $nbt->
setTag(self::TAG_ITEM, $this->item->nbtSerialize());
87 $nbt->
setByte(self::TAG_SPAWNED_IN_CREATIVE, $this->spawnedInCreative ? 1 : 0);
92 $owner = $this->getOwningEntity();
95 parent::onFirstUpdate($currentTick);
98 protected function entityBaseTick(
int $tickDiff = 1) : bool{
104 return parent::entityBaseTick($tickDiff);
112 parent::onHitEntity($entityHit, $hitResult);
114 $this->canCollide =
false;
116 $this->setMotion(
new Vector3($this->motion->x * -0.01, $this->motion->y * -0.1, $this->motion->z * -0.01));
120 parent::onHitBlock($blockHit, $hitResult);
121 $this->canCollide =
true;
125 public function getItem() :
Item{
126 return clone $this->item;
129 public function setItem(
Item $item) : void{
131 throw new \InvalidArgumentException(
"Trident must have a count of at least 1");
133 if($this->item->hasEnchantments() !== $item->hasEnchantments()){
134 $this->networkPropertiesDirty =
true;
136 $this->item = clone $item;
139 public function canCollideWith(Entity $entity) : bool{
140 return $this->canCollide && $entity->getId() !== $this->ownerId && parent::canCollideWith($entity);
143 public function onCollideWithPlayer(Player $player) : void{
144 if($this->blockHit !== null){
145 $this->pickup($player);
149 private function pickup(Player $player) : void{
150 $shouldDespawn = false;
152 $playerInventory = $player->getInventory();
153 $ev =
new EntityItemPickupEvent($player, $this, $this->getItem(), $playerInventory);
154 if($player->hasFiniteResources() && !$playerInventory->canAddItem($ev->getItem())){
157 if($this->spawnedInCreative){
159 $shouldDespawn =
true;
163 if(!$ev->isCancelled()){
164 $ev->getInventory()?->addItem($ev->getItem());
165 $shouldDespawn =
true;
170 NetworkBroadcastUtils::broadcastEntityEvent(
172 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onPickUpItem($recipients, $player, $this)
174 $this->flagForDespawn();
178 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
179 parent::syncNetworkData($properties);
181 $properties->setGenericFlag(EntityMetadataFlags::ENCHANTED, $this->item->hasEnchantments());