Skip to content

Commit

Permalink
Release 0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
xqwtxon committed Feb 24, 2023
1 parent a2ca57a commit 595a10b
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 106 deletions.
Binary file removed ProfanityFilter.png
Binary file not shown.
8 changes: 8 additions & 0 deletions github.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugin_name": "ProfanityFilter",
"version": "0.0.7",
"channel": "stable"
"api": "4.0.0",
"author": "ReinfyTeam",
"artifactUrl": "https://github.com/ReinfyTeam/ProfanityFilter/releases/download/v0.0.7/ProfanityFilter.phar"
}
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: ProfanityFilter
version: 0.0.7-BETA
version: 0.0.7
main: ReinfyTeam\ProfanityFilter\Loader
api: 4.0.0
src-namespace-prefix: ReinfyTeam\ProfanityFilter
Expand Down
10 changes: 10 additions & 0 deletions src/Command/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use pocketmine\plugin\PluginOwned;
use pocketmine\utils\TextFormat as T;
use ReinfyTeam\ProfanityFilter\Loader;
use ReinfyTeam\ProfanityFilter\PluginAPI;
use ReinfyTeam\ProfanityFilter\Utils\Forms\SimpleForm;
use ReinfyTeam\ProfanityFilter\Utils\Language;
use ReinfyTeam\ProfanityFilter\Utils\PluginUtils;
Expand Down Expand Up @@ -93,6 +94,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
foreach ($this->plugin->getProfanity()->get("banned-words") as $word) {
$sender->sendMessage("- " . $word);
}
$sender->sendMessage($this->language->translateMessage("banned-words-description-1"));
$sender->sendMessage($this->language->translateMessage("banned-words-description-2"));
break;
default:
Expand Down Expand Up @@ -134,6 +136,14 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
foreach ($this->plugin->getProfanity()->get("banned-words") as $word) {
$sender->sendMessage("- " . $word);
}
foreach (PluginAPI::defaultProfanity() as $word) {
$sender->sendMessage("- " . $word);
}
if (Loader::getInstance()->getProvidedProfanities() !== null) {
foreach (Loader::getInstance()->getProvidedProfanities() as $word) {
$sender->sendMessage("- " . $word);
}
}
$sender->sendMessage($this->language->translateMessage("banned-words-description-2"));
break;
default:
Expand Down
6 changes: 3 additions & 3 deletions src/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EventListener implements Listener {
public function __construct(string $type, string $provider) {
$this->plugin = Loader::getInstance();
$this->type = $type;
$this->duration = Loader::getInstance()->getDuration();
$this->duration = PluginUtils::getDuration();
$this->provider = $provider;
}

Expand All @@ -54,9 +54,9 @@ public function onChat(PlayerChatEvent $event) : void {
$message = $event->getMessage();
$player = $event->getPlayer();
if (strtolower($this->provider) === "custom") {
$words = $this->plugin->getProfanity()->get("banned-words");
$words = Loader::getInstance()->getProfanity()->get("banned-words");
} else {
$words = (array) $this->plugin->getProvidedProfanities();
$words = (array) ($this->plugin->getProvidedProfanities() ?? PluginAPI::defaultProfanity());
}
if ($player->hasPermission(($this->plugin->getConfig()->get("bypass-permission") ?? "profanityfilter.bypass"))) {
return;
Expand Down
93 changes: 4 additions & 89 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,21 @@

namespace ReinfyTeam\ProfanityFilter;

use DateInterval;
use DateTime;
use pocketmine\permission\DefaultPermissions;
use pocketmine\permission\Permission;
use pocketmine\permission\PermissionManager;
use pocketmine\player\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\Config;
use pocketmine\utils\SingletonTrait;
use ReinfyTeam\ProfanityFilter\Command\DefaultCommand;
use ReinfyTeam\ProfanityFilter\Tasks\UpdateTask;
use ReinfyTeam\ProfanityFilter\Tasks\PoggitUpdateTask;
use ReinfyTeam\ProfanityFilter\Utils\Language;
use function count;
use function fclose;
use function file;
use function file_exists;
use function ltrim;
use function mkdir;
use function preg_match_all;
use function preg_replace;
use function rename;
use function str_replace;
use function stream_get_contents;
use function strlen;
use function strtoupper;
use function strval;
use function substr;
use function trim;
use function unlink;
use function yaml_parse;

Expand Down Expand Up @@ -117,24 +104,8 @@ private function registerCommands() : void {
$this->getServer()->getCommandMap()->register($this->getDescription()->getName(), new DefaultCommand());
}

/**
* Format Message. Dont call it directly.
*/
public function formatMessage(string $message, ?Player $player = null) : string { // TODO: Move this in the event class
$message = str_replace("{type}", $this->getConfig()->get("punishment-type") . "ed", $message);

if ($player === null) {
return $message;
}

$message = str_replace("{player_name}", $player->getName(), $message);
$message = str_replace("{player_ping}", strval($player->getNetworkSession()->getPing()), $message);

return $message;
}

private function checkUpdate() : void {
$this->getServer()->getAsyncPool()->submitTask(new UpdateTask($this->getDescription()->getName(), $this->getDescription()->getVersion()));
$this->getServer()->getAsyncPool()->submitTask(new PoggitUpdateTask($this->getDescription()->getName(), $this->getDescription()->getVersion()));
}

/**
Expand All @@ -161,64 +132,6 @@ private function saveResources() : void {
}
}

public function getDuration() {
if ($this->getConfig()->get("ban-duration") === "Forever") {
return null;
} else {
return $this->stringToTimestamp($this->getConfig()->get("ban-duration"));
}
}

/**
* Convert String to Timestamp
*
* @return ?array
*/
private function stringToTimestamp(string $string) : ?array {
/**
* Rules:
* Integers without suffix are considered as seconds
* "s" is for seconds
* "m" is for minutes
* "h" is for hours
* "d" is for days
* "w" is for weeks
* "mo" is for months
* "y" is for years
*/
if (trim($string) === "") {
return null;
}
$t = new DateTime();
preg_match_all("/[0-9]+(y|mo|w|d|h|m|s)|[0-9]+/", $string, $found);
if (count($found[0]) < 1) {
return null;
}
$found[2] = preg_replace("/[^0-9]/", "", $found[0]);
foreach ($found[2] as $k => $i) {
switch ($c = $found[1][$k]) {
case "y":
case "w":
case "d":
$t->add(new DateInterval("P" . $i . strtoupper($c)));
break;
case "mo":
$t->add(new DateInterval("P" . $i . strtoupper(substr($c, 0, strlen($c) - 1))));
break;
case "h":
case "m":
case "s":
$t->add(new DateInterval("PT" . $i . strtoupper($c)));
break;
default:
$t->add(new DateInterval("PT" . $i . "S"));
break;
}
$string = str_replace($found[0][$k], "", $string);
}
return [$t, ltrim(str_replace($found[0], "", $string))];
}

private function loadPermission() : void {
$this->registerPermission(($this->getConfig()->get("command-permission") ?? "profanityfilter.command"));
$this->registerPermission(($this->getConfig()->get("bypass-permission") ?? "profanityfilter.bypass"));
Expand All @@ -227,6 +140,8 @@ private function loadPermission() : void {
/**
* Register Permission on Plugin
* Custom Permission in Config.yml
* ---
* Introduced in v0.0.6-BETA
*/
private function registerPermission(string $perm) : void {
$permission = new Permission($perm);
Expand Down
80 changes: 80 additions & 0 deletions src/Tasks/GithubUpdateTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
*
* ____ _ __ _____
* | _ \ ___ (_) _ __ / _| _ _ |_ _| ___ __ _ _ __ ___
* | |_) | / _ \ | | | '_ \ | |_ | | | | | | / _ \ / _` | | '_ ` _ \
* | _ < | __/ | | | | | | | _| | |_| | | | | __/ | (_| | | | | | | |
* |_| \_\ \___| |_| |_| |_| |_| \__, | |_| \___| \__,_| |_| |_| |_|
* |___/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author ReinfyTeam
* @link https://github.com/ReinfyTeam/
*
*
*/

declare(strict_types=1);

namespace ReinfyTeam\ProfanityFilter\Tasks;

use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\Internet;
use ReinfyTeam\ProfanityFilter\Utils\Language;
use function json_decode;
use function version_compare;
use function vsprintf;

class GithubUpdateTask extends AsyncTask {
private const GIT_URL = "https://raw.githubusercontent.com/ReinfyTeam/ProfanityFilter/main/github.json";

public function __construct(private string $pluginName, private string $pluginVersion) {
//NOOP
}

public function onRun() : void {
$json = Internet::getURL(self::GIT_URL, 10, [], $err);

if($json->getBody() != null){
$releases = json_decode($json->getBody(), true);
if($releases != null){
$highestVersion = $releases["highestVersion"];
$artifactUrl = $releases["artifactUrl"];
$api = $releases["api"];
} else {
$highestVersion = "";
$artifactUrl = "";
$api = "";
$err = "Unable to read json object properties.";
}
}

$this->setResult([$highestVersion, $artifactUrl, $api, $err]);
}

public function onCompletion() : void {
$lang = new Language();
[$highestVersion, $artifactUrl, $api, $err] = $this->getResult();
$plugin = Server::getInstance()->getPluginManager()->getPlugin($this->pluginName);
if ($plugin === null) {
return;
}
if ($err !== null) {
Server::getInstance()->getLogger()->critical($lang->translateMessage("new-update-prefix") . " " . vsprintf($lang->translateMessage("update-error"), [$err . " Giving up..."]));
return;
}

if ($highestVersion !== $this->pluginVersion) {
Server::getInstance()->getLogger()->warning($lang->translateMessage("new-update-prefix") . " " . vsprintf($lang->translateMessage("new-update-found"), [$highestVersion, $api]));
} else {
Server::getInstance()->getLogger()->notice($lang->translateMessage("new-update-prefix") . " " . $lang->translateMessage("no-updates-found"));
}
}
}
9 changes: 7 additions & 2 deletions src/Tasks/UpdateTask.php → src/Tasks/PoggitUpdateTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
use pocketmine\Server;
use pocketmine\utils\Internet;
use ReinfyTeam\ProfanityFilter\Utils\Language;
use ReinfyTeam\ProfanityFilter\Loader;
use ReinfyTeam\ProfanityFilter\Tasks\GithubUpdateTask;
use function json_decode;
use function version_compare;
use function vsprintf;

class UpdateTask extends AsyncTask {
class PoggitUpdateTask extends AsyncTask {
private const POGGIT_RELEASES_URL = "https://poggit.pmmp.io/releases.min.json?name=";

public function __construct(private string $pluginName, private string $pluginVersion) {
Expand Down Expand Up @@ -66,14 +68,17 @@ public function onCompletion() : void {
$lang = new Language();
[$highestVersion, $artifactUrl, $api, $err] = $this->getResult();
if ($highestVersion === null || $artifactUrl === null || $api === null) {
Server::getInstance()->getLogger()->critical($lang->translateMessage("new-update-prefix") . " " . vsprintf($lang->translateMessage("update-error"), ["Trying to update on github..."]));
Loader::getInstance()->getServer()->getAsyncPool()->submitTask(new GithubUpdateTask(Loader::getInstance()->getDescription()->getName(), Loader::getInstance()->getDescription()->getVersion()));
return;
} // Issue: https://github.com/ReinfyTeam/ProfanityFilter/issues/107
$plugin = Server::getInstance()->getPluginManager()->getPlugin($this->pluginName);
if ($plugin === null) {
return;
}
if ($err !== null) {
Server::getInstance()->getLogger()->critical($lang->translateMessage("new-update-prefix") . " " . vsprintf($lang->translateMessage("update-error"), [$err]));
Server::getInstance()->getLogger()->critical($lang->translateMessage("new-update-prefix") . " " . vsprintf($lang->translateMessage("update-error"), ["Trying to update on github..."]));
Loader::getInstance()->getServer()->getAsyncPool()->submitTask(new GithubUpdateTask(Loader::getInstance()->getDescription()->getName(), Loader::getInstance()->getDescription()->getVersion()));
return;
}

Expand Down
Loading

0 comments on commit 595a10b

Please sign in to comment.