From da23cf685d2a8c39339d39213ba12eb9ead7f7b2 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 13 Oct 2014 18:04:40 +0200 Subject: [PATCH] Added PlayerBucketEvent and children, improved Bucket usage and Liquid placing --- src/pocketmine/block/Liquid.php | 12 ++- .../event/player/PlayerBucketEmptyEvent.php | 34 ++++++++ .../event/player/PlayerBucketEvent.php | 86 +++++++++++++++++++ .../event/player/PlayerBucketFillEvent.php | 34 ++++++++ src/pocketmine/item/Bucket.php | 51 +++++------ src/pocketmine/math/Vector3.php | 7 +- 6 files changed, 193 insertions(+), 31 deletions(-) create mode 100644 src/pocketmine/event/player/PlayerBucketEmptyEvent.php create mode 100644 src/pocketmine/event/player/PlayerBucketEvent.php create mode 100644 src/pocketmine/event/player/PlayerBucketFillEvent.php diff --git a/src/pocketmine/block/Liquid.php b/src/pocketmine/block/Liquid.php index 489224e6a..4813f4aaf 100644 --- a/src/pocketmine/block/Liquid.php +++ b/src/pocketmine/block/Liquid.php @@ -37,8 +37,8 @@ abstract class Liquid extends Transparent{ public $isFullBlock = true; public $adjacentSources = 0; - public $isOptimalFlowDirection = [0, 0, 0]; - public $flowCost = [0, 0, 0]; + public $isOptimalFlowDirection = [0, 0, 0, 0]; + public $flowCost = [0, 0, 0, 0]; public function getFluidHeightPercent(){ $d = $this->meta; @@ -316,7 +316,9 @@ abstract class Liquid extends Transparent{ } $blockSide = $this->getLevel()->getBlock(new Vector3($x, $y, $z)); - if(!$blockSide->isFlowable or ($blockSide instanceof Liquid and $blockSide->getDamage() === 0)){ + if(!$blockSide->isFlowable and !($blockSide instanceof Liquid)){ + continue; + }elseif($blockSide instanceof Liquid and $blockSide->getDamage() === 0){ continue; }elseif($blockSide->getSide(0)->isFlowable){ return $accumulatedCost; @@ -356,7 +358,9 @@ abstract class Liquid extends Transparent{ } $block = $this->getLevel()->getBlock(new Vector3($x, $y, $z)); - if(!$block->isFlowable or ($block instanceof Liquid and $block->getDamage() === 0)){ + if(!$block->isFlowable and !($block instanceof Liquid)){ + continue; + }elseif($block instanceof Liquid and $block->getDamage() === 0){ continue; }elseif($block->getSide(0)->isFlowable){ $this->flowCost[$j] = 0; diff --git a/src/pocketmine/event/player/PlayerBucketEmptyEvent.php b/src/pocketmine/event/player/PlayerBucketEmptyEvent.php new file mode 100644 index 000000000..3e0955c53 --- /dev/null +++ b/src/pocketmine/event/player/PlayerBucketEmptyEvent.php @@ -0,0 +1,34 @@ +player = $who; + $this->blockClicked = $blockClicked; + $this->blockFace = (int) $blockFace; + $this->item = $itemInHand; + $this->bucket = $bucket; + } + + /** + * Returns the bucket used in this event + * + * @return Item + */ + public function getBucket(){ + return $this->bucket; + } + + /** + * Returns the item in hand after the event + * + * @return Item + */ + public function getItem(){ + return $this->item; + } + + /** + * @param Item $item + */ + public function setItem(Item $item){ + $this->item = $item; + } + + /** + * @return Block + */ + public function getBlockClicked(){ + return $this->blockClicked; + } +} \ No newline at end of file diff --git a/src/pocketmine/event/player/PlayerBucketFillEvent.php b/src/pocketmine/event/player/PlayerBucketFillEvent.php new file mode 100644 index 000000000..af19c7bf6 --- /dev/null +++ b/src/pocketmine/event/player/PlayerBucketFillEvent.php @@ -0,0 +1,34 @@ +meta === Item::AIR){ - if($target instanceof Liquid){ - $level->setBlock($target, new Air(), true); - if(($player->gamemode & 0x01) === 0){ - $this->meta = ($target instanceof Water) ? Item::WATER : Item::LAVA; - } + $targetBlock = Block::get($this->meta); - return true; + if($targetBlock instanceof Air){ + if($target instanceof Liquid and $target->getDamage() === 0){ + $result = clone $this; + $result->setDamage($target->getID()); + $player->getServer()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $block, $face, $this, $result)); + if(!$ev->isCancelled()){ + $player->getLevel()->setBlock($target, new Air(), true, true); + if($player->isSurvival()){ + $player->getInventory()->setItemInHand($ev->getItem(), $player); + } + return true; + }else{ + $player->getInventory()->sendContents($player); + } } - }elseif($this->meta === Item::WATER){ - //Support Make Non-Support Water to Support Water - if($block->getID() === self::AIR || ($block instanceof Water && ($block->getDamage() & 0x07) != 0x00)){ - $water = Block::get(Item::WATER, 0, $block); - $water->place(clone $this, $block, $target, $face, $fx, $fy, $fz, $player); - if(($player->gamemode & 0x01) === 0){ - $this->meta = 0; + }elseif($targetBlock instanceof Liquid){ + $result = clone $this; + $result->setDamage(0); + $player->getServer()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $block, $face, $this, $result)); + if(!$ev->isCancelled()){ + $player->getLevel()->setBlock($block, $targetBlock, true, true); + if($player->isSurvival()){ + $player->getInventory()->setItemInHand($ev->getItem(), $player); } - - return true; - } - }elseif($this->meta === Item::LAVA){ - $lava = Block::get(Item::LAVA, 0, $block); - $lava->place(clone $this, $block, $target, $face, $fx, $fy, $fz, $player); - if($block->getID() === self::AIR){ - if(($player->gamemode & 0x01) === 0){ - $this->meta = 0; - } - return true; + }else{ + $player->getInventory()->sendContents($player); } } diff --git a/src/pocketmine/math/Vector3.php b/src/pocketmine/math/Vector3.php index dcea259c7..8a8864d93 100644 --- a/src/pocketmine/math/Vector3.php +++ b/src/pocketmine/math/Vector3.php @@ -97,7 +97,7 @@ class Vector3{ */ public function add($x, $y = 0, $z = 0){ if($x instanceof Vector3){ - return $this->add($x->x, $x->y, $x->z); + return new Vector3($this->x + $x->x, $this->y + $x->y, $this->z + $x->z); }else{ return new Vector3($this->x + $x, $this->y + $y, $this->z + $z); } @@ -131,7 +131,10 @@ class Vector3{ } public function floor(){ - return new Vector3(Math::floorFloat($this->x), Math::floorFloat($this->y), Math::floorFloat($this->z)); + $x = (int) $this->x; + $y = (int) $this->y; + $z = (int) $this->z; + return new Vector3($this->x >= $x ? $x : $x - 1, $this->y >= $y ? $y : $y - 1, $this->z >= $z ? $z : $z - 1); } public function round(){