Skip to content
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

Wind Charge Implementation #6427

Open
wants to merge 9 commits into
base: minor-next
Choose a base branch
from
7 changes: 7 additions & 0 deletions src/block/Bell.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\projectile\Projectile;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
Expand Down Expand Up @@ -134,6 +135,12 @@ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player
return false;
}

public function onProjectileInteraction(Projectile $projectile) : void{
if($projectile instanceof WindCharge) {
$this->ring($this->facing);
}
}

public function onProjectileHit(Projectile $projectile, RayTraceResult $hitResult) : void{
$faceHit = Facing::opposite($projectile->getHorizontalFacing());
if($this->isValidFaceToRing($faceHit)){
Expand Down
9 changes: 9 additions & 0 deletions src/block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,15 @@ public function onScheduledUpdate() : void{

}

/**
* Called when this block is updated by a state altering projectile in the world.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence doesn't mean anything to me. It doesn't tell me why an update was triggered, nor what kind of state alterations should take place.

The function is trying to look and sound generic, but it's not.
"interaction" is also too vague.

*
* @param Projectile $projectile The projectile affecting the block
*/
public function onProjectileInteraction(Projectile $projectile) : void {

}

/**
* Do actions when interacted by Item. Returns if it has done anything
*
Expand Down
23 changes: 18 additions & 5 deletions src/block/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

use pocketmine\block\utils\AnyFacingTrait;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\projectile\Projectile;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
Expand Down Expand Up @@ -63,11 +65,7 @@ abstract protected function getActivationTime() : int;

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
if(!$this->pressed){
$this->pressed = true;
$world = $this->position->getWorld();
$world->setBlock($this->position, $this);
$world->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime());
$world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound());
$this->press();
}

return true;
Expand All @@ -91,4 +89,19 @@ public function onNearbyBlockChange() : void{
private function canBeSupportedAt(Block $block, int $face) : bool{
return $block->getAdjacentSupportType(Facing::opposite($face))->hasCenterSupport();
}

public function onProjectileInteraction(Projectile $projectile) : void{
if($projectile instanceof WindCharge) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to check if its not pressed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->pressed(); in this context, thereby extending the pushed duration if already down, I do not see a need to change this, thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by checking that it is not activated, you will avoid extra sound.

$this->press();
}
}

public function press() : void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be private or protected, making it public has implications beyond this PR's scope

$this->pressed = true;

$world = $this->position->getWorld();
$world->setBlock($this->position, $this);
$world->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime());
$world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound());
}
}
12 changes: 12 additions & 0 deletions src/block/CakeWithCandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

use pocketmine\block\utils\CandleTrait;
use pocketmine\entity\Living;
use pocketmine\entity\projectile\Projectile;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\world\sound\FlintSteelSound;

class CakeWithCandle extends BaseCake{
use CandleTrait {
Expand Down Expand Up @@ -59,6 +62,15 @@ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player
return parent::onInteract($item, $face, $clickVector, $player, $returnedItems);
}

public function onProjectileInteraction(Projectile $projectile) : void{
if($projectile instanceof WindCharge && $this->lit) {

$world = $this->position->getWorld();
$world->setBlock($this->position, $this->setLit(false));
$world->addSound($this->position, new FlintSteelSound());
}
}

public function getDropsForCompatibleTool(Item $item) : array{
return [$this->getCandle()->asItem()];
}
Expand Down
13 changes: 13 additions & 0 deletions src/block/Candle.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use pocketmine\block\utils\CandleTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\projectile\Projectile;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\Axis;
use pocketmine\math\AxisAlignedBB;
Expand All @@ -34,6 +36,7 @@
use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\BlockTransaction;
use pocketmine\world\sound\FlintSteelSound;

class Candle extends Transparent{
use CandleTrait {
Expand Down Expand Up @@ -98,6 +101,16 @@ protected function getCandleIfCompatibleType(Block $block) : ?Candle{
return $block instanceof Candle && $block->hasSameTypeId($this) ? $block : null;
}

public function onProjectileInteraction(Projectile $projectile) : void{
if($projectile instanceof WindCharge && $this->lit) {

$newCandle = $this->setLit(false);
$world = $this->position->getWorld();
$world->setBlock($this->position, $newCandle);
$world->addSound($this->position, new FlintSteelSound());
}
}

public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool{
$candle = $this->getCandleIfCompatibleType($blockReplace);
return $candle !== null ? $candle->count < self::MAX_COUNT : parent::canBePlacedAt($blockReplace, $clickVector, $face, $isClickedBlock);
Expand Down
44 changes: 33 additions & 11 deletions src/block/Door.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\projectile\Projectile;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
Expand Down Expand Up @@ -142,17 +144,7 @@ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Blo
}

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
$this->open = !$this->open;

$other = $this->getSide($this->top ? Facing::DOWN : Facing::UP);
$world = $this->position->getWorld();
if($other instanceof Door && $other->hasSameTypeId($this)){
$other->open = $this->open;
$world->setBlock($other->position, $other);
}

$world->setBlock($this->position, $this);
$world->addSound($this->position, new DoorSound());
$this->toggle();

return true;
}
Expand All @@ -176,4 +168,34 @@ public function getAffectedBlocks() : array{
private function canBeSupportedAt(Block $block) : bool{
return $block->getAdjacentSupportType(Facing::DOWN)->hasEdgeSupport();
}

public function onProjectileInteraction(Projectile $projectile) : void{
if($projectile instanceof WindCharge) {
if($this->getTypeId() === BlockTypeIds::IRON_DOOR) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not liking this check, same as before with trapdoors

return;
}

// This is to avoid calling Door::onProjectileInteraction() twice due to two tiles.
if($this->top) {
Comment on lines +178 to +179
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if the projectile hit the upper one, no toggle will be happened ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is my best attempt at handling this issue, if it hits both blocks the door hits, (open, close)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be useful before toggling to check the other state. If its not the same as ours when projectile hits, we can ignore ? Just a theory

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels a little hacky to be honest

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe hacky but can avoid to have unexpected side effect if only the top block is hitten by the effect area.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, only checking the one half will cause bugs. It's the same as if a player were clicking the top or bottom half, so the logic should be basically the same.

return;
}

$this->toggle();
}
}

public function toggle() : void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be private or protected, making it public has implications beyond this PR's scope

$this->open = !$this->open;

$other = $this->getSide($this->top ? Facing::DOWN : Facing::UP);
$world = $this->position->getWorld();

if($other instanceof Door && $other->hasSameTypeId($this)){
$other->open = $this->open;
$world->setBlock($other->position, $other);
}

$world->setBlock($this->position, $this);
$world->addSound($this->position, new DoorSound());
}
}
15 changes: 14 additions & 1 deletion src/block/Lever.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

use pocketmine\block\utils\LeverFacing;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\projectile\Projectile;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\Axis;
use pocketmine\math\Facing;
Expand Down Expand Up @@ -91,14 +93,25 @@ public function onNearbyBlockChange() : void{
}

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
$this->toggle();

return true;
}

public function onProjectileInteraction(Projectile $projectile) : void{
if($projectile instanceof WindCharge) {
$this->toggle();
}
}

public function toggle() : void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be private or protected, making it public has implications beyond this PR's scope

$this->activated = !$this->activated;
$world = $this->position->getWorld();
$world->setBlock($this->position, $this);
$world->addSound(
$this->position->add(0.5, 0.5, 0.5),
$this->activated ? new RedstonePowerOnSound() : new RedstonePowerOffSound()
);
return true;
}

private function canBeSupportedAt(Block $block, int $face) : bool{
Expand Down
16 changes: 15 additions & 1 deletion src/block/Trapdoor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\projectile\Projectile;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
Expand Down Expand Up @@ -85,10 +87,22 @@ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Blo
}

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
$this->toggle();

return true;
}

public function onProjectileInteraction(Projectile $projectile) : void{
if($projectile instanceof WindCharge && $this->getTypeId() !== BlockTypeIds::IRON_TRAPDOOR){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not liking the iron trapdoor check hardcoded in here.

$this->toggle();
}
}

public function toggle() : void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be private or protected, making it public has implications beyond this PR's scope

$this->open = !$this->open;

$world = $this->position->getWorld();
$world->setBlock($this->position, $this);
$world->addSound($this->position, new DoorSound());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ private function register1to1ItemMappings() : void{
$this->map1to1Item(Ids::WHEAT, Items::WHEAT());
$this->map1to1Item(Ids::WHEAT_SEEDS, Items::WHEAT_SEEDS());
$this->map1to1Item(Ids::WILD_ARMOR_TRIM_SMITHING_TEMPLATE, Items::WILD_ARMOR_TRIM_SMITHING_TEMPLATE());
$this->map1to1Item(Ids::WIND_CHARGE, Items::WIND_CHARGE());
$this->map1to1Item(Ids::WOODEN_AXE, Items::WOODEN_AXE());
$this->map1to1Item(Ids::WOODEN_HOE, Items::WOODEN_HOE());
$this->map1to1Item(Ids::WOODEN_PICKAXE, Items::WOODEN_PICKAXE());
Expand Down
5 changes: 5 additions & 0 deletions src/entity/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use pocketmine\entity\projectile\ExperienceBottle;
use pocketmine\entity\projectile\Snowball;
use pocketmine\entity\projectile\SplashPotion;
use pocketmine\entity\projectile\WindCharge;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
Expand Down Expand Up @@ -169,6 +170,10 @@ public function __construct(){
return new Villager(Helper::parseLocation($nbt, $world), $nbt);
}, ['Villager', 'minecraft:villager']);

$this->register(WindCharge::class, function(World $world, CompoundTag $nbt) : WindCharge {
return new WindCharge(Helper::parseLocation($nbt, $world), null, $nbt);
}, ['Wind Charge', 'minecraft:wind_charge']);

$this->register(Zombie::class, function(World $world, CompoundTag $nbt) : Zombie{
return new Zombie(Helper::parseLocation($nbt, $world), $nbt);
}, ['Zombie', 'minecraft:zombie']);
Expand Down
Loading