Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Added worldguard regions support #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 15 additions & 2 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
74 changes: 68 additions & 6 deletions src/Thunder33345/CustomFallDamagePE/CustomFallDamage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}