Skip to content

Commit

Permalink
add pmmp#5906
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshy3282 committed Oct 2, 2024
1 parent a1890db commit 97a26fd
Show file tree
Hide file tree
Showing 14 changed files with 568 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Additions
> https://github.com/pmmp/PocketMine-MP/pull/5838
> https://github.com/pmmp/PocketMine-MP/pull/5861
> https://github.com/pmmp/PocketMine-MP/pull/5864
> https://github.com/pmmp/PocketMine-MP/pull/5906

## What is this?
Expand Down
4 changes: 3 additions & 1 deletion src/block/Barrel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\tile\Barrel as TileBarrel;
use pocketmine\block\utils\AnyFacingTrait;
use pocketmine\block\utils\HopperInteractableContainerTrait;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Item;
use pocketmine\math\Facing;
Expand All @@ -33,8 +34,9 @@
use pocketmine\world\BlockTransaction;
use function abs;

class Barrel extends Opaque{
class Barrel extends Opaque implements HopperInteractable{
use AnyFacingTrait;
use HopperInteractableContainerTrait;

protected bool $open = false;

Expand Down
104 changes: 103 additions & 1 deletion src/block/BrewingStand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@

namespace pocketmine\block;

use pocketmine\block\inventory\BrewingStandInventory;
use pocketmine\block\tile\BrewingStand as TileBrewingStand;
use pocketmine\block\tile\Hopper as TileHopper;
use pocketmine\block\utils\BrewingStandSlot;
use pocketmine\block\utils\HopperTransferHelper;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\inventory\Inventory;
use pocketmine\item\Item;
use pocketmine\item\ItemTypeIds;
use pocketmine\item\Potion;
use pocketmine\item\PotionType;
use pocketmine\math\Axis;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
Expand All @@ -36,7 +43,7 @@
use function array_key_exists;
use function spl_object_id;

class BrewingStand extends Transparent{
class BrewingStand extends Transparent implements HopperInteractable{

/**
* @var BrewingStandSlot[]
Expand Down Expand Up @@ -128,4 +135,99 @@ public function onScheduledUpdate() : void{
}
}
}

public function doHopperPush(Hopper $hopperBlock) : bool{
$currentTile = $this->position->getWorld()->getTile($this->position);
if(!$currentTile instanceof TileBrewingStand){
return false;
}

$tileHopper = $this->position->getWorld()->getTile($hopperBlock->position);
if(!$tileHopper instanceof TileHopper){
return false;
}

$sourceInventory = $tileHopper->getInventory();
$targetInventory = $currentTile->getInventory();

$hopperFacing = $hopperBlock->getFacing();

foreach($sourceInventory->getContents() as $item){
if($item->isNull()){
continue;
}

$singleItem = $item->pop();

if($hopperFacing === Facing::DOWN){
$ingredient = $targetInventory->getItem(BrewingStandInventory::SLOT_INGREDIENT);
if($ingredient->isNull() || $ingredient->canStackWith($singleItem)){
$this->transferItem($sourceInventory, $targetInventory, $singleItem, BrewingStandInventory::SLOT_INGREDIENT);

return true;
}
}else{
if($singleItem->getTypeId() === ItemTypeIds::BLAZE_POWDER){
$this->transferItem($sourceInventory, $targetInventory, $singleItem, BrewingStandInventory::SLOT_FUEL);

return true;
}elseif($singleItem instanceof Potion && $singleItem->getType() === PotionType::WATER){
foreach(BrewingStandSlot::cases() as $slot){
$bottle = $targetInventory->getItem($slot->getSlotNumber());
if($bottle->isNull()){
$this->transferItem($sourceInventory, $targetInventory, $singleItem, $slot->getSlotNumber());

return true;
}
}
}
}
}

return false;
}

public function doHopperPull(Hopper $hopperBlock) : bool{
$currentTile = $this->position->getWorld()->getTile($this->position);
if(!$currentTile instanceof TileBrewingStand){
return false;
}

$tileHopper = $this->position->getWorld()->getTile($hopperBlock->position);
if(!$tileHopper instanceof TileHopper){
return false;
}

$sourceInventory = $currentTile->getInventory();
$targetInventory = $tileHopper->getInventory();

foreach(BrewingStandSlot::cases() as $slot){
$bottle = $sourceInventory->getItem($slot->getSlotNumber());
if(!$bottle->isNull()){
if(HopperTransferHelper::transferSpecificItem(
$sourceInventory,
$targetInventory,
$bottle
)){
return true;
}
}
}

return false;
}

private function transferItem(Inventory $sourceInventory, Inventory $targetInventory, Item $item, int $slot) : void{
$sourceInventory->removeItem($item);

$currentItem = $targetInventory->getItem($slot);

if($currentItem->isNull()){
$targetInventory->setItem($slot, $item);
return;
}

$currentItem->setCount($currentItem->getCount() + 1);
$targetInventory->setItem($slot, $currentItem);
}
}
4 changes: 3 additions & 1 deletion src/block/Chest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\tile\Chest as TileChest;
use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
use pocketmine\block\utils\HopperInteractableContainerTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\event\block\ChestPairEvent;
use pocketmine\item\Item;
Expand All @@ -33,8 +34,9 @@
use pocketmine\math\Vector3;
use pocketmine\player\Player;

class Chest extends Transparent{
class Chest extends Transparent implements HopperInteractable{
use FacesOppositePlacingPlayerTrait;
use HopperInteractableContainerTrait;

/**
* @return AxisAlignedBB[]
Expand Down
74 changes: 73 additions & 1 deletion src/block/Furnace.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@

namespace pocketmine\block;

use pocketmine\block\inventory\FurnaceInventory;
use pocketmine\block\tile\Furnace as TileFurnace;
use pocketmine\block\tile\Hopper as TileHopper;
use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
use pocketmine\block\utils\HopperTransferHelper;
use pocketmine\block\utils\LightableTrait;
use pocketmine\crafting\FurnaceType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\inventory\Inventory;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use function mt_rand;

class Furnace extends Opaque{
class Furnace extends Opaque implements HopperInteractable{
use FacesOppositePlacingPlayerTrait;
use LightableTrait;

Expand Down Expand Up @@ -78,4 +83,71 @@ public function onScheduledUpdate() : void{
$world->scheduleDelayedBlockUpdate($this->position, 1); //TODO: check this
}
}

public function doHopperPush(Hopper $hopperBlock) : bool{
$currentTile = $this->position->getWorld()->getTile($this->position);
if(!$currentTile instanceof TileFurnace){
return false;
}

$tileHopper = $this->position->getWorld()->getTile($hopperBlock->position);
if(!$tileHopper instanceof TileHopper){
return false;
}

$sourceInventory = $tileHopper->getInventory();
$targetInventory = $currentTile->getInventory();

$hopperFacing = $hopperBlock->getFacing();

foreach($sourceInventory->getContents() as $item) {
if($item->isNull()){
continue;
}

$singleItem = $item->pop();
if($hopperFacing === Facing::DOWN && $targetInventory->canAddSmelting($singleItem)){
$this->transferItem($sourceInventory, $targetInventory, $singleItem, FurnaceInventory::SLOT_INPUT);
}elseif($hopperFacing !== Facing::DOWN && $targetInventory->canAddFuel($singleItem)){
$this->transferItem($sourceInventory, $targetInventory, $singleItem, FurnaceInventory::SLOT_FUEL);
}
return true;
}
return false;
}

public function doHopperPull(Hopper $hopperBlock) : bool{
$currentTile = $this->position->getWorld()->getTile($this->position);
if(!$currentTile instanceof TileFurnace){
return false;
}

$tileHopper = $this->position->getWorld()->getTile($hopperBlock->position);
if(!$tileHopper instanceof TileHopper){
return false;
}

$sourceInventory = $currentTile->getInventory();
$targetInventory = $tileHopper->getInventory();

return HopperTransferHelper::transferSpecificItem(
$sourceInventory,
$targetInventory,
$sourceInventory->getResult()
);
}

private function transferItem(Inventory $sourceInventory, Inventory $targetInventory, Item $item, int $slot) : void{
$sourceInventory->removeItem($item);

$currentItem = $targetInventory->getItem($slot);

if($currentItem->isNull()){
$targetInventory->setItem($slot, $item);
return;
}

$currentItem->setCount($currentItem->getCount() + 1);
$targetInventory->setItem($slot, $currentItem);
}
}
Loading

0 comments on commit 97a26fd

Please sign in to comment.