JoshyPHP It seems using templates could work... well, sort of, for some reason the text isn't changing.
<?php
namespace MrPowerGamerBR\HelloWorld\Listener;
use Flarum\Event\ConfigureFormatter;
use Illuminate\Contracts\Events\Dispatcher;
class FormatPosts
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureFormatter::class, [$this, 'addServerIP']);
$events->listen(ConfigureFormatterRenderer::class, [$this, 'renderMe']);
}
// $parameters = array();
/**
* @param ConfigureFormatter $event
*/
public function addServerIP(ConfigureFormatter $event)
{
$configurator = $event->configurator;
/* $tagName = 'USERNEWMENTION';
$tag = $configurator->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->attributes['id']->required = false;
$tag->template = '<a href="{$PROFILE_URL}{@username}" class="UserMention">@<xsl:value-of select="@username"/></a>';
$tag->filterChain->prepend([static::class, 'addId'])
->addParameterByName('userRepository')
->setJS('function() { return true; }');
$configurator->Preg->match('/\Bo(?<username>[a-z0-9_-]+)(?!#)/i', $tagName);
// Create the template parameter "username" with default value "you"
// $parameters['username'] = 'you'; */
$event->configurator->Censor->add('apple*');
$configurator = $event->configurator;
$tagName = 'SERVERTAG';
$tag = $configurator->tags->add($tagName);
$tag->attributes->add('ip')->filterChain->append('#identifier');
$tag->attributes->add('config')->required = false;
$tag->template =
'<xsl:value-of select="$username"/>';
$tag->filterChain->append([static::class, 'checkServerIP']);
$configurator->Preg->match('/\\[IP\\:(?<ip>.*)\\]/', $tagName);
}
/**
* @param ConfigureFormatterRenderer $event
*/
public function renderMe(ConfigureFormatterRenderer $event)
{
// echo "RENDER";
$event->renderer->setParameter('username', "HELLOTHERE");
}
public static function checkServerIP($tag)
{
$json = file_get_contents('https://mcapi.ca/query/' . str_replace("_", ".", $tag->getAttribute('ip')) . '/info');
$obj = json_decode($json);
$status = $obj->status;
if ($status == false) {
$tag->setAttribute('config', str_replace("_", ".", $tag->getAttribute('ip')) . " está offline!");
return true;
} else {
$tag->setAttribute('config', $obj->motd . "\n" . $obj->players->online . "/" . $obj->players->max . " Players Online!");
return true;
}
}
}
When I said "dynamic", it is because users can do this:
[IP:play_hypixel_net] (Yeah, underscores, I couldn't get Preg to match dots)
And the result will be "Hypixel Network - MYSTERY BOX UPDATE! » RANKED SKYWARS SEASON 2! « 27223/52000 Players Online!"
However, the number of players can change any moment, that's why I need to reformat the post when the user opens the discussion (of course, I don't need to update it everytime when the user is IN the discussion, only when he reloads/opens the page).
I tried throwing some code in the render section, however, instead of replacing, it only turned into "nothing" (it showed only a empty text, instead of changing to "HELLOTHERE")
It seems the mentions extension also uses the render, I will take a look at it to see how it was made.
If all else fails, I will make it replace the text with a JavaScript script that will get the info from the server.