From 25aff15d5a0148684ee0508e80bc0b4eae8896c5 Mon Sep 17 00:00:00 2001 From: Aaron Salyer Date: Sat, 10 Feb 2018 10:10:09 -0500 Subject: [PATCH 1/4] Bump API for testing. --- plugin.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.yml b/plugin.yml index 74c19e9..3c83b19 100644 --- a/plugin.yml +++ b/plugin.yml @@ -2,7 +2,7 @@ name: FantasyProtection version: 2.0 author: Enrick3344 main: FantasyProtection\Main -api: [3.0.0-ALPHA7, 3.0.0-ALPHA8, 3.0.0-ALPHA9, 3.0.0-ALPHA10] +api: [3.0.0-ALPHA7, 3.0.0-ALPHA8, 3.0.0-ALPHA9, 3.0.0-ALPHA10, 3.0.0-ALPHA11] permission: fantasyprotection.break.bypass: From ab7c6022cb6b2e3d537d059084bc7f07b6bc82e4 Mon Sep 17 00:00:00 2001 From: Aaron Salyer Date: Sat, 10 Feb 2018 10:12:24 -0500 Subject: [PATCH 2/4] Main: Remove unused imports. When using a class or item that shares a namespace with the current item, it is not necessary to import that class or item. --- src/FantasyProtection/Main.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/FantasyProtection/Main.php b/src/FantasyProtection/Main.php index 01355ff..dbcde04 100644 --- a/src/FantasyProtection/Main.php +++ b/src/FantasyProtection/Main.php @@ -25,12 +25,7 @@ namespace FantasyProtection; use pocketmine\plugin\PluginBase; -use pocketmine\Player; -use pocketmine\Server; -use pocketmine\utils\Config; use pocketmine\utils\TextFormat; -//Plugin File -use FantasyProtection\ProtectionCommand; class Main extends PluginBase{ From 7620b53f9a7e027d82a1311b29a652d93f8aa254 Mon Sep 17 00:00:00 2001 From: Aaron Salyer Date: Sat, 10 Feb 2018 10:14:40 -0500 Subject: [PATCH 3/4] EventListener: Add private class property $prefix. By reducing the number of time that the configuration file is accessed for any variable, performance is increased. Also updated some methods to ensure that Player methods are only called on Players. --- src/FantasyProtection/EventListener.php | 37 +++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/FantasyProtection/EventListener.php b/src/FantasyProtection/EventListener.php index 14ed21e..b0d83c9 100644 --- a/src/FantasyProtection/EventListener.php +++ b/src/FantasyProtection/EventListener.php @@ -40,11 +40,16 @@ class EventListener implements Listener{ /** @var Main */ private $plugin; + + private $prefix; + + /** * @param Main $plugin */ public function __construct(Main $plugin) { $this->plugin = $plugin; + $this->prefix = $this->plugin->getConfig()->get("Prefix"); } public function onExhaust(PlayerExhaustEvent $event){ $player = $event->getPlayer(); @@ -56,7 +61,6 @@ public function onExhaust(PlayerExhaustEvent $event){ } public function onBreak(BlockBreakEvent $event){ - $prefix = $this->plugin->getConfig()->get("Prefix"); $message = $this->plugin->getConfig()->get("Break-Message"); $lockmessage = $this->plugin->getConfig()->get("Lock-Message"); $player = $event->getPlayer(); @@ -65,19 +69,18 @@ public function onBreak(BlockBreakEvent $event){ $lock = $this->plugin->getConfig()->get("Lock"); if(in_array($world, $lock)){ $event->setCancelled(true); - $player->sendMessage($this->plugin->translateColors($prefix . " " . $lockmessage)); + $player->sendMessage($this->plugin->translateColors($this->prefix . " " . $lockmessage)); }elseif(in_array($world, $break)){ if($player->hasPermission("fantasyplus.break.bypass")){ return true; }else{ $event->setCancelled(); - $player->sendMessage($this->plugin->translateColors($prefix . " " . $message)); + $player->sendMessage($this->plugin->translateColors($this->prefix . " " . $message)); } } } public function onPlace(BlockPlaceEvent $event){ - $prefix = $this->plugin->getConfig()->get("Prefix"); $message = $this->plugin->getConfig()->get("Place-Message"); $lockmessage = $this->plugin->getConfig()->get("Lock-Message"); $player = $event->getPlayer(); @@ -86,19 +89,18 @@ public function onPlace(BlockPlaceEvent $event){ $lock = $this->plugin->getConfig()->get("Lock"); if(in_array($world, $lock)){ $event->setCancelled(true); - $player->sendMessage($this->plugin->translateColors($prefix . " " . $lockmessage)); + $player->sendMessage($this->plugin->translateColors($this->prefix . " " . $lockmessage)); }elseif(in_array($world, $place)){ if($player->hasPermission("fantasyprotection.place.bypass")){ return true; }else{ $event->setCancelled(); - $player->sendMessage($this->plugin->translateColors($prefix . " " . $message)); + $player->sendMessage($this->plugin->translateColors($this->prefix . " " . $message)); } } } public function onDrop(PlayerDropItemEvent $event){ - $prefix = $this->plugin->getConfig()->get("Prefix"); $message = $this->plugin->getConfig()->get("Drop-Message"); $player = $event->getPlayer(); $world = $player->getLevel()->getName(); @@ -109,7 +111,7 @@ public function onDrop(PlayerDropItemEvent $event){ return true; }else{ $event->setCancelled(); - $player->sendMessage($this->plugin->translateColors($prefix . " " . $message)); + $player->sendMessage($this->plugin->translateColors($this->prefix . " " . $message)); } } } @@ -125,7 +127,7 @@ public function onLevelChange(EntityLevelChangeEvent $event){ return true; }else{ $event->setCancelled(); - $player->sendMessage($this->plugin->translateColors($prefix . " " . $message)); + $player->sendMessage($this->plugin->translateColors($this->prefix . " " . $message)); } } } @@ -139,16 +141,15 @@ public function onHurt(EntityDamageEvent $event){ if(in_array($world, $damage)){ $event->setCancelled(); } - } - if($event->getEntity() instanceof Player && $event instanceof EntityDamageByEntityEvent) { - if($event->getDamager() instanceof Player){ - $prefix = $this->plugin->getConfig()->get("Prefix"); - $message = $this->plugin->getConfig()->get("PVP-Message"); - $pvp = $this->plugin->getConfig()->get("PVP"); + if($event->getEntity() instanceof Player && $event instanceof EntityDamageByEntityEvent){ $player = $event->getDamager(); - if(in_array($world, $pvp)){ - $event->getDamager()->sendMessage($this->plugin->translateColors($prefix . " " . $message)); - $event->setCancelled(); + if($player instanceof Player){ + $message = $this->plugin->getConfig()->get("PVP-Message"); + $pvp = $this->plugin->getConfig()->get("PVP"); + if(in_array($world, $pvp)){ + $player->sendMessage($this->plugin->translateColors($this->prefix . " " . $message)); + $event->setCancelled(); + } } } } From fd131605f8d6c1b6db5551004025a467b300474b Mon Sep 17 00:00:00 2001 From: Aaron Salyer Date: Sat, 10 Feb 2018 10:17:23 -0500 Subject: [PATCH 4/4] ProtectionCommand: Clean up unnecessary imports. Add check in place to ensure Command Sender is a Player before calling Player methods during commands. Fixed undefined variables $level on 251 and 284. --- src/FantasyProtection/ProtectionCommand.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/FantasyProtection/ProtectionCommand.php b/src/FantasyProtection/ProtectionCommand.php index 0a8492d..b61b3e7 100644 --- a/src/FantasyProtection/ProtectionCommand.php +++ b/src/FantasyProtection/ProtectionCommand.php @@ -26,11 +26,8 @@ use pocketmine\command\Command; use pocketmine\command\CommandSender; -use pocketmine\utils\TextFormat; use pocketmine\Player; -use pocketmine\level\Level; //Plugins Files. -use FantasyProtection\Main; class ProtectionCommand extends Command{ @@ -46,7 +43,7 @@ public function __construct(Main $plugin){ } public function execute(CommandSender $sender, string $label, array $args) : bool{ - if(isset($args[0])){ + if(isset($args[0]) and $sender instanceof Player){ switch($args[0]){ case "placing":{ if(count($args) == 2) { @@ -251,7 +248,7 @@ public function execute(CommandSender $sender, string $label, array $args) : boo $world = $sender->getLevel()->getName(); $lock = $this->plugin->getConfig()->get("Lock"); if(in_array($world, $lock)){ - $sender->sendMessage("§r§f`•§5>§c Level ".$level." Is Already Locked!"); + $sender->sendMessage("§r§f`•§5>§c Level ".$world." Is Already Locked!"); break; } $level = $sender->getLevel()->getName(); @@ -284,7 +281,7 @@ public function execute(CommandSender $sender, string $label, array $args) : boo $this->plugin->getConfig()->save(); $sender->sendMessage("§r§f`•§5>§b You've sucessfully Unlocked Level " . $level); }else{ - $sender->sendMessage("§r§f`•§5>§c Level ".$level." Was not Locked!"); + $sender->sendMessage("§r§f`•§5>§c Level ".$world." Was not Locked!"); } } break;