Skip to content

Instantly share code, notes, and snippets.

@wetnun wetnun/get_player_icon.php
Last active Aug 29, 2015

Embed
What would you like to do?
Moving parts for minecraft map with mapcrafter and player markers, powered by cron!
#!/usr/bin/php -f
<?php
// original version was from TJ09
// forum link: http://forums.bukkit.org/threads/info-mapmarkers-v0-3-4-1-1r6.843/
$sizefactor = 1.5;
$cachetime = 60*60*24;
$destination = "/var/www/html/map2/static/markers/";
if(sizeof($argv) > 1) {
$username = preg_replace("/[^a-zA-Z0-9_]/", "", $argv[1]);
} else {
die("No username provided\n");
}
// If it's cached, then yay.
$filename = "$destination/$username.png";
if(file_exists($filename) && filemtime($filename) > time() - $cachetime) {
echo "Cached\n";
exit(0);
}
if (file_exists("$filename.fail") && filemtime("$filename.fail") > time() - (60 * 60 * 4)) {
die("Failed to get user icon previously, waiting to retry...\n");
}
$src = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/{$username}.png");
if(!$src) {
touch("$filename.fail");
die("Failed to get skin for $username\n");
}
$img = imagecreatetruecolor(16, 32);
imagealphablending($img, false);
imagesavealpha($img, true);
imagefill($img, 0, 0, imagecolorallocatealpha($img, 255, 0, 255, 127));
imagecopy($img, $src, 4, 0, 8, 8, 8, 8); //Head
imagecopy($img, $src, 4, 8, 20, 20, 8, 12); //Body
imagecopy($img, $src, 0, 8, 44, 20, 4, 12); //Arm-L
imagecopyresampled($img, $src, 12, 8, 47, 20, 4, 12, -4, 12); //Arm-R
imagecopy($img, $src, 4, 20, 4, 20, 4, 12); //Leg-L
imagecopyresampled($img, $src, 8, 20, 7, 20, 4, 12, -4, 12); //Leg-R
// Enable alpha blending so hat blends with face.
imagealphablending($img, true);
imagecopy($img, $src, 4, 0, 40, 8, 8, 8); //Hat
$img_big = imagecreatetruecolor(16*$sizefactor, 32*$sizefactor);
imagealphablending($img_big, false);
imagesavealpha($img_big, true);
imagecopyresampled($img_big, $img, 0, 0, 0, 0, 16*$sizefactor, 32*$sizefactor, 16, 32);
imagepng($img_big, $filename);
echo "Created\n";
?>
#!/usr/bin/perl
use strict;
use YAML::XS;
use Data::Dumper;
my %players;
my $NBT = "/usr/bin/nbt2yaml";
foreach my $f (glob("/opt/minecraft_server/world/playerdata/*.dat")) {
my ($uuid) = $f =~ /\/([^\/]+?)\.dat$/;
my $data = `$NBT $f`;
my $yaml = Load($data);
my $pos;
my $dim;
foreach my $h (@{$yaml->{""}}) {
if (defined $h->{Pos}) {
$pos = $h;
}
if (defined $h->{Dimension}) {
$dim = $h->{Dimension};
}
}
my $name = `zgrep $uuid /opt/minecraft_server/logs/*.log* | head -1 | grep -oE 'player [^ ]* ' | cut -d' ' -f2`;
chomp($name);
# check that we have an icon...
my $res = `/home/minecraft/get_player_icon.php $name`;
$players{$name} = { x => $pos->{Pos}->[0], y => $pos->{Pos}->[1], z => $pos->{Pos}->[2], dimension => $dim };
}
my $TEMPLATE = <<'TEMPL';
{ "players": [
PLAYMARKS
]
}
TEMPL
my $markers = "";
my $pcnt = scalar(keys %players);
my $c = 1;
foreach my $p (keys %players) {
my $icon = "player";
if (-e "/var/www/html/map2/static/markers/$p.png") {
$icon = $p;
}
my $world = $players{$p}{dimension} == 0 ? "world" : "nether";
my $mark = <<MARKER;
{
"username": "$p",
"icon": "$icon",
"world": "$world",
"x": ${$players{$p}{x}},
"y": ${$players{$p}{y}},
"z": ${$players{$p}{z}}
}
MARKER
$markers .= $mark;
$markers .= "," if ++$c <= $pcnt;
}
$TEMPLATE =~ s/PLAYMARKS/$markers/m;
if (open(my $fh,">/var/www/html/map2/players.json")) {
print $fh $TEMPLATE;
close $fh;
} else {
print "Failed to open marker file: $!\n";
}
.....
var INTERVAL = 60 * 1000;
var ANIMATED = true;
var JSON_PATH = "players.json";
var IMG_PATH = "static/markers/{icon}.png";
var IMG_SIZE_FACTOR = 1.5;
function PlayerMarker(ui, username, icon, world, pos) {
this.ui = ui;
this.username = username;
this.world = world;
this.active = true;
this.marker = L.marker(this.ui.mcToLatLng(pos.x, pos.z, pos.y), {
title: this.username,
icon: L.icon({
iconUrl: IMG_PATH.replace("{icon}", icon),
iconSize: [16 * IMG_SIZE_FACTOR, 32 * IMG_SIZE_FACTOR],
}),
});
this.marker.addTo(this.ui.lmap);
this.moveCounter = 0;
this.start = null;
this.destination = pos;
}
......
MapPlayerMarkerHandler.prototype.create = function() {
ui = this.ui;
var handler = function(self) {
return function() {
$.ajax({
dataType: "json",
url: JSON_PATH,
success: function(data) {
self.updatePlayers(data);
},
error: function(xhr,err,txt) {
console.log('player error: '+err+' -> '+txt);
}
});
// I was getting errors, but they were never reported, so black hole of doom
//$.getJSON(JSON_PATH, function(data) { console.log('wtf...');self.updatePlayers(data); });
};
}(this);
window.setTimeout(handler, 500);
window.setInterval(handler, INTERVAL);
};
....
MapPlayerMarkerHandler.prototype.updatePlayers = function(data) {
if(!data)
return;
var globalPlayersOnline = [];
var worldPlayersOnline = 0;
for(var i = 0; i < data["players"].length; i++) {
var user = data["players"][i];
var username = user.username;
var pos = {x: user.x, z: user.z, y: user.y};
var player;
if(user.username in this.players) {
player = this.players[username];
} else {
player = new PlayerMarker(ui, username, user.icon, user.world, pos);
this.players[username] = player;
}
player.setActive(user.world == this.currentWorld);
if(player.active) {
worldPlayersOnline++;
player.move(pos);
}
globalPlayersOnline.push(username);
}
for(var name in this.players) {
if(globalPlayersOnline.indexOf(name) == -1) {
this.players[name].setActive(false);
delete this.players[name];
}
}
document.title = "(" + worldPlayersOnline + "/" + globalPlayersOnline.length + ") " + this.documentTitle;
};
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.