Minecraft Programming – Listening to Events

Standard

Today we learned how to listen to several of events in Minecraft. Scriptcraft has library of events, so we can create listeners to responds to them.

Last week kids were going crazy inside Minecraft, they killed each other or destroy others’ buildings. Here is a code to count how many times a player breaking blocks:

var breaks = {};

/*
  every time a player joins the game reset their block-break-count to 0
*/
function initializeBreakCount( event ){
 breaks[event.player.name] = 0;     
}
events.connection( initializeBreakCount );

/* 
  every time a player breaks a block increase their block-break-count
*/
function incrementBreakCount( event ){
  breaks[event.player.name] += 1; // add 1
  var breakCount = breaks[event.player.name];
  echo( event.player, 'You broke ' + breakCount + ' blocks');
}
events.blockDestroy( incrementBreakCount );

And this one is a code to count how many times a player placing blocks:

var place = {};

/*
  every time a player joins the game reset their block-place-count to 0
*/
function initializePlaceCount( event ){
  place[event.player.name] = 0;     
}
events.connection( initializePlaceCount );

/* 
  every time a player places a block increase their block-place-count
*/
function incrementPlaceCount( event ){
  place[event.player.name] += 1; // add 1
  var placeCount = place[event.player.name];
  echo( event.player, 'You place ' + placeCount + ' blocks');
}
events.blockPlace( incrementPlaceCount );

These are how the players get messages about their breaking and placing activities

Tangkapan layar 2015-04-25 19.53.45
Tangkapan layar 2015-04-25 19.54.04

var count = {};

The code below combine two codes before so that for each breaking a player will gets the score decrease and got an increase for each placing:

/*
  every time a player joins the game reset their block-place-count to 0
*/
function initializeCount( event ){
  count[event.player.name] = 0;     
}
events.connection( initializeCount );

/* 
  every time a player places a block increase their block-place-count
*/
function incrementCount( event ){
  count[event.player.name] += 1; // add 1
  var theCount = count[event.player.name];
  echo( event.player, 'Your score ' + theCount + ', you have just place a block');
}
events.blockPlace( incrementCount );

/* 
  every time a player breaks a block increase their block-break-count
*/
function decrementCount( event ){
  count[event.player.name] -= 1; // minus 1
  var theCount = count[event.player.name];
  echo( event.player, 'Your score ' + theCount + ', you have just break a block');
}

events.blockDestroy( decrementCount );

Tangkapan layar 2015-04-25 19.48.02
Tangkapan layar 2015-04-25 19.48.37

It worked to encourage the kids to build more buildings, but for destroying they were using TNT instead, and oh, the killing each other was still going on so the codes really needs some improvements!
And lots of reporting to the server below to help the admin anticipate even the mildest act of vandalism 😀

Bildschirmfoto 2015-04-25 um 9.20.45 vorm.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s