From 160c633c080a936235681b699e01b8fbdb074a38 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 26 May 2014 12:22:28 +0200 Subject: [PATCH] Updated trigger_error to Exceptions, fixed bug in Plugin task deletion --- src/pocketmine/Player.php | 65 +++++++++++-------- src/pocketmine/Server.php | 8 +-- src/pocketmine/event/HandlerList.php | 9 ++- src/pocketmine/inventory/Inventory.php | 16 +++++ .../metadata/BlockMetadataStore.php | 8 +-- src/pocketmine/metadata/MetadataStore.php | 12 +++- .../protocol/ContainerSetSlotPacket.php | 2 + src/pocketmine/permission/PermissibleBase.php | 22 +++---- src/pocketmine/permission/Permission.php | 6 +- .../permission/PermissionAttachment.php | 6 +- .../permission/PermissionAttachmentInfo.php | 6 +- src/pocketmine/plugin/PharPluginLoader.php | 10 ++- src/pocketmine/plugin/PluginDescription.php | 8 ++- src/pocketmine/plugin/PluginManager.php | 16 ++--- src/pocketmine/scheduler/ServerScheduler.php | 16 +++-- 15 files changed, 127 insertions(+), 83 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 75d47b169..381f54e42 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2009,6 +2009,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if($this->spawned === false or $this->blocked === true){ 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(isset($this->toCraft[-1])){ @@ -2020,14 +2023,20 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } if($packet->windowid == 0){ //Crafting! + //TODO: crafting event + + $craft = false; $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 - $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; $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 - $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){ $this->toCraft[-1] = 0; } @@ -2038,7 +2047,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if(count($this->toCraft) === 0){ $this->toCraft[-1] = 0; } - $use = Item::get($slot->getID(), $slot->getDamage(), $slot->getCount()); + $use = clone $slot; $this->craftingItems[$packet->slot] = $use; $this->toCraft[$packet->slot] = $craftItem; $craft = true; @@ -2057,11 +2066,12 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } $this->craftingItems = []; } + break; }else{ $this->toCraft = []; $this->craftingItems = []; } - if($packet->windowid == 0 or !isset($this->windowIndex[$packet->windowid])){ + if(!isset($this->windowIndex[$packet->windowid])){ break; } @@ -2079,37 +2089,40 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if($slot->getCount() < $item->getCount()){ $it = clone $item; $it->setCount($item->getCount() - $slot->getCount()); - $remaining = $this->inventory->removeItem($it); - if(count($remaining) > 0){ - /** @var Item $it */ - $it = array_pop($remaining); - $item->setCount($item->getCount() - $it->getCount()); + if(!$this->inventory->contains($it)){ + $this->inventory->sendContents($this); + $inv->sendContents($this); + break; } + $this->inventory->removeItem($it); }elseif($slot->getCount() > $item->getCount()){ $it = clone $item; - $it->setCount($slot->count - $item->count); - $remaining = $this->inventory->addItem($it); - if(count($remaining) > 0){ - /** @var Item $it */ - $it = array_pop($remaining); - $item->setCount($item->getCount() + $it->getCount()); + $it->setCount($slot->getCount() - $item->getCount()); + if(!$this->inventory->canAddItem($it)){ + $this->inventory->sendContents($this); + $inv->sendContents($this); + break; } + $this->inventory->addItem($it); } - if($inv->getType()->getDefaultTitle() === "Furnace" and $packet->slot == 2){ - switch($slot->getID()){ - case Item::IRON_INGOT: - $this->awardAchievement("acquireIron"); - 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(); + }else{ //same slot replace + if(!$this->inventory->contains($item)){ + $this->inventory->sendContents($this); + $inv->sendContents($this); break; } + $this->inventory->removeItem($item); $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); break; diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 5736ea3ca..adc974f64 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -784,12 +784,12 @@ class Server{ * @param string $name * * @return bool + * + * @throws \Exception */ public function loadLevel($name){ if(trim($name) === ""){ - trigger_error("Invalid empty level name", E_USER_WARNING); - - return false; + throw new \Exception("Invalid empty level name"); } if($this->isLevelLoaded($name)){ return true; @@ -1288,7 +1288,7 @@ class Server{ if($this->getDefaultLevel() === null){ $default = $this->getConfigString("level-name", "world"); 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"; $this->setConfigString("level-name", "world"); } diff --git a/src/pocketmine/event/HandlerList.php b/src/pocketmine/event/HandlerList.php index acf0b8ff1..29fc190f8 100644 --- a/src/pocketmine/event/HandlerList.php +++ b/src/pocketmine/event/HandlerList.php @@ -80,14 +80,17 @@ class HandlerList{ self::$allLists[] = $this; } + /** + * @param RegisteredListener $listener + * + * @throws \Exception + */ public function register(RegisteredListener $listener){ if($listener->getPriority() < EventPriority::MONITOR or $listener->getPriority() > EventPriority::LOWEST){ return; } if(isset($this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)])){ - trigger_error("This listener is already registered to priority " . $listener->getPriority(), E_USER_WARNING); - - return; + throw new \Exception("This listener is already registered to priority " . $listener->getPriority()); } $this->handlers = null; $this->handlerSlots[$listener->getPriority()][spl_object_hash($listener)] = $listener; diff --git a/src/pocketmine/inventory/Inventory.php b/src/pocketmine/inventory/Inventory.php index 3bdd02815..c10c39238 100644 --- a/src/pocketmine/inventory/Inventory.php +++ b/src/pocketmine/inventory/Inventory.php @@ -97,6 +97,22 @@ interface Inventory{ */ 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. * It will check id, amount, and metadata (if not null) diff --git a/src/pocketmine/metadata/BlockMetadataStore.php b/src/pocketmine/metadata/BlockMetadataStore.php index 162a47190..fbefe5af3 100644 --- a/src/pocketmine/metadata/BlockMetadataStore.php +++ b/src/pocketmine/metadata/BlockMetadataStore.php @@ -44,7 +44,7 @@ class BlockMetadataStore extends MetadataStore{ if($block->getLevel() === $this->owningLevel){ return parent::getMetadata($block, $metadataKey); }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){ return parent::hasMetadata($block, $metadataKey); }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){ parent::hasMetadata($block, $metadataKey, $owningPlugin); }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){ parent::setMetadata($block, $metadataKey, $newMetadatavalue); }else{ - trigger_error("Block does not belong to world " . $this->owningLevel->getName()); + throw new \Exception("Block does not belong to world " . $this->owningLevel->getName()); } } } \ No newline at end of file diff --git a/src/pocketmine/metadata/MetadataStore.php b/src/pocketmine/metadata/MetadataStore.php index 68ebe7b24..d980f276e 100644 --- a/src/pocketmine/metadata/MetadataStore.php +++ b/src/pocketmine/metadata/MetadataStore.php @@ -36,13 +36,13 @@ abstract class MetadataStore{ * @param mixed $subject * @param string $metadataKey * @param MetadataValue $newMetadataValue + * + * @throws \Exception */ public function setMetadata($subject, $metadataKey, MetadataValue $newMetadataValue){ $owningPlugin = $newMetadataValue->getOwningPlugin(); if($owningPlugin === null){ - trigger_error("Plugin cannot be null", E_USER_WARNING); - - return; + throw new \Exception("Plugin cannot be null"); } $key = $this->disambiguate($subject, $metadataKey); @@ -63,6 +63,8 @@ abstract class MetadataStore{ * @param string $metadataKey * * @return MetadataValue[] + * + * @throws \Exception */ public function getMetadata($subject, $metadataKey){ $key = $this->disambiguate($subject, $metadataKey); @@ -80,6 +82,8 @@ abstract class MetadataStore{ * @param string $metadataKey * * @return bool + * + * @throws \Exception */ public function hasMetadata($subject, $metadataKey){ return isset($this->metadataMap[$this->disambiguate($subject, $metadataKey)]); @@ -91,6 +95,8 @@ abstract class MetadataStore{ * @param mixed $subject * @param string $metadataKey * @param Plugin $owningPlugin + * + * @throws \Exception */ public function removeMetadata($subject, $metadataKey, Plugin $owningPlugin){ $key = $this->disambiguate($subject, $metadataKey); diff --git a/src/pocketmine/network/protocol/ContainerSetSlotPacket.php b/src/pocketmine/network/protocol/ContainerSetSlotPacket.php index c74ecb783..c24119150 100644 --- a/src/pocketmine/network/protocol/ContainerSetSlotPacket.php +++ b/src/pocketmine/network/protocol/ContainerSetSlotPacket.php @@ -21,10 +21,12 @@ namespace pocketmine\network\protocol; +use pocketmine\item\Item; class ContainerSetSlotPacket extends DataPacket{ public $windowid; public $slot; + /** @var Item */ public $item; public function pid(){ diff --git a/src/pocketmine/permission/PermissibleBase.php b/src/pocketmine/permission/PermissibleBase.php index 094b8fe88..371c1ab8a 100644 --- a/src/pocketmine/permission/PermissibleBase.php +++ b/src/pocketmine/permission/PermissibleBase.php @@ -66,12 +66,12 @@ class PermissibleBase implements Permissible{ /** * @param bool $value + * + * @throws \Exception */ public function setOp($value){ if($this->opable === null){ - trigger_error("Cannot change op value as no ServerOperator is set", E_USER_WARNING); - - return; + throw new \Exception("Cannot change op value as no ServerOperator is set"); }else{ $this->opable->setOp($value); } @@ -118,16 +118,14 @@ class PermissibleBase implements Permissible{ * @param bool $value * * @return PermissionAttachment + * + * @throws \Exception */ public function addAttachment(Plugin $plugin, $name = null, $value = null){ if($plugin === null){ - trigger_error("Plugin cannot be null", E_USER_WARNING); - - return null; + throw new \Exception("Plugin cannot be null"); }elseif(!$plugin->isEnabled()){ - trigger_error("Plugin " . $plugin->getDescription()->getName() . " is disabled", E_USER_WARNING); - - return null; + throw new \Exception("Plugin " . $plugin->getDescription()->getName() . " is disabled"); } $result = new PermissionAttachment($plugin, $this->parent); @@ -144,13 +142,11 @@ class PermissibleBase implements Permissible{ /** * @param PermissionAttachment $attachment * - * @return void + * @throws \Exception */ public function removeAttachment(PermissionAttachment $attachment){ if($attachment === null){ - trigger_error("Attachment cannot be null", E_USER_WARNING); - - return; + throw new \Exception("Attachment cannot be null"); } if(isset($this->attachments[spl_object_hash($attachment)])){ diff --git a/src/pocketmine/permission/Permission.php b/src/pocketmine/permission/Permission.php index 6a1c2a91c..23977abea 100644 --- a/src/pocketmine/permission/Permission.php +++ b/src/pocketmine/permission/Permission.php @@ -214,6 +214,8 @@ class Permission{ * @param array $output * * @return Permission + * + * @throws \Exception */ public static function loadPermission($name, array $data, $default = self::DEFAULT_OP, &$output = []){ $desc = null; @@ -223,7 +225,7 @@ class Permission{ if($value !== null){ $default = $value; }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; } }else{ - trigger_error("'children' key is of wrong type", E_USER_WARNING); + throw new \Exception("'children' key is of wrong type"); } } diff --git a/src/pocketmine/permission/PermissionAttachment.php b/src/pocketmine/permission/PermissionAttachment.php index af0e836b9..f1a93c309 100644 --- a/src/pocketmine/permission/PermissionAttachment.php +++ b/src/pocketmine/permission/PermissionAttachment.php @@ -41,12 +41,12 @@ class PermissionAttachment{ /** * @param Plugin $plugin * @param Permissible $permissible + * + * @throws \Exception */ public function __construct(Plugin $plugin, Permissible $permissible){ if(!$plugin->isEnabled()){ - trigger_error("Plugin " . $plugin->getDescription()->getName() . " is disabled", E_USER_WARNING); - - return; + throw new \Exception("Plugin " . $plugin->getDescription()->getName() . " is disabled"); } $this->permissible = $permissible; diff --git a/src/pocketmine/permission/PermissionAttachmentInfo.php b/src/pocketmine/permission/PermissionAttachmentInfo.php index 5bb3aba6b..ecb66c1c2 100644 --- a/src/pocketmine/permission/PermissionAttachmentInfo.php +++ b/src/pocketmine/permission/PermissionAttachmentInfo.php @@ -40,12 +40,12 @@ class PermissionAttachmentInfo{ * @param string $permission * @param PermissionAttachment $attachment * @param bool $value + * + * @throws \Exception */ public function __construct(Permissible $permissible, $permission, $attachment, $value){ if($permission === null){ - trigger_error("Permission may not be null", E_USER_WARNING); - - return; + throw new \Exception("Permission may not be null"); } $this->permissible = $permissible; diff --git a/src/pocketmine/plugin/PharPluginLoader.php b/src/pocketmine/plugin/PharPluginLoader.php index 4a50b913e..07558620c 100644 --- a/src/pocketmine/plugin/PharPluginLoader.php +++ b/src/pocketmine/plugin/PharPluginLoader.php @@ -46,15 +46,15 @@ class PharPluginLoader implements PluginLoader{ * @param string $file * * @return Plugin + * + * @throws \Exception */ public function loadPlugin($file){ if(\Phar::isValidPharFilename($file) and ($description = $this->getPluginDescription($file)) instanceof PluginDescription){ console("[INFO] Loading " . $description->getFullName()); $dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName(); 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); - - return null; + throw new \Exception("Projected dataFolder '" . $dataFolder . "' for " . $description->getName() . " exists and is not a directory"); } $file = "phar://$file"; $className = $description->getMain(); @@ -68,9 +68,7 @@ class PharPluginLoader implements PluginLoader{ return $plugin; }else{ - trigger_error("Couldn't load plugin " . $description->getName() . ": main class not found", E_USER_WARNING); - - return null; + throw new \Exception("Couldn't load plugin " . $description->getName() . ": main class not found"); } } diff --git a/src/pocketmine/plugin/PluginDescription.php b/src/pocketmine/plugin/PluginDescription.php index efdd5ac10..650f2385a 100644 --- a/src/pocketmine/plugin/PluginDescription.php +++ b/src/pocketmine/plugin/PluginDescription.php @@ -50,12 +50,14 @@ class PluginDescription{ $this->loadMap(\yaml_parse($yamlString)); } + /** + * @param array $plugin + * @throws \Exception + */ private function loadMap(array $plugin){ $this->name = preg_replace("[^A-Za-z0-9 _.-]", "", $plugin["name"]); if($this->name === ""){ - trigger_error("Invalid PluginDescription name", E_USER_WARNING); - - return; + throw new \Exception("Invalid PluginDescription name"); } $this->name = str_replace(" ", "_", $this->name); $this->version = $plugin["version"]; diff --git a/src/pocketmine/plugin/PluginManager.php b/src/pocketmine/plugin/PluginManager.php index d562a87f8..482560801 100644 --- a/src/pocketmine/plugin/PluginManager.php +++ b/src/pocketmine/plugin/PluginManager.php @@ -638,12 +638,12 @@ class PluginManager{ * * @param Listener $listener * @param Plugin $plugin + * + * @throws \Exception */ public function registerEvents(Listener $listener, Plugin $plugin){ if(!$plugin->isEnabled()){ - trigger_error("Plugin attempted to register " . get_class($listener) . " while not enabled", E_USER_WARNING); - - return; + throw new \Exception("Plugin attempted to register " . get_class($listener) . " while not enabled"); } $reflection = new \ReflectionClass(get_class($listener)); @@ -680,17 +680,15 @@ class PluginManager{ * @param EventExecutor $executor * @param Plugin $plugin * @param bool $ignoreCancelled + * + * @throws \Exception */ public function registerEvent($event, Listener $listener, $priority, EventExecutor $executor, Plugin $plugin, $ignoreCancelled = false){ if(!is_subclass_of($event, "pocketmine\\event\\Event")){ - trigger_error($event . " is not a valid Event", E_USER_WARNING); - - return; + throw new \Exception($event . " is not a valid Event"); } if(!$plugin->isEnabled()){ - trigger_error("Plugin attempted to register " . $event . " while not enabled"); - - return; + throw new \Exception("Plugin attempted to register " . $event . " while not enabled"); } $this->getEventListeners($event)->register(new RegisteredListener($listener, $executor, $priority, $plugin, $ignoreCancelled)); diff --git a/src/pocketmine/scheduler/ServerScheduler.php b/src/pocketmine/scheduler/ServerScheduler.php index d54e08e5e..7fa22896c 100644 --- a/src/pocketmine/scheduler/ServerScheduler.php +++ b/src/pocketmine/scheduler/ServerScheduler.php @@ -123,7 +123,8 @@ class ServerScheduler{ */ public function cancelTasks(Plugin $plugin){ foreach($this->tasks as $taskId => $task){ - if($task->getTask() instanceof PluginTask){ + $ptask = $task->getTask(); + if($ptask instanceof PluginTask and $ptask->getOwner() === $plugin){ $task->cancel(); unset($this->tasks[$taskId]); } @@ -148,11 +149,18 @@ class ServerScheduler{ return isset($this->tasks[$taskId]); } + /** + * @param Task $task + * @param $delay + * @param $period + * + * @return null|TaskHandler + * + * @throws \Exception + */ private function addTask(Task $task, $delay, $period){ if($task instanceof PluginTask and !$task->getOwner()->isEnabled()){ - trigger_error("Plugin attempted to register a task while disabled", E_USER_WARNING); - - return null; + throw new \Exception("Plugin attempted to register a task while disabled"); } if($delay <= 0){