Skip to content

Commit

Permalink
👽 Fix all code to suit API 5.0.0
Browse files Browse the repository at this point in the history
+ ♻️ Remove commands and work with config.yml
  • Loading branch information
PresentKim committed Jan 25, 2024
1 parent 0c35279 commit d348e77
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 403 deletions.
7 changes: 0 additions & 7 deletions .poggit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@


Set item&arrow's lifespan!

## Command
| name | arguments | description | permission |
| :------: | :------------------------: | :---------------------: | :---------------: |
| lifespan | <item / arrow> <lifespan> | Set entity's lifespan | `OP` lifespan.cmd |

<br/><br/>

## Required API
- PocketMine-MP : higher than [Build #501](https://jenkins.pmmp.io/job/PocketMine-MP/501)
15 changes: 1 addition & 14 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
28 changes: 0 additions & 28 deletions resources/command/eng.yml

This file was deleted.

28 changes: 0 additions & 28 deletions resources/command/kor.yml

This file was deleted.

29 changes: 0 additions & 29 deletions resources/command/rus.yml

This file was deleted.

9 changes: 9 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 0 additions & 22 deletions resources/locale/eng.ini

This file was deleted.

22 changes: 0 additions & 22 deletions resources/locale/kor.ini

This file was deleted.

23 changes: 0 additions & 23 deletions resources/locale/rus.ini

This file was deleted.

118 changes: 28 additions & 90 deletions src/kim/present/lifespan/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)();
}
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit d348e77

Please sign in to comment.