Fix classroom module for use with CanaryMod
This commit is contained in:
parent
6cb61c0e95
commit
aae6710427
5 changed files with 51 additions and 37 deletions
|
@ -5316,7 +5316,7 @@ variable named after the player.
|
||||||
|
|
||||||
So for example, if player 'walterh' joins the server, a `walterh`
|
So for example, if player 'walterh' joins the server, a `walterh`
|
||||||
global variable is created. If a file `greet.js` with the following
|
global variable is created. If a file `greet.js` with the following
|
||||||
content is dropped into the `plugins/scriptcraft/players/walterh`
|
content is dropped into the `scriptcraft/players/walterh`
|
||||||
directory...
|
directory...
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
@ -5330,7 +5330,7 @@ lets every player/student create their own functions without having
|
||||||
naming collisions.
|
naming collisions.
|
||||||
|
|
||||||
It's strongly recommended that the
|
It's strongly recommended that the
|
||||||
`craftbukkit/plugins/scriptcraft/players/` directory is shared so that
|
`scriptcraft/players/` directory is shared so that
|
||||||
others can connect to it and drop .js files into their student
|
others can connect to it and drop .js files into their student
|
||||||
directories. On Ubuntu, select the folder in Nautilus (the default
|
directories. On Ubuntu, select the folder in Nautilus (the default
|
||||||
file browser) then right-click and choose *Sharing Options*, check the
|
file browser) then right-click and choose *Sharing Options*, check the
|
||||||
|
|
|
@ -113,7 +113,7 @@ public class ScriptCraftPlugin extends Plugin implements PluginListener, Command
|
||||||
@Command(
|
@Command(
|
||||||
aliases = { "js" },
|
aliases = { "js" },
|
||||||
description = "Execute Javascript code",
|
description = "Execute Javascript code",
|
||||||
permissions = { "scriptcraft.evaluate", "*" },
|
permissions = { "scriptcraft.evaluate" },
|
||||||
toolTip = "/js javascript expression")
|
toolTip = "/js javascript expression")
|
||||||
public void jsCommand(MessageReceiver sender, String[] args) {
|
public void jsCommand(MessageReceiver sender, String[] args) {
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPer
|
||||||
|
|
||||||
exports.plugin = _plugin;
|
exports.plugin = _plugin;
|
||||||
|
|
||||||
exports.autoload = function( context, pluginDir, logger, options ) {
|
exports.autoload = function( context, pluginDir, options ) {
|
||||||
var _canonize = function( file ) {
|
var _canonize = function( file ) {
|
||||||
return '' + file.canonicalPath.replaceAll('\\\\','/');
|
return '' + file.canonicalPath.replaceAll('\\\\','/');
|
||||||
};
|
};
|
||||||
|
@ -66,7 +66,7 @@ exports.autoload = function( context, pluginDir, logger, options ) {
|
||||||
|
|
||||||
var len = sourceFiles.length;
|
var len = sourceFiles.length;
|
||||||
if ( config && config.verbose ) {
|
if ( config && config.verbose ) {
|
||||||
logger.info( len + ' scriptcraft plugins found in ' + pluginDir );
|
console.info( len + ' scriptcraft plugins found in ' + pluginDir );
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( var i = 0; i < len; i++ ) {
|
for ( var i = 0; i < len; i++ ) {
|
||||||
|
@ -83,12 +83,8 @@ exports.autoload = function( context, pluginDir, logger, options ) {
|
||||||
context[property] = module[property];
|
context[property] = module[property];
|
||||||
}
|
}
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
if ( typeof logger != 'undefined' ) {
|
|
||||||
var msg = 'Plugin ' + pluginPath + ' ' + e ;
|
var msg = 'Plugin ' + pluginPath + ' ' + e ;
|
||||||
__plugin.canary ? logger.error( msg ) : logger.severe( msg );
|
console.error( msg );
|
||||||
} else {
|
|
||||||
java.lang.System.out.println( 'Error: Plugin ' + pluginPath + ' ' + e );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(pluginDir));
|
}(pluginDir));
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
/*global require*/
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
|
|
||||||
## Modules in Scriptcraft
|
## Modules in Scriptcraft
|
||||||
|
@ -764,6 +765,6 @@ function __onEnable ( __engine, __plugin, __script ) {
|
||||||
}
|
}
|
||||||
__onDisableImpl = _onDisable;
|
__onDisableImpl = _onDisable;
|
||||||
global.__onCommand = __onCommand;
|
global.__onCommand = __onCommand;
|
||||||
plugins.autoload( global, new File(jsPluginsRootDir,'plugins'), logger );
|
plugins.autoload( global, new File(jsPluginsRootDir,'plugins') );
|
||||||
require('legacy-check')(jsPluginsRootDir);
|
require('legacy-check')(jsPluginsRootDir);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
'use strict';
|
||||||
|
/*global require, exports, __plugin, __dirname, echo, persist, isOp, events, Packages */
|
||||||
var utils = require('utils'),
|
var utils = require('utils'),
|
||||||
autoload = require('plugin').autoload,
|
autoload = require('plugin').autoload,
|
||||||
logger = __plugin.logger,
|
|
||||||
foreach = utils.foreach,
|
foreach = utils.foreach,
|
||||||
watchDir = utils.watchDir,
|
watchDir = utils.watchDir,
|
||||||
unwatchDir = utils.unwatchDir,
|
unwatchDir = utils.unwatchDir,
|
||||||
|
@ -30,7 +31,7 @@ variable named after the player.
|
||||||
|
|
||||||
So for example, if player 'walterh' joins the server, a `walterh`
|
So for example, if player 'walterh' joins the server, a `walterh`
|
||||||
global variable is created. If a file `greet.js` with the following
|
global variable is created. If a file `greet.js` with the following
|
||||||
content is dropped into the `plugins/scriptcraft/players/walterh`
|
content is dropped into the `scriptcraft/players/walterh`
|
||||||
directory...
|
directory...
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
@ -44,7 +45,7 @@ lets every player/student create their own functions without having
|
||||||
naming collisions.
|
naming collisions.
|
||||||
|
|
||||||
It's strongly recommended that the
|
It's strongly recommended that the
|
||||||
`craftbukkit/plugins/scriptcraft/players/` directory is shared so that
|
`scriptcraft/players/` directory is shared so that
|
||||||
others can connect to it and drop .js files into their student
|
others can connect to it and drop .js files into their student
|
||||||
directories. On Ubuntu, select the folder in Nautilus (the default
|
directories. On Ubuntu, select the folder in Nautilus (the default
|
||||||
file browser) then right-click and choose *Sharing Options*, check the
|
file browser) then right-click and choose *Sharing Options*, check the
|
||||||
|
@ -90,10 +91,11 @@ Only ops users can run the classroom.allowScripting() function - this is so that
|
||||||
don't try to bar themselves and each other from scripting.
|
don't try to bar themselves and each other from scripting.
|
||||||
|
|
||||||
***/
|
***/
|
||||||
var _store = { enableScripting: false },
|
var store = persist('classroom', { enableScripting: false }),
|
||||||
File = java.io.File;
|
File = java.io.File;
|
||||||
|
|
||||||
function revokeScripting ( player ) {
|
function revokeScripting ( player ) {
|
||||||
|
if (__plugin.bukkit){
|
||||||
foreach( player.getEffectivePermissions(), function( perm ) {
|
foreach( player.getEffectivePermissions(), function( perm ) {
|
||||||
if ( (''+perm.permission).indexOf( 'scriptcraft.' ) == 0 ) {
|
if ( (''+perm.permission).indexOf( 'scriptcraft.' ) == 0 ) {
|
||||||
if ( perm.attachment ) {
|
if ( perm.attachment ) {
|
||||||
|
@ -101,31 +103,44 @@ function revokeScripting ( player ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
if (__plugin.canary){
|
||||||
|
//
|
||||||
|
var Canary = Packages.net.canarymod.Canary;
|
||||||
|
Canary.permissionManager().removePlayerPermission('scriptcraft.evaluate',player);
|
||||||
|
}
|
||||||
var playerName = '' + player.name;
|
var playerName = '' + player.name;
|
||||||
playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,'');
|
playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,'');
|
||||||
var playerDir = new File( playersDir + playerName );
|
var playerDir = new File( playersDir + playerName );
|
||||||
unwatchDir( playerDir );
|
unwatchDir( playerDir );
|
||||||
}
|
}
|
||||||
exports.classroomAutoloadTime = {};
|
var classroomAutoloadTime = {};
|
||||||
|
exports.classroomAutoloadTime = classroomAutoloadTime;
|
||||||
|
|
||||||
function grantScripting( player ) {
|
function grantScripting( player ) {
|
||||||
console.log('Enabling scripting for player ' + player.name);
|
console.log('Enabling scripting for player ' + player.name);
|
||||||
var playerName = '' + player.name;
|
var playerName = '' + player.name;
|
||||||
playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,'');
|
playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,'');
|
||||||
var playerDir = new File( playersDir + playerName );
|
var playerDir = new File( playersDir + playerName );
|
||||||
playerDir.mkdirs();
|
playerDir.mkdirs();
|
||||||
|
if (__plugin.bukkit){
|
||||||
player.addAttachment( __plugin, 'scriptcraft.*', true );
|
player.addAttachment( __plugin, 'scriptcraft.*', true );
|
||||||
|
}
|
||||||
|
if (__plugin.canary){
|
||||||
|
player.permissionProvider.addPermission('scriptcraft.evaluate',true);
|
||||||
|
}
|
||||||
var playerContext = {};
|
var playerContext = {};
|
||||||
autoload( playerContext, playerDir, logger, { cache: false });
|
autoload( playerContext, playerDir, { cache: false });
|
||||||
global[playerName] = playerContext;
|
global[playerName] = playerContext;
|
||||||
watchDir( playerDir, function( changedDir ){
|
watchDir( playerDir, function( changedDir ){
|
||||||
var currentTime = new java.util.Date().getTime();
|
var currentTime = new java.util.Date().getTime();
|
||||||
//this check is here because this callback might get called multiple times for the watch interval
|
//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
|
//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)
|
//(this happens only in Linux because in Windows the folder lastModifiedTime is not changed)
|
||||||
if(currentTime-exports.classroomAutoloadTime[playerName]>1000) {
|
if (currentTime - classroomAutoloadTime[playerName]>1000 ) {
|
||||||
autoload(playerContext, playerDir, logger, { cache: false });
|
autoload(playerContext, playerDir, { cache: false });
|
||||||
}
|
}
|
||||||
exports.classroomAutoloadTime[playerName] = currentTime;
|
classroomAutoloadTime[playerName] = currentTime;
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -137,7 +152,7 @@ function grantScripting( player ) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var classroom = plugin('classroom', {
|
var classroom = {
|
||||||
allowScripting: function (/* boolean: true or false */ canScript, sender ) {
|
allowScripting: function (/* boolean: true or false */ canScript, sender ) {
|
||||||
sender = utils.player(sender);
|
sender = utils.player(sender);
|
||||||
if ( !sender ) {
|
if ( !sender ) {
|
||||||
|
@ -153,26 +168,28 @@ var classroom = plugin('classroom', {
|
||||||
echo( sender, 'Only operators can use this function');
|
echo( sender, 'Only operators can use this function');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach( server.onlinePlayers, canScript ? grantScripting : revokeScripting);
|
foreach( utils.players(), function(player){
|
||||||
_store.enableScripting = canScript;
|
if (!isOp(player)){
|
||||||
|
canScript ? grantScripting(player) : revokeScripting(player);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
store.enableScripting = canScript;
|
||||||
|
|
||||||
echo( sender, 'Scripting turned ' + ( canScript ? 'on' : 'off' ) +
|
echo( sender, 'Scripting turned ' + ( canScript ? 'on' : 'off' ) +
|
||||||
' for all players on server ' + serverAddress);
|
' for all players on server ' + serverAddress);
|
||||||
},
|
}
|
||||||
store: _store
|
};
|
||||||
}, true);
|
|
||||||
|
|
||||||
exports.classroom = classroom;
|
exports.classroom = classroom;
|
||||||
|
|
||||||
if (__plugin.canary){
|
if (__plugin.canary){
|
||||||
events.connection( function( event ) {
|
events.connection( function( event ) {
|
||||||
if ( _store.enableScripting ) {
|
if ( store.enableScripting ) {
|
||||||
grantScripting(event.player);
|
grantScripting(event.player);
|
||||||
}
|
}
|
||||||
}, 'CRITICAL');
|
}, 'CRITICAL');
|
||||||
} else {
|
} else {
|
||||||
events.playerJoin( function( event ) {
|
events.playerJoin( function( event ) {
|
||||||
if ( _store.enableScripting ) {
|
if ( store.enableScripting ) {
|
||||||
grantScripting(event.player);
|
grantScripting(event.player);
|
||||||
}
|
}
|
||||||
}, 'HIGHEST');
|
}, 'HIGHEST');
|
||||||
|
|
Reference in a new issue