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){
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;

View File

@ -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");
}

View File

@ -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;

View File

@ -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)

View File

@ -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());
}
}
}

View File

@ -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);

View File

@ -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(){

View File

@ -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)])){

View File

@ -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");
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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");
}
}

View File

@ -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"];

View File

@ -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));

View File

@ -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){