From b1e0f82cbf2f585ed729245a6883d713effd1793 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 5 Oct 2018 16:46:10 +0100 Subject: [PATCH 1/4] PluginManager: Stop catching exceptions thrown by event handlers (#2472) The basic principle here is "if you're not expecting it, don't catch it". Event handlers are **never** supposed to throw exceptions. If they do throw exceptions, it's usually going to one of two things; 1. Broken code producing an error 2. Code triggering (and not catching) a runtime error Both 1) and 2) boil down to defective code on the part of the event handler, and thus should not be caught by the caller, but instead allowed to crash the server and produce a crashdump. It's also undesirable to catch unexpected errors here for a few other reasons - It leaves the owner of the event handler in an unknown, potentially unstable state - It allows broken code to cause event handlers to spam the logger in events that happen frequently (for example movement handlers) - It allows the process to continue down a train of further undefined behaviour, which may lead to more errors or ultimately a crash, so it makes no sense to hold off the inevitable. This has a few advantages that are not merely inverted disadvantages: - Crash dumps will now be created and automatically submitted for defective event handlers, allowing quicker issue location, debugging and fixing in plugins without manual user interaction - Event calling now isn't dependent on Server to work. --- src/pocketmine/plugin/PluginManager.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/pocketmine/plugin/PluginManager.php b/src/pocketmine/plugin/PluginManager.php index b0bc7001e2..6ad25fa30a 100644 --- a/src/pocketmine/plugin/PluginManager.php +++ b/src/pocketmine/plugin/PluginManager.php @@ -698,18 +698,7 @@ class PluginManager{ continue; } - try{ - $registration->callEvent($event); - }catch(\Throwable $e){ - $this->server->getLogger()->critical( - $this->server->getLanguage()->translateString("pocketmine.plugin.eventError", [ - $event->getEventName(), - $registration->getPlugin()->getDescription()->getFullName(), - $e->getMessage(), - get_class($registration->getListener()) - ])); - $this->server->getLogger()->logException($e); - } + $registration->callEvent($event); } $currentList = $currentList->getParent(); From 6efef3bbc71b0eb48ff63e2f01d6575552dd2ad6 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 5 Oct 2018 16:55:37 +0100 Subject: [PATCH 2/4] Move event calling functionality to Event->call() method This is dependent on the changes made in b1e0f82cbf2f585ed729245a6883d713effd1793. This now makes it possible to call events without fetching a Server reference, allowing to eliminate a vast array of Server dependencies. --- src/pocketmine/event/Event.php | 37 +++++++++++++++++++++++++ src/pocketmine/plugin/PluginManager.php | 32 +++------------------ 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/pocketmine/event/Event.php b/src/pocketmine/event/Event.php index c89e9522f3..a481fa85f8 100644 --- a/src/pocketmine/event/Event.php +++ b/src/pocketmine/event/Event.php @@ -27,6 +27,9 @@ declare(strict_types=1); namespace pocketmine\event; abstract class Event{ + private const MAX_EVENT_CALL_DEPTH = 50; + /** @var int */ + private static $eventCallDepth = 1; /** @var string|null */ protected $eventName = null; @@ -67,4 +70,38 @@ abstract class Event{ /** @var Event $this */ $this->isCancelled = $value; } + + /** + * Calls event handlers registered for this event. + * + * @throws \RuntimeException if event call recursion reaches the max depth limit + * + * @throws \ReflectionException + */ + public function call() : void{ + if(self::$eventCallDepth >= self::MAX_EVENT_CALL_DEPTH){ + //this exception will be caught by the parent event call if all else fails + throw new \RuntimeException("Recursive event call detected (reached max depth of " . self::MAX_EVENT_CALL_DEPTH . " calls)"); + } + + $handlerList = HandlerList::getHandlerListFor(get_class($this)); + assert($handlerList !== null, "Called event should have a valid HandlerList"); + + ++self::$eventCallDepth; + foreach(EventPriority::ALL as $priority){ + $currentList = $handlerList; + while($currentList !== null){ + foreach($currentList->getListenersByPriority($priority) as $registration){ + if(!$registration->getPlugin()->isEnabled()){ + continue; + } + + $registration->callEvent($this); + } + + $currentList = $currentList->getParent(); + } + } + --self::$eventCallDepth; + } } diff --git a/src/pocketmine/plugin/PluginManager.php b/src/pocketmine/plugin/PluginManager.php index 6ad25fa30a..6fb3805286 100644 --- a/src/pocketmine/plugin/PluginManager.php +++ b/src/pocketmine/plugin/PluginManager.php @@ -43,7 +43,6 @@ use pocketmine\utils\Utils; * Manages all the plugins */ class PluginManager{ - private const MAX_EVENT_CALL_DEPTH = 50; /** @var Server */ private $server; @@ -66,9 +65,6 @@ class PluginManager{ */ protected $fileAssociations = []; - /** @var int */ - private $eventCallDepth = 0; - /** @var string|null */ private $pluginDataDirectory; @@ -678,33 +674,13 @@ class PluginManager{ /** * Calls an event * + * @deprecated + * @see Event::call() + * * @param Event $event */ public function callEvent(Event $event){ - if($this->eventCallDepth >= self::MAX_EVENT_CALL_DEPTH){ - //this exception will be caught by the parent event call if all else fails - throw new \RuntimeException("Recursive event call detected (reached max depth of " . self::MAX_EVENT_CALL_DEPTH . " calls)"); - } - - $handlerList = HandlerList::getHandlerListFor(get_class($event)); - assert($handlerList !== null, "Called event should have a valid HandlerList"); - - ++$this->eventCallDepth; - foreach(EventPriority::ALL as $priority){ - $currentList = $handlerList; - while($currentList !== null){ - foreach($currentList->getListenersByPriority($priority) as $registration){ - if(!$registration->getPlugin()->isEnabled()){ - continue; - } - - $registration->callEvent($event); - } - - $currentList = $currentList->getParent(); - } - } - --$this->eventCallDepth; + $event->call(); } /** From 1dd6591ac19d8b4daf984f2c4514bae3dd226e24 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 5 Oct 2018 17:27:29 +0100 Subject: [PATCH 3/4] Migrate a bunch of PluginManager->callEvent() usages to Event->call This has the triple bonus effect of a) making a lot of code easier to read, b) reducing Server::getInstance() usages, and c) removing a whole bunch of Server dependencies. The network and block namespaces are untouched by this commit due to potential for merge conflicts. These should be dealt with separately on master. --- src/pocketmine/MemoryManager.php | 2 +- src/pocketmine/Player.php | 84 +++++++++++-------- src/pocketmine/Server.php | 16 ++-- src/pocketmine/entity/Entity.php | 17 ++-- src/pocketmine/entity/Human.php | 5 +- src/pocketmine/entity/Living.php | 8 +- src/pocketmine/entity/object/FallingBlock.php | 3 +- src/pocketmine/entity/object/ItemEntity.php | 8 +- src/pocketmine/entity/object/PrimedTNT.php | 4 +- src/pocketmine/entity/projectile/Arrow.php | 2 +- .../entity/projectile/Projectile.php | 4 +- .../ArmorInventoryEventProcessor.php | 4 +- src/pocketmine/inventory/BaseInventory.php | 3 +- .../EntityInventoryEventProcessor.php | 4 +- src/pocketmine/inventory/PlayerInventory.php | 3 +- .../transaction/CraftingTransaction.php | 3 +- .../transaction/InventoryTransaction.php | 4 +- .../transaction/action/DropItemAction.php | 3 +- src/pocketmine/item/Bow.php | 5 +- src/pocketmine/item/Bucket.php | 6 +- src/pocketmine/item/ProjectileItem.php | 3 +- src/pocketmine/level/Explosion.php | 6 +- src/pocketmine/level/Level.php | 25 +++--- src/pocketmine/plugin/PluginManager.php | 4 +- src/pocketmine/tile/Furnace.php | 7 +- src/pocketmine/tile/Sign.php | 2 +- src/pocketmine/updater/AutoUpdater.php | 2 +- 27 files changed, 138 insertions(+), 99 deletions(-) diff --git a/src/pocketmine/MemoryManager.php b/src/pocketmine/MemoryManager.php index 34cc31f521..93ebf8536b 100644 --- a/src/pocketmine/MemoryManager.php +++ b/src/pocketmine/MemoryManager.php @@ -185,7 +185,7 @@ class MemoryManager{ } $ev = new LowMemoryEvent($memory, $limit, $global, $triggerCount); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); $cycles = 0; if($this->garbageCollectionTrigger){ diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 8117c6c2dc..bbca132996 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -793,7 +793,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } $ev = new PlayerChangeSkinEvent($this, $this->getSkin(), $skin); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ $this->sendSkin([$this]); @@ -1024,11 +1024,12 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ PermissionManager::getInstance()->subscribeToPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this); } - $this->server->getPluginManager()->callEvent($ev = new PlayerJoinEvent($this, + $ev = new PlayerJoinEvent($this, new TranslationContainer(TextFormat::YELLOW . "%multiplayer.player.joined", [ $this->getDisplayName() ]) - )); + ); + $ev->call(); if(strlen(trim((string) $ev->getJoinMessage())) > 0){ $this->server->broadcastMessage($ev->getJoinMessage()); } @@ -1213,7 +1214,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $pos = $pos->floor(); $b = $this->level->getBlock($pos); - $this->server->getPluginManager()->callEvent($ev = new PlayerBedEnterEvent($this, $b)); + $ev = new PlayerBedEnterEvent($this, $b); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -1240,7 +1242,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ if($b instanceof Bed){ $b->setOccupied(false); } - $this->server->getPluginManager()->callEvent($ev = new PlayerBedLeaveEvent($this, $b)); + (new PlayerBedLeaveEvent($this, $b))->call(); $this->sleeping = null; $this->propertyManager->setBlockPos(self::DATA_PLAYER_BED_POSITION, null); @@ -1280,7 +1282,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return false; } } - $this->server->getPluginManager()->callEvent($ev = new PlayerAchievementAwardedEvent($this, $achievementId)); + $ev = new PlayerAchievementAwardedEvent($this, $achievementId); + $ev->call(); if(!$ev->isCancelled()){ $this->achievements[$achievementId] = true; Achievement::broadcast($this, $achievementId); @@ -1343,7 +1346,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return false; } - $this->server->getPluginManager()->callEvent($ev = new PlayerGameModeChangeEvent($this, $gm)); + $ev = new PlayerGameModeChangeEvent($this, $gm); + $ev->call(); if($ev->isCancelled()){ if($client){ //gamemode change by client in the GUI $this->sendGamemode(); @@ -1560,7 +1564,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $ev = new PlayerIllegalMoveEvent($this, $newPos, new Vector3($this->lastX, $this->lastY, $this->lastZ)); $ev->setCancelled($this->allowMovementCheats); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if(!$ev->isCancelled()){ $revert = true; @@ -1590,7 +1594,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $ev = new PlayerMoveEvent($this, $from, $to); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if(!($revert = $ev->isCancelled())){ //Yes, this is intended if($to->distanceSquared($ev->getTo()) > 0.01){ //If plugins modify the destination @@ -1630,7 +1634,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } public function jump() : void{ - $this->server->getPluginManager()->callEvent(new PlayerJumpEvent($this)); + (new PlayerJumpEvent($this))->call(); parent::jump(); } @@ -1862,7 +1866,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->setSkin($skin); - $this->server->getPluginManager()->callEvent($ev = new PlayerPreLoginEvent($this, "Plugin reason")); + $ev = new PlayerPreLoginEvent($this, "Plugin reason"); + $ev->call(); if($ev->isCancelled()){ $this->close("", $ev->getKickMessage()); @@ -2041,7 +2046,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->level->registerChunkLoader($this, ((int) floor($pos[0])) >> 4, ((int) floor($pos[2])) >> 4, true); parent::__construct($this->level, $this->namedtag); - $this->server->getPluginManager()->callEvent($ev = new PlayerLoginEvent($this, "Plugin reason")); + $ev = new PlayerLoginEvent($this, "Plugin reason"); + $ev->call(); if($ev->isCancelled()){ $this->close($this->getLeaveMessage(), $ev->getKickMessage()); @@ -2143,8 +2149,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } $ev = new PlayerCommandPreprocessEvent($this, $messagePart); - - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ break; @@ -2155,7 +2160,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->server->dispatchCommand($ev->getPlayer(), substr($ev->getMessage(), 1)); Timings::$playerCommandTimer->stopTiming(); }else{ - $this->server->getPluginManager()->callEvent($ev = new PlayerChatEvent($this, $ev->getMessage())); + $ev = new PlayerChatEvent($this, $ev->getMessage()); + $ev->call(); if(!$ev->isCancelled()){ $this->server->broadcastMessage($this->getServer()->getLanguage()->translateString($ev->getFormat(), [$ev->getPlayer()->getDisplayName(), $ev->getMessage()]), $ev->getRecipients()); } @@ -2411,8 +2417,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $ev->setCancelled(); } - $this->server->getPluginManager()->callEvent($ev); - + $ev->call(); if($ev->isCancelled()){ $this->inventory->sendHeldItem($this); return true; @@ -2557,7 +2562,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ if($this->hasItemCooldown($slot)){ $ev->setCancelled(); } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled() or !$this->consumeObject($slot)){ $this->inventory->sendContents($this); @@ -2660,7 +2665,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $ev->setCancelled(); } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if(!$ev->isCancelled()){ $this->inventory->setItemInHand($ev->getResultItem()); } @@ -2690,7 +2695,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $ev->setCancelled(); } - $this->getServer()->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ $this->inventory->sendHeldItem($this); break; @@ -2769,7 +2774,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ public function toggleSprint(bool $sprint) : void{ $ev = new PlayerToggleSprintEvent($this, $sprint); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ $this->sendData($this); }else{ @@ -2779,7 +2784,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ public function toggleSneak(bool $sneak) : void{ $ev = new PlayerToggleSneakEvent($this, $sneak); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ $this->sendData($this); }else{ @@ -2792,7 +2797,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return true; } - $this->server->getPluginManager()->callEvent($ev = new PlayerAnimationEvent($this, $packet->action)); + $ev = new PlayerAnimationEvent($this, $packet->action); + $ev->call(); if($ev->isCancelled()){ return true; } @@ -2836,7 +2842,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->doCloseInventory(); if(isset($this->windowIndex[$packet->windowId])){ - $this->server->getPluginManager()->callEvent(new InventoryCloseEvent($this->windowIndex[$packet->windowId], $this)); + (new InventoryCloseEvent($this->windowIndex[$packet->windowId], $this))->call(); $this->removeWindow($this->windowIndex[$packet->windowId]); return true; }elseif($packet->windowId === 255){ @@ -2859,7 +2865,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->kick($this->server->getLanguage()->translateString("kick.reason.cheat", ["%ability.flight"])); return true; }elseif($isFlying !== $this->isFlying()){ - $this->server->getPluginManager()->callEvent($ev = new PlayerToggleFlightEvent($this, $isFlying)); + $ev = new PlayerToggleFlightEvent($this, $isFlying); + $ev->call(); if($ev->isCancelled()){ $this->sendSettings(); }else{ @@ -2927,7 +2934,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $ev->setCancelled(); } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ $tile->spawnTo($this); return true; @@ -3000,7 +3007,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return false; } - $this->getServer()->getPluginManager()->callEvent($event = new PlayerEditBookEvent($this, $oldBook, $newBook, $packet->type, $modifiedPages)); + $event = new PlayerEditBookEvent($this, $oldBook, $newBook, $packet->type, $modifiedPages); + $event->call(); if($event->isCancelled()){ return true; } @@ -3035,7 +3043,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $timings = Timings::getSendDataPacketTimings($packet); $timings->startTiming(); - $this->server->getPluginManager()->callEvent($ev = new DataPacketSendEvent($this, $packet)); + $ev = new DataPacketSendEvent($this, $packet); + $ev->call(); if($ev->isCancelled()){ $timings->stopTiming(); return false; @@ -3066,7 +3075,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $timings = Timings::getSendDataPacketTimings($packet); $timings->startTiming(); try{ - $this->server->getPluginManager()->callEvent($ev = new DataPacketSendEvent($this, $packet)); + $ev = new DataPacketSendEvent($this, $packet); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -3114,8 +3124,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * @return bool if transfer was successful. */ public function transfer(string $address, int $port = 19132, string $message = "transfer") : bool{ - $this->server->getPluginManager()->callEvent($ev = new PlayerTransferEvent($this, $address, $port, $message)); - + $ev = new PlayerTransferEvent($this, $address, $port, $message); + $ev->call(); if(!$ev->isCancelled()){ $pk = new TransferPacket(); $pk->address = $ev->getAddress(); @@ -3139,7 +3149,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * @return bool */ public function kick(string $reason = "", bool $isAdmin = true, $quitMessage = null) : bool{ - $this->server->getPluginManager()->callEvent($ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage())); + $ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage()); + $ev->call(); if(!$ev->isCancelled()){ $reason = $ev->getReason(); $message = $reason; @@ -3384,7 +3395,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->stopSleep(); if($this->spawned){ - $this->server->getPluginManager()->callEvent($ev = new PlayerQuitEvent($this, $message, $reason)); + $ev = new PlayerQuitEvent($this, $message, $reason); + $ev->call(); if($ev->getQuitMessage() != ""){ $this->server->broadcastMessage($ev->getQuitMessage()); } @@ -3532,7 +3544,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ //main inventory and drops the rest on the ground. $this->doCloseInventory(); - $this->server->getPluginManager()->callEvent($ev = new PlayerDeathEvent($this, $this->getDrops())); + $ev = new PlayerDeathEvent($this, $this->getDrops()); + $ev->call(); if(!$ev->getKeepInventory()){ foreach($ev->getDrops() as $item){ @@ -3567,7 +3580,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return; } - $this->server->getPluginManager()->callEvent($ev = new PlayerRespawnEvent($this, $this->getSpawn())); + $ev = new PlayerRespawnEvent($this, $this->getSpawn()); + $ev->call(); $realSpawn = Position::fromObject($ev->getRespawnPosition()->add(0.5, 0, 0.5), $ev->getRespawnPosition()->getLevel()); $this->teleport($realSpawn); diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 08d64b04ee..7e8e34e963 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -823,7 +823,7 @@ class Server{ $ev = new PlayerDataSaveEvent($nbtTag, $name); $ev->setCancelled(!$this->shouldSavePlayerData()); - $this->pluginManager->callEvent($ev); + $ev->call(); if(!$ev->isCancelled()){ $nbt = new BigEndianNBTStream(); @@ -1045,7 +1045,7 @@ class Server{ $this->levels[$level->getId()] = $level; - $this->getPluginManager()->callEvent(new LevelLoadEvent($level)); + (new LevelLoadEvent($level))->call(); $level->setTickRate($this->baseTickRate); @@ -1094,9 +1094,9 @@ class Server{ $level->setTickRate($this->baseTickRate); - $this->getPluginManager()->callEvent(new LevelInitEvent($level)); + (new LevelInitEvent($level))->call(); - $this->getPluginManager()->callEvent(new LevelLoadEvent($level)); + (new LevelLoadEvent($level))->call(); $this->getLogger()->notice($this->getLanguage()->translateString("pocketmine.level.backgroundGeneration", [$name])); @@ -1936,7 +1936,8 @@ class Server{ public function checkConsole(){ Timings::$serverCommandTimer->startTiming(); while(($line = $this->console->getLine()) !== null){ - $this->pluginManager->callEvent($ev = new ServerCommandEvent($this->consoleSender, $line)); + $ev = new ServerCommandEvent($this->consoleSender, $line); + $ev->call(); if(!$ev->isCancelled()){ $this->dispatchCommand($ev->getSender(), $ev->getCommand()); } @@ -1955,7 +1956,8 @@ class Server{ */ public function dispatchCommand(CommandSender $sender, string $commandLine, bool $internal = false) : bool{ if(!$internal){ - $this->pluginManager->callEvent($ev = new CommandEvent($sender, $commandLine)); + $ev = new CommandEvent($sender, $commandLine); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -2536,7 +2538,7 @@ class Server{ } if(($this->tickCounter & 0b111111111) === 0){ - $this->getPluginManager()->callEvent($this->queryRegenerateTask = new QueryRegenerateEvent($this, 5)); + ($this->queryRegenerateTask = new QueryRegenerateEvent($this, 5))->call(); if($this->queryHandler !== null){ $this->queryHandler->regenerateInfo(); } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index d363d43ac0..5b88b895f0 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -559,7 +559,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ $this->level->addEntity($this); $this->lastUpdate = $this->server->getTick(); - $this->server->getPluginManager()->callEvent(new EntitySpawnEvent($this)); + (new EntitySpawnEvent($this))->call(); $this->scheduleUpdate(); @@ -901,7 +901,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ * @param EntityDamageEvent $source */ public function attack(EntityDamageEvent $source) : void{ - $this->server->getPluginManager()->callEvent($source); + $source->call(); if($source->isCancelled()){ return; } @@ -915,7 +915,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ * @param EntityRegainHealthEvent $source */ public function heal(EntityRegainHealthEvent $source) : void{ - $this->server->getPluginManager()->callEvent($source); + $source->call(); if($source->isCancelled()){ return; } @@ -1837,7 +1837,8 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ public function setMotion(Vector3 $motion) : bool{ if(!$this->justCreated){ - $this->server->getPluginManager()->callEvent($ev = new EntityMotionEvent($this, $motion)); + $ev = new EntityMotionEvent($this, $motion); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -1870,7 +1871,8 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ } $from = Position::fromObject($this, $this->level); $to = Position::fromObject($pos, $pos instanceof Position ? $pos->getLevel() : $this->level); - $this->server->getPluginManager()->callEvent($ev = new EntityTeleportEvent($this, $from, $to)); + $ev = new EntityTeleportEvent($this, $from, $to); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -1896,7 +1898,8 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ } if($this->isValid()){ - $this->server->getPluginManager()->callEvent($ev = new EntityLevelChangeEvent($this, $this->level, $targetLevel)); + $ev = new EntityLevelChangeEvent($this, $this->level, $targetLevel); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -2022,7 +2025,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ */ public function close() : void{ if(!$this->closed){ - $this->server->getPluginManager()->callEvent(new EntityDespawnEvent($this)); + (new EntityDespawnEvent($this))->call(); $this->closed = true; $this->despawnFromAll(); diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index da5a7220bb..e349fdb78a 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -269,7 +269,8 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ * @return float the amount of exhaustion level increased */ public function exhaust(float $amount, int $cause = PlayerExhaustEvent::CAUSE_CUSTOM) : float{ - $this->server->getPluginManager()->callEvent($ev = new PlayerExhaustEvent($this, $amount, $cause)); + $ev = new PlayerExhaustEvent($this, $amount, $cause); + $ev->call(); if($ev->isCancelled()){ return 0.0; } @@ -466,7 +467,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ protected function setXpAndProgress(?int $level, ?float $progress) : bool{ if(!$this->justCreated){ $ev = new PlayerExperienceChangeEvent($this, $this->getXpLevel(), $this->getXpProgress(), $level, $progress); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ return false; diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index 0d5561fbe4..cdce53d19f 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -205,7 +205,8 @@ abstract class Living extends Entity implements Damageable{ if(isset($this->effects[$effectId])){ $effect = $this->effects[$effectId]; $hasExpired = $effect->hasExpired(); - $this->server->getPluginManager()->callEvent($ev = new EntityEffectRemoveEvent($this, $effect)); + $ev = new EntityEffectRemoveEvent($this, $effect); + $ev->call(); if($ev->isCancelled()){ if($hasExpired and !$ev->getEffect()->hasExpired()){ //altered duration of an expired effect to make it not get removed $this->sendEffectAdd($ev->getEffect(), true); @@ -278,7 +279,7 @@ abstract class Living extends Entity implements Damageable{ $ev = new EntityEffectAddEvent($this, $effect, $oldEffect); $ev->setCancelled($cancelled); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -621,7 +622,8 @@ abstract class Living extends Entity implements Damageable{ } protected function onDeath() : void{ - $this->server->getPluginManager()->callEvent($ev = new EntityDeathEvent($this, $this->getDrops())); + $ev = new EntityDeathEvent($this, $this->getDrops()); + $ev->call(); foreach($ev->getDrops() as $item){ $this->getLevel()->dropItem($this, $item); } diff --git a/src/pocketmine/entity/object/FallingBlock.php b/src/pocketmine/entity/object/FallingBlock.php index 73ab3824ed..5cd09718d7 100644 --- a/src/pocketmine/entity/object/FallingBlock.php +++ b/src/pocketmine/entity/object/FallingBlock.php @@ -113,7 +113,8 @@ class FallingBlock extends Entity{ //FIXME: anvils are supposed to destroy torches $this->getLevel()->dropItem($this, ItemFactory::get($this->getBlock(), $this->getDamage())); }else{ - $this->server->getPluginManager()->callEvent($ev = new EntityBlockChangeEvent($this, $block, $blockTarget ?? $this->block)); + $ev = new EntityBlockChangeEvent($this, $block, $blockTarget ?? $this->block); + $ev->call(); if(!$ev->isCancelled()){ $this->getLevel()->setBlock($pos, $ev->getTo(), true); } diff --git a/src/pocketmine/entity/object/ItemEntity.php b/src/pocketmine/entity/object/ItemEntity.php index b50e2b3620..55db0e4d26 100644 --- a/src/pocketmine/entity/object/ItemEntity.php +++ b/src/pocketmine/entity/object/ItemEntity.php @@ -78,7 +78,7 @@ class ItemEntity extends Entity{ } - $this->server->getPluginManager()->callEvent(new ItemSpawnEvent($this)); + (new ItemSpawnEvent($this))->call(); } public function entityBaseTick(int $tickDiff = 1) : bool{ @@ -96,7 +96,8 @@ class ItemEntity extends Entity{ $this->age += $tickDiff; if($this->age > 6000){ - $this->server->getPluginManager()->callEvent($ev = new ItemDespawnEvent($this)); + $ev = new ItemDespawnEvent($this); + $ev->call(); if($ev->isCancelled()){ $this->age = 0; }else{ @@ -212,7 +213,8 @@ class ItemEntity extends Entity{ return; } - $this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($playerInventory, $this)); + $ev = new InventoryPickupItemEvent($playerInventory, $this); + $ev->call(); if($ev->isCancelled()){ return; } diff --git a/src/pocketmine/entity/object/PrimedTNT.php b/src/pocketmine/entity/object/PrimedTNT.php index b7839c2b3e..5afc76cc82 100644 --- a/src/pocketmine/entity/object/PrimedTNT.php +++ b/src/pocketmine/entity/object/PrimedTNT.php @@ -102,8 +102,8 @@ class PrimedTNT extends Entity implements Explosive{ } public function explode() : void{ - $this->server->getPluginManager()->callEvent($ev = new ExplosionPrimeEvent($this, 4)); - + $ev = new ExplosionPrimeEvent($this, 4); + $ev->call(); if(!$ev->isCancelled()){ $explosion = new Explosion($this, $ev->getForce(), $this); if($ev->isBlockBreaking()){ diff --git a/src/pocketmine/entity/projectile/Arrow.php b/src/pocketmine/entity/projectile/Arrow.php index 3c6f4a3464..8fd237baf5 100644 --- a/src/pocketmine/entity/projectile/Arrow.php +++ b/src/pocketmine/entity/projectile/Arrow.php @@ -186,7 +186,7 @@ class Arrow extends Projectile{ $ev->setCancelled(); } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ return; } diff --git a/src/pocketmine/entity/projectile/Projectile.php b/src/pocketmine/entity/projectile/Projectile.php index e7fb2c3f14..e78715a5bf 100644 --- a/src/pocketmine/entity/projectile/Projectile.php +++ b/src/pocketmine/entity/projectile/Projectile.php @@ -241,7 +241,7 @@ abstract class Projectile extends Entity{ } if($ev !== null){ - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); $this->onHit($ev); if($ev instanceof ProjectileHitEntityEvent){ @@ -315,7 +315,7 @@ abstract class Projectile extends Entity{ if($this->fireTicks > 0){ $ev = new EntityCombustByEntityEvent($this, $entityHit, 5); - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if(!$ev->isCancelled()){ $entityHit->setOnFire($ev->getDuration()); } diff --git a/src/pocketmine/inventory/ArmorInventoryEventProcessor.php b/src/pocketmine/inventory/ArmorInventoryEventProcessor.php index e7e5ea1199..f0866571e7 100644 --- a/src/pocketmine/inventory/ArmorInventoryEventProcessor.php +++ b/src/pocketmine/inventory/ArmorInventoryEventProcessor.php @@ -26,7 +26,6 @@ namespace pocketmine\inventory; use pocketmine\entity\Entity; use pocketmine\event\entity\EntityArmorChangeEvent; use pocketmine\item\Item; -use pocketmine\Server; class ArmorInventoryEventProcessor implements InventoryEventProcessor{ /** @var Entity */ @@ -37,7 +36,8 @@ class ArmorInventoryEventProcessor implements InventoryEventProcessor{ } public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{ - Server::getInstance()->getPluginManager()->callEvent($ev = new EntityArmorChangeEvent($this->entity, $oldItem, $newItem, $slot)); + $ev = new EntityArmorChangeEvent($this->entity, $oldItem, $newItem, $slot); + $ev->call(); if($ev->isCancelled()){ return null; } diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 337b3b80a3..27aa63e162 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -398,7 +398,8 @@ abstract class BaseInventory implements Inventory{ } public function open(Player $who) : bool{ - $who->getServer()->getPluginManager()->callEvent($ev = new InventoryOpenEvent($this, $who)); + $ev = new InventoryOpenEvent($this, $who); + $ev->call(); if($ev->isCancelled()){ return false; } diff --git a/src/pocketmine/inventory/EntityInventoryEventProcessor.php b/src/pocketmine/inventory/EntityInventoryEventProcessor.php index 7e71afed94..1b4fbf0eb0 100644 --- a/src/pocketmine/inventory/EntityInventoryEventProcessor.php +++ b/src/pocketmine/inventory/EntityInventoryEventProcessor.php @@ -26,7 +26,6 @@ namespace pocketmine\inventory; use pocketmine\entity\Entity; use pocketmine\event\entity\EntityInventoryChangeEvent; use pocketmine\item\Item; -use pocketmine\Server; class EntityInventoryEventProcessor implements InventoryEventProcessor{ /** @var Entity */ @@ -37,7 +36,8 @@ class EntityInventoryEventProcessor implements InventoryEventProcessor{ } public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{ - Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->entity, $oldItem, $newItem, $slot)); + $ev = new EntityInventoryChangeEvent($this->entity, $oldItem, $newItem, $slot); + $ev->call(); if($ev->isCancelled()){ return null; } diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php index 97ed33c424..b5317d0e85 100644 --- a/src/pocketmine/inventory/PlayerInventory.php +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -69,7 +69,8 @@ class PlayerInventory extends BaseInventory{ return false; } - $this->getHolder()->getLevel()->getServer()->getPluginManager()->callEvent($ev = new PlayerItemHeldEvent($this->getHolder(), $this->getItem($hotbarSlot), $hotbarSlot)); + $ev = new PlayerItemHeldEvent($this->getHolder(), $this->getItem($hotbarSlot), $hotbarSlot); + $ev->call(); if($ev->isCancelled()){ $this->sendHeldItem($this->getHolder()); diff --git a/src/pocketmine/inventory/transaction/CraftingTransaction.php b/src/pocketmine/inventory/transaction/CraftingTransaction.php index 6de3a4b522..fe383f64cf 100644 --- a/src/pocketmine/inventory/transaction/CraftingTransaction.php +++ b/src/pocketmine/inventory/transaction/CraftingTransaction.php @@ -134,7 +134,8 @@ class CraftingTransaction extends InventoryTransaction{ } protected function callExecuteEvent() : bool{ - $this->source->getServer()->getPluginManager()->callEvent($ev = new CraftItemEvent($this, $this->recipe, $this->repetitions, $this->inputs, $this->outputs)); + $ev = new CraftItemEvent($this, $this->recipe, $this->repetitions, $this->inputs, $this->outputs); + $ev->call(); return !$ev->isCancelled(); } diff --git a/src/pocketmine/inventory/transaction/InventoryTransaction.php b/src/pocketmine/inventory/transaction/InventoryTransaction.php index 21885bdcbc..f9b956543e 100644 --- a/src/pocketmine/inventory/transaction/InventoryTransaction.php +++ b/src/pocketmine/inventory/transaction/InventoryTransaction.php @@ -29,7 +29,6 @@ use pocketmine\inventory\transaction\action\InventoryAction; use pocketmine\inventory\transaction\action\SlotChangeAction; use pocketmine\item\Item; use pocketmine\Player; -use pocketmine\Server; /** * This InventoryTransaction only allows doing Transaction between one / two inventories @@ -250,7 +249,8 @@ class InventoryTransaction{ } protected function callExecuteEvent() : bool{ - Server::getInstance()->getPluginManager()->callEvent($ev = new InventoryTransactionEvent($this)); + $ev = new InventoryTransactionEvent($this); + $ev->call(); return !$ev->isCancelled(); } diff --git a/src/pocketmine/inventory/transaction/action/DropItemAction.php b/src/pocketmine/inventory/transaction/action/DropItemAction.php index 6605b5a39a..5cb486e064 100644 --- a/src/pocketmine/inventory/transaction/action/DropItemAction.php +++ b/src/pocketmine/inventory/transaction/action/DropItemAction.php @@ -42,7 +42,8 @@ class DropItemAction extends InventoryAction{ } public function onPreExecute(Player $source) : bool{ - $source->getServer()->getPluginManager()->callEvent($ev = new PlayerDropItemEvent($source, $this->targetItem)); + $ev = new PlayerDropItemEvent($source, $this->targetItem); + $ev->call(); if($ev->isCancelled()){ return false; } diff --git a/src/pocketmine/item/Bow.php b/src/pocketmine/item/Bow.php index fa999ff725..60aed426d8 100644 --- a/src/pocketmine/item/Bow.php +++ b/src/pocketmine/item/Bow.php @@ -87,7 +87,7 @@ class Bow extends Tool{ $ev->setCancelled(); } - $player->getServer()->getPluginManager()->callEvent($ev); + $ev->call(); $entity = $ev->getProjectile(); //This might have been changed by plugins @@ -104,7 +104,8 @@ class Bow extends Tool{ } if($entity instanceof Projectile){ - $player->getServer()->getPluginManager()->callEvent($projectileEv = new ProjectileLaunchEvent($entity)); + $projectileEv = new ProjectileLaunchEvent($entity); + $projectileEv->call(); if($projectileEv->isCancelled()){ $ev->getProjectile()->flagForDespawn(); }else{ diff --git a/src/pocketmine/item/Bucket.php b/src/pocketmine/item/Bucket.php index ce9365d82b..6660b79502 100644 --- a/src/pocketmine/item/Bucket.php +++ b/src/pocketmine/item/Bucket.php @@ -59,7 +59,8 @@ class Bucket extends Item implements Consumable{ $stack->pop(); $resultItem = ItemFactory::get(Item::BUCKET, $blockClicked->getFlowingForm()->getId()); - $player->getServer()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $blockReplace, $face, $this, $resultItem)); + $ev = new PlayerBucketFillEvent($player, $blockReplace, $face, $this, $resultItem); + $ev->call(); if(!$ev->isCancelled()){ $player->getLevel()->setBlock($blockClicked, BlockFactory::get(Block::AIR), true, true); $player->getLevel()->broadcastLevelSoundEvent($blockClicked->add(0.5, 0.5, 0.5), $blockClicked->getBucketFillSound()); @@ -80,7 +81,8 @@ class Bucket extends Item implements Consumable{ } } }elseif($resultBlock instanceof Liquid and $blockReplace->canBeReplaced()){ - $player->getServer()->getPluginManager()->callEvent($ev = new PlayerBucketEmptyEvent($player, $blockReplace, $face, $this, ItemFactory::get(Item::BUCKET))); + $ev = new PlayerBucketEmptyEvent($player, $blockReplace, $face, $this, ItemFactory::get(Item::BUCKET)); + $ev->call(); if(!$ev->isCancelled()){ $player->getLevel()->setBlock($blockReplace, $resultBlock->getFlowingForm(), true, true); $player->getLevel()->broadcastLevelSoundEvent($blockClicked->add(0.5, 0.5, 0.5), $resultBlock->getBucketEmptySound()); diff --git a/src/pocketmine/item/ProjectileItem.php b/src/pocketmine/item/ProjectileItem.php index a544a06800..e06cd398e1 100644 --- a/src/pocketmine/item/ProjectileItem.php +++ b/src/pocketmine/item/ProjectileItem.php @@ -58,7 +58,8 @@ abstract class ProjectileItem extends Item{ $this->count--; if($projectile instanceof Projectile){ - $player->getServer()->getPluginManager()->callEvent($projectileEv = new ProjectileLaunchEvent($projectile)); + $projectileEv = new ProjectileLaunchEvent($projectile); + $projectileEv->call(); if($projectileEv->isCancelled()){ $projectile->flagForDespawn(); }else{ diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index cc45e20820..ea4fa95bbd 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -153,7 +153,8 @@ class Explosion{ $yield = (1 / $this->size) * 100; if($this->what instanceof Entity){ - $this->level->getServer()->getPluginManager()->callEvent($ev = new EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield)); + $ev = new EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield); + $ev->call(); if($ev->isCancelled()){ return false; }else{ @@ -234,7 +235,8 @@ class Explosion{ continue; } if(!isset($this->affectedBlocks[$index = Level::blockHash($sideBlock->x, $sideBlock->y, $sideBlock->z)]) and !isset($updateBlocks[$index])){ - $this->level->getServer()->getPluginManager()->callEvent($ev = new BlockUpdateEvent($this->level->getBlockAt($sideBlock->x, $sideBlock->y, $sideBlock->z))); + $ev = new BlockUpdateEvent($this->level->getBlockAt($sideBlock->x, $sideBlock->y, $sideBlock->z)); + $ev->call(); if(!$ev->isCancelled()){ foreach($this->level->getNearbyEntities(new AxisAlignedBB($sideBlock->x - 1, $sideBlock->y - 1, $sideBlock->z - 1, $sideBlock->x + 2, $sideBlock->y + 2, $sideBlock->z + 2)) as $entity){ $entity->onNearbyBlockChange(); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index ee7fddb1eb..fb8e078498 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -544,7 +544,7 @@ class Level implements ChunkManager, Metadatable{ $ev->setCancelled(true); } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if(!$force and $ev->isCancelled()){ return false; @@ -748,7 +748,8 @@ class Level implements ChunkManager, Metadatable{ $block = $this->getBlockAt($x, $y, $z); $block->clearCaches(); //for blocks like fences, force recalculation of connected AABBs - $this->server->getPluginManager()->callEvent($ev = new BlockUpdateEvent($block)); + $ev = new BlockUpdateEvent($block); + $ev->call(); if(!$ev->isCancelled()){ $block->onNearbyBlockChange(); } @@ -1049,7 +1050,7 @@ class Level implements ChunkManager, Metadatable{ return false; } - $this->server->getPluginManager()->callEvent(new LevelSaveEvent($this)); + (new LevelSaveEvent($this))->call(); $this->provider->setTime($this->time); $this->saveChunks(); @@ -1557,7 +1558,8 @@ class Level implements ChunkManager, Metadatable{ if($update){ $this->updateAllLight($block); - $this->server->getPluginManager()->callEvent($ev = new BlockUpdateEvent($block)); + $ev = new BlockUpdateEvent($block); + $ev->call(); if(!$ev->isCancelled()){ foreach($this->getNearbyEntities(new AxisAlignedBB($block->x - 1, $block->y - 1, $block->z - 1, $block->x + 2, $block->y + 2, $block->z + 2)) as $entity){ $entity->onNearbyBlockChange(); @@ -1720,7 +1722,7 @@ class Level implements ChunkManager, Metadatable{ $ev->setCancelled(!$canBreak); } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -1810,7 +1812,7 @@ class Level implements ChunkManager, Metadatable{ $ev->setCancelled(); //set it to cancelled so plugins can bypass this } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if(!$ev->isCancelled()){ if(!$player->isSneaking() and $blockClicked->onActivate($item, $player)){ return true; @@ -1882,7 +1884,7 @@ class Level implements ChunkManager, Metadatable{ $ev->setCancelled(!$canPlace); } - $this->server->getPluginManager()->callEvent($ev); + $ev->call(); if($ev->isCancelled()){ return false; } @@ -2344,7 +2346,7 @@ class Level implements ChunkManager, Metadatable{ $oldChunk = $this->getChunk($x, $z, false); $this->setChunk($x, $z, $chunk, false); if(($oldChunk === null or !$oldChunk->isPopulated()) and $chunk->isPopulated()){ - $this->server->getPluginManager()->callEvent(new ChunkPopulateEvent($this, $chunk)); + (new ChunkPopulateEvent($this, $chunk))->call(); foreach($this->getChunkLoaders($x, $z) as $loader){ $loader->onChunkPopulated($chunk); @@ -2472,7 +2474,7 @@ class Level implements ChunkManager, Metadatable{ public function setSpawnLocation(Vector3 $pos){ $previousSpawn = $this->getSpawnLocation(); $this->provider->setSpawn($pos); - $this->server->getPluginManager()->callEvent(new SpawnChangeEvent($this, $previousSpawn)); + (new SpawnChangeEvent($this, $previousSpawn))->call(); } public function requestChunk(int $x, int $z, Player $player){ @@ -2694,7 +2696,7 @@ class Level implements ChunkManager, Metadatable{ $chunk->initChunk($this); - $this->server->getPluginManager()->callEvent(new ChunkLoadEvent($this, $chunk, !$chunk->isGenerated())); + (new ChunkLoadEvent($this, $chunk, !$chunk->isGenerated()))->call(); if(!$chunk->isLightPopulated() and $chunk->isPopulated() and $this->getServer()->getProperty("chunk-ticking.light-updates", false)){ $this->getServer()->getAsyncPool()->submitTask(new LightPopulationTask($this, $chunk)); @@ -2748,7 +2750,8 @@ class Level implements ChunkManager, Metadatable{ $chunk = $this->chunks[$chunkHash] ?? null; if($chunk !== null){ - $this->server->getPluginManager()->callEvent($ev = new ChunkUnloadEvent($this, $chunk)); + $ev = new ChunkUnloadEvent($this, $chunk); + $ev->call(); if($ev->isCancelled()){ $this->timings->doChunkUnload->stopTiming(); diff --git a/src/pocketmine/plugin/PluginManager.php b/src/pocketmine/plugin/PluginManager.php index 6fb3805286..122bb7dd5b 100644 --- a/src/pocketmine/plugin/PluginManager.php +++ b/src/pocketmine/plugin/PluginManager.php @@ -563,7 +563,7 @@ class PluginManager{ $this->enabledPlugins[$plugin->getDescription()->getName()] = $plugin; - $this->server->getPluginManager()->callEvent(new PluginEnableEvent($plugin)); + (new PluginEnableEvent($plugin))->call(); }catch(\Throwable $e){ $this->server->getLogger()->logException($e); $this->disablePlugin($plugin); @@ -640,7 +640,7 @@ class PluginManager{ public function disablePlugin(Plugin $plugin){ if($plugin->isEnabled()){ $this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.plugin.disable", [$plugin->getDescription()->getFullName()])); - $this->callEvent(new PluginDisableEvent($plugin)); + (new PluginDisableEvent($plugin))->call(); unset($this->enabledPlugins[$plugin->getDescription()->getName()]); diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 2790f52d2c..f20184fc3e 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -136,8 +136,8 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ } protected function checkFuel(Item $fuel){ - $this->server->getPluginManager()->callEvent($ev = new FurnaceBurnEvent($this, $fuel, $fuel->getFuelTime())); - + $ev = new FurnaceBurnEvent($this, $fuel, $fuel->getFuelTime()); + $ev->call(); if($ev->isCancelled()){ return; } @@ -189,7 +189,8 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ if($this->cookTime >= 200){ //10 seconds $product = ItemFactory::get($smelt->getResult()->getId(), $smelt->getResult()->getDamage(), $product->getCount() + 1); - $this->server->getPluginManager()->callEvent($ev = new FurnaceSmeltEvent($this, $raw, $product)); + $ev = new FurnaceSmeltEvent($this, $raw, $product); + $ev->call(); if(!$ev->isCancelled()){ $this->inventory->setResult($ev->getResult()); diff --git a/src/pocketmine/tile/Sign.php b/src/pocketmine/tile/Sign.php index bf28e8942d..8d63d7a654 100644 --- a/src/pocketmine/tile/Sign.php +++ b/src/pocketmine/tile/Sign.php @@ -143,7 +143,7 @@ class Sign extends Spawnable{ $removeFormat = $player->getRemoveFormat(); $ev = new SignChangeEvent($this->getBlock(), $player, array_map(function(string $line) use ($removeFormat){ return TextFormat::clean($line, $removeFormat); }, $lines)); - $this->level->getServer()->getPluginManager()->callEvent($ev); + $ev->call(); if(!$ev->isCancelled()){ $this->setText(...$ev->getLines()); diff --git a/src/pocketmine/updater/AutoUpdater.php b/src/pocketmine/updater/AutoUpdater.php index 087b7f8a69..4235d5a03f 100644 --- a/src/pocketmine/updater/AutoUpdater.php +++ b/src/pocketmine/updater/AutoUpdater.php @@ -62,7 +62,7 @@ class AutoUpdater{ $this->updateInfo = $updateInfo; $this->checkUpdate(); if($this->hasUpdate()){ - $this->server->getPluginManager()->callEvent(new UpdateNotifyEvent($this)); + (new UpdateNotifyEvent($this))->call(); if($this->server->getProperty("auto-updater.on-update.warn-console", true)){ $this->showConsoleUpdate(); } From d75c830a7e9cdc9b4bad3fa3808fe14219f721ce Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 5 Oct 2018 17:43:45 +0100 Subject: [PATCH 4/4] Add -f parameter to lint.sh to allow it to not be useless in cygwin find can conflict with windows' built in find command, which causes it to bug out when running tests. --- tests/lint.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/lint.sh b/tests/lint.sh index 085c7741bd..6e5547f881 100755 --- a/tests/lint.sh +++ b/tests/lint.sh @@ -2,8 +2,9 @@ PHP_BINARY="php" DIR="" +FIND="find" -while getopts "p:d:" OPTION 2> /dev/null; do +while getopts "p:d:f:" OPTION 2> /dev/null; do case ${OPTION} in p) PHP_BINARY="$OPTARG" @@ -11,6 +12,9 @@ while getopts "p:d:" OPTION 2> /dev/null; do d) DIR="$OPTARG" ;; + f) + FIND="$OPTARG" + ;; esac done @@ -21,7 +25,7 @@ fi echo Running PHP lint scans on \"$DIR\"... -OUTPUT=`find "$DIR" -name "*.php" -print0 | xargs -0 -n1 -P4 "$PHP_BINARY" -l` +OUTPUT=`$FIND "$DIR" -name "*.php" -print0 | xargs -0 -n1 -P4 "$PHP_BINARY" -l` if [ $? -ne 0 ]; then echo $OUTPUT | grep -v "No syntax errors"