Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sametgirginer committed Jul 4, 2022
0 parents commit 31eb71a
Show file tree
Hide file tree
Showing 50 changed files with 533 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace MinecraftServerChecker;

class Cron
{
public static function getData()
{
$db = \XF::db();
$client = \XF::app()->http()->client(['headers' => ['Accept' => 'application/json']]);
$servers = $db->query('SELECT * FROM xf_msc_servers')->fetchAll();

if ($servers) {
foreach ($servers as $server) {
$thread = $db->query('SELECT * FROM xf_thread WHERE thread_id = ?', $server['thread_id'])->fetch();

if (!$thread) {
$db->query('DELETE FROM xf_msc_servers WHERE thread_id = ?', $server['thread_id']);
} else if ($thread['discussion_state'] === "visible") {
$response = $client->get('https://api.mcsrvstat.us/2/' . $server['ip']);
$data = \GuzzleHttp\json_decode($response->getBody(), true);

$status = ($data['online'] === true) ? 1 : 0;
$online = 0;
$max = 1;

if ($data['online']) {
$online = $data['players']['online'];
$max = $data['players']['max'];
}

$db->query('UPDATE xf_msc_servers SET status = ?, online = ?, max = ?, last_update = ? WHERE thread_id = ?', [$status, $online, $max, time(), $server['thread_id']]);
} else {
$db->query('UPDATE xf_msc_servers SET status = ?, online = ?, max = ?, last_update = ? WHERE thread_id = ?', [0, 0, 1, time(), $server['thread_id']]);
}
}
}
}
}
70 changes: 70 additions & 0 deletions Listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace MinecraftServerChecker;

use XF\Mvc\Entity\Entity;
use XF\Template\Templater;

class Listener
{
public static function threadPostSave(Entity $entity)
{
$db = \XF::db();
$thread_id = $entity->getValue('thread_id');
$old_server_ip = $entity->getValue('custom_fields');
$new_server_ip = $entity->getNewValues('custom_fields');
$thread = $db->fetchOne('SELECT * FROM xf_msc_servers WHERE thread_id = ?', $thread_id);

$server_ip = "";

if ($old_server_ip) $server_ip = $old_server_ip['msc_server_ip'];
else if ($new_server_ip) $server_ip = $new_server_ip['custom_fields']['msc_server_ip'];

if ($server_ip) {
$client = \XF::app()->http()->client(['headers' => ['Accept' => 'application/json']]);
$response = $client->get('https://api.mcsrvstat.us/2/' . $server_ip);
$data = \GuzzleHttp\json_decode($response->getBody(), true);

$server_ip = strtolower($server_ip);
$status = ($data['online'] === true) ? 1 : 0;
$online = 0;
$max = 1;

if ($data['online']) {
$online = $data['players']['online'];
$max = $data['players']['max'];
}

if (!$thread && $server_ip) {
$db->insert('xf_msc_servers', [
'thread_id' => $thread_id,
'ip' => $server_ip,
'status' => $status,
'online' => $online,
'max' => $max,
'last_update' => time(),
]);
} else if ($thread && $server_ip && $new_server_ip) {
$db->query('UPDATE xf_msc_servers SET ip = ?, status = ?, online = ?, max = ? WHERE thread_id = ?', [$server_ip, $status, $online, $max, $thread_id]);
}
} else if ($thread && !$server_ip) {
$db->query('DELETE FROM xf_msc_servers WHERE thread_id = ?', $thread_id);
}
}

public static function templaterMacroPreRender(Templater $templater, &$type, &$template, &$name, array &$arguments, array &$globalVars)
{
$thread_id = $globalVars['thread']->thread_id;

$db = \XF::Db();
$server = $db->query('SELECT * FROM xf_msc_servers WHERE thread_id = ?', $thread_id)->fetch();
$status = ($server['status']) ? 'online' : 'offline';

$templater->addDefaultParams([
'msc_ip' => $server['ip'],
'msc_status' => $status,
'msc_online' => $server['online'],
'msc_max' => $server['max'],
]);
}
}
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Minecraft Server Checker for Threads - XenForo 2
It simply pulls server data with Custom Thread Field.

## Features

- Server data is updated with CRON every 5 minutes.
- CRON: `*/5 * * * *`
- API: [mcsrvstat.us](https://mcsrvstat.us/)
- Easy setup & detailed informations.
- Supports regex for custom thread field.
- REGEX: `/(([a-zA-Z0-9-])+[.]+([a-zA-Z0-9-])+[.]+([a-zA-Z0-9-])+)|([a-zA-Z0-9-])+[.]+([a-zA-Z0-9-])+/g`

## Installing XenForo add-on

1. Download latest release.
2. **Upload .zip** to your XenForo forum using the "**Install/upgrade from archive**" button in **Admin-Add-ons** (admin.php?add-ons).
3. **Select applicable forums** for the **custom thread field** (admin.php?custom-thread-fields/msc_server_ip/edit).
4. Go to a applicable thread and fill in the "**Minecraft Server IP Address**" field.

### CSS Examples (for extra.less)
------------
```css
.msc-status {
color: #efefef;
}

.msc-online {
background: #249a24;
}

.msc-offline {
background: #9a2424;
}

.msc-copyip {
color: #185886;
border-color: #e5e5e5;
}
```
58 changes: 58 additions & 0 deletions Setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace MinecraftServerChecker;

use XF\AddOn\AbstractSetup;
use XF\Db\Schema\Create;

class Setup extends AbstractSetup
{
public function install(array $stepParams = [])
{
$em = \XF::app()->em();

$this->schemaManager()->createTable('xf_msc_servers', function(Create $table) {
$table->addColumn('thread_id', 'int');
$table->addColumn('ip', 'varchar', 100);
$table->addColumn('status', 'tinyint')->setDefault(0);
$table->addColumn('online', 'int')->setDefault(0);
$table->addColumn('max', 'int')->setDefault(1);
$table->addColumn('last_update', 'varchar', 100);
$table->addPrimaryKey('thread_id');
});

if (!$em->find('XF:ThreadField', 'msc_server_ip')) {
$field = $em->create('XF:ThreadField');

$title = $field->getMasterPhrase(true);
$title->phrase_text = "Minecraft Server IP Address";

$description = $field->getMasterPhrase(false);
$description->phrase_text = "Enter the valid ip address for the server connection.";

$field->set('field_id', 'msc_server_ip');
$field->set('match_type', 'regex');
$field->setFromEncoded('match_params', '{"regex":"(([a-zA-Z0-9-])+[.]+([a-zA-Z0-9-])+[.]+([a-zA-Z0-9-])+)|([a-zA-Z0-9-])+[.]+([a-zA-Z0-9-])+"}');

$field->addCascadedSave($title);
$field->addCascadedSave($description);
$field->save();
}
}

public function upgrade(array $stepParams = [])
{
// First Version
}

public function uninstall(array $stepParams = [])
{
$em = \XF::app()->em();

$field = $em->find('XF:ThreadField', 'msc_server_ip');
if ($field) $field->delete();

$this->schemaManager()->dropTable('xf_msc_servers');

}
}
2 changes: 2 additions & 0 deletions _data/activity_summary_definitions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<activity_summary_definitions/>
2 changes: 2 additions & 0 deletions _data/admin_navigation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<admin_navigation/>
2 changes: 2 additions & 0 deletions _data/admin_permission.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<admin_permission/>
2 changes: 2 additions & 0 deletions _data/advertising_positions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<advertising_positions/>
2 changes: 2 additions & 0 deletions _data/api_scopes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<api_scopes/>
2 changes: 2 additions & 0 deletions _data/bb_code_media_sites.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<bb_code_media_sites/>
2 changes: 2 additions & 0 deletions _data/bb_codes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<bb_codes/>
2 changes: 2 additions & 0 deletions _data/class_extensions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<class_extensions/>
5 changes: 5 additions & 0 deletions _data/code_event_listeners.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<code_event_listeners>
<listener event_id="entity_post_save" execute_order="10" callback_class="MinecraftServerChecker\Listener" callback_method="threadPostSave" active="1" hint="XF\Entity\Thread" description="The event code will run when the custom thread field is saved."/>
<listener event_id="templater_macro_pre_render" execute_order="10" callback_class="MinecraftServerChecker\Listener" callback_method="templaterMacroPreRender" active="1" hint="public:msc_server_info:item" description="Macro variables"/>
</code_event_listeners>
2 changes: 2 additions & 0 deletions _data/code_events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<code_events/>
2 changes: 2 additions & 0 deletions _data/content_type_fields.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<content_type_fields/>
4 changes: 4 additions & 0 deletions _data/cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<cron>
<entry entry_id="msc_servers_cron" cron_class="MinecraftServerChecker\Cron" cron_method="getData" active="1"><![CDATA[{"day_type":"dom","dom":[-1],"hours":[-1],"minutes":[0,5,10,15,20,25,30,35,40,45,50,55]}]]></entry>
</cron>
2 changes: 2 additions & 0 deletions _data/help_pages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<help_pages/>
2 changes: 2 additions & 0 deletions _data/member_stats.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<member_stats/>
2 changes: 2 additions & 0 deletions _data/navigation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation/>
2 changes: 2 additions & 0 deletions _data/option_groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<option_groups/>
2 changes: 2 additions & 0 deletions _data/options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<options/>
2 changes: 2 additions & 0 deletions _data/permission_interface_groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<permission_interface_groups/>
2 changes: 2 additions & 0 deletions _data/permissions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<permissions/>
9 changes: 9 additions & 0 deletions _data/phrases.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<phrases>
<phrase title="cron_entry.msc_servers_cron" version_id="1000" version_string="1.0.0"><![CDATA[Get server data]]></phrase>
<phrase title="msc_copied_ip_address" version_id="1000" version_string="1.0.0"><![CDATA[The server ip address has been copied.]]></phrase>
<phrase title="msc_copy_ip_address" version_id="1000" version_string="1.0.0"><![CDATA[Copy server ip address]]></phrase>
<phrase title="msc_max_players" version_id="1000" version_string="1.0.0"><![CDATA[Max Players]]></phrase>
<phrase title="msc_offline_server" version_id="1000" version_string="1.0.0"><![CDATA[OFFLINE]]></phrase>
<phrase title="msc_online_players" version_id="1000" version_string="1.0.0"><![CDATA[Online Players]]></phrase>
</phrases>
2 changes: 2 additions & 0 deletions _data/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<routes/>
2 changes: 2 additions & 0 deletions _data/style_properties.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<style_properties/>
2 changes: 2 additions & 0 deletions _data/style_property_groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<style_property_groups/>
57 changes: 57 additions & 0 deletions _data/template_modifications.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<template_modifications>
<modification type="public" template="forum_view" modification_key="msc_copy_js" description="Adds copy clipboard button for server ip address." execution_order="10" enabled="1" action="str_replace">
<find><![CDATA[<xf:extension name="thread_list">]]></find>
<replace><![CDATA[$0
<script>
function msc_copy_ip(ip) {
var copyText = document.getElementById("mscip-" + ip);
var input = document.createElement("textarea");
input.value = copyText.textContent;
document.body.appendChild(input);
input.select();
document.execCommand("Copy");
input.remove();
XF.alert('{{ phrase("msc_copied_ip_address") }}', 'Copied!');
}
</script>]]></replace>
</modification>
<modification type="public" template="structured_list.less" modification_key="msc_structured_list" description="Adds style support for Minecraft Server information." execution_order="10" enabled="1" action="str_replace">
<find><![CDATA[.structItem-title
{]]></find>
<replace><![CDATA[$0
.msc-status {
color: #efefef;
border-color: transparent;
border-radius: 5px;
}
.msc-online {
background: #249a24;
}
.msc-offline {
background: #9a2424;
}
.msc-copyip {
color: #185886;
border-color: #e5e5e5;
}
.msc-copyip:hover {
cursor: pointer;
}
.msc-ip {
display: none;
}]]></replace>
</modification>
<modification type="public" template="thread_list_macros" modification_key="msc_server_info_block" description="Adds server information to the thread list." execution_order="10" enabled="1" action="str_replace">
<find><![CDATA[<div class="structItem-title">]]></find>
<replace><![CDATA[$0
<xf:macro name="{{ $templateOverrides.msc_server_info ?: 'msc_server_info::item' }}" />]]></replace>
</modification>
</template_modifications>
22 changes: 22 additions & 0 deletions _data/templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<templates>
<template type="public" title="msc_server_info" version_id="1000" version_string="1.0.0"><![CDATA[<xf:macro name="item" arg-thread="{$thread}">
<xf:if is="$thread.custom_fields.msc_server_ip">
<span class="label msc-status msc-{$msc_status}">
<xf:if is="$msc_online">
<span data-xf-init="tooltip" data-original-title="{{ phrase('msc_online_players') }}">{$msc_online}</span>/<span data-xf-init="tooltip" data-original-title="{{ phrase('msc_max_players') }}">{$msc_max}</span>
<xf:else />
<span>{{ phrase('msc_offline_server') }}</span>
</xf:if>
</span>
<a class="labelLink" onclick="msc_copy_ip('{$msc_ip}')"
data-xf-init="tooltip" data-original-title="{{ phrase('msc_copy_ip_address') }}" >
<span class="label msc-copyip"><i class="fas fa-copy"></i></span>
<span id="mscip-{$msc_ip}" class="msc-ip">{$msc_ip}</span>
</a>
</xf:if>
</xf:macro>]]></template>
</templates>
2 changes: 2 additions & 0 deletions _data/widget_definitions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<widget_definitions/>
2 changes: 2 additions & 0 deletions _data/widget_positions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<widget_positions/>
8 changes: 8 additions & 0 deletions _output/code_event_listeners/_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"entity_post_save_38510f988e511bd8ddcbdd99d2a8a6df.json": {
"hash": "e46abd2f4015042969bbaaa4c5d27ce4"
},
"templater_macro_pre_render_b1b054a9c2fdb298921f5512c6f52fae.json": {
"hash": "92ea5b25bc24994f38d1d8138e926e82"
}
}
Loading

0 comments on commit 31eb71a

Please sign in to comment.