diff --git a/.poggit.yml b/.poggit.yml index 79c60b2..85f0d34 100644 --- a/.poggit.yml +++ b/.poggit.yml @@ -6,11 +6,4 @@ projects: icon: "icon.png" lint: phpstan: false - libs: - - src: blugin/libcommand/libcommand - version: ^1.3.3 - - src: blugin/libtranslator/libtranslator - version: ^1.0.5 - - src: Blugin/virions/SingletonTrait - version: ^1.0.0 ... diff --git a/README.md b/README.md index 21d4f16..900e252 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,6 @@ Set item&arrow's lifespan! - -## Command -| name | arguments | description | permission | -| :------: | :------------------------: | :---------------------: | :---------------: | -| lifespan | | Set entity's lifespan | `OP` lifespan.cmd | - -

## Required API - PocketMine-MP : higher than [Build #501](https://jenkins.pmmp.io/job/PocketMine-MP/501) diff --git a/plugin.yml b/plugin.yml index 1f7f258..9c4e522 100644 --- a/plugin.yml +++ b/plugin.yml @@ -2,20 +2,7 @@ name: Lifespan main: kim\present\lifespan\Main version: 3.0.0 -api: [3.0.0] +api: [5.0.0] author: PresentKim description: Set item and arrow lifespan! -load: STARTUP - -permissions: - lifespan.cmd: - description: Lifespan command - default: op - children: - lifespan.cmd.item: - description: Lifespan command - item - default: op - lifespan.cmd.arrow: - description: Lifespan command - arrow - default: op ... diff --git a/resources/command/eng.yml b/resources/command/eng.yml deleted file mode 100644 index 93299a5..0000000 --- a/resources/command/eng.yml +++ /dev/null @@ -1,28 +0,0 @@ -# Advanced configuration file for LifeSpan plugin -# Some of these settings are safe, others can break your server if modified incorrectly -# New settings/defaults won't appear automatically on this file when upgrading - -lifespan: - #Name of main command - #You can change this to be what you want - name: "lifespan" - - #Permission of main command - #You can change this to be what you want - permission: "op" - - #Aliases of main command - #You can add, replace, or remove them as you like - aliases: ["lifetime", "ls", "수명"] - - children: - #Setting of subcommands - #Same as main command setting - item: - name: "Item" - aliases: ["i", "itementity", "아이템"] - permission: "op" - arrow: - name: "Arrow" - aliases: ["a", "arrowentity", "화살"] - permission: "op" \ No newline at end of file diff --git a/resources/command/kor.yml b/resources/command/kor.yml deleted file mode 100644 index 0c6a007..0000000 --- a/resources/command/kor.yml +++ /dev/null @@ -1,28 +0,0 @@ -# LifeSpan 플러그인을 위한 고급 설정 파일 -# 이 설정들 중 일부는 안전하지만, 또 다른 것들은 잘못 수정했을 경우 서버에 오류가 발생할 수 있습니다 -# 새로운 설정/기본은 이 파일을 업그레이드할 때 자동으로 나타나지 않습니다 - -lifespan: - #메인 명령어의 이름 - #원하는대로 수정할 수 있습니다 - name: "lifespan" - - #메인 명령어의 권한 - #원하는대로 수정할 수 있습니다 - permission: "op" - - #메인 명령어의 별칭들 - #원하는대로 추가, 수정, 삭제할 수 있습니다 - aliases: ["lifetime", "ls", "수명"] - - children: - #하위 명령어들의 설정 - #메인 명령어 설정과 동일합니다 - item: - name: "Item" - aliases: ["i", "itementity", "아이템"] - permission: "op" - arrow: - name: "Arrow" - aliases: ["a", "arrowentity", "화살"] - permission: "op" \ No newline at end of file diff --git a/resources/command/rus.yml b/resources/command/rus.yml deleted file mode 100644 index ee57224..0000000 --- a/resources/command/rus.yml +++ /dev/null @@ -1,29 +0,0 @@ -# Файл продвинутой конфигурации плагина Lifespan -# Некоторые из этих настроек являются безопасными, другие могут сломать ваш сервер, если они изменены неправильно -# Новые настройки / значения по умолчанию не будут отображаться автоматически в этом файле при обновлении -# Translated by @SteinsSquad - -lifespan: - #Название комманды - #Вы можете изменить это так, как захотите - name: "lifespan" - - #Право основной комманды - #Вы можете изменить это так, как захотите - permission: "op" - - #Псевдоним основной комманды - #Вы можете изменять, удалять, добавлять это так, как захотите - aliases: ["lifetime", "ls", "수명"] - - children: - #Настройка суб-комманд - #Также, как с настройкой комманды - item: - name: "Item" - aliases: ["i", "itementity", "아이템"] - permission: "op" - arrow: - name: "Arrow" - aliases: ["a", "arrowentity", "화살"] - permission: "op" \ No newline at end of file diff --git a/resources/config.yml b/resources/config.yml new file mode 100644 index 0000000..ad6aba6 --- /dev/null +++ b/resources/config.yml @@ -0,0 +1,9 @@ +# Main configuration file for Lifespan +# Some of these settings are safe, others can break your server if modified incorrectly +# New settings/defaults won't appear automatically in this file when upgrading. + +# Item entity lifespan [float: seconds] (default: 300) +item-lifespan: 300 + +# arrow entity lifespan [float: seconds] (default: 60) +arrow-lifespan: 60 \ No newline at end of file diff --git a/resources/locale/eng.ini b/resources/locale/eng.ini deleted file mode 100644 index 2016d6b..0000000 --- a/resources/locale/eng.ini +++ /dev/null @@ -1,22 +0,0 @@ -; Language configuration file for Lifespan plugin -; New settings/defaults won't appear automatically on this file when upgrading - -language.name = English - - -; generic command messages -commands.generic.num.invalid = {%0} is not number -commands.generic.num.tooBig = {%0} is too big, it must be at most {%1} -commands.generic.num.tooSmall = {%0} is too small, it must be at least {%1} - - -; main command messages -commands.lifespan.description = Set entity's lifespan - -commands.lifespan.item.name = Item -commands.lifespan.item.success = Item's lifespan was setted to {%0} seconds -commands.lifespan.item.parameter.seconds = seconds - -commands.lifespan.arrow.name = Arrow -commands.lifespan.arrow.success = Arrow's lifespan was setted to {%0} seconds -commands.lifespan.arrow.parameter.seconds = seconds \ No newline at end of file diff --git a/resources/locale/kor.ini b/resources/locale/kor.ini deleted file mode 100644 index 6dac6fe..0000000 --- a/resources/locale/kor.ini +++ /dev/null @@ -1,22 +0,0 @@ -; Lifespan 플러그인을 위한 언어 설정 파일 -; 새로운 설정/기본은 이 파일을 업그레이드할 때 자동으로 나타나지 않습니다 - -language.name = 한국어 - - -; generic command messages -commands.generic.num.invalid = {%0}는 숫자가 아닙니다 -commands.generic.num.tooBig = 입력한 숫자({%0})가 너무 큽니다, 최대 {%1} 이하여야 합니다 -commands.generic.num.tooSmall = 입력한 숫자({%0})가 너무 작습니다, 최소 {%1} 이상이어야 합니다 - - -; main command messages -commands.lifespan.description = 엔티티의 수명을 설정 - -commands.lifespan.item.name = 아이템 -commands.lifespan.item.success = 아이템의 수명이 {%0}초로 설정되었습니다 -commands.lifespan.item.parameter.seconds = 초 - -commands.lifespan.arrow.name = 화살 -commands.lifespan.arrow.success = 화살의 수명이 {%0}초로 설정되었습니다 -commands.lifespan.arrow.parameter.seconds = 초 \ No newline at end of file diff --git a/resources/locale/rus.ini b/resources/locale/rus.ini deleted file mode 100644 index 6d43327..0000000 --- a/resources/locale/rus.ini +++ /dev/null @@ -1,23 +0,0 @@ -; Языковая кофигурация плагина Lifespan -; Новые настройки/значения по умолчанию не будут отображаться автоматически в этом файле при обновлении -; Translated by @SteinsSquad - -language.name = Русский - - -; Основные сообщения -commands.generic.num.invalid = {%0} не является числом -commands.generic.num.tooBig = {%0} слишком большое число, оно должно быть не больше {%1} -commands.generic.num.tooSmall = {%0} слишком маленькое число, оно должно быть не меньше {%1} - - -; Основые сообщения комманд -commands.lifespan.description = Установить продолжительность жизни сущности - -commands.lifespan.item.name = Item -commands.lifespan.item.success = Продолжительность жизни Item установленна на {%0} секунд. -commands.lifespan.item.parameter.seconds = seconds - -commands.lifespan.arrow.name = Arrow -commands.lifespan.arrow.success = Продолжительность жизни Arrow установленна на {%0} секунд. -commands.lifespan.arrow.parameter.seconds = seconds \ No newline at end of file diff --git a/src/kim/present/lifespan/Main.php b/src/kim/present/lifespan/Main.php index 24b97aa..dbc4c00 100644 --- a/src/kim/present/lifespan/Main.php +++ b/src/kim/present/lifespan/Main.php @@ -27,108 +27,46 @@ namespace kim\present\lifespan; -use blugin\lib\command\BaseCommandTrait; -use blugin\lib\command\listener\AvaliableCommandListener; -use blugin\lib\translator\traits\TranslatorHolderTrait; -use blugin\lib\translator\TranslatorHolder; -use blugin\traits\singleton\SingletonTrait; -use kim\present\lifespan\command\overload\ArrowLifespanOverload; -use kim\present\lifespan\command\overload\ItemLifespanOverload; +use Closure; +use InvalidArgumentException; use pocketmine\entity\object\ItemEntity; use pocketmine\entity\projectile\Arrow; use pocketmine\event\entity\EntitySpawnEvent; +use pocketmine\event\entity\ProjectileHitEvent; use pocketmine\event\Listener; use pocketmine\plugin\PluginBase; -class Main extends PluginBase implements Listener, TranslatorHolder{ - use TranslatorHolderTrait, BaseCommandTrait, SingletonTrait; - - public const TAG_ITEM = "Item"; - public const TAG_ARROW = "Arrow"; - - /** @var int[] */ - private $typeMap; - - /** @var int (short) */ - private $itemLifespan = 6000; - - /** @var int (short) */ - private $arrowLifespan = 1200; - - public function onLoad() : void{ - self::$instance = $this; - - $this->loadLanguage(); - $this->getBaseCommand(); - } +class Main extends PluginBase implements Listener{ + private int $itemLifespan = 6000; + private int $arrowLifespan = 1200; public function onEnable() : void{ - //Register main command with subcommands - $command = $this->getBaseCommand(); - $command->addOverload(new ItemLifespanOverload($command)); - $command->addOverload(new ArrowLifespanOverload($command)); - $this->getServer()->getCommandMap()->register($this->getName(), $command); - - //Load lifespan data - $dataPath = "{$this->getDataFolder()}lifespan.json"; - if(!file_exists($dataPath)){ - $this->itemLifespan = 6000; //default: 5 minutes - $this->arrowLifespan = 1200; //default: 60 seconds - return; - } + $config = $this->getConfig(); + $this->setItemLifespan($config->getNested("item-lifespan", 300) * 20); + $this->setArrowLifespan($config->getNested("arrow-lifespan", 60) * 20); - $content = file_get_contents($dataPath); - if($content === false) - throw new \RuntimeException("Unable to load lifespan.json file"); - - $data = json_decode($content, true); - if(!is_array($data) || count($data) < 2 || !isset($data[self::TAG_ITEM]) || !is_numeric($data[self::TAG_ITEM]) || !isset($data[self::TAG_ARROW]) || !is_numeric($data[self::TAG_ARROW])){ - throw new \RuntimeException("Invalid data in lifespan.json file. Must be int array"); - } - $this->setItemLifespan((int) $data[self::TAG_ITEM]); - $this->setArrowLifespan((int) $data[self::TAG_ARROW]); - - //Register event listeners $this->getServer()->getPluginManager()->registerEvents($this, $this); - AvaliableCommandListener::register($this); } - public function onDisable() : void{ - //Unregister main command with subcommands - $this->getServer()->getCommandMap()->unregister($this->getBaseCommand()); - - //Save lifespan data - $dataPath = "{$this->getDataFolder()}lifespan.json"; - file_put_contents($dataPath, json_encode([ - self::TAG_ITEM => $this->itemLifespan, - self::TAG_ARROW => $this->arrowLifespan - ], JSON_PRETTY_PRINT)); - } - - /** - * @priority MONITOR - */ + /** @priority MONITOR */ public function onEntitySpawnEvent(EntitySpawnEvent $event) : void{ $entity = $event->getEntity(); if($entity instanceof ItemEntity){ - static $itemLifeProperty = null; - if($itemLifeProperty === null){ - $itemReflection = new \ReflectionClass(ItemEntity::class); - $itemLifeProperty = $itemReflection->getProperty("age"); - $itemLifeProperty->setAccessible(true); - } - $before = $itemLifeProperty->getValue($entity); - $itemLifeProperty->setValue($entity, min(0x7fff, max(0, $before + 6000 - $this->getItemLifespan()))); - }elseif($entity instanceof Arrow){ - static $arrowLifeProperty = null; - if($arrowLifeProperty === null){ - $arrowReflection = new \ReflectionClass(Arrow::class); - $arrowLifeProperty = $arrowReflection->getProperty("collideTicks"); - $arrowLifeProperty->setAccessible(true); - } + $entity->setDespawnDelay($this->getItemLifespan()); + } + } - $before = $arrowLifeProperty->getValue($entity); - $arrowLifeProperty->setValue($entity, min(0x7fff, max(0, $before + 1200 - $this->getArrowLifespan()))); + /** @priority MONITOR */ + public function onProjectileHitEvent(ProjectileHitEvent $event) : void{ + $entity = $event->getEntity(); + if($entity instanceof Arrow){ + Closure::bind( //HACK: Closure bind hack to access inaccessible members + closure: function() use ($entity) : void{ + $entity->collideTicks = 1200 - $this->getArrowLifespan(); + }, + newThis: $this, + newScope: Arrow::class + )(); } } @@ -138,9 +76,9 @@ public function getItemLifespan() : int{ public function setItemLifespan(int $value) : void{ if($value < 0){ - throw new \InvalidArgumentException("Value {$value} is too small, it must be at least 0"); + throw new InvalidArgumentException("Value $value is too small, it must be at least 0"); }elseif($value > 0x7fff){ - throw new \InvalidArgumentException("Value {$value} is too big, it must be at most 0x7fff"); + throw new InvalidArgumentException("Value $value is too big, it must be at most 0x7fff"); } $this->itemLifespan = $value; } @@ -151,9 +89,9 @@ public function getArrowLifespan() : int{ public function setArrowLifespan(int $value) : void{ if($value < 0){ - throw new \InvalidArgumentException("Value {$value} is too small, it must be at least 0"); + throw new InvalidArgumentException("Value $value is too small, it must be at least 0"); }elseif($value > 0x7fff){ - throw new \InvalidArgumentException("Value {$value} is too big, it must be at most 0x7fff"); + throw new InvalidArgumentException("Value $value is too big, it must be at most 0x7fff"); } $this->arrowLifespan = $value; } diff --git a/src/kim/present/lifespan/command/overload/ArrowLifespanOverload.php b/src/kim/present/lifespan/command/overload/ArrowLifespanOverload.php deleted file mode 100644 index 10ae77c..0000000 --- a/src/kim/present/lifespan/command/overload/ArrowLifespanOverload.php +++ /dev/null @@ -1,46 +0,0 @@ - value */ - public function handle(CommandSender $sender, array $args, Overload $overload) : bool{ - Lifespan::getInstance()->setArrowLifespan((int) ($args["seconds"] * 20)); - $this->sendMessage($sender, "success", [(string) $args["seconds"]]); - return true; - } -} \ No newline at end of file diff --git a/src/kim/present/lifespan/command/overload/ItemLifespanOverload.php b/src/kim/present/lifespan/command/overload/ItemLifespanOverload.php deleted file mode 100644 index 1fd1e47..0000000 --- a/src/kim/present/lifespan/command/overload/ItemLifespanOverload.php +++ /dev/null @@ -1,46 +0,0 @@ - value */ - public function handle(CommandSender $sender, array $args, Overload $overload) : bool{ - Lifespan::getInstance()->setItemLifespan((int) ($args["seconds"] * 20)); - $this->sendMessage($sender, "success", [(string) $args["seconds"]]); - return true; - } -} \ No newline at end of file diff --git a/src/kim/present/lifespan/command/overload/LifespanOverload.php b/src/kim/present/lifespan/command/overload/LifespanOverload.php deleted file mode 100644 index 8a0c758..0000000 --- a/src/kim/present/lifespan/command/overload/LifespanOverload.php +++ /dev/null @@ -1,41 +0,0 @@ -addParamater((new FloatParameter("seconds"))->setMin(0)->setMax(0x7fff)); - $this->setHandler($this); - } -} \ No newline at end of file