mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-08-30 15:01:19 +00:00
Add FarmlandHydrationChangeEvent (#5916)
This commit is contained in:
parent
59c88fe7f7
commit
a5d8ef7a6c
@ -26,6 +26,7 @@ namespace pocketmine\block;
|
|||||||
use pocketmine\data\runtime\RuntimeDataDescriber;
|
use pocketmine\data\runtime\RuntimeDataDescriber;
|
||||||
use pocketmine\entity\Entity;
|
use pocketmine\entity\Entity;
|
||||||
use pocketmine\entity\Living;
|
use pocketmine\entity\Living;
|
||||||
|
use pocketmine\event\block\FarmlandHydrationChangeEvent;
|
||||||
use pocketmine\event\entity\EntityTrampleFarmlandEvent;
|
use pocketmine\event\entity\EntityTrampleFarmlandEvent;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\math\AxisAlignedBB;
|
use pocketmine\math\AxisAlignedBB;
|
||||||
@ -73,14 +74,22 @@ class Farmland extends Transparent{
|
|||||||
$world = $this->position->getWorld();
|
$world = $this->position->getWorld();
|
||||||
if(!$this->canHydrate()){
|
if(!$this->canHydrate()){
|
||||||
if($this->wetness > 0){
|
if($this->wetness > 0){
|
||||||
$this->wetness--;
|
$event = new FarmlandHydrationChangeEvent($this, $this->wetness, $this->wetness - 1);
|
||||||
$world->setBlock($this->position, $this, false);
|
$event->call();
|
||||||
|
if(!$event->isCancelled()){
|
||||||
|
$this->wetness = $event->getNewHydration();
|
||||||
|
$world->setBlock($this->position, $this, false);
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
$world->setBlock($this->position, VanillaBlocks::DIRT());
|
$world->setBlock($this->position, VanillaBlocks::DIRT());
|
||||||
}
|
}
|
||||||
}elseif($this->wetness < self::MAX_WETNESS){
|
}elseif($this->wetness < self::MAX_WETNESS){
|
||||||
$this->wetness = self::MAX_WETNESS;
|
$event = new FarmlandHydrationChangeEvent($this, $this->wetness, self::MAX_WETNESS);
|
||||||
$world->setBlock($this->position, $this, false);
|
$event->call();
|
||||||
|
if(!$event->isCancelled()){
|
||||||
|
$this->wetness = $event->getNewHydration();
|
||||||
|
$world->setBlock($this->position, $this, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
src/event/block/FarmlandHydrationChangeEvent.php
Normal file
59
src/event/block/FarmlandHydrationChangeEvent.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?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\block\Farmland;
|
||||||
|
use pocketmine\event\Cancellable;
|
||||||
|
use pocketmine\event\CancellableTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when farmland hydration is updated.
|
||||||
|
*/
|
||||||
|
class FarmlandHydrationChangeEvent extends BlockEvent implements Cancellable{
|
||||||
|
use CancellableTrait;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
Block $block,
|
||||||
|
private int $oldHydration,
|
||||||
|
private int $newHydration,
|
||||||
|
){
|
||||||
|
parent::__construct($block);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOldHydration() : int{
|
||||||
|
return $this->oldHydration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getNewHydration() : int{
|
||||||
|
return $this->newHydration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setNewHydration(int $hydration) : void{
|
||||||
|
if($hydration < 0 || $hydration > Farmland::MAX_WETNESS){
|
||||||
|
throw new \InvalidArgumentException("Hydration must be in range 0 ... " . Farmland::MAX_WETNESS);
|
||||||
|
}
|
||||||
|
$this->newHydration = $hydration;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user