diff --git a/readme.md b/readme.md index 50eefcf..48e069f 100644 --- a/readme.md +++ b/readme.md @@ -4,6 +4,8 @@ Customize your PMMP server's fall damage! Multi World Supported! -You can multiply, add, subtract, cutoff fall damage of any world by any set value +You can multiply, add, subtract, cutoff fall damage of any world by any set value. + +Extra Features: World Guard Regions support Examples included in the config \ No newline at end of file diff --git a/resources/config.yml b/resources/config.yml index 3c7b614..3f66cfb 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -18,6 +18,19 @@ default-cutoff: 0, #0 for default no cutoff cutoff: { - world: 5 #anything below or equal to 5fg will be reduced to 0 - } + world: 5 #anything below or equal to 5 will be reduced to 0 + }, + + worldguard: false, #enable world guard support, must have world guard installed + #this allows you to define custom fall damage modification for each regions + #when a region is not defined, it will try to use the world, then default/global setting + regions: { #world guard region settings + area31: { #the region name(one with highest priority) + multiplier: 0.7, + sum: 5, + cutoff: 8, + }, + }, + + playeronly: true, #ignore other non players entities(this applies for all worlds and regions) } \ No newline at end of file diff --git a/src/Thunder33345/CustomFallDamagePE/CustomFallDamage.php b/src/Thunder33345/CustomFallDamagePE/CustomFallDamage.php index cbb8985..a079b3a 100644 --- a/src/Thunder33345/CustomFallDamagePE/CustomFallDamage.php +++ b/src/Thunder33345/CustomFallDamagePE/CustomFallDamage.php @@ -4,35 +4,97 @@ namespace Thunder33345\CustomFallDamagePE; +use Chalapa13\WorldGuard\WorldGuard; use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\Listener; +use pocketmine\level\Position; +use pocketmine\Player; use pocketmine\plugin\PluginBase; class CustomFallDamage extends PluginBase implements Listener { + /** @var WorldGuard $worldGuard */ + private $worldGuard; + public function onEnable() { $this->saveDefaultConfig(); $this->getServer()->getPluginManager()->registerEvents($this, $this); + if($this->getConfig()->get('worldguard', false)){ + /** @var WorldGuard $wg */ + $wg = $this->getServer()->getPluginManager()->getPlugin("WorldGuard"); + if(!$wg instanceof WorldGuard){ + $this->getLogger()->critical("World Guard not found, Shutting down..."); + $this->getServer()->getPluginManager()->disablePlugin($this); + return; + } + $this->worldGuard = $wg; + + } } + /** + * @param EntityDamageEvent $event + * + * @priority HIGHEST + * @ignoreCancelled TRUE + */ public function onFall(EntityDamageEvent $event) { if($event->getCause() !== EntityDamageEvent::CAUSE_FALL) return; - $worldName = $event->getEntity()->getLevel()->getName(); + if($this->getConfig()->get('playeronly', true)) + if(!$event->getEntity() instanceof Player) return; - $multiplier = $this->getConfig()->getNested('multiplier.' . $worldName, $this->getConfig()->get('default-multiplier')); - $total = $event->getBaseDamage() * $multiplier; + if($this->getWorldGuard()){ + $entity = $event->getEntity(); + $regionName = $this->getRegionName($entity->asPosition()); + if(is_string($regionName)){ + $regionConfig = $this->getConfig()->getNested('region.' . $regionName, false); + if($regionConfig !== false and is_array($regionConfig)){ + $multiplier = $this->getConfig()->getNested('region.' . $regionName . '.multiplier', 1); + $sum = $this->getConfig()->getNested('region.' . $regionName . '.sum', 0); + $cutoff = $this->getConfig()->getNested('region.' . $regionName . '.cutoff', 0); + $this->handelEvent($event, $multiplier, $sum, $cutoff); + return; + } + } + } + $this->handleWorld($event); + } - $sum = $this->getConfig()->getNested('sum.' . $worldName, $this->getConfig()->get('default-sum')); - $total = $total + $sum; + private function handleWorld(EntityDamageEvent $event) + { + $worldName = $event->getEntity()->getLevel()->getName(); - $cutoff = $this->getConfig()->getNested('cutoff.' . $worldName, $this->getConfig()->get('default-cutoff')); + $multiplier = $this->getConfig()->getNested('multiplier.' . $worldName, $this->getConfig()->get('default-multiplier', 1)); + $sum = $this->getConfig()->getNested('sum.' . $worldName, $this->getConfig()->get('default-sum', 0)); + $cutoff = $this->getConfig()->getNested('cutoff.' . $worldName, $this->getConfig()->get('default-cutoff', 0)); + $this->handelEvent($event, $multiplier, $sum, $cutoff); + } + private function handelEvent(EntityDamageEvent $event, float $multiplier = 0, float $sum = 0, float $cutoff = 0) + { + $total = $event->getBaseDamage() * $multiplier; + $total = $total + $sum; if($cutoff >= $total) $event->setBaseDamage(0); else $event->setBaseDamage($total); } + + public function getRegionName(Position $position):?string + { + if(!$this->getWorldGuard()){ + return null; + } + $str = $this->worldGuard->getRegionNameFromPosition($position); + if($str == '') return null; + return $str; + } + + public function getWorldGuard():bool + { + return $this->worldGuard instanceof WorldGuard AND $this->worldGuard->isEnabled(); + } } \ No newline at end of file