Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary shortcode support and prettier MotD Display #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions css/widrick_style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

span .widrick_minecraft_motd
{
background-color: #333;
}
3 changes: 2 additions & 1 deletion libs/minecraft_query/MinecraftQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private function GetStatus( $Challenge )
}
else if( $Last != false )
{
$Info[ $Last ] = mb_convert_encoding( $Value, 'UTF-8' );
//$Info[ $Last ] = mb_convert_encoding( $Value, 'UTF-8' );
$Info[$Last] = $Value;
}
}

Expand Down
30 changes: 30 additions & 0 deletions shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php



function widrick_minecraft_serverStats_shortcode($atts, $content = null)
{
$instance = $atts;
$instance['avatar_size'] = "20";
$instance['show_server_motd'] = "on";
$instance['show_status'] = "on";
$instance['show_host'] = "on";
$instance['show_port'] = "on";
$instance['show_server_platform'] = "on";
$instance['show_server_software'] = "on";
$instance['show_game_version'] = "on";
$instance['show_players'] = "on";
$instance['show_players_avatar'] = "on";
$instance['set_seconds'] = "15";

$client = new GoneTone\ApiClient(
$instance['host'],
$instance['server_port'],
$instance['query_port']);

$queryServer = $client->queryCall();
$pingServer = $client->pingCall();

require dirname(__FILE__) . '/templates/widget.php';
}
add_shortcode('widrick_serverStats','widrick_minecraft_serverStats_shortcode');
2 changes: 2 additions & 0 deletions templates/partials/online.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<p>
<?php if (isset($instance['show_server_motd']) && $instance['show_server_motd'] == 'on') : ?>
<?php

$cleanHostName = str_replace(array("§k", "§l", "§m", "§n", "§o", "§r", "§1", "§2", "§3", "§4", "§5", "§6", "§7", "§8", "§9", "§a", "§b", "§c", "§d", "§e", "§f"), "", $server['server']['server_motd']);
$cleanHostName = widrick_format_hostName($server['server']['server_motd']);
?>
<?php echo esc_html__('Server MOTD: ', 'server-status-for-minecraft-pc-pe'); ?><strong><span style="color:green;font-weight:bold"><refresh class="motd"><?php echo $cleanHostName; ?></refresh></span></strong>
<?php endif; ?>
Expand Down
3 changes: 2 additions & 1 deletion widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
require dirname(__FILE__) . '/libs/Widgetize.php';
require dirname(__FILE__) . '/libs/ApiClient.php';
require dirname(__FILE__) . '/shortcode.php';
require dirname(__FILE__) . '/widrick_motd_correction.php';

class MCServerStatus_Widget extends Widgetize {
/**
Expand Down Expand Up @@ -64,7 +66,6 @@ public function widget(array $args, array $instance) {
if (in_array($instance['host'], array('127.0.0.1', 'localhost'))) {
$instance['host'] = $_SERVER['SERVER_ADDR'];
}

$client = new GoneTone\ApiClient($instance['host'], $instance['server_port'], $instance['query_port']);
$queryServer = $client->queryCall();
$pingServer = $client->pingCall();
Expand Down
149 changes: 149 additions & 0 deletions widrick_motd_correction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

//http://php.net/manual/en/function.str-split.php#107658

function widrick_format_hostName($string)
{
wp_enqueue_style('widrick_format_HostName');
$inSpan = false;
$skipChar = false;

$stringArr = str_split($string);

$formattedHostName = "<span class='widrick_minecraft_motd'>";
$formatState = Array();


foreach($stringArr as $key => $char)
{
if($skipChar === true)
{
$skipChar = false;
continue;
}
if(ord($char) === 167)
{
$nextChar = $stringArr[$key+1];
if(ord($nextChar) <= ord('f'))
{
$colorCode = widrick_format_colorCode($nextChar);
if($colorCode !== null)
{
//--New color = end all formats per spec
$formattedHostName .= widrick_format_reset($formatState);
if($inSpan === true)
{
$formattedHostName .= "</span>";
$inSpan = false;
}
$formattedHostName .= "<span style='color:" . $colorCode . "'>";
$inSpan = true;
}
$skipChar = true;
continue;
}
elseif(ord($nextChar) >= ord('k') && ord($nextChar) <= ord('r'))
{
$formatCode = widrick_format_formatCode($nextChar);
if($formatCode !== null && $formatCode != "RESET")
{
$formattedHostName .= $formatCode;
$formatState[$formatCode] = substr($formatCode,0,1) . '/' . substr($formatCode,1);
}
elseif($formatCode == "RESET")
{
$formattedHostName .= widrick_format_reset($formatState);
}
$skipChar = true;
continue;
}
elseif(ord($nextChar) == 167)
continue; //STAHP!
}
//Throw out unicode decorations
$formattedHostName .= $char;
}

//clean up
$formattedHostName .= widrick_format_reset($formatState);
if($inSpan === true)
$formattedHostName .= '</span>';
return $formattedHostName . '</span>';

}
function widrick_format_reset(&$formatState)
{
$retStr = "";
foreach(array_reverse($formatState) as $key => $state)
{
$retStr .= $state;
unset($formatState[$key]);
}
return $retStr;
}
function widrick_format_formatCode($code)
{
$code = strtolower($code);
switch($code)
{
case "k":
return null;// Unimplemented
case "l":
return "<b>";
case "m":
return null;//Not implemented
case "n":
return "<u>";
case "r":
return "RESET";
default:
return null;
}
}


function widrick_format_colorCode($code)
{

$code = strtolower($code);
switch($code)
{
case "0":
return "#000";
case "1":
return "#0000AA";
case "2":
return "#00AA00";
case "3":
return "#00AAAA";
case "4":
return "#AA0000";
case "5":
return "#AA00AA";
case "6":
return "#FFAA00";
case "7":
return "#AAAAAA";
case "8":
return "#555555";
case "9":
return "#5555FF";
case "a":
return "#55ff55";
case "b":
return "#55ffff";
case "c":
return "#ff5555";
case "d":
return "#ff55ff";
case "e":
return "#ffff55";

case "f":
return "#ffffff";
default:
return null;
}
}

wp_register_style('widrick_format_HostName',plugin_dir_url(__FILE__).'css/widrick_style.css');