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

Implemented Composter #4742

Open
wants to merge 42 commits into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
6cdd880
upload files
NTT1906 Jan 19, 2022
405d58a
Merge branch 'next-minor' into composter-new
NTT1906 Jan 19, 2022
164a3f3
43h3j
NTT1906 Jan 19, 2022
24d4bc5
Merge branch 'next-minor' into composter-new
dktapps Jan 28, 2022
93e9000
Merge branch 'stable' into composter-new
NTT1906 Feb 9, 2022
0b3baf6
fix composter's drops, breaking time and composting logic
NTT1906 Feb 9, 2022
e049fab
Merge branch 'stable' into composter-new
NTT1906 Jul 18, 2023
de577cb
Added more compostable items and changed how the register works
NTT1906 Jul 18, 2023
18cb0b5
dummy change...
NTT1906 Jul 18, 2023
a896659
Added Composter's sounds and CropGrowthEmitterParticle
NTT1906 Jul 19, 2023
92a911a
update to work in 5.0
NTT1906 Jul 19, 2023
455585d
weird...
NTT1906 Jul 19, 2023
6c63fa1
Merge branch 'pmmp:stable' into composter-new
NTT1906 Jul 19, 2023
b26b062
Merge remote-tracking branch 'upstream/minor-next' into composter-new
NTT1906 Jul 19, 2023
22206b6
resolve conflicts and lint
NTT1906 Jul 19, 2023
1be0bc4
Merge remote-tracking branch 'upstream/minor-next' into composter-new
NTT1906 Aug 9, 2023
b656193
apply uggested changes
NTT1906 Aug 9, 2023
b1fd07d
Update CompostFactory.php
NTT1906 Aug 9, 2023
015ab8d
Update src/block/Composter.php
NTT1906 Aug 10, 2023
efbd1fa
Update src/block/Composter.php
NTT1906 Aug 10, 2023
ed64ac1
Update src/block/Composter.php
NTT1906 Aug 10, 2023
b3c625f
added const IMMATURE_LEVEL
NTT1906 Aug 10, 2023
9dd2a70
upload some missing changes
NTT1906 Aug 10, 2023
d06ee93
validate the value of percentage in register function
NTT1906 Aug 10, 2023
ccd95d3
add composter events
NTT1906 Aug 10, 2023
3ee1e83
remove empty space covering parenthesis
NTT1906 Aug 10, 2023
3267c47
lint
NTT1906 Aug 10, 2023
1621294
Merge remote-tracking branch 'upstream/minor-next' into composter-new
NTT1906 Sep 2, 2023
0bd106d
apply requested changes
NTT1906 Sep 2, 2023
bd6224f
add missing line
NTT1906 Sep 4, 2023
e1c204d
Update src/event/block/ComposterMatureEvent.php
NTT1906 Sep 4, 2023
cb7510c
Update src/block/Composter.php
NTT1906 Sep 4, 2023
f2c7abe
remove redundant percentage const
NTT1906 Sep 4, 2023
a71c85e
documented getPercentage function
NTT1906 Sep 4, 2023
036e221
revert to tab?
NTT1906 Sep 4, 2023
af9ec5b
revert to tab 2?
NTT1906 Sep 4, 2023
f79bf15
revert to tab
NTT1906 Sep 4, 2023
583581b
?
NTT1906 Sep 4, 2023
7a0ef42
Merge branch 'minor-next' of https://github.com/pmmp/PocketMine-MP in…
NTT1906 Oct 3, 2024
2b462ef
Remove the wrong cobblestone_wall mapping
NTT1906 Oct 3, 2024
abfa564
Add more available compostible blocks and items
NTT1906 Oct 3, 2024
1931dee
Fix minor code style issue
NTT1906 Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/block/BlockTypeIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,9 @@ private function __construct(){
public const COPPER_TRAPDOOR = 10735;
public const CHISELED_COPPER = 10736;
public const COPPER_GRATE = 10737;
public const COMPOSTER = 10738;

public const FIRST_UNUSED_BLOCK_ID = 10738;
public const FIRST_UNUSED_BLOCK_ID = 10739;

private static int $nextDynamicId = self::FIRST_UNUSED_BLOCK_ID;

Expand Down
203 changes: 203 additions & 0 deletions src/block/Composter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?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\block;

use pocketmine\block\utils\CompostFactory;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\ComposterEmptyEvent;
use pocketmine\event\block\ComposterFillEvent;
use pocketmine\event\block\ComposterMatureEvent;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
use pocketmine\world\particle\CropGrowthEmitterParticle;
use pocketmine\world\sound\ComposterEmptySound;
use pocketmine\world\sound\ComposterFillSound;
use pocketmine\world\sound\ComposterFillSuccessSound;
use pocketmine\world\sound\ComposterReadySound;
use function max;
use function mt_rand;

class Composter extends Transparent{

public const EMPTY_LEVEL = 0;
public const FULL_LEVEL = 7;
public const COLLECTING_LEVEL = 8;

protected int $fillLevel = self::EMPTY_LEVEL;

protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->boundedInt(4, self::EMPTY_LEVEL, self::COLLECTING_LEVEL, $this->fillLevel);
}

protected function recalculateCollisionBoxes() : array{
$empty_layer = (max(1, 15 - 2 * $this->fillLevel) - (int) ($this->fillLevel === 0)) / 16;
$boxes = [AxisAlignedBB::one()->trim(Facing::UP, $empty_layer)];

foreach(Facing::HORIZONTAL as $side){
$boxes[] = AxisAlignedBB::one()->trim(Facing::opposite($side), 14 / 16);
}
return $boxes;
}

public function isEmpty() : bool{
return $this->fillLevel === self::EMPTY_LEVEL;
}

public function isFull() : bool{
return $this->fillLevel === self::FULL_LEVEL;
}

public function canCollect() : bool{
return $this->fillLevel === self::COLLECTING_LEVEL;
}

public function getFillLevel() : int{
return $this->fillLevel;
}

/** @return $this */
public function setFillLevel(int $fillLevel) : self{
if($fillLevel < 0 || $fillLevel > self::COLLECTING_LEVEL){
throw new \InvalidArgumentException("Layers must be in range " . self::EMPTY_LEVEL . " ... " . self::COLLECTING_LEVEL);
}
$this->fillLevel = $fillLevel;
return $this;
}

public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($this->isFull()){
NTT1906 marked this conversation as resolved.
Show resolved Hide resolved
$this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 20);
}
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}

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

public function onScheduledUpdate() : void{
if($this->isFull()){
$block = clone $this;
$block->setFillLevel(self::COLLECTING_LEVEL);

if(ComposterMatureEvent::hasHandlers()){
$ev = new ComposterMatureEvent($this, $block);
$ev->call();
if($ev->isCancelled()){
return;
}
$block = $ev->getNewState();
}

$this->position->getWorld()->addSound($this->position, new ComposterReadySound());
$this->position->getWorld()->setBlock($this->position, $block);
}
}

public function addItem(Item $item) : bool{
if($this->canCollect()){
$this->empty();
return false;
}
if($this->isFull() || !CompostFactory::getInstance()->isCompostable($item)){
return false;
}

$this->position->getWorld()->addParticle($this->position->add(0.5, 0.5, 0.5), new CropGrowthEmitterParticle());

$success = mt_rand(1,100) <= CompostFactory::getInstance()->getPercentage($item);
if(ComposterFillEvent::hasHandlers()){
$ev = new ComposterFillEvent($this, $item, $this->fillLevel, $success);
$ev->call();
if($ev->isCancelled()){
return false;
}
$success = $ev->isSuccess();
}
if($success){
$this->fillLevel++;
$this->position->getWorld()->addSound($this->position, new ComposterFillSuccessSound());
$this->position->getWorld()->setBlock($this->position, $this);

if($this->isFull()){
$this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 20);
}
}else{
$this->position->getWorld()->addSound($this->position, new ComposterFillSound());
}
return true;
}

public function empty() : void{
$drops = [VanillaItems::BONE_MEAL()];
if(ComposterEmptyEvent::hasHandlers()){
$ev = new ComposterEmptyEvent($this, $drops);
$ev->call();
if($ev->isCancelled()){
return;
}
$drops = $ev->getDrops();
}
foreach($drops as $drop){
$this->position->getWorld()->dropItem(
$this->position->add(0.5, 0.85, 0.5),
$drop,
new Vector3(0, 0, 0)
);
}
$this->fillLevel = self::EMPTY_LEVEL;
$this->position->getWorld()->addParticle($this->position, new CropGrowthEmitterParticle());
$this->position->getWorld()->addSound($this->position, new ComposterEmptySound());
$this->position->getWorld()->setBlock($this->position, $this);
}

public function getDropsForCompatibleTool(Item $item) : array{
return $this->canCollect() ? [
VanillaBlocks::COMPOSTER()->asItem(),
VanillaItems::BONE_MEAL()
] : [
VanillaBlocks::COMPOSTER()->asItem()
];
}

public function getFlameEncouragement() : int{
return 5;
}

public function getFlammability() : int{
return 20;
}

public function getFuelTime() : int{
return 50;
}
}
2 changes: 2 additions & 0 deletions src/block/VanillaBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
* @method static Wall COBBLESTONE_WALL()
* @method static Cobweb COBWEB()
* @method static CocoaBlock COCOA_POD()
* @method static Composter COMPOSTER()
* @method static ChemistryTable COMPOUND_CREATOR()
* @method static Concrete CONCRETE()
* @method static ConcretePowder CONCRETE_POWDER()
Expand Down Expand Up @@ -866,6 +867,7 @@ public function getBreakTime(Item $item) : float{
self::register("mossy_cobblestone_stairs", new Stair(new BID(Ids::MOSSY_COBBLESTONE_STAIRS), "Mossy Cobblestone Stairs", $cobblestoneBreakInfo));

self::register("cobweb", new Cobweb(new BID(Ids::COBWEB), "Cobweb", new Info(new BreakInfo(4.0, ToolType::SWORD | ToolType::SHEARS, 1))));
self::register("composter", new Composter(new BID(Ids::COMPOSTER), "Composter", new Info(BreakInfo::axe(2))));
self::register("cocoa_pod", new CocoaBlock(new BID(Ids::COCOA_POD), "Cocoa Block", new Info(BreakInfo::axe(0.2, null, 15.0))));
self::register("coral_block", new CoralBlock(new BID(Ids::CORAL_BLOCK), "Coral Block", new Info(BreakInfo::pickaxe(7.0, ToolTier::WOOD))));
self::register("daylight_sensor", new DaylightSensor(new BID(Ids::DAYLIGHT_SENSOR, TileDaylightSensor::class), "Daylight Sensor", new Info(BreakInfo::axe(0.2))));
Expand Down
Loading