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

Event when a block support is broken #6258

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/event/block/BlockSupportBreakEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\event\block;

use pocketmine\block\Block;
use pocketmine\item\Item;

/**
* Called when a block is destroyed because its support has been destroyed.
*/
class BlockSupportBreakEvent extends BlockEvent{
/** @var Item[] */
protected array $blockDrops = [];

/**
* @param Item[] $drops
*/
public function __construct(
Block $block,
array $drops = [],
protected int $xpDrops = 0
){
parent::__construct($block);
$this->setDrops($drops);
}

/**
* @return Item[]
*/
public function getDrops() : array{
return $this->blockDrops;
}

/**
* @param Item[] $drops
*/
public function setDrops(array $drops) : void{
$this->setDropsVariadic(...$drops);
}

/**
* Variadic hack for easy array member type enforcement.
*/
public function setDropsVariadic(Item ...$drops) : void{
$this->blockDrops = $drops;
}

/**
* Returns how much XP will be dropped by breaking this block.
*/
public function getXpDropAmount() : int{
return $this->xpDrops;
}

/**
* Sets how much XP will be dropped by breaking this block.
*/
public function setXpDropAmount(int $amount) : void{
if($amount < 0){
throw new \InvalidArgumentException("Amount must be at least zero");
}
$this->xpDrops = $amount;
}
}
10 changes: 9 additions & 1 deletion src/world/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use pocketmine\entity\object\ItemEntity;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\block\BlockSupportBreakEvent;
use pocketmine\event\block\BlockUpdateEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\world\ChunkLoadEvent;
Expand Down Expand Up @@ -2096,7 +2097,14 @@ public function useBreakOn(Vector3 $vector, Item &$item = null, ?Player $player

}elseif(!$target->getBreakInfo()->isBreakable()){
return false;
}
}else{
$ev = new BlockSupportBreakEvent($target, $drops, $xpDrop);

$ev->call();

$drops = $ev->getDrops();
$xpDrop = $ev->getXpDropAmount();
}

foreach($affectedBlocks as $t){
$this->destroyBlockInternal($t, $item, $player, $createParticles, $returnedItems);
Expand Down