From 03e2a03a5f8868216dfc89eb78f51523ff228d6b Mon Sep 17 00:00:00 2001 From: Tob14s K <73059862+Tobias-2006@users.noreply.github.com> Date: Sat, 1 Oct 2022 21:15:01 +0200 Subject: [PATCH] Add https://github.com/Tobias-2006/ClearLag/issues/6 --- plugin.yml | 2 +- resources/config.yml | 7 ++++++- src/preferences/Preferences.php | 9 +++++++++ src/preferences/types/RemovalPreferences.php | 17 ++++++++++++++++- src/task/ClearTask.php | 3 ++- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/plugin.yml b/plugin.yml index c76acf8..6d53a10 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ --- name: ClearLag -version: 2.0.0 +version: 2.1.0 main: tobias14\clearlag\ClearLag src-namespace-prefix: tobias14\clearlag api: 4.0.0 diff --git a/resources/config.yml b/resources/config.yml index 7b91624..a425f9e 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -9,7 +9,7 @@ # \_____ |_|\___|\__,_|_| |______\__,_|\__, | # __/ | # |___/ -metadata/version: 2.0.0 +metadata/version: 2.1.0 # metadata/autor: tobias14 # metadata/website: https://github.com/Tobias-2006/ClearLag # @@ -67,6 +67,11 @@ clearlag/preferences: # True by default. logging/console: true + # Set here when ClearLag sends alert messages. + # E.g. ["15m", "10m", "5m", "1m", "3s", "2s", "1s"] + # Empty array, for the default times. + logging/alert-times: [] + # Specify here which entities are to be removed and which are not. # All entities that inherit from Human are automatically excluded. entities: diff --git a/src/preferences/Preferences.php b/src/preferences/Preferences.php index 8198b2c..4bfe812 100644 --- a/src/preferences/Preferences.php +++ b/src/preferences/Preferences.php @@ -90,9 +90,18 @@ public function __construct(Config $config) $clearDelay = 0x384; // 15 minutes } + try { + $alertTimes = array_map(function(string $timeString): int { + return TimeUtils::parseTimeString($timeString); + }, $config->getNested('clearlag/preferences.logging/alert-times', [])); + } catch(Exception) { + $alertTimes = []; + } + $this->removalPreferences = new RemovalPreferences( $clearDelay, $config->getNested('clearlag/preferences.logging/console', true), + $alertTimes, $entityPreferences, $config->getNested('clearlag/preferences.entities.entity/blacklist', []) ); diff --git a/src/preferences/types/RemovalPreferences.php b/src/preferences/types/RemovalPreferences.php index b8d1e57..ea95d01 100644 --- a/src/preferences/types/RemovalPreferences.php +++ b/src/preferences/types/RemovalPreferences.php @@ -9,10 +9,17 @@ final class RemovalPreferences /** * @param int $delay Time span between each entity removal. * @param bool $consoleLogging + * @param int[] $alertTimes * @param EntityPreferences $entityPreferences * @param string[] $blacklist List of entity-/item-names that should not be removed. */ - public function __construct(private int $delay, private bool $consoleLogging, private EntityPreferences $entityPreferences, private array $blacklist) {} + public function __construct( + private int $delay, + private bool $consoleLogging, + private array $alertTimes, + private EntityPreferences $entityPreferences, + private array $blacklist + ) {} /** * @return int @@ -30,6 +37,14 @@ public function getConsoleLogging(): bool return $this->consoleLogging; } + /** + * @return int[] + */ + public function getAlertTimes(): array + { + return $this->alertTimes; + } + /** * @return EntityPreferences */ diff --git a/src/task/ClearTask.php b/src/task/ClearTask.php index 9864399..979e9d8 100644 --- a/src/task/ClearTask.php +++ b/src/task/ClearTask.php @@ -35,7 +35,8 @@ public function __construct(private Preferences $preferences) public function onRun(): void { - if(in_array($this->timer, self::HARDCODED_ALERT_TIMES)) { + $alertTimes = $this->preferences->getRemovalPreferences()->getAlertTimes(); + if(in_array($this->timer, $alertTimes !== [] ? $alertTimes : self::HARDCODED_ALERT_TIMES)) { $message = Messages::ALERT_MESSAGE(); $unit = $this->preferences->getTranslations()->get(TimeUtils::getHighestUnit($this->timer))?->getText(); $message->replace(['{TIME}' => (string) TimeUtils::toHighestUnit($this->timer), '{UNIT}' => $unit]);