Added FurnaceBurnEvent

This commit is contained in:
Shoghi Cervantes 2014-10-09 12:36:57 +02:00
parent 22ad75c5a0
commit c67d4dae7b
2 changed files with 114 additions and 18 deletions

View File

@ -0,0 +1,85 @@
<?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/
*
*
*/
namespace pocketmine\event\inventory;
use pocketmine\event\block\BlockEvent;
use pocketmine\event\Cancellable;
use pocketmine\item\Item;
use pocketmine\tile\Furnace;
class FurnaceBurnEvent extends BlockEvent implements Cancellable{
public static $handlerList = null;
private $furnace;
private $fuel;
private $burnTime;
private $burning = true;
public function __construct(Furnace $furnace, Item $fuel, $burnTime){
parent::__construct($furnace->getBlock());
$this->fuel = $fuel;
$this->burnTime = (int) $burnTime;
$this->furnace = $furnace;
}
/**
* @return Furnace
*/
public function getFurnace(){
return $this->furnace;
}
/**
* @return Item
*/
public function getFuel(){
return $this->fuel;
}
/**
* @return int
*/
public function getBurnTime(){
return $this->burnTime;
}
/**
* @param int $burnTime
*/
public function setBurnTime($burnTime){
$this->burnTime = (int) $burnTime;
}
/**
* @return bool
*/
public function isBurning(){
return $this->burning;
}
/**
* @param bool $burning
*/
public function setBurning($burning){
$this->burning = (bool) $burning;
}
}

View File

@ -22,6 +22,7 @@
namespace pocketmine\tile;
use pocketmine\block\Block;
use pocketmine\event\inventory\FurnaceBurnEvent;
use pocketmine\inventory\FurnaceInventory;
use pocketmine\inventory\FurnaceRecipe;
use pocketmine\inventory\InventoryHolder;
@ -166,6 +167,29 @@ class Furnace extends Tile implements InventoryHolder, Container{
return $this->inventory;
}
protected function checkFuel(Item $fuel){
$this->server->getPluginManager()->callEvent($ev = new FurnaceBurnEvent($this, $fuel, $fuel->getFuelTime()));
if($ev->isCancelled()){
return;
}
$this->namedtag->MaxTime = new Short("MaxTime", $ev->getBurnTime());
$this->namedtag->BurnTime = new Short("BurnTime", $ev->getBurnTime());
$this->namedtag->BurnTicks = new Short("BurnTicks", 0);
if($this->getBlock()->getID() === Item::FURNACE){
$this->getLevel()->setBlock($this, Block::get(Item::BURNING_FURNACE, $this->getBlock()->getDamage()), true);
}
if($this->namedtag["BurnTime"] > 0 and $ev->isBurning()){
$fuel->setCount($fuel->getCount() - 1);
if($fuel->getCount() === 0){
$fuel = Item::get(Item::AIR, 0, 0);
}
$this->inventory->setFuel($fuel);
}
}
public function onUpdate(){
if($this->closed === true){
return false;
@ -181,21 +205,9 @@ class Furnace extends Tile implements InventoryHolder, Container{
$smelt = $this->server->getCraftingManager()->matchFurnaceRecipe($raw);
$canSmelt = ($smelt instanceof FurnaceRecipe and $raw->getCount() > 0 and (($smelt->getResult()->equals($product, true) and $product->getCount() < $product->getMaxStackSize()) or $product->getID() === Item::AIR));
if($this->namedtag["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== null and $fuel->getCount() > 0){
$this->lastUpdate = microtime(true);
$time = floor($fuel->getFuelTime());
$this->namedtag->MaxTime = new Short("MaxTime", $time);
$this->namedtag->BurnTime = new Short("BurnTime", $time);
$this->namedtag->BurnTicks = new Short("BurnTicks", 0);
$fuel->setCount($fuel->getCount() - 1);
if($fuel->getCount() === 0){
$fuel = Item::get(Item::AIR, 0, 0);
}
$this->inventory->setFuel($fuel);
$current = $this->getLevel()->getBlock($this);
if($current->getID() === Item::FURNACE){
$this->getLevel()->setBlock($this, Block::get(Item::BURNING_FURNACE, $current->getDamage()), true);
}
$this->checkFuel($fuel);
}
if($this->namedtag["BurnTime"] > 0){
$this->namedtag->BurnTime = new Short("BurnTime", $this->namedtag["BurnTime"] - 1);
$this->namedtag->BurnTicks = new Short("BurnTicks", ceil(($this->namedtag["BurnTime"] / $this->namedtag["MaxTime"] * 200)));
@ -220,10 +232,9 @@ class Furnace extends Tile implements InventoryHolder, Container{
$this->namedtag->CookTime = new Short("CookTime", 0);
}
$ret = true;
}else{
$current = $this->getLevel()->getBlock($this);
if($current->getID() === Item::BURNING_FURNACE){
$this->getLevel()->setBlock($this, Block::get(Item::FURNACE, $current->getDamage()), true);
}else{;
if($this->getBlock()->getID() === Item::BURNING_FURNACE){
$this->getLevel()->setBlock($this, Block::get(Item::FURNACE, $this->getBlock()->getDamage()), true);
}
$this->namedtag->BurnTime = new Short("BurnTime", 0);
$this->namedtag->CookTime = new Short("CookTime", 0);