-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Trident #4547
Open
IvanCraft623
wants to merge
8
commits into
pmmp:minor-next
Choose a base branch
from
IvanCraft623:master
base: minor-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement Trident #4547
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
910de73
Implement Trident
IvanCraft623 d32129a
Merge branch 'minor-next' into master
IvanCraft623 e01c31e
Remove spaces on if statement
IvanCraft623 3aeb4e6
Remove spaces on if statement
IvanCraft623 55bbdce
Merge branch 'minor-next' into master
IvanCraft623 d55a3fc
Give TridentEntity a poped TridentItem
IvanCraft623 1728f0f
Rename TridentHitSound => TridentHitEntitySound
IvanCraft623 b559dc7
Oops... CS
IvanCraft623 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?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\entity\projectile; | ||
|
||
use pocketmine\block\Block; | ||
use pocketmine\entity\Entity; | ||
use pocketmine\entity\EntitySizeInfo; | ||
use pocketmine\entity\Location; | ||
use pocketmine\event\entity\EntityItemPickupEvent; | ||
use pocketmine\item\Trident as TridentItem; | ||
use pocketmine\math\RayTraceResult; | ||
use pocketmine\math\Vector3; | ||
use pocketmine\nbt\tag\CompoundTag; | ||
use pocketmine\network\mcpe\EntityEventBroadcaster; | ||
use pocketmine\network\mcpe\NetworkBroadcastUtils; | ||
use pocketmine\network\mcpe\protocol\types\entity\EntityIds; | ||
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection; | ||
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; | ||
use pocketmine\player\Player; | ||
use pocketmine\world\sound\TridentHitEntitySound; | ||
use pocketmine\world\sound\TridentHitGroundSound; | ||
|
||
class Trident extends Projectile{ | ||
|
||
public const TAG_ITEM = "Trident"; //TAG_Compound | ||
protected const TAG_SPAWNED_IN_CREATIVE = "isCreative"; //TAG_Byte | ||
|
||
public static function getNetworkTypeId() : string{ return EntityIds::THROWN_TRIDENT; } | ||
|
||
protected float $damage = 8.0; | ||
|
||
protected bool $canCollide = true; | ||
|
||
protected bool $spawnedInCreative = false; | ||
|
||
public function __construct( | ||
Location $location, | ||
protected TridentItem $item, | ||
?Entity $shootingEntity, | ||
?CompoundTag $nbt = null | ||
){ | ||
if($item->isNull()){ | ||
throw new \InvalidArgumentException("Trident must have a count of at least 1"); | ||
} | ||
parent::__construct($location, $shootingEntity, $nbt); | ||
} | ||
|
||
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.35, 0.25); } | ||
|
||
protected function getInitialDragMultiplier() : float{ return 0.01; } | ||
|
||
protected function getInitialGravity() : float{ return 0.1; } | ||
|
||
protected function initEntity(CompoundTag $nbt) : void{ | ||
parent::initEntity($nbt); | ||
|
||
$this->spawnedInCreative = $nbt->getByte(self::TAG_SPAWNED_IN_CREATIVE, 0) === 1; | ||
} | ||
|
||
public function saveNBT() : CompoundTag{ | ||
$nbt = parent::saveNBT(); | ||
$nbt->setTag(self::TAG_ITEM, $this->item->nbtSerialize()); | ||
$nbt->setByte(self::TAG_SPAWNED_IN_CREATIVE, $this->spawnedInCreative ? 1 : 0); | ||
return $nbt; | ||
} | ||
|
||
protected function onFirstUpdate(int $currentTick) : void{ | ||
$owner = $this->getOwningEntity(); | ||
$this->spawnedInCreative = $owner instanceof Player && $owner->isCreative(); | ||
|
||
parent::onFirstUpdate($currentTick); | ||
} | ||
|
||
protected function entityBaseTick(int $tickDiff = 1) : bool{ | ||
if($this->closed){ | ||
return false; | ||
} | ||
//TODO: Loyalty enchantment. | ||
|
||
return parent::entityBaseTick($tickDiff); | ||
} | ||
|
||
protected function despawnsOnEntityHit() : bool{ | ||
return false; | ||
} | ||
|
||
protected function onHitEntity(Entity $entityHit, RayTraceResult $hitResult) : void{ | ||
parent::onHitEntity($entityHit, $hitResult); | ||
|
||
$this->canCollide = false; | ||
$this->broadcastSound(new TridentHitEntitySound()); | ||
$this->setMotion(new Vector3($this->motion->x * -0.01, $this->motion->y * -0.1, $this->motion->z * -0.01)); | ||
} | ||
|
||
protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{ | ||
parent::onHitBlock($blockHit, $hitResult); | ||
$this->canCollide = true; | ||
$this->broadcastSound(new TridentHitGroundSound()); | ||
} | ||
|
||
public function getItem() : TridentItem{ | ||
return clone $this->item; | ||
} | ||
|
||
public function setItem(TridentItem $item) : void{ | ||
if($item->isNull()){ | ||
throw new \InvalidArgumentException("Trident must have a count of at least 1"); | ||
} | ||
if($this->item->hasEnchantments() !== $item->hasEnchantments()){ | ||
$this->networkPropertiesDirty = true; | ||
} | ||
$this->item = clone $item; | ||
} | ||
|
||
public function canCollideWith(Entity $entity) : bool{ | ||
return $this->canCollide && $entity->getId() !== $this->ownerId && parent::canCollideWith($entity); | ||
} | ||
|
||
public function onCollideWithPlayer(Player $player) : void{ | ||
if($this->blockHit !== null){ | ||
$this->pickup($player); | ||
} | ||
} | ||
|
||
private function pickup(Player $player) : void{ | ||
$shouldDespawn = false; | ||
|
||
$playerInventory = $player->getInventory(); | ||
$ev = new EntityItemPickupEvent($player, $this, $this->getItem(), $playerInventory); | ||
if($player->hasFiniteResources() && !$playerInventory->canAddItem($ev->getItem())){ | ||
$ev->cancel(); | ||
} | ||
if($this->spawnedInCreative){ | ||
$ev->cancel(); | ||
$shouldDespawn = true; | ||
} | ||
|
||
$ev->call(); | ||
if(!$ev->isCancelled()){ | ||
$ev->getInventory()?->addItem($ev->getItem()); | ||
$shouldDespawn = true; | ||
} | ||
|
||
if($shouldDespawn){ | ||
//even if the item was not actually picked up, the animation must be displayed. | ||
NetworkBroadcastUtils::broadcastEntityEvent( | ||
$this->getViewers(), | ||
fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onPickUpItem($recipients, $player, $this) | ||
); | ||
$this->flagForDespawn(); | ||
} | ||
} | ||
|
||
protected function syncNetworkData(EntityMetadataCollection $properties) : void{ | ||
parent::syncNetworkData($properties); | ||
|
||
$properties->setGenericFlag(EntityMetadataFlags::ENCHANTED, $this->item->hasEnchantments()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to require this to be a TridentItem? It doesn't look like it needs any specific methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistency, if I do
TridentEntity->getItem()
I would expect aTridentItem
.