1: <?php
2:
3: namespace aliuly\common;
4: use aliuly\common\Session;
5: use aliuly\common\MPMU;
6:
7: use pocketmine\event\entity\EntityDamageEvent;
8: use pocketmine\event\entity\EntityDamageByEntityEvent;
9: use pocketmine\plugin\PluginBase;
10:
11: 12: 13: 14: 15: 16: 17:
18: class ShieldSession extends Session {
19: protected $api;
20: 21: 22: 23:
24: public function __construct(PluginBase $owner, $hard = true) {
25: $bag = $owner->getServer()->getPluginManager()->getPlugin("GrabBag");
26: if ($bag && $bag->isEnabled() && MPMU::apiCheck($bag->getDescription()->getVersion(),"2.3") && $bag->api->getFeature("shield")) {
27: $this->api = $bag->api;
28: return;
29: }
30: parent::__construct($owner);
31: $this->api = null;
32: }
33: 34: 35: 36: 37:
38: public function isShielded(Player $target) {
39: if ($this->api !== null) return $this->api->isShielded($target);
40: return $this->getState("shield",$target,false);
41: }
42: 43: 44: 45: 46:
47: public function setShield(Player $target,$mode) {
48: if ($this->api !== null) {
49: $this->api->setShield($target,$mode);
50: return;
51: }
52: if ($mode) {
53: $this->setState("shield",$target,true);
54: } else {
55: $this->unsetState("shield",$target);
56: }
57: }
58:
59: public function onDamage(EntityDamageEvent $ev) {
60: if ($ev->isCancelled()) return;
61: if(!($ev instanceof EntityDamageByEntityEvent)) return;
62: if (!($ev->getEntity() instanceof Player)) return;
63: if (!$this->getState("shield",$ev->getEntity(),false)) return;
64: $ev->setCancelled();
65: }
66: }
67: