Skip to content

Instantly share code, notes, and snippets.

@amgraham
Last active December 11, 2015 14:58
Show Gist options
  • Save amgraham/4617667 to your computer and use it in GitHub Desktop.
Save amgraham/4617667 to your computer and use it in GitHub Desktop.
Generate a list of logged-in players & show their skins head.
<?php
/* DEMO ---------------------------------------------------------------------------------------------------------------------- */
/* This file is a working example, so with a few edits to this file and server.properties and a file or two created (with accurate permissions) on your server, you can be up and running in no time.
You will need to edit server.properties to enable a remote connection in your Minecraft server: 'enable-query=true' amd set a port for querying your server: 'query.port=25565'. You shouldn't use the default port number.
Edit this file just below (the first block of code): YOURSERVER.COM & YOURPORTNUMBER to the URL for your Minecraft server and the port number you specified in server.properties (not your server-port; this one needs to be query.port).
This script needs one file to store the player names in temporarily, and a folder to store their skins, we store player names for 5 seconds by default and skins for 24 hours. We do this to stop overloading your server, and http://minecraft.net/skin/, with pointless requests every few seconds.
Create the file 'players' and a folder named 'skins', both need full access by the server.
Please note that if a player hasn't yet changed their default skin (still Steve) the player thumb will be a missing file. I can't distribute it, so you'll need to grab a copy of the original (say, from a theme) and save it as "skins/default-skin.png" /
You should be all done! Visit this page in your browser, and you should see a list of people currently online. */
/* QUERY --------------------------------------------------------------------------------------------------------------------- */
/* You could use different code to pull from a static file (either one you maintain separately, or whitelist.txt), you don't have to use PHP Minecraft Query, I just prefer it: check out https://github.com/xPaw/PHP-Minecraft-Query/blob/master/view.php.*/
// requiring php-minecraft-query class
require 'MinecraftQuery.class.php';
// connection variable for php-minecraft-query
define( 'MQ_SERVER_ADDR', 'YOURSERVER.COM' );
define( 'MQ_SERVER_PORT', YOURPORTNUMBER );
define( 'MQ_TIMEOUT', 1 );
// start the query and connect
$Query = new MinecraftQuery( );
/* CACHE --------------------------------------------------------------------------------------------------------------------- */
/* I hide all of this behind a five-second delay so I'm not constantly connecting and querying my server. You're implementation would be different; but something like the following should suffice as a starting point: */
if (filemtime("players") < (time() - 5)) :
$Query->Connect( MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_TIMEOUT );
$players = json_decode(json_encode($Query->GetPlayers())); // yes, this part is stupid. i'm 100% open to suggestions.
file_put_contents("players", json_encode($players));
else:
$players = json_decode(file_get_contents("players"));
endif;
/* THUMB --------------------------------------------------------------------------------------------------------------------- */
/* This is a function I use for the thumbnails of players. It assumes that the generated thumb will exist at : './skins/PLAYERNAME.png' */
function buildThumb($playa) {
// assume by default we don't want to generate a new thumb
$build = false;
if (!file_exists("./skins/".$playa.".png")):
// if the file doesn't exist, generate a new one
$build = true;
elseif(filemtime("./skins/".$playa.".png") < time() - (24 * 60 * 60)):
// if the file is older than a day, generate a new one
// you can adjust this to your needs: the '24' above represents hours
// (48 * 60 * 60) would be two days
// (7 * 24 * 60 * 60) would be one week
// (30 * 24 * 60 * 60) would be a month (sort of)
$build = true;
endif;
// now, if we're going to build, start the process
if ($build):
// check out the reply from the skin server, blocking any errors from output
$headers = @get_headers("http://minecraft.net/skin/".$playa.".png");
if($headers[2] != "HTTP/1.1 200 OK"):
// receiving anything other that a '200' status code here means the player hasn't changed from the default skin
// load up the local copy of the default for our needs
// IMPORTANT: you are going to have to supply a copy of this yourself.
$file = "skins/default-skin.png";
else:
// there is a skin on the server, use it for our needs
$file = "http://minecraft.net/skin/".htmlspecialchars( $playa ).".png";
endif;
$dst_x = 0;
$dst_y = 0;
// this is where the "face" begins in skin.png
// 8 from the top, 8 from the left
$src_x = 8; // Crop Start X
$src_y = 8; // Crop Start Y
// this is how big we want the thumb to be (note it's bigger, stretching will occur. this is intended.)
$dst_w = 40; // Thumb width
$dst_h = 40; // Thumb height
// the face is 8 pixels square in skin.png
$src_w = 8; // $src_x + $dst_w
$src_h = 8; // $src_y + $dst_h
// then generate the image and save it locally
$dst_image = imagecreatetruecolor($dst_w,$dst_h);
$src_image = imagecreatefrompng($file);
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagepng($dst_image, "skins/".$playa.".png");
endif;
}
/* DISPLAY ------------------------------------------------------------------------------------------------------------------- */
/* So now, we have to generate the player list: this is based upon a call with PHP-Minecraft-Query (from xPaw), if you have a list of players differently, use that: */?>
<?php if( $players !== false): ?>
<article>
<h1>Currently Online</h1>
<ul>
<?php foreach( $players as $player ): buildThumb($player); ?>
<li><?php echo $player; ?><br /><img src="skins/<?php echo $player; ?>.png" alt="<?php echo $player; ?>" /></li>
<?php endforeach; ?>
</ul>
</article>
<?php else: ?>
<em>Nobody is online</em>
<?php endif; ?>
<?php
/* LICENSE ------------------------------------------------------------------------------------------------------------------- */
/* This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License: http://creativecommons.org/licenses/by-nc-sa/3.0/ */ ?>
@SPKuja
Copy link

SPKuja commented Mar 7, 2014

Hi, this is a really cool script, but it doesn't generate the players faces, it just takes it from the default skin. When you say there needs to be a "players" file, what type of files does it need to be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment