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`
|
||||
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...
|
||||
|
||||
```javascript
|
||||
|
@ -5330,7 +5330,7 @@ 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
|
||||
`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
|
||||
|
|
|
@ -113,7 +113,7 @@ public class ScriptCraftPlugin extends Plugin implements PluginListener, Command
|
|||
@Command(
|
||||
aliases = { "js" },
|
||||
description = "Execute Javascript code",
|
||||
permissions = { "scriptcraft.evaluate", "*" },
|
||||
permissions = { "scriptcraft.evaluate" },
|
||||
toolTip = "/js javascript expression")
|
||||
public void jsCommand(MessageReceiver sender, String[] args) {
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPer
|
|||
|
||||
exports.plugin = _plugin;
|
||||
|
||||
exports.autoload = function( context, pluginDir, logger, options ) {
|
||||
exports.autoload = function( context, pluginDir, options ) {
|
||||
var _canonize = function( file ) {
|
||||
return '' + file.canonicalPath.replaceAll('\\\\','/');
|
||||
};
|
||||
|
@ -66,7 +66,7 @@ exports.autoload = function( context, pluginDir, logger, options ) {
|
|||
|
||||
var len = sourceFiles.length;
|
||||
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++ ) {
|
||||
|
@ -83,12 +83,8 @@ exports.autoload = function( context, pluginDir, logger, options ) {
|
|||
context[property] = module[property];
|
||||
}
|
||||
} catch ( e ) {
|
||||
if ( typeof logger != 'undefined' ) {
|
||||
var msg = 'Plugin ' + pluginPath + ' ' + e ;
|
||||
__plugin.canary ? logger.error( msg ) : logger.severe( msg );
|
||||
} else {
|
||||
java.lang.System.out.println( 'Error: Plugin ' + pluginPath + ' ' + e );
|
||||
}
|
||||
var msg = 'Plugin ' + pluginPath + ' ' + e ;
|
||||
console.error( msg );
|
||||
}
|
||||
}
|
||||
}(pluginDir));
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
'use strict';
|
||||
/*global require*/
|
||||
/************************************************************************
|
||||
|
||||
## Modules in Scriptcraft
|
||||
|
@ -764,6 +765,6 @@ function __onEnable ( __engine, __plugin, __script ) {
|
|||
}
|
||||
__onDisableImpl = _onDisable;
|
||||
global.__onCommand = __onCommand;
|
||||
plugins.autoload( global, new File(jsPluginsRootDir,'plugins'), logger );
|
||||
plugins.autoload( global, new File(jsPluginsRootDir,'plugins') );
|
||||
require('legacy-check')(jsPluginsRootDir);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
'use strict';
|
||||
/*global require, exports, __plugin, __dirname, echo, persist, isOp, events, Packages */
|
||||
var utils = require('utils'),
|
||||
autoload = require('plugin').autoload,
|
||||
logger = __plugin.logger,
|
||||
foreach = utils.foreach,
|
||||
watchDir = utils.watchDir,
|
||||
unwatchDir = utils.unwatchDir,
|
||||
|
@ -30,7 +31,7 @@ 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`
|
||||
content is dropped into the `scriptcraft/players/walterh`
|
||||
directory...
|
||||
|
||||
```javascript
|
||||
|
@ -44,7 +45,7 @@ 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
|
||||
`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
|
||||
|
@ -90,42 +91,56 @@ Only ops users can run the classroom.allowScripting() function - this is so that
|
|||
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;
|
||||
|
||||
function revokeScripting ( player ) {
|
||||
foreach( player.getEffectivePermissions(), function( perm ) {
|
||||
if ( (''+perm.permission).indexOf( 'scriptcraft.' ) == 0 ) {
|
||||
if ( perm.attachment ) {
|
||||
perm.attachment.remove();
|
||||
if (__plugin.bukkit){
|
||||
foreach( player.getEffectivePermissions(), function( perm ) {
|
||||
if ( (''+perm.permission).indexOf( 'scriptcraft.' ) == 0 ) {
|
||||
if ( perm.attachment ) {
|
||||
perm.attachment.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
if (__plugin.canary){
|
||||
//
|
||||
var Canary = Packages.net.canarymod.Canary;
|
||||
Canary.permissionManager().removePlayerPermission('scriptcraft.evaluate',player);
|
||||
}
|
||||
var playerName = '' + player.name;
|
||||
playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,'');
|
||||
var playerDir = new File( playersDir + playerName );
|
||||
unwatchDir( playerDir );
|
||||
}
|
||||
exports.classroomAutoloadTime = {};
|
||||
var classroomAutoloadTime = {};
|
||||
exports.classroomAutoloadTime = classroomAutoloadTime;
|
||||
|
||||
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 );
|
||||
if (__plugin.bukkit){
|
||||
player.addAttachment( __plugin, 'scriptcraft.*', true );
|
||||
}
|
||||
if (__plugin.canary){
|
||||
player.permissionProvider.addPermission('scriptcraft.evaluate',true);
|
||||
}
|
||||
var playerContext = {};
|
||||
autoload( playerContext, playerDir, logger, { cache: false });
|
||||
autoload( playerContext, playerDir, { cache: false });
|
||||
global[playerName] = playerContext;
|
||||
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)
|
||||
if(currentTime-exports.classroomAutoloadTime[playerName]>1000) {
|
||||
autoload(playerContext, playerDir, logger, { cache: false });
|
||||
if (currentTime - classroomAutoloadTime[playerName]>1000 ) {
|
||||
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 ) {
|
||||
sender = utils.player(sender);
|
||||
if ( !sender ) {
|
||||
|
@ -153,26 +168,28 @@ var classroom = plugin('classroom', {
|
|||
echo( sender, 'Only operators can use this function');
|
||||
return;
|
||||
}
|
||||
foreach( server.onlinePlayers, canScript ? grantScripting : revokeScripting);
|
||||
_store.enableScripting = canScript;
|
||||
foreach( utils.players(), function(player){
|
||||
if (!isOp(player)){
|
||||
canScript ? grantScripting(player) : revokeScripting(player);
|
||||
}
|
||||
});
|
||||
store.enableScripting = canScript;
|
||||
|
||||
echo( sender, 'Scripting turned ' + ( canScript ? 'on' : 'off' ) +
|
||||
' for all players on server ' + serverAddress);
|
||||
},
|
||||
store: _store
|
||||
}, true);
|
||||
|
||||
}
|
||||
};
|
||||
exports.classroom = classroom;
|
||||
|
||||
if (__plugin.canary){
|
||||
events.connection( function( event ) {
|
||||
if ( _store.enableScripting ) {
|
||||
if ( store.enableScripting ) {
|
||||
grantScripting(event.player);
|
||||
}
|
||||
}, 'CRITICAL');
|
||||
} else {
|
||||
events.playerJoin( function( event ) {
|
||||
if ( _store.enableScripting ) {
|
||||
if ( store.enableScripting ) {
|
||||
grantScripting(event.player);
|
||||
}
|
||||
}, 'HIGHEST');
|
||||
|
|
Reference in a new issue