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/js-plugins/core/_primitives.js

132 lines
4.1 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-18 00:28:12 +01:00
// Define these primitive methods used by drone.js (and potentiall others)
2013-01-08 00:59:42 +01:00
//
2013-01-18 00:28:12 +01:00
// 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(){
if (typeof __self == "undefined")
return;
return __self.location;
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getMousePos = function(){
if (typeof __self == "undefined")
return;
// __self might be CONSOLE or a CommandBlock
if (!__self.getTargetBlock)
return;
var targetedBlock = __self.getTargetBlock(null,5);
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;
2013-01-16 08:07:11 +01:00
2013-01-20 02:29:43 +01:00
var world = (__self instanceof org.bukkit.entity.Player)?__self.location.world:(__self instanceof org.bukkit.command.BlockCommandSender)?__self.block.location.world:null;
var block = world.getBlockAt(x,y,z);
/*
2013-01-18 00:28:12 +01:00
if (blockId === 6){
var treeType = null;
switch (metadata){
case 0:
treeType = org.bukkit.TreeType.BIG_TREE;
break;
case 1:
treeType = org.bukkit.TreeType.REDWOOD;
break;
case 2:
treeType = org.bukkit.TreeType.BIRCH;
break;
case 3:
treeType = org.bukkit.TreeType.JUNGLE;
break;
}
return world.generateTree(block.location,treeType);
}else{
2013-01-20 02:29:43 +01:00
*/
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){
putBlock(x,y,z,blockId,meta);
var block = _getBlockObject(x,y,z);
state = block.state;
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-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getBlockObject = function(x,y,z){
var world = _getWorld();
if (world)
return world.getBlockAt(x,y,z);
};
2013-01-08 00:59:42 +01:00
2013-01-18 00:28:12 +01:00
var _getWorld = function(){
2013-01-20 02:29:43 +01:00
if (__self instanceof org.bukkit.entity.Player)
return __self.location.world;
2013-01-18 00:28:12 +01:00
if (typeof __self == "undefined")
return;
if (__self instanceof org.bukkit.command.BlockCommandSender)
return __self.block.location.world;
};
var _notifyAdministrators = function(msg){
var ops = __plugin.server.operators;
for (var i = 0; i < ops.size();i++){
if (ops[i].isOnline())
ops[i].player.chat(msg);
}
__plugin.logger.info(msg);
};
var _echo = function(msg){
__plugin.logger.info(msg);
if (typeof __self == "undefined"){
java.lang.System.out.println(msg);
return;
}
__self.sendMessage(msg);
};
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
}());