Updated trigger_error to Exceptions, fixed bug in Plugin task deletion

This commit is contained in:
Shoghi Cervantes 2014-05-26 12:22:28 +02:00
parent ffa3e8a0aa
commit 160c633c08
15 changed files with 127 additions and 83 deletions

View File

@ -2009,6 +2009,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($this->spawned === false or $this->blocked === true){ if($this->spawned === false or $this->blocked === true){
break; break;
} }
/** @var $packet \pocketmine\network\protocol\ContainerSetContentPacket */
console("[TRANSACTION] {$this->username} {$packet->windowid}[ ".$packet->item->getID().":".$packet->item->getDamage()."(".$packet->item->getCount().") -> #{$packet->slot} ]");
if($this->lastCraft <= (microtime(true) - 1)){ if($this->lastCraft <= (microtime(true) - 1)){
if(isset($this->toCraft[-1])){ if(isset($this->toCraft[-1])){
@ -2020,14 +2023,20 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
if($packet->windowid == 0){ //Crafting! if($packet->windowid == 0){ //Crafting!
//TODO: crafting event
$craft = false; $craft = false;
$slot = $this->inventory->getItem($packet->slot); $slot = $this->inventory->getItem($packet->slot);
if($slot->getCount() >= $packet->item->getCount() and (($slot->getID() === $packet->item->getID() and $slot->getDamage() === $packet->item->getDamage()) or ($packet->item->getID() === Item::AIR and $packet->item->getCount() === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe if($slot->getCount() >= $packet->item->getCount() and (($slot->getID() === $packet->item->getID() and $slot->getDamage() === $packet->item->getDamage()) or ($packet->item->getID() === Item::AIR and $packet->item->getCount() === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe
$use = Item::get($slot->getID(), $slot->getDamage(), $slot->getCount() - $packet->item->getCount()); $use = clone $slot;
$use->setCount($slot->getCount() - $packet->item->getCount());
$this->craftingItems[$packet->slot] = $use; $this->craftingItems[$packet->slot] = $use;
$craft = true; $craft = true;
}elseif($slot->getCount() <= $packet->item->getCount() and ($slot->getID() === Item::AIR or ($slot->getID() === $packet->item->getID() and $slot->getDamage() === $packet->item->getDamage()))){ //Crafting final }elseif($slot->getCount() <= $packet->item->getCount() and ($slot->getID() === Item::AIR or ($slot->getID() === $packet->item->getID() and $slot->getDamage() === $packet->item->getDamage()))){ //Crafting final
$craftItem = Item::get($packet->item->getID(), $packet->item->getDamage(), $packet->item->getCount() - $slot->getCount()); /** @var Item $craftItem */
$craftItem = clone $packet->item;
$craftItem->setCount($packet->item->getCount() - $slot->getCount());
if(count($this->toCraft) === 0){ if(count($this->toCraft) === 0){
$this->toCraft[-1] = 0; $this->toCraft[-1] = 0;
} }
@ -2038,7 +2047,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if(count($this->toCraft) === 0){ if(count($this->toCraft) === 0){
$this->toCraft[-1] = 0; $this->toCraft[-1] = 0;
} }
$use = Item::get($slot->getID(), $slot->getDamage(), $slot->getCount()); $use = clone $slot;
$this->craftingItems[$packet->slot] = $use; $this->craftingItems[$packet->slot] = $use;
$this->toCraft[$packet->slot] = $craftItem; $this->toCraft[$packet->slot] = $craftItem;
$craft = true; $craft = true;
@ -2057,11 +2066,12 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
$this->craftingItems = []; $this->craftingItems = [];
} }
break;
}else{ }else{
$this->toCraft = []; $this->toCraft = [];
$this->craftingItems = []; $this->craftingItems = [];
} }
if($packet->windowid == 0 or !isset($this->windowIndex[$packet->windowid])){ if(!isset($this->windowIndex[$packet->windowid])){
break; break;
} }
@ -2079,37 +2089,40 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($slot->getCount() < $item->getCount()){ if($slot->getCount() < $item->getCount()){
$it = clone $item; $it = clone $item;
$it->setCount($item->getCount() - $slot->getCount()); $it->setCount($item->getCount() - $slot->getCount());
$remaining = $this->inventory->removeItem($it); if(!$this->inventory->contains($it)){
if(count($remaining) > 0){ $this->inventory->sendContents($this);
/** @var Item $it */ $inv->sendContents($this);
$it = array_pop($remaining); break;
$item->setCount($item->getCount() - $it->getCount());
} }
$this->inventory->removeItem($it);
}elseif($slot->getCount() > $item->getCount()){ }elseif($slot->getCount() > $item->getCount()){
$it = clone $item; $it = clone $item;
$it->setCount($slot->count - $item->count); $it->setCount($slot->getCount() - $item->getCount());
$remaining = $this->inventory->addItem($it); if(!$this->inventory->canAddItem($it)){
if(count($remaining) > 0){ $this->inventory->sendContents($this);
/** @var Item $it */ $inv->sendContents($this);
$it = array_pop($remaining); break;
$item->setCount($item->getCount() + $it->getCount());
} }
$this->inventory->addItem($it);
} }
if($inv->getType()->getDefaultTitle() === "Furnace" and $packet->slot == 2){ }else{ //same slot replace
switch($slot->getID()){ if(!$this->inventory->contains($item)){
case Item::IRON_INGOT: $this->inventory->sendContents($this);
$this->awardAchievement("acquireIron"); $inv->sendContents($this);
break;
}
}
}else{ //TODO: check this. I don't know what is this for
$remaining = $this->inventory->removeItem($item);
if(count($remaining) > 0){
$this->inventory->removeItem();
break; break;
} }
$this->inventory->removeItem($item);
$this->inventory->addItem($slot); $this->inventory->addItem($slot);
} }
if($inv->getType()->getDefaultTitle() === "Furnace" and $packet->slot === 2){
switch($slot->getID()){
case Item::IRON_INGOT:
$this->awardAchievement("acquireIron");
break;
}
}
$inv->setItem($packet->slot, $item); $inv->setItem($packet->slot, $item);
break; break;

View File

@ -784,12 +784,12 @@ class Server{
* @param string $name * @param string $name
* *
* @return bool * @return bool
*
* @throws \Exception
*/ */
public function loadLevel($name){ public function loadLevel($name){
if(trim($name) === ""){ if(trim($name) === ""){
trigger_error("Invalid empty level name", E_USER_WARNING); throw new \Exception("Invalid empty level name");
return false;
} }
if($this->isLevelLoaded($name)){ if($this->isLevelLoaded($name)){
return true; return true;
@ -1288,7 +1288,7 @@ class Server{
if($this->getDefaultLevel() === null){ if($this->getDefaultLevel() === null){
$default = $this->getConfigString("level-name", "world"); $default = $this->getConfigString("level-name", "world");
if(trim($default) == ""){ if(trim($default) == ""){
trigger_error("level-name cannot be null", E_USER_WARNING); trigger_error("level-name cannot be null, using default", E_USER_WARNING);
$default = "world"; $default = "world";
$this->setConfigString("level-name", "world"); $this->setConfigString("level-name", "world");
} }

View File

@ -80,14 +80,17 @@ class HandlerList{
self::$allLists[] = $this; self::$allLists[] = $this;
} }
/**
* @param RegisteredListener $listener
*
* @throws \Exception
*/
public function register(RegisteredListener $listener){ public function register(RegisteredListener $listener){
if($listener->getPriority() < EventPriority::MONITOR or $listener->getPriority() > EventPriority::LOWEST){ if($listener->getPriority() < EventPriority::MONITOR or $listener->getPriority() > EventPriority::LOWEST){
return; return;
} }
if(isset($this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)])){ if(isset($this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)])){
trigger_error("This listener is already registered to priority " . $listener->getPriority(), E_USER_WARNING); throw new \Exception("This listener is already registered to priority " . $listener->getPriority());
return;
} }
$this->handlers = null; $this->handlers = null;
$this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)] = $listener; $this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)] = $listener;

View File

@ -97,6 +97,22 @@ interface Inventory{
*/ */
public function getContents(); public function getContents();
/**
* @param Item[] $items
*/
public function setContents(array $items);
/**
* @param Player|Player[] $target
*/
public function sendContents($target);
/**
* @param int $index
* @param Player|Player[] $target
*/
public function sendSlot($index, $target);
/** /**
* Checks if the inventory contains any Item with the same material data. * Checks if the inventory contains any Item with the same material data.
* It will check id, amount, and metadata (if not null) * It will check id, amount, and metadata (if not null)

View File

@ -44,7 +44,7 @@ class BlockMetadataStore extends MetadataStore{
if($block->getLevel() === $this->owningLevel){ if($block->getLevel() === $this->owningLevel){
return parent::getMetadata($block, $metadataKey); return parent::getMetadata($block, $metadataKey);
}else{ }else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName()); throw new \Exception("Block does not belong to world " . $this->owningLevel->getName());
} }
} }
@ -52,7 +52,7 @@ class BlockMetadataStore extends MetadataStore{
if($block->getLevel() === $this->owningLevel){ if($block->getLevel() === $this->owningLevel){
return parent::hasMetadata($block, $metadataKey); return parent::hasMetadata($block, $metadataKey);
}else{ }else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName()); throw new \Exception("Block does not belong to world " . $this->owningLevel->getName());
} }
} }
@ -60,7 +60,7 @@ class BlockMetadataStore extends MetadataStore{
if($block->getLevel() === $this->owningLevel){ if($block->getLevel() === $this->owningLevel){
parent::hasMetadata($block, $metadataKey, $owningPlugin); parent::hasMetadata($block, $metadataKey, $owningPlugin);
}else{ }else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName()); throw new \Exception("Block does not belong to world " . $this->owningLevel->getName());
} }
} }
@ -68,7 +68,7 @@ class BlockMetadataStore extends MetadataStore{
if($block->getLevel() === $this->owningLevel){ if($block->getLevel() === $this->owningLevel){
parent::setMetadata($block, $metadataKey, $newMetadatavalue); parent::setMetadata($block, $metadataKey, $newMetadatavalue);
}else{ }else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName()); throw new \Exception("Block does not belong to world " . $this->owningLevel->getName());
} }
} }
} }

View File

@ -36,13 +36,13 @@ abstract class MetadataStore{
* @param mixed $subject * @param mixed $subject
* @param string $metadataKey * @param string $metadataKey
* @param MetadataValue $newMetadataValue * @param MetadataValue $newMetadataValue
*
* @throws \Exception
*/ */
public function setMetadata($subject, $metadataKey, MetadataValue $newMetadataValue){ public function setMetadata($subject, $metadataKey, MetadataValue $newMetadataValue){
$owningPlugin = $newMetadataValue->getOwningPlugin(); $owningPlugin = $newMetadataValue->getOwningPlugin();
if($owningPlugin === null){ if($owningPlugin === null){
trigger_error("Plugin cannot be null", E_USER_WARNING); throw new \Exception("Plugin cannot be null");
return;
} }
$key = $this->disambiguate($subject, $metadataKey); $key = $this->disambiguate($subject, $metadataKey);
@ -63,6 +63,8 @@ abstract class MetadataStore{
* @param string $metadataKey * @param string $metadataKey
* *
* @return MetadataValue[] * @return MetadataValue[]
*
* @throws \Exception
*/ */
public function getMetadata($subject, $metadataKey){ public function getMetadata($subject, $metadataKey){
$key = $this->disambiguate($subject, $metadataKey); $key = $this->disambiguate($subject, $metadataKey);
@ -80,6 +82,8 @@ abstract class MetadataStore{
* @param string $metadataKey * @param string $metadataKey
* *
* @return bool * @return bool
*
* @throws \Exception
*/ */
public function hasMetadata($subject, $metadataKey){ public function hasMetadata($subject, $metadataKey){
return isset($this->metadataMap[$this->disambiguate($subject, $metadataKey)]); return isset($this->metadataMap[$this->disambiguate($subject, $metadataKey)]);
@ -91,6 +95,8 @@ abstract class MetadataStore{
* @param mixed $subject * @param mixed $subject
* @param string $metadataKey * @param string $metadataKey
* @param Plugin $owningPlugin * @param Plugin $owningPlugin
*
* @throws \Exception
*/ */
public function removeMetadata($subject, $metadataKey, Plugin $owningPlugin){ public function removeMetadata($subject, $metadataKey, Plugin $owningPlugin){
$key = $this->disambiguate($subject, $metadataKey); $key = $this->disambiguate($subject, $metadataKey);

View File

@ -21,10 +21,12 @@
namespace pocketmine\network\protocol; namespace pocketmine\network\protocol;
use pocketmine\item\Item;
class ContainerSetSlotPacket extends DataPacket{ class ContainerSetSlotPacket extends DataPacket{
public $windowid; public $windowid;
public $slot; public $slot;
/** @var Item */
public $item; public $item;
public function pid(){ public function pid(){

View File

@ -66,12 +66,12 @@ class PermissibleBase implements Permissible{
/** /**
* @param bool $value * @param bool $value
*
* @throws \Exception
*/ */
public function setOp($value){ public function setOp($value){
if($this->opable === null){ if($this->opable === null){
trigger_error("Cannot change op value as no ServerOperator is set", E_USER_WARNING); throw new \Exception("Cannot change op value as no ServerOperator is set");
return;
}else{ }else{
$this->opable->setOp($value); $this->opable->setOp($value);
} }
@ -118,16 +118,14 @@ class PermissibleBase implements Permissible{
* @param bool $value * @param bool $value
* *
* @return PermissionAttachment * @return PermissionAttachment
*
* @throws \Exception
*/ */
public function addAttachment(Plugin $plugin, $name = null, $value = null){ public function addAttachment(Plugin $plugin, $name = null, $value = null){
if($plugin === null){ if($plugin === null){
trigger_error("Plugin cannot be null", E_USER_WARNING); throw new \Exception("Plugin cannot be null");
return null;
}elseif(!$plugin->isEnabled()){ }elseif(!$plugin->isEnabled()){
trigger_error("Plugin " . $plugin->getDescription()->getName() . " is disabled", E_USER_WARNING); throw new \Exception("Plugin " . $plugin->getDescription()->getName() . " is disabled");
return null;
} }
$result = new PermissionAttachment($plugin, $this->parent); $result = new PermissionAttachment($plugin, $this->parent);
@ -144,13 +142,11 @@ class PermissibleBase implements Permissible{
/** /**
* @param PermissionAttachment $attachment * @param PermissionAttachment $attachment
* *
* @return void * @throws \Exception
*/ */
public function removeAttachment(PermissionAttachment $attachment){ public function removeAttachment(PermissionAttachment $attachment){
if($attachment === null){ if($attachment === null){
trigger_error("Attachment cannot be null", E_USER_WARNING); throw new \Exception("Attachment cannot be null");
return;
} }
if(isset($this->attachments[spl_object_hash($attachment)])){ if(isset($this->attachments[spl_object_hash($attachment)])){

View File

@ -214,6 +214,8 @@ class Permission{
* @param array $output * @param array $output
* *
* @return Permission * @return Permission
*
* @throws \Exception
*/ */
public static function loadPermission($name, array $data, $default = self::DEFAULT_OP, &$output = []){ public static function loadPermission($name, array $data, $default = self::DEFAULT_OP, &$output = []){
$desc = null; $desc = null;
@ -223,7 +225,7 @@ class Permission{
if($value !== null){ if($value !== null){
$default = $value; $default = $value;
}else{ }else{
trigger_error("'default' key contained unknown value", E_USER_WARNING); throw new \Exception("'default' key contained unknown value");
} }
} }
@ -238,7 +240,7 @@ class Permission{
$children[$k] = true; $children[$k] = true;
} }
}else{ }else{
trigger_error("'children' key is of wrong type", E_USER_WARNING); throw new \Exception("'children' key is of wrong type");
} }
} }

View File

@ -41,12 +41,12 @@ class PermissionAttachment{
/** /**
* @param Plugin $plugin * @param Plugin $plugin
* @param Permissible $permissible * @param Permissible $permissible
*
* @throws \Exception
*/ */
public function __construct(Plugin $plugin, Permissible $permissible){ public function __construct(Plugin $plugin, Permissible $permissible){
if(!$plugin->isEnabled()){ if(!$plugin->isEnabled()){
trigger_error("Plugin " . $plugin->getDescription()->getName() . " is disabled", E_USER_WARNING); throw new \Exception("Plugin " . $plugin->getDescription()->getName() . " is disabled");
return;
} }
$this->permissible = $permissible; $this->permissible = $permissible;

View File

@ -40,12 +40,12 @@ class PermissionAttachmentInfo{
* @param string $permission * @param string $permission
* @param PermissionAttachment $attachment * @param PermissionAttachment $attachment
* @param bool $value * @param bool $value
*
* @throws \Exception
*/ */
public function __construct(Permissible $permissible, $permission, $attachment, $value){ public function __construct(Permissible $permissible, $permission, $attachment, $value){
if($permission === null){ if($permission === null){
trigger_error("Permission may not be null", E_USER_WARNING); throw new \Exception("Permission may not be null");
return;
} }
$this->permissible = $permissible; $this->permissible = $permissible;

View File

@ -46,15 +46,15 @@ class PharPluginLoader implements PluginLoader{
* @param string $file * @param string $file
* *
* @return Plugin * @return Plugin
*
* @throws \Exception
*/ */
public function loadPlugin($file){ public function loadPlugin($file){
if(\Phar::isValidPharFilename($file) and ($description = $this->getPluginDescription($file)) instanceof PluginDescription){ if(\Phar::isValidPharFilename($file) and ($description = $this->getPluginDescription($file)) instanceof PluginDescription){
console("[INFO] Loading " . $description->getFullName()); console("[INFO] Loading " . $description->getFullName());
$dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName(); $dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName();
if(file_exists($dataFolder) and !is_dir($dataFolder)){ if(file_exists($dataFolder) and !is_dir($dataFolder)){
trigger_error("Projected dataFolder '" . $dataFolder . "' for " . $description->getName() . " exists and is not a directory", E_USER_WARNING); throw new \Exception("Projected dataFolder '" . $dataFolder . "' for " . $description->getName() . " exists and is not a directory");
return null;
} }
$file = "phar://$file"; $file = "phar://$file";
$className = $description->getMain(); $className = $description->getMain();
@ -68,9 +68,7 @@ class PharPluginLoader implements PluginLoader{
return $plugin; return $plugin;
}else{ }else{
trigger_error("Couldn't load plugin " . $description->getName() . ": main class not found", E_USER_WARNING); throw new \Exception("Couldn't load plugin " . $description->getName() . ": main class not found");
return null;
} }
} }

View File

@ -50,12 +50,14 @@ class PluginDescription{
$this->loadMap(\yaml_parse($yamlString)); $this->loadMap(\yaml_parse($yamlString));
} }
/**
* @param array $plugin
* @throws \Exception
*/
private function loadMap(array $plugin){ private function loadMap(array $plugin){
$this->name = preg_replace("[^A-Za-z0-9 _.-]", "", $plugin["name"]); $this->name = preg_replace("[^A-Za-z0-9 _.-]", "", $plugin["name"]);
if($this->name === ""){ if($this->name === ""){
trigger_error("Invalid PluginDescription name", E_USER_WARNING); throw new \Exception("Invalid PluginDescription name");
return;
} }
$this->name = str_replace(" ", "_", $this->name); $this->name = str_replace(" ", "_", $this->name);
$this->version = $plugin["version"]; $this->version = $plugin["version"];

View File

@ -638,12 +638,12 @@ class PluginManager{
* *
* @param Listener $listener * @param Listener $listener
* @param Plugin $plugin * @param Plugin $plugin
*
* @throws \Exception
*/ */
public function registerEvents(Listener $listener, Plugin $plugin){ public function registerEvents(Listener $listener, Plugin $plugin){
if(!$plugin->isEnabled()){ if(!$plugin->isEnabled()){
trigger_error("Plugin attempted to register " . get_class($listener) . " while not enabled", E_USER_WARNING); throw new \Exception("Plugin attempted to register " . get_class($listener) . " while not enabled");
return;
} }
$reflection = new \ReflectionClass(get_class($listener)); $reflection = new \ReflectionClass(get_class($listener));
@ -680,17 +680,15 @@ class PluginManager{
* @param EventExecutor $executor * @param EventExecutor $executor
* @param Plugin $plugin * @param Plugin $plugin
* @param bool $ignoreCancelled * @param bool $ignoreCancelled
*
* @throws \Exception
*/ */
public function registerEvent($event, Listener $listener, $priority, EventExecutor $executor, Plugin $plugin, $ignoreCancelled = false){ public function registerEvent($event, Listener $listener, $priority, EventExecutor $executor, Plugin $plugin, $ignoreCancelled = false){
if(!is_subclass_of($event, "pocketmine\\event\\Event")){ if(!is_subclass_of($event, "pocketmine\\event\\Event")){
trigger_error($event . " is not a valid Event", E_USER_WARNING); throw new \Exception($event . " is not a valid Event");
return;
} }
if(!$plugin->isEnabled()){ if(!$plugin->isEnabled()){
trigger_error("Plugin attempted to register " . $event . " while not enabled"); throw new \Exception("Plugin attempted to register " . $event . " while not enabled");
return;
} }
$this->getEventListeners($event)->register(new RegisteredListener($listener, $executor, $priority, $plugin, $ignoreCancelled)); $this->getEventListeners($event)->register(new RegisteredListener($listener, $executor, $priority, $plugin, $ignoreCancelled));

View File

@ -123,7 +123,8 @@ class ServerScheduler{
*/ */
public function cancelTasks(Plugin $plugin){ public function cancelTasks(Plugin $plugin){
foreach($this->tasks as $taskId => $task){ foreach($this->tasks as $taskId => $task){
if($task->getTask() instanceof PluginTask){ $ptask = $task->getTask();
if($ptask instanceof PluginTask and $ptask->getOwner() === $plugin){
$task->cancel(); $task->cancel();
unset($this->tasks[$taskId]); unset($this->tasks[$taskId]);
} }
@ -148,11 +149,18 @@ class ServerScheduler{
return isset($this->tasks[$taskId]); return isset($this->tasks[$taskId]);
} }
/**
* @param Task $task
* @param $delay
* @param $period
*
* @return null|TaskHandler
*
* @throws \Exception
*/
private function addTask(Task $task, $delay, $period){ private function addTask(Task $task, $delay, $period){
if($task instanceof PluginTask and !$task->getOwner()->isEnabled()){ if($task instanceof PluginTask and !$task->getOwner()->isEnabled()){
trigger_error("Plugin attempted to register a task while disabled", E_USER_WARNING); throw new \Exception("Plugin attempted to register a task while disabled");
return null;
} }
if($delay <= 0){ if($delay <= 0){