Showing posts with label techtronics. Show all posts
Showing posts with label techtronics. Show all posts

Wednesday, December 15, 2010

Minecraft Multiplayer teleporting with a server-side script!

So I play Minecraft with a few buddies on a stock (currently updated) Minecraft server on my Windows 7 box.  One of my friends decided to build a house way out in the middle of nowhere and I couldn't figure out how to get back there.  I decided to dig around in the Minecraft server directory structure, and found the player.dat files.  I assumed they held the players' location and inventory, so I tried reverse engineering them with a hex editor to determine a player's saved location when they were logged out, but it wasn't as obvious as I'd hoped.  I decided to get a little crazy, and just copy the player.dat file over my own, and log into the game.  Low and behold, I was standing in his place with his inventory!

That got me to thinking about ways I could do this automatically as a sort of teleportation.  I ended up coming up with this script:

#!C:\Perl\bin\perl -w

use IPC::Open2;
use strict;
use warnings;

my $minecraft_server_path = 'c:\MineCraft';
my $world_name = 'blockatopia';

my $pid = open2(*OUTPUT, *INPUT, 'java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui 2>&1');

while (my $line = <OUTPUT>){
    print $line;

    if ($line =~ /(\w+) issued server command: ([\w\s]+$)/){

        my $player_name = $1;
        my $command = $2;

        if (defined $command){
            if ($command =~ /^save secret location ([0-9a-zA-Z_]+)\s*$/){
                my $save_point_name = $1;

                #Kick the player so their dat file gets updated
                send_command("kick $player_name\n");

                #give the sever time to perform the kick
                sleep 1;

                #Make sure the location directory exists
                unless ( -e "$minecraft_server_path\\$world_name\\players\\secret_locations" ) {
                    system("mkdir $minecraft_server_path\\$world_name\\players\\secret_locations");
                }

                #Make a copy of that user's .dat file and rename it to the save location listed above
                system("copy $minecraft_server_path\\$world_name\\players\\$player_name.dat $minecraft_server_path\\$world_name\\players\\secret_locations\\$save_point_name.dat");
            }

            if ($command =~ /^save location ([0-9a-zA-Z_]+)\s*$/){
                my $save_point_name = $1;

                #Kick the player so their dat file gets updated
                send_command("kick $player_name\n");

                #give the sever time to perform the kick
                sleep 1;

                #Make sure the location directory exists
                unless ( -e "$minecraft_server_path\\$world_name\\players\\location" ) {
                    system("mkdir $minecraft_server_path\\$world_name\\players\\location");
                }

                #Make a copy of that user's .dat file and rename it to the save location listed above
                system("copy $minecraft_server_path\\$world_name\\players\\$player_name.dat $minecraft_server_path\\$world_name\\players\\location\\$save_point_name.dat");
            }

            if ($command =~ /^be ([0-9a-zA-Z_]+)\s*$/){
                my $save_point_name = $1;

                #Kick the player so they'll have to load it when they log back in
                send_command("kick $player_name\n");

                #give the sever time to perform the kick
                sleep 1;

                #Overwrite the user's dat file with the player's dat file if it exists
                if ( -e "$minecraft_server_path\\$world_name\\players\\$save_point_name.dat" ) {
                    system("copy $minecraft_server_path\\$world_name\\players\\$save_point_name.dat $minecraft_server_path\\$world_name\\players\\$player_name.dat");
                }
                else {
                    send_command("tell $player_name That player does not exist.\n");
                }
            }

            if ($command =~ /^secret warp ([0-9a-zA-Z_]+)\s*$/){
                my $save_point_name = $1;

                #Kick the player so they'll have to load it when they log back in
                send_command("kick $player_name\n");

                #give the sever time to perform the kick
                sleep 1;

                #Make sure the location directory exists
                unless ( -e "$minecraft_server_path\\$world_name\\players\\secret_locations" ) {
                    system("mkdir $minecraft_server_path\\$world_name\\players\\secret_locations");
                }

                #Overwrite the user's dat file with the saved location dat file if it exists
                if ( -e "$minecraft_server_path\\$world_name\\players\\secret_locations\\$save_point_name.dat" ) {
                    system("copy $minecraft_server_path\\$world_name\\players\\secret_locations\\$save_point_name.dat $minecraft_server_path\\$world_name\\players\\$player_name.dat");
                }
                else {
                    send_command("tell $player_name That location does not exist.\n");
                }
            }
           
            if ($command =~ /^warp ([0-9a-zA-Z_]+)\s*$/){
                my $save_point_name = $1;

                #Kick the player so they'll have to load it when they log back in
                send_command("kick $player_name\n");

                #give the sever time to perform the kick
                sleep 1;

                #Make sure the location directory exists
                unless ( -e "$minecraft_server_path\\$world_name\\players\\location" ) {
                    system("mkdir $minecraft_server_path\\$world_name\\players\\location");
                }

                #Overwrite the user's dat file with the saved location dat file if it exists
                if ( -e "$minecraft_server_path\\$world_name\\players\\location\\$save_point_name.dat" ) {
                    system("copy $minecraft_server_path\\$world_name\\players\\location\\$save_point_name.dat $minecraft_server_path\\$world_name\\players\\$player_name.dat");
                }
                else {
                    send_command("tell $player_name That location does not exist.\n");
                }
            }
           
            if ($command =~ /^list locations\s*$/){
                my $save_point_name = $1;

                #give the sever time to perform the kick
                sleep 1;

                #Make sure the location directory exists
                unless ( -e "$minecraft_server_path\\$world_name\\players\\location" ) {
                    system("mkdir $minecraft_server_path\\$world_name\\players\\location");
                }
               
                my $files = `dir /B $minecraft_server_path\\$world_name\\players\\location`;
                my @files = split "\n", $files;

                foreach my $file (@files) {
                    $file =~ s/\.dat//g;

                    send_command("tell $player_name [$file]\n");
                }
            }
            if ($command =~ /^help\s*$/){
                #give the server time to finish it's help command
                sleep 1;

                #Now send our help information
                send_command("tell $player_name /save location name_of_place\n");
                send_command("tell $player_name /warp name_of_place\n");
                send_command("tell $player_name /list locations\n");
            }
        }
    }
    if ($line =~ /poop/g){
        send_command("say Not allowed to say poop!\n");
        print "[Wrapper sent]say You said poop!\n";
    }
}

sub send_command {
    my $command = shift;

    print INPUT $command;
    print "[Wrapper sent]$command";
}


This script is a wrapper around the actual minecraft executable.  It uses a module to read the output of the server (all of player's entered commands and chatting) and based on strings that it sees, sends commands back to the server along with copying and moving files around on the server side.  Once this script is running your minecraft server, you can execute the following commands in game by hitting 't' and then one of the commands below:

/save secret location name_of_warp_location
/save location name_of_warp_location
/secret warp name_of_warp_location
/warp name_of_warp_location
/be player_name
/list locations


Command Definitions are as follows:

/save secret location name_of_warp_location - saves the current location (and inventory) of the player under the name specified by "name_of_warp_location", but does not add this location to the public list.  (See the /list command below)
/save location name_of_warp_location - saves the current location (and inventory) of the player under the name specified by "name_of_warp_location", and adds it to the public locations list (See the /list command below)

/secret warp name_of_warp_location - teleports the current player to the location specified (and gives them the inventory saved at that location).  Only works with secret locations
/warp name_of_warp_location - teleports the current player to the location specified (and gives them the inventory saved at that location).  Only works with non-secret locations

/be player_name - teleports the current player to the last known location of player_name (and gives current player a copy of everything that was in player_name's inventory)

/list locations - lists all publicly saved locations on this server.  (Also lists the users currently logged on because this command piggy-backs the normal /list command)
NOTE:  All of the "save location" commands will perform a kick of the player that performs them in order to force the players location to be saved in the back-end file before it gets copied.  Don't freak out when you get kicked, just log back in and the location will be in /list locations.  Also, all of the "warp" commands will kick a player before copying their file because the login process is the only way that I know of to load a player.dat file back into a player :).

Now, the more complicated problem of making Perl run on your windows machine is a different story.  I used the Active Perl installation from the folks over at ActiveState.com in order to get it running on my machine and it was fairly straight-forward.  If not, well..there are plenty of howto's on the internet that can help you out with that ;).  Once Perl's installed, just modify these two variables to match your Minecraft server location and world name:


my $minecraft_server_path = 'c:\MineCraft';
my $world_name = 'blockatopia';


For those of you who have a nice Linux box that will run the Minecraft server worth a darn (Sadly, mine is in dire need of an upgrade) this script should work just fine for you as long as you reverse all of the \\ to //.  I didn't feel like putting the time into this to make it truly cross-platform... my bad ;).

Hope this helps you all enjoy Minecraft a little more.  It's certainly made my time more "productive"...wait...scratch that ;)

Wednesday, October 27, 2010

Web page slideshow

So I was tasked with making a web page slideshow for some community televisions at work, but after some research, all I was able to find were image slideshows.  Here's a simple page I threw together that uses a json configuration file to display multiple web pages for a defined period of time, and then slide to the next one.  Below is the html and javascript.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Slideshow</title>

<style>
body {
    overflow: hidden;
    cursor: none;
}

#page2 {
    display: none;
}
</style>

<script src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
    var slideshow;

    var iteration = 0;
    var timer;
    var slideshow_count = 0;
    var op;
    var fade_timer;
    var current_page = 1;

    //Initialize the slideshow settings from the json config file
    $(document).ready(function() {
        //This primes the loop and gets everything going

        //We're using the ajax method instead of getJSON because caching is automatically
        //turned off with the .ajax call.  Also, when specifying the dataType jsonp,
        //the returned json data is automatically placed into a script tag and executed
        //with the callback function.  Our data source here has a static callback called
        //slideshow_settings, so we don't have to pass anything extra in.
        $.ajax({
            url: "slideshow_settings.json",
            dataType: "jsonp"
        });
    });

    function get_next_iteration () {
        if (iteration + 1 >= slideshow_count) {
            return 0;
        }
        else {
            return iteration + 1;
        }
    }
   
    function other_page () {
        if (current_page == 1) {
            return 2;
        }
        else {
            return 1;
        }
    }

    function slideshow_settings (settings) {
        iteration = -1;

        slideshow = settings;
        slideshow_count = settings.dashboards.length;

        do_next();
    }

    function switch_pages () {
        $('#page' + current_page).fadeOut(1000, function() {
            $(this).css('display', 'none')
            $('#page' + other_page()).fadeOut(0, function() {
                $(this).fadeIn(1000, function() {
                    current_page = other_page();
                    $('#page' + other_page()).attr('src', slideshow.dashboards[get_next_iteration()].url);
                    timer = setTimeout(do_next, (slideshow.dashboards[iteration].duration * 1000));
                });
            });
        });
    }

    function do_next () {
        //Clean up after ourselves to prevent memory leaks
        clearTimeout(timer);

        //Loop has started anew.  Check our config file again.
        if (iteration+1 >= slideshow_count ) {
            $.ajax({
                url: "slideshow_settings.json",
                dataType: "jsonp"
            });
        }
        //Do the work
        else {
            iteration++;
            switch_pages();
        }
    }   
</script>
</head>

<body style="margin: 0px; scroll: off; background: black;">

<iframe id="page1" style="width: 1920px; height: 1080px; border: 0px;"></iframe>
<iframe id="page2" style="width: 1920px; height: 1080px; border: 0px;"></iframe>

</body>
</html>

Wednesday, August 18, 2010

Motorola Droid USB Tethering

I found out today (the hard way) that the new tether option included with the Android 2.2 update doesn't actually do anything. Maybe there's a fee I can pay Verizon to make it so, but that's not happening.

In my research to find out how to make the tethering feature work, I discovered a method of tethering I'd not heard of before (and apparently it's been around awhile.)

You don't have you root your phone AND it's completely free. Win win right? It just takes a little bit of configuration on your computer. For this post I'm going to focus on OSX because that's what I use, and there seem to be a plethora of others out there explaining how this works in Windows.

I used this guy's blog as a basis for what I did, and I changed very little: http://jimcortez.com/blog/?p=37

His first step is to install openvpn on your Mac. I already had Viscosity installed on mine and I wanted to try and use it instead of installing a separate openvpn client and using the custom script provided. This method instead allows you to use the Viscosity icon on your menu bar to initiate your tether. It's cleaner in my opinion and allows me to use one central location to initialize the tether and then my work vpn.

For this method you need only follow a fraction of the steps provided from the blog above.

1. First, install Viscosity. You can find it here.
2. Install Azilink to your phone. Instead of going through the trouble of installing the SDK, I find it's easier to just install it directly on your phone from a web browser.

On your phone do the following:
  • Menu->Settings->Applications->Check the "Unknown Sources" box.
  • Install "Astro File Manager" from the Marketplace
  • Open Astro->Menu->More->Preferences->Check the "Enable Browser Download" box.
  • (Remember to uncheck that same box when you've finished downloading things from the web...that feature can break your ability to view attachments in Gmail)
  • Download Azilink here.
  • Open Astro and browse to your download directory. Clicking on the file should give you the option to install it (if you've followed the previous directions properly.)
3. Now run Azilink and check the "Service Started" box to make sure it's enabled.
4. Enable USB Debugging: Settings->Applications->Development->USB debugging
5. Create a file with these contents:

#viscosity startonopen false
#viscosity dhcp true
#viscosity dnssupport true
#viscosity name azilink
remote 127.0.0.1 41927
ping 10
dev tun
proto tcp-client
route 0.0.0.0 128.0.0.0
route 128.0.0.0 128.0.0.0
socket-flags TCP_NODELAY
dhcp-option DNS 192.168.56.1
script-security 2
ifconfig 192.168.56.2 192.168.56.1

5. Make sure Viscosity is running and click on the menu icon for it and go to Preferences.
6. Click on the plus (+) icon and then Import the file you just created.
7. Now click on the azilink entry that you just created and then Edit
8. Under "Certificates" choose "Static Key" (You get a weird error about a missing certificate if you don't do this.)

Now connect tether your phone to your laptop, and click on the Viscosity icon and then "Connect azilink."

If everything went according to plan, you should be surfing the inter-tubes now :)

Tuesday, August 17, 2010

Android 2.2 for the Motorola Droid

Did I forget to mention I planned on talking tech in this blog? It's true. This is proof. Luckily for you I'll be tagging each of these blogs appropriately. The tag 'daddyhood' will refer to just that, while 'techtronics' will be anything tech related I care to share. I'm sure I'll add more tags later on for fun.

Onward to the topic of discussion!

I manually installed the official Android 2.2 update on my Motorola Droid today because I couldn't bear to wait for them to push it out to me automatically. I used this site:

http://www.droid-life.com/2010/08/03/manual-android-2-2-update-for-motorola-droid/

I've only been using the new version for about 6 hours now, and I have to say, I'm impressed. I've been feeling a bit tired of my Droid as of late, and have seriously contemplated upgrading to the X or the Droid 2. (Of course, I was delayed in action by yet another set of rumors that the iPhone is coming to Verizon in January...who knows if it'll actually happen.) ANYWAYS, this upgrade makes my phone feel brand new again. Here are some of the reasons why:

1) My phone is WAY more responsive. No laggy screen scrolls, and much less hesitation when I open an application. The phone feels twice as fast.

2) The Gmail app has forward and back buttons for scrolling through emails now! I know..this should have been added in version 1.0, but somehow it was missed. Anyone who uses Gmail on their Droid has to have shared my pain in this. Also, there's a button on the inbox screen that allows you to switch to a different email account if you have more than one configured. As far as I'm concerned, the Gmail app is now complete.

3) The update installed a new and up to date version of the official Facebook application. This version provides an icon to access your inbox on Facebook, as well as forward and backward arrows to cycle through messages and pictures. Why the inbox was left out previously, I still don't know.

4) The camera has been noticeably enhanced from 10 fps to 20 fps in the viewfinder window. What this means is that the camera "feels" less sluggish. I haven't taken many pics with it yet, so more on that later.

5) Manually typing in "smileys" in any sort of text is less of a pain now! I know..small gripe, but for some reason someone decided that all colons (:) were meant to rest right up against the letter previous to them. This was an auto-correct feature that I never quite figured out how to disable on my phone (I actually didn't try *that* hard, but still). This behavior is gone now it seems.

6) Tethering! I have the option..haven't tried to use it, but hey, I have the option!

7) Weird sound problems fixed. I think this was a specific problem with my phone, but my phone started to develop really strange audio problems. Music stored on my device (including ringtones and sound effects) started sounding very distorted at times, and almost seemed like things were moving in slow motion. (Now that I think of it...a reboot could have fixed the problem...)


Those are the pros I've found so far that directly affect me. Here are some of the cons:

1) My Microsoft VPN still doesn't work reliably. We use 128 bit encryption at work, and have discovered that for some reason, the higher the encryption, the less likely the vpn is to work. Even at lower encryption levels the vpn is intermittent. So I say boo to this. One more reason to root the phone and install openvpn...

2) Flash is s-l-o-w. I installed the flash app, and attempted to watch a clip on abc.com. It wasn't very watchable. Not a big deal...I don't *need* flash, but still.

3) Home screen still doesn't auto-rotate when the phone is flipped sideways. I realize I can slide the keyboard out and get that behavior, but that's not what I want. I mainly use the on-screen keyboard when I'm using the phone. I'd like to be able to turn the phone sideways and just use the touch screen to interact with the home screen. The "auto-rotate" flag isn't doing it's job properly in my opinion.

4) No hotspot. :( I know this was announced, but I'm still sad to see it's not present on my Droid. I may root my phone yet.

That's all I have so far. I may find other goodies soon, but my first impression is that it's a great move in the right direction for the Android operating system on these phones.

Waverly