2014-02-10 21:55:32 +01:00
|
|
|
var utils = require('utils'),
|
|
|
|
autoload = require('plugin').autoload,
|
|
|
|
logger = __plugin.logger,
|
|
|
|
foreach = utils.foreach,
|
2014-06-28 14:32:55 +02:00
|
|
|
watchDir = utils.watchDir,
|
|
|
|
unwatchDir = utils.unwatchDir,
|
2014-02-10 21:55:32 +01:00
|
|
|
playersDir = __dirname + '/../../players/',
|
|
|
|
serverAddress = utils.serverAddress();
|
2013-12-24 01:18:43 +01:00
|
|
|
|
2013-06-07 01:01:46 +02:00
|
|
|
/************************************************************************
|
2014-01-04 19:39:49 +01:00
|
|
|
## Classroom Plugin
|
2013-12-28 09:44:40 +01:00
|
|
|
|
2013-06-07 20:50:12 +02:00
|
|
|
The `classroom` object contains a couple of utility functions for use
|
|
|
|
in a classroom setting. The goal of these functions is to make it
|
|
|
|
easier for tutors to facilitate ScriptCraft for use by students in a
|
|
|
|
classroom environment. Although granting ScriptCraft access to
|
|
|
|
students on a shared server is potentially risky (Students can
|
|
|
|
potentially abuse it), it is slighlty less risky than granting
|
|
|
|
operator privileges to each student. (Enterprising students will
|
|
|
|
quickly realise how to grant themselves and others operator privileges
|
|
|
|
once they have access to ScriptCraft).
|
|
|
|
|
|
|
|
The goal of this module is not so much to enforce restrictions
|
2014-02-10 21:55:32 +01:00
|
|
|
(security or otherwise) but to make it easier for tutors to setup a
|
|
|
|
shared server so students can learn Javascript. When scripting is
|
|
|
|
turned on, every player who joins the server will have a dedicated
|
|
|
|
directory into which they can save scripts. All scripts in such
|
|
|
|
directories are automatically watched and loaded into a global
|
|
|
|
variable named after the player.
|
|
|
|
|
|
|
|
So for example, if player 'walterh' joins the server, a `walterh`
|
|
|
|
global variable is created. If a file `greet.js` with the following
|
|
|
|
content is dropped into the `plugins/scriptcraft/players/walterh`
|
|
|
|
directory...
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
exports.hi = function( player ){
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Hi ' + player.name);
|
2014-02-10 21:55:32 +01:00
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
... then it can be invoked like this: `/js walterh.hi( self )` . This
|
|
|
|
lets every player/student create their own functions without having
|
|
|
|
naming collisions.
|
|
|
|
|
|
|
|
It's strongly recommended that the
|
|
|
|
`craftbukkit/plugins/scriptcraft/players/` directory is shared so that
|
|
|
|
others can connect to it and drop .js files into their student
|
|
|
|
directories. On Ubuntu, select the folder in Nautilus (the default
|
|
|
|
file browser) then right-click and choose *Sharing Options*, check the
|
|
|
|
*Share this folder* checkbox and the *Allow others to create and
|
|
|
|
delete files* and *Guest access* checkboxes. Click *Create Share*
|
|
|
|
button to close the sharing options dialog. Students can then access
|
|
|
|
the shared folder as follows...
|
|
|
|
|
|
|
|
* Windows: Open Explorer, Go to \\{serverAddress}\players\
|
|
|
|
* Macintosh: Open Finder, Go to smb://{serverAddress}/players/
|
|
|
|
* Linux: Open Nautilus, Go to smb://{serverAddress}/players/
|
|
|
|
|
|
|
|
... where {serverAddress} is the ip address of the server (this is
|
|
|
|
displayed to whoever invokes the classroom.allowScripting() function.)
|
2013-06-07 01:01:46 +02:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
### classroom.allowScripting() function
|
|
|
|
|
2013-06-07 01:01:46 +02:00
|
|
|
Allow or disallow anyone who connects to the server (or is already
|
|
|
|
connected) to use ScriptCraft. This function is preferable to granting 'ops' privileges
|
|
|
|
to every student in a Minecraft classroom environment.
|
|
|
|
|
2014-02-10 21:55:32 +01:00
|
|
|
Whenever any file is added/edited or removed from any of the players/
|
|
|
|
directories the contents are automatically reloaded. This is to
|
|
|
|
facilitate quick turnaround time for students getting to grips with
|
|
|
|
Javascript.
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Parameters
|
2013-06-07 01:01:46 +02:00
|
|
|
|
|
|
|
* canScript : true or false
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Example
|
|
|
|
|
2013-06-07 01:01:46 +02:00
|
|
|
To allow all players (and any players who connect to the server) to
|
|
|
|
use the `js` and `jsp` commands...
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
/js classroom.allowScripting( true, self )
|
2013-06-07 01:01:46 +02:00
|
|
|
|
|
|
|
To disallow scripting (and prevent players who join the server from using the commands)...
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
/js classroom.allowScripting( false, self )
|
2013-06-07 01:01:46 +02:00
|
|
|
|
|
|
|
Only ops users can run the classroom.allowScripting() function - this is so that students
|
|
|
|
don't try to bar themselves and each other from scripting.
|
|
|
|
|
|
|
|
***/
|
2014-02-10 21:55:32 +01:00
|
|
|
var _store = { enableScripting: false },
|
|
|
|
File = java.io.File;
|
|
|
|
|
|
|
|
function revokeScripting ( player ) {
|
|
|
|
foreach( player.getEffectivePermissions(), function( perm ) {
|
|
|
|
if ( (''+perm.permission).indexOf( 'scriptcraft.' ) == 0 ) {
|
|
|
|
if ( perm.attachment ) {
|
|
|
|
perm.attachment.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-02-11 22:10:56 +01:00
|
|
|
var playerName = '' + player.name;
|
|
|
|
playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,'');
|
|
|
|
var playerDir = new File( playersDir + playerName );
|
2014-06-28 14:32:55 +02:00
|
|
|
unwatchDir( playerDir );
|
2014-02-10 21:55:32 +01:00
|
|
|
}
|
2014-06-28 15:02:14 +02:00
|
|
|
exports.classroomAutoloadTime = {};
|
2014-02-10 21:55:32 +01:00
|
|
|
function grantScripting( player ) {
|
|
|
|
console.log('Enabling scripting for player ' + player.name);
|
|
|
|
var playerName = '' + player.name;
|
|
|
|
playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,'');
|
|
|
|
var playerDir = new File( playersDir + playerName );
|
|
|
|
playerDir.mkdirs();
|
|
|
|
player.addAttachment( __plugin, 'scriptcraft.*', true );
|
|
|
|
var playerContext = {};
|
|
|
|
autoload( playerContext, playerDir, logger, { cache: false });
|
|
|
|
global[playerName] = playerContext;
|
2014-06-28 14:32:55 +02:00
|
|
|
watchDir( playerDir, function( changedDir ){
|
|
|
|
var currentTime = new java.util.Date().getTime();
|
|
|
|
//this check is here because this callback might get called multiple times for the watch interval
|
|
|
|
//one call for the file change and another for directory change
|
|
|
|
//(this happens only in Linux because in Windows the folder lastModifiedTime is not changed)
|
2014-06-28 15:02:14 +02:00
|
|
|
if(currentTime-exports.classroomAutoloadTime[playerName]>1000) {
|
2014-06-28 14:32:55 +02:00
|
|
|
autoload(playerContext, playerDir, logger, { cache: false });
|
|
|
|
}
|
2014-06-28 15:02:14 +02:00
|
|
|
exports.classroomAutoloadTime[playerName] = currentTime;
|
2014-02-10 21:55:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Create your own minecraft mods by adding javascript (.js) files');
|
|
|
|
echo( player, ' Windows: Open Explorer, go to \\\\' + serverAddress + '\\players\\' + player.name);
|
|
|
|
echo( player, ' Macintosh: Open Finder, Go to smb://' + serverAddress + '/players/' + player.name);
|
|
|
|
echo( player, ' Linux: Open Nautilus, Go to smb://' + serverAddress + '/players/' + player.name);
|
2014-02-10 21:55:32 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var classroom = plugin('classroom', {
|
|
|
|
allowScripting: function (/* boolean: true or false */ canScript, sender ) {
|
2014-02-10 21:55:32 +01:00
|
|
|
sender = utils.player(sender);
|
|
|
|
if ( !sender ) {
|
2014-01-29 20:49:15 +01:00
|
|
|
console.log( 'Attempt to set classroom scripting without credentials' );
|
|
|
|
console.log( 'classroom.allowScripting(boolean, sender)' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
only operators should be allowed run this function
|
|
|
|
*/
|
2014-12-22 16:23:06 +01:00
|
|
|
if ( !isOp(sender) ) {
|
2014-02-10 21:55:32 +01:00
|
|
|
console.log( 'Attempt to set classroom scripting without credentials: ' + sender.name );
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( sender, 'Only operators can use this function');
|
2014-01-29 20:49:15 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-02-10 21:55:32 +01:00
|
|
|
foreach( server.onlinePlayers, canScript ? grantScripting : revokeScripting);
|
2014-01-29 20:49:15 +01:00
|
|
|
_store.enableScripting = canScript;
|
2014-02-10 21:55:32 +01:00
|
|
|
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( sender, 'Scripting turned ' + ( canScript ? 'on' : 'off' ) +
|
2014-02-10 21:55:32 +01:00
|
|
|
' for all players on server ' + serverAddress);
|
2014-01-29 20:49:15 +01:00
|
|
|
},
|
|
|
|
store: _store
|
2013-12-24 23:47:57 +01:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
exports.classroom = classroom;
|
|
|
|
|
2014-09-30 00:42:41 +02:00
|
|
|
if (__plugin.canary){
|
|
|
|
events.connection( function( event ) {
|
|
|
|
if ( _store.enableScripting ) {
|
|
|
|
grantScripting(event.player);
|
|
|
|
}
|
|
|
|
}, 'CRITICAL');
|
|
|
|
} else {
|
|
|
|
events.playerJoin( function( event ) {
|
|
|
|
if ( _store.enableScripting ) {
|
|
|
|
grantScripting(event.player);
|
|
|
|
}
|
|
|
|
}, 'HIGHEST');
|
|
|
|
}
|