This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/main/javascript/core/_primitives.js

136 lines
4 KiB
JavaScript
Raw Normal View History

2013-01-19 18:17:52 +01:00
var global = this;
2013-01-08 00:59:42 +01:00
//
2013-01-27 05:37:07 +01:00
// Define these primitive methods used by drone.js (and potentially others)
//
// getPlayerPos
// returns the player's x,y,z and yaw (direction)
//
// getMousePos
// returns the x,y,z of the current block being targeted.
//
// putBlock(x,y,z,blockId,metadata)
// puts a block at a location in current world
//
// getBlock(x,y,z)
// gets the block and metadata (returned as a string in form '35:15')
//
// putSign(texts,x,y,z,blockId,metadata)
// puts a sign at the given location
//
// notifyAdministrators(msg)
// sends a message to all admins/ops.
//
// echo(msg)
// prints a message on screen to current user.
2013-01-08 00:59:42 +01:00
//
(function(){
2013-01-18 00:28:12 +01:00
//
// only execute once
//
if (typeof getPlayerPos != "undefined"){
return;
}
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getPlayerPos = function(){
2013-01-25 00:47:36 +01:00
if (typeof self == "undefined")
2013-01-18 00:28:12 +01:00
return;
2013-01-25 00:47:36 +01:00
return self.location;
2013-01-18 00:28:12 +01:00
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getMousePos = function(){
2013-01-25 00:47:36 +01:00
if (typeof self == "undefined")
2013-01-18 00:28:12 +01:00
return;
2013-01-25 00:47:36 +01:00
// self might be CONSOLE or a CommandBlock
if (!self.getTargetBlock)
2013-01-18 00:28:12 +01:00
return;
2013-01-25 00:47:36 +01:00
var targetedBlock = self.getTargetBlock(null,5);
2013-01-18 00:28:12 +01:00
if (targetedBlock == null || targetedBlock.isEmpty()){
return null;
}
return targetedBlock.location;
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _putBlock = function(x,y,z,blockId,metadata){
2013-01-20 02:29:43 +01:00
if (typeof metadata == "undefined")
2013-01-18 00:28:12 +01:00
metadata = 0;
var pl = org.bukkit.entity.Player;
var cs = org.bukkit.command.BlockCommandSender;
2013-01-25 00:47:36 +01:00
var world = (self instanceof pl)?self.location.world:(self instanceof cs)?self.block.location.world:null;
2013-01-20 02:29:43 +01:00
var block = world.getBlockAt(x,y,z);
if (block.typeId != blockId || block.data != metadata)
block.setTypeIdAndData(blockId,metadata,false);
2013-01-18 00:28:12 +01:00
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
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");
2013-01-18 00:28:12 +01:00
putBlock(x,y,z,blockId,meta);
var block = _getBlockObject(x,y,z);
2013-01-26 14:47:16 +01:00
var state = block.state;
2013-01-18 00:28:12 +01:00
if (state instanceof org.bukkit.block.Sign){
for (var i = 0;i < texts.length; i++)
state.setLine(i%4,texts[i]);
state.update(true);
}
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getBlock = function(x,y,z){
var block = _getBlockObject(x,y,z);
if (block)
return "" + block.typeId + ":" + block.data;
2013-01-25 01:07:39 +01:00
2013-01-18 00:28:12 +01:00
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getBlockObject = function(x,y,z){
var world = _getWorld();
2013-01-25 01:07:39 +01:00
if (world){
if (typeof z == "undefined"){
var loc = _getMousePos()
if (loc)
return world.getBlockAt(loc);
}else{
return world.getBlockAt(x,y,z);
}
}
2013-01-18 00:28:12 +01:00
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getWorld = function(){
2013-01-25 00:47:36 +01:00
if (self instanceof org.bukkit.entity.Player)
return self.location.world;
if (typeof self == "undefined")
2013-01-18 00:28:12 +01:00
return;
2013-01-25 00:47:36 +01:00
if (self instanceof org.bukkit.command.BlockCommandSender)
return self.block.location.world;
};
2013-01-18 00:28:12 +01:00
var _notifyAdministrators = function(msg){
2013-01-28 20:49:44 +01:00
var ops = __plugin.server.operators.toArray();
for (var i = 0; i < ops.length;i++){
var op = ops[i];
if (op.isOnline())
op.chat(msg);
2013-01-18 00:28:12 +01:00
}
__plugin.logger.info(msg);
};
var _echo = function(msg){
__plugin.logger.info(msg);
2013-01-25 00:47:36 +01:00
if (typeof self == "undefined"){
2013-01-18 00:28:12 +01:00
java.lang.System.out.println(msg);
return;
}
2013-01-25 00:47:36 +01:00
self.sendMessage(msg);
2013-01-18 00:28:12 +01:00
};
2013-01-08 00:59:42 +01:00
2013-01-19 18:17:52 +01:00
global.getPlayerPos = _getPlayerPos;
global.getMousePos = _getMousePos;
global.putBlock = _putBlock;
global.getBlock = _getBlock;
global.putSign = _putSign;
global.notifyAdministrators = _notifyAdministrators;
global.echo = _echo;
2013-01-18 00:28:12 +01:00
2013-01-08 00:59:42 +01:00
}());