Merge remote branch 'origin/master' into some-docs

This commit is contained in:
Chris Ruppel 2013-01-26 22:37:27 -06:00
commit 01322d00cd
6 changed files with 209 additions and 81 deletions

View file

@ -97,7 +97,7 @@ the Bukkit ScriptCraft plugin...
* `__plugin` - the ScriptCraft Plugin itself. This is a useful starting point for accessing other Bukkit objects. The `__plugin` object is of type [org.bukkit.plugin.java.JavaPlugin][api] and all of its properties and methods are accessible. For example... `js __plugin.server.motd` returns the server's message of the day (javascript is more concise than the equivalent java code: __plugin.getServer().getMotd() ).
* `self` - The player/command-block or server console operator who invoked the js command. Again, this is a good jumping off point for diving into the Bukkit API.
* `bukkit` - The top-level Bukkit object. See the [Bukkit API docs][bukapi] for reference.
* `server` - The top-level org.bukkit.Server object. See the [Bukkit API docs][bukapi] for reference.
[dl]: http://walterhiggins.net/blog/files/scriptcraft/
[api]: http://jd.bukkit.org/apidocs/org/bukkit/plugin/java/JavaPlugin.html
@ -107,8 +107,14 @@ the Bukkit ScriptCraft plugin...
Further Reading
===============
* If you want to get started using ScriptCraft to Learn Javascript I recommend [reading this][yp].
* If you want to delve deeper into creating your own minecraft mod, I recommend [reading this][mm].
You can find more information about [ScriptCraft on my blog][blog].
[blog]: http://walterhiggins.net/blog/cat-index-scriptcraft.html
[buk]: https://github.com/walterhiggins/ScriptCraft/blob/master/bukkit.md
[yp]: http://walterhiggins.net/blog/YoungPersonProgrammingMinecraft
[mm]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later

View file

@ -1,73 +1,75 @@
plugin("alias", {
help: function(){
return [
"/jsp alias set <alias> <commands> : Set a shortcut/alias for one or more commands (separated by ';')\n" +
"For example: '/jsp alias set sunny time set 4000; weather clear'\n" +
"/jsp sunny (is the same as..\n/time set 4000\n/weather clear",
"/jsp alias delete <alias> : Removes a shortcut/alias",
"/jsp alias list : shows a list of the player's command aliases",
"/jsp alias help : Shows this message"
];
},
set: function(player, alias, commands){
var aliases = this.store.players;
var name = player.name;
aliases[name] = aliases[name] || {};
aliases[name][alias] = commands;
},
remove: function(player, alias){
var aliases = this.store.players;
if (aliases[player.name])
delete aliases[player.name][alias];
},
list: function(player){
var result = [];
var aliases = this.store.players[player.name];
for (var a in aliases)
result.push(a + " = " + aliases[a].join(";"));
return result;
}
help: function(){
return [
"/jsp alias set <alias> <commands> : Set a shortcut/alias for one or more commands (separated by ';')\n" +
"For example: '/jsp alias set sunny time set 4000; weather clear'\n" +
"/jsp sunny (is the same as..\n/time set 4000\n/weather clear",
"/jsp alias delete <alias> : Removes a shortcut/alias",
"/jsp alias list : shows a list of the player's command aliases",
"/jsp alias help : Shows this message"
];
},
set: function(player, alias, commands){
var aliases = this.store.players;
var name = player.name;
aliases[name] = aliases[name] || {};
aliases[name][alias] = commands;
},
remove: function(player, alias){
var aliases = this.store.players;
if (aliases[player.name])
delete aliases[player.name][alias];
},
list: function(player){
var result = [];
var aliases = this.store.players[player.name];
for (var a in aliases)
result.push(a + " = " + aliases[a].join(";"));
return result;
}
},true);
alias.store.players = alias.store.players || {};
command("alias",function(params){
/*
this function also intercepts command options for /jsp
*/
if (params[0] === "help"){
self.sendMessage(alias.help());
return;
}
if (params[0] === "set"){
var aliasCmd = params[1];
var cmdStr = params.slice(2).join(' ');
var cmds = cmdStr.split(';');
alias.set(self,aliasCmd,cmds);
return;
}
if (params[0] === "delete"){
alias.remove(self,params[1]);
return ;
}
if (params[0] === "list"){
self.sendMessage(alias.list(self));
return;
}
var playerHasAliases = alias.store.players[self.name];
if (!playerHasAliases)
return false;
// is it an alias?
var commands = playerHasAliases[params[0]];
if (!commands)
return false;
for (var i = 0;i < commands.length; i++){
// fill in template
var cmd = commands[i];
cmd = cmd.replace(/{([0-9]*)}/g,function(dummy,index){ return params[index];})
self.performCommand(cmd);
}
return true;
/*
this function also intercepts command options for /jsp
*/
if (params[0] === "help"){
self.sendMessage(alias.help());
return;
}
if (params[0] === "set"){
var aliasCmd = params[1];
var cmdStr = params.slice(2).join(' ');
var cmds = cmdStr.split(';');
alias.set(self,aliasCmd,cmds);
return;
}
if (params[0] === "delete"){
alias.remove(self,params[1]);
return ;
}
if (params[0] === "list"){
self.sendMessage(alias.list(self));
return;
}
if (params.length == 0)
return self.sendMessage(alias.help());
var playerHasAliases = alias.store.players[self.name];
if (!playerHasAliases)
return false;
// is it an alias?
var commands = playerHasAliases[params[0]];
if (!commands)
return false;
for (var i = 0;i < commands.length; i++){
// fill in template
var cmd = commands[i];
cmd = cmd.replace(/{([0-9]*)}/g,function(dummy,index){ return params[index] || "";})
self.performCommand(cmd);
}
return true;
},["help","set","delete","list"],true);

View file

@ -65,9 +65,11 @@ var global = this;
};
var _putSign = function(texts, x, y, z, blockId, meta){
if (blockId != 63 && blockId != 68)
throw new Error("Invalid Parameter: blockId must be 63 or 68");
putBlock(x,y,z,blockId,meta);
var block = _getBlockObject(x,y,z);
state = block.state;
var state = block.state;
if (state instanceof org.bukkit.block.Sign){
for (var i = 0;i < texts.length; i++)
state.setLine(i%4,texts[i]);

View file

@ -21,9 +21,9 @@
var global = this;
var verbose = verbose || false;
/*
wph 20130124 - make self, plugin and bukkit public - these are far more useful now that tab-complete works.
wph 20130124 - make self, plugin and server public - these are far more useful now that tab-complete works.
*/
var bukkit = org.bukkit.Bukkit;
var server = org.bukkit.Bukkit.server;
//
// private implementation
//
@ -128,7 +128,6 @@ var bukkit = org.bukkit.Bukkit;
Save a javascript object to a file (saves using JSON notation)
*/
var _save = function(object, filename){
print(filename);
var objectToStr = null;
try{
objectToStr = JSON.stringify(object);
@ -175,7 +174,8 @@ var bukkit = org.bukkit.Bukkit;
command management - allow for non-ops to execute approved javascript code.
*/
var _commands = {};
var _command = function(name,func,options,intercepts){
var _command = function(name,func,options,intercepts)
{
if (typeof name == "undefined"){
// it's an invocation from the Java Plugin!
if (__cmdArgs.length === 0)
@ -241,7 +241,7 @@ var bukkit = org.bukkit.Bukkit;
for (var j = 0;j < _javaLangObjectMethods.length; j++)
if (_javaLangObjectMethods[j] == i)
continue propertyLoop;
if (typeof o[i] == "function")
if (typeof o[i] == "function" )
result.push(i+"()");
else
result.push(i);
@ -266,13 +266,37 @@ var bukkit = org.bukkit.Bukkit;
var __onTabCompleteJSP = function() {
var result = global.__onTC_result;
var args = global.__onTC_args;
var cmd = _commands[args[0]];
if (cmd)
for (var i = 0;i < cmd.options.length; i++)
result.add(cmd.options[i]);
else
for (var i in _commands)
result.add(i);
var cmdInput = args[0];
var cmd = _commands[cmdInput];
if (cmd){
var opts = cmd.options;
var len = opts.length;
if (args.length == 1){
for (var i = 0;i < len; i++)
result.add(opts[i]);
}else{
// partial e.g. /jsp chat_color dar
for (var i = 0;i < len; i++){
if (opts[i].indexOf(args[1]) == 0){
result.add(opts[i]);
}
}
}
}else{
if (args.length == 0){
for (var i in _commands)
result.add(i);
}else{
// partial e.g. /jsp al
// should tabcomplete to alias
//
for (var c in _commands){
if (c.indexOf(cmdInput) == 0){
result.add(c);
}
}
}
}
return result;
};
/*

View file

@ -341,7 +341,7 @@ var Drone = Drone || {
}
var bm = _getBlockIdAndMeta(block);
block = bm[0];
meta = bm[1];
var meta = bm[1];
if (block != 63 && block != 68){
print("ERROR: Invalid block id for use in signs");
return;
@ -653,7 +653,7 @@ var Drone = Drone || {
return [parseInt(bs),0];
}
b = parseInt(bs.substring(0,sp));
md = parseInt(bs.substring(sp+1,bs.length));
var md = parseInt(bs.substring(sp+1,bs.length));
return [b,md];
}else{
return [b,0];

View file

@ -0,0 +1,94 @@
load(__folder + "events/events.js");
/*
OK - this is a rough and ready prototype of a simple multi-player shoot-em-up.
Get a bunch of players in close proximity and issue the following commands...
/js var redTeam = ['<player1>','<player2>',...etc]
/js var blueTeam = ['<player3>','<player4>,...etc]
/js var greenTeam = ['<player5>','<player6>,...etc]
/js new SnowBallFight({red: redTeam,blue: blueTeam,green: greenTeam},60).start();
(where <player1> etc are the names of actual players)
You specify the teams in the game as an object where each property's name is a team name and
each property's value is the list of players on that team.
You specify the duration of the game (in seconds)
You kick off the game with the start() method.
I need to work on a better in-game mechanism for players to choose teams and start the game
but this will do for now.
When the game starts, each player is put in survival mode and given 192 snowballs. The aim of the
game is to hit players on opposing teams. If you hit a player on your own team, you lose a point.
At the end of the game the scores for each team are broadcast. Create a small arena
with a couple of small buildings for cover to make the game more fun :-)
*/
var SnowBallFight = function(teams,duration)
{
this.teams = teams;
this.duration = duration;
};
SnowBallFight.prototype.start = function()
{
// put all players in survival mode and give them each 200 snowballs
var snowBalls = new org.bukkit.inventory.ItemStack(org.bukkit.Material.SNOW_BALL, 64);
var teamScores = {};
var gameOver = false;
for (var teamName in this.teams){
teamScores[teamName] = 0;
var team = this.teams[teamName];
for (var i = 0;i < team.length;i++)
{
var player = server.getPlayer(team[i]);
player.gameMode = org.bukkit.GameMode.SURVIVAL;
player.inventory.addItem([snowBalls,snowBalls,snowBalls]);
}
}
var that = this;
var _getTeam = function(player){
for (var teamName in that.teams){
var team = that.teams[teamName];
for (var i = 0;i < team.length; i++){
if (team[i] == player.name)
return teamName;
}
}
return null;
};
var listener = events.on("entity.EntityDamageByEntityEvent",function(l,e){
var damager = e.damager;
var damagee = e.entity;
var damage = e.damage;
var shooter = damager.shooter;
if (damager instanceof org.bukkit.entity.Snowball){
var damagerTeam = _getTeam(shooter);
if (!damagerTeam)
return; // shooter wasn't in game
var damageeTeam = _getTeam(damagee);
if (!damageeTeam)
return; // damagee wasn't in game
if (damagerTeam != damageeTeam){
teamScores[damagerTeam]++;
}else{
teamScores[damagerTeam]--;
}
}
if (gameOver)
e.handlers.unregister(l);
});
var tick = function(){
while (that.duration--){
java.lang.Thread.sleep(1000);
}
if (that.duration <=0){
for (var tn in teamScores){
server.broadcastMessage("Team " + tn + " scored " + teamScores[tn]);
}
gameOver = true;
}
};
new java.lang.Thread(tick).start();
};