PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
Grass.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
27use pocketmine\block\utils\DirtType;
38use function mt_rand;
39
40class Grass extends Opaque{
41
42 public function getDropsForCompatibleTool(Item $item) : array{
43 return [
44 VanillaBlocks::DIRT()->asItem()
45 ];
46 }
47
48 public function isAffectedBySilkTouch() : bool{
49 return true;
50 }
51
52 public function ticksRandomly() : bool{
53 return true;
54 }
55
56 public function onRandomTick() : void{
57 $world = $this->position->getWorld();
58 $lightAbove = $world->getFullLightAt($this->position->x, $this->position->y + 1, $this->position->z);
59 if($lightAbove < 4 && $world->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)->getLightFilter() >= 2){
60 //grass dies
61 BlockEventHelper::spread($this, VanillaBlocks::DIRT(), $this);
62 }elseif($lightAbove >= 9){
63 //try grass spread
64 for($i = 0; $i < 4; ++$i){
65 $x = mt_rand($this->position->x - 1, $this->position->x + 1);
66 $y = mt_rand($this->position->y - 3, $this->position->y + 1);
67 $z = mt_rand($this->position->z - 1, $this->position->z + 1);
68
69 $b = $world->getBlockAt($x, $y, $z);
70 if(
71 !($b instanceof Dirt) ||
72 $b->getDirtType() !== DirtType::NORMAL ||
73 $world->getFullLightAt($x, $y + 1, $z) < 4 ||
74 $world->getBlockAt($x, $y + 1, $z)->getLightFilter() >= 2
75 ){
76 continue;
77 }
78
79 BlockEventHelper::spread($b, VanillaBlocks::GRASS(), $this);
80 }
81 }
82 }
83
84 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
85 if($this->getSide(Facing::UP)->getTypeId() !== BlockTypeIds::AIR){
86 return false;
87 }
88 $world = $this->position->getWorld();
89 if($item instanceof Fertilizer){
90 $item->pop();
91 TallGrassObject::growGrass($world, $this->position, new Random(mt_rand()), 8, 2);
92
93 return true;
94 }
95 if($face !== Facing::DOWN){
96 if($item instanceof Hoe){
97 $item->applyDamage(1);
98 $newBlock = VanillaBlocks::FARMLAND();
99 $world->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock));
100 $world->setBlock($this->position, $newBlock);
101
102 return true;
103 }elseif($item instanceof Shovel){
104 $item->applyDamage(1);
105 $newBlock = VanillaBlocks::GRASS_PATH();
106 $world->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock));
107 $world->setBlock($this->position, $newBlock);
108
109 return true;
110 }
111 }
112
113 return false;
114 }
115}
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition: Grass.php:84
getDropsForCompatibleTool(Item $item)
Definition: Grass.php:42