Skip to content

Commit

Permalink
Added another api (api.keyubu.net)
Browse files Browse the repository at this point in the history
  • Loading branch information
sametgirginer committed Nov 3, 2023
1 parent b36c5a2 commit a035d10
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 34 deletions.
15 changes: 2 additions & 13 deletions Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@ public static function getData()
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']]);
$data = Server::getServerData($server['ip']);
$db->query('UPDATE xf_msc_servers SET status = ?, online = ?, max = ?, last_update = ? WHERE thread_id = ?', [$data['status'], $data['online'], $data['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']]);
}
Expand Down
22 changes: 6 additions & 16 deletions Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class Listener
{
Expand All @@ -23,31 +24,20 @@ public static function threadPostSave(Entity $entity)
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'];
}
$data = Server::getServerData($server_ip);

if (!$thread && $server_ip) {
$db->insert('xf_msc_servers', [
'thread_id' => $thread_id,
'ip' => $server_ip,
'status' => $status,
'online' => $online,
'max' => $max,
'status' => $data['status'],
'online' => $data['online'],
'max' => $data['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]);
$db->query('UPDATE xf_msc_servers SET ip = ?, status = ?, online = ?, max = ? WHERE thread_id = ?', [$server_ip, $data['status'], $data['online'], $data['max'], $thread_id]);
}
} else if ($thread && !$server_ip) {
$db->query('DELETE FROM xf_msc_servers WHERE thread_id = ?', $thread_id);
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ It simply pulls server data with Custom Thread Field.

- Server data is updated with CRON every 5 minutes.
- CRON: `*/5 * * * *`
- API: [mcsrvstat.us](https://mcsrvstat.us/)
- API1: [mcsrvstat.us](https://mcsrvstat.us/)
- API2: [keyubu.com](https://keyubu.com/)
- 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`
Expand Down
44 changes: 44 additions & 0 deletions Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MinecraftServerChecker;

class Server
{
public static function getServerData(String $server_ip)
{
$api = \XF::options()->msc_api;
$client = \XF::app()->http()->client(['headers' => ['Accept' => 'application/json']]);

$status = 0;
$online = 0;
$max = 1;

if ($api === "mcsrvstatus") {
$response = $client->get('https://api.mcsrvstat.us/2/' . $server_ip);
$rawdata = \GuzzleHttp\json_decode($response->getBody(), true);

$status = ($rawdata['online'] === true) ? 1 : 0;
if ($rawdata['online']) {
$online = $rawdata['players']['online'];
$max = $rawdata['players']['max'];
}
} else {
$response = $client->get('https://api.keyubu.net/minecraft/json.php?host=' . $server_ip . '&port=25565');
$rawdata = \GuzzleHttp\json_decode($response->getBody(), true);

$status = ($rawdata['online'] === true) ? 1 : 0;
if ($rawdata['online']) {
$online = $rawdata['players']['online'];
$max = $rawdata['players']['max'];
}
}

$data = [
'status' => $status,
'online' => $online,
'max' => $max
];

return $data;
}
}
4 changes: 3 additions & 1 deletion _data/option_groups.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<option_groups/>
<option_groups>
<group group_id="minecraft_server_checker" display_order="10" debug_only="0"/>
</option_groups>
9 changes: 8 additions & 1 deletion _data/options.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<options/>
<options>
<option option_id="msc_api" edit_format="select" data_type="string" advanced="0">
<default_value></default_value>
<edit_format_params>mcsrvstatus=mcsrvstat.us
keyubu=api.keyubu.net</edit_format_params>
<relation group_id="minecraft_server_checker" display_order="1"/>
</option>
</options>
4 changes: 4 additions & 0 deletions _data/phrases.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<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>
<phrase title="option.msc_api" version_id="1050" version_string="1.0.5"><![CDATA[Server API]]></phrase>
<phrase title="option_explain.msc_api" version_id="1050" version_string="1.0.5"><![CDATA[]]></phrase>
<phrase title="option_group.minecraft_server_checker" version_id="1050" version_string="1.0.5"><![CDATA[Minecraft Server Checker]]></phrase>
<phrase title="option_group_description.minecraft_server_checker" version_id="1050" version_string="1.0.5"><![CDATA[]]></phrase>
</phrases>
5 changes: 5 additions & 0 deletions _output/option_groups/_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"minecraft_server_checker.json": {
"hash": "988c5d61421a11ab962ac26f38b35ba7"
}
}
6 changes: 6 additions & 0 deletions _output/option_groups/minecraft_server_checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"icon": "",
"display_order": 10,
"advanced": false,
"debug_only": false
}
5 changes: 5 additions & 0 deletions _output/options/_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"msc_api.json": {
"hash": "a8a67554ba7b59e6f941b8bd0d10a4a3"
}
}
13 changes: 13 additions & 0 deletions _output/options/msc_api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"edit_format": "select",
"edit_format_params": "mcsrvstatus=mcsrvstat.us\nkeyubu=api.keyubu.net",
"data_type": "string",
"sub_options": [],
"validation_class": "",
"validation_method": "",
"advanced": false,
"default_value": "",
"relations": {
"minecraft_server_checker": 1
}
}
24 changes: 24 additions & 0 deletions _output/phrases/_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,29 @@
"version_id": 1000,
"version_string": "1.0.0",
"hash": "ccf4c59cfa0dba7b63811d654a6feb14"
},
"option.msc_api.txt": {
"global_cache": false,
"version_id": 1050,
"version_string": "1.0.5",
"hash": "a34f3170c0666670620c8eafa9269035"
},
"option_explain.msc_api.txt": {
"global_cache": false,
"version_id": 1050,
"version_string": "1.0.5",
"hash": "d41d8cd98f00b204e9800998ecf8427e"
},
"option_group.minecraft_server_checker.txt": {
"global_cache": false,
"version_id": 1050,
"version_string": "1.0.5",
"hash": "f8acec4d0605129cfdcbbd4c7c411b61"
},
"option_group_description.minecraft_server_checker.txt": {
"global_cache": false,
"version_id": 1050,
"version_string": "1.0.5",
"hash": "d41d8cd98f00b204e9800998ecf8427e"
}
}
1 change: 1 addition & 0 deletions _output/phrases/option.msc_api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Server API
Empty file.
1 change: 1 addition & 0 deletions _output/phrases/option_group.minecraft_server_checker.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Minecraft Server Checker
Empty file.
4 changes: 2 additions & 2 deletions addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"legacy_addon_id": "",
"title": "Minecraft Server Checker",
"description": "It pulls server data with custom thread fields and adds it to the thread list.",
"version_id": 1040,
"version_string": "1.0.4",
"version_id": 1050,
"version_string": "1.0.5",
"dev": "Samet Girginer",
"dev_url": "https://github.com/sametgirginer",
"faq_url": "",
Expand Down

0 comments on commit a035d10

Please sign in to comment.