mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-15 02:12:42 -04:00
removed some unnecessary api stuff and unused jnlua classes
This commit is contained in:
parent
8395814e36
commit
ce69b1c7dd
@ -1,194 +0,0 @@
|
||||
/*
|
||||
* $Id: LuaConsole.java 79 2012-01-08 11:08:32Z andre@naef.com $
|
||||
* See LICENSE.txt for license terms.
|
||||
*/
|
||||
|
||||
package com.naef.jnlua.console;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import com.naef.jnlua.LuaException;
|
||||
import com.naef.jnlua.LuaRuntimeException;
|
||||
import com.naef.jnlua.LuaState;
|
||||
|
||||
/**
|
||||
* A simple Lua console.
|
||||
*
|
||||
* <p>
|
||||
* The console collects input until a line with the sole content of the word
|
||||
* <i>go</i> is encountered. At that point, the collected input is run as a Lua
|
||||
* chunk. If the Lua chunk loads and runs successfully, the console displays the
|
||||
* returned values of the chunk as well as the execution time based on a
|
||||
* <code>System.nanoTime()</code> measurement. Otherwise, the console shows the
|
||||
* error that has occurred.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Expressions can be printed by prepending <i>=</i> to the expression at the
|
||||
* beginning of a chunk. The console translates <i>=</i> into
|
||||
* <code>return</code> followed by a space and executes the chunk immediately.
|
||||
* No separate <i>go</i> is required. Therefore, expressions printed this way
|
||||
* must be entered on a single line.
|
||||
* </p>
|
||||
*/
|
||||
public class LuaConsole {
|
||||
// -- Static
|
||||
private static final String[] EMPTY_ARGS = new String[0];
|
||||
|
||||
/**
|
||||
* Main routine.
|
||||
*
|
||||
* @param args
|
||||
* the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
LuaConsole luaConsole = new LuaConsole(args);
|
||||
luaConsole.run();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
// -- State
|
||||
private LuaState luaState;
|
||||
|
||||
// -- Construction
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*/
|
||||
public LuaConsole() {
|
||||
this(EMPTY_ARGS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the specified command line arguments. The
|
||||
* arguments are passed to Lua as the <code>argv</code> global variable.
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public LuaConsole(String[] args) {
|
||||
luaState = new LuaState();
|
||||
|
||||
// Process arguments
|
||||
luaState.newTable(args.length, 0);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
luaState.pushString(args[i]);
|
||||
luaState.rawSet(-2, i + 1);
|
||||
}
|
||||
luaState.setGlobal("argv");
|
||||
|
||||
// Open standard libraries
|
||||
luaState.openLibs();
|
||||
|
||||
// Set buffer mode
|
||||
luaState.load("io.stdout:setvbuf(\"no\")", "=consoleInitStdout");
|
||||
luaState.call(0, 0);
|
||||
luaState.load("io.stderr:setvbuf(\"no\")", "=consoleInitStderr");
|
||||
luaState.call(0, 0);
|
||||
}
|
||||
|
||||
// -- Properties
|
||||
/**
|
||||
* Returns the Lua state of this console.
|
||||
*
|
||||
* @return the Lua state
|
||||
*/
|
||||
public LuaState getLuaState() {
|
||||
return luaState;
|
||||
}
|
||||
|
||||
// -- Operations
|
||||
/**
|
||||
* Runs the console.
|
||||
*/
|
||||
public void run() {
|
||||
// Banner
|
||||
System.out.println(String.format("JNLua %s Console using Lua %s.",
|
||||
LuaState.VERSION, LuaState.LUA_VERSION));
|
||||
System.out.print("Type 'go' on an empty line to evaluate a chunk. ");
|
||||
System.out.println("Type =<expression> to print an expression.");
|
||||
|
||||
// Prepare reader
|
||||
BufferedReader bufferedReader = new BufferedReader(
|
||||
new InputStreamReader(System.in));
|
||||
try {
|
||||
// Process chunks
|
||||
chunk: while (true) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutputStreamWriter outWriter = new OutputStreamWriter(out,
|
||||
"UTF-8");
|
||||
boolean firstLine = true;
|
||||
|
||||
// Process lines
|
||||
while (true) {
|
||||
String line = bufferedReader.readLine();
|
||||
if (line == null) {
|
||||
break chunk;
|
||||
}
|
||||
if (line.equals("go")) {
|
||||
outWriter.flush();
|
||||
InputStream in = new ByteArrayInputStream(out
|
||||
.toByteArray());
|
||||
runChunk(in);
|
||||
continue chunk;
|
||||
}
|
||||
if (firstLine && line.startsWith("=")) {
|
||||
outWriter.write("return " + line.substring(1));
|
||||
outWriter.flush();
|
||||
InputStream in = new ByteArrayInputStream(out
|
||||
.toByteArray());
|
||||
runChunk(in);
|
||||
continue chunk;
|
||||
}
|
||||
outWriter.write(line);
|
||||
outWriter.write('\n');
|
||||
firstLine = false;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.print("IO error: ");
|
||||
System.out.print(e.getMessage());
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a chunk of Lua code from an input stream.
|
||||
*/
|
||||
protected void runChunk(InputStream in) throws IOException {
|
||||
try {
|
||||
long start = System.nanoTime();
|
||||
luaState.setTop(0);
|
||||
luaState.load(in, "=console", "t");
|
||||
luaState.call(0, LuaState.MULTRET);
|
||||
long stop = System.nanoTime();
|
||||
for (int i = 1; i <= luaState.getTop(); i++) {
|
||||
if (i > 1) {
|
||||
System.out.print(", ");
|
||||
}
|
||||
switch (luaState.type(i)) {
|
||||
case BOOLEAN:
|
||||
System.out.print(Boolean.valueOf(luaState.toBoolean(i)));
|
||||
break;
|
||||
case NUMBER:
|
||||
case STRING:
|
||||
System.out.print(luaState.toString(i));
|
||||
break;
|
||||
default:
|
||||
System.out.print(luaState.typeName(i));
|
||||
}
|
||||
}
|
||||
System.out.print("\t#msec=");
|
||||
System.out.print(String.format("%.3f", (stop - start) / 1000000.0));
|
||||
System.out.println();
|
||||
} catch (LuaRuntimeException e) {
|
||||
e.printLuaStackTrace();
|
||||
} catch (LuaException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<body>
|
||||
|
||||
<p>Provides the JNLua console.</p>
|
||||
</body>
|
||||
</html>
|
@ -1,46 +0,0 @@
|
||||
/*
|
||||
* $Id: CompiledLuaScript.java 38 2012-01-04 22:44:15Z andre@naef.com $
|
||||
* See LICENSE.txt for license terms.
|
||||
*/
|
||||
|
||||
package com.naef.jnlua.script;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* Compiled script implementation conforming to JSR 223: Scripting for the Java
|
||||
* Platform.
|
||||
*/
|
||||
class CompiledLuaScript extends CompiledScript {
|
||||
// -- State
|
||||
private LuaScriptEngine engine;
|
||||
private byte[] script;
|
||||
|
||||
// -- Construction
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*/
|
||||
public CompiledLuaScript(LuaScriptEngine engine, byte[] script) {
|
||||
this.engine = engine;
|
||||
this.script = script;
|
||||
}
|
||||
|
||||
// -- CompiledScript methods
|
||||
@Override
|
||||
public Object eval(ScriptContext context) throws ScriptException {
|
||||
synchronized (engine.getLuaState()) {
|
||||
engine.loadChunk(new ByteArrayInputStream(script), context, "b");
|
||||
return engine.callChunk(context);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
/*
|
||||
* $Id: LuaBindings.java 38 2012-01-04 22:44:15Z andre@naef.com $
|
||||
* See LICENSE.txt for license terms.
|
||||
*/
|
||||
|
||||
package com.naef.jnlua.script;
|
||||
|
||||
import javax.script.Bindings;
|
||||
|
||||
import com.naef.jnlua.LuaState;
|
||||
import com.naef.jnlua.util.AbstractTableMap;
|
||||
|
||||
/**
|
||||
* Lua bindings implementation conforming to JSR 223: Scripting for the Java
|
||||
* Platform.
|
||||
*/
|
||||
class LuaBindings extends AbstractTableMap<String> implements Bindings {
|
||||
// -- State
|
||||
private LuaScriptEngine scriptEngine;
|
||||
|
||||
// -- Construction
|
||||
public LuaBindings(LuaScriptEngine scriptEngine) {
|
||||
this.scriptEngine = scriptEngine;
|
||||
}
|
||||
|
||||
// -- AbstractTableMap methods
|
||||
@Override
|
||||
protected void checkKey(Object key) {
|
||||
super.checkKey(key);
|
||||
if (!(key instanceof String)) {
|
||||
throw new IllegalArgumentException("key must be a string");
|
||||
}
|
||||
if (((String) key).length() == 0) {
|
||||
throw new IllegalArgumentException("key must not be empty");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean filterKeys() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean acceptKey(int index) {
|
||||
return getLuaState().isString(index)
|
||||
&& getLuaState().toString(index).length() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String convertKey(int index) {
|
||||
return getLuaState().toString(index);
|
||||
}
|
||||
|
||||
// -- LuaProxy methods
|
||||
@Override
|
||||
public LuaState getLuaState() {
|
||||
return scriptEngine.getLuaState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushValue() {
|
||||
getLuaState().rawGet(LuaState.REGISTRYINDEX, LuaState.RIDX_GLOBALS);
|
||||
}
|
||||
|
||||
// -- Package-private methods
|
||||
/**
|
||||
* Returns the script engine.
|
||||
*/
|
||||
LuaScriptEngine getScriptEngine() {
|
||||
return scriptEngine;
|
||||
}
|
||||
}
|
@ -1,418 +0,0 @@
|
||||
/*
|
||||
* $Id: LuaScriptEngine.java 53 2012-01-05 16:58:58Z andre@naef.com $
|
||||
* See LICENSE.txt for license terms.
|
||||
*/
|
||||
|
||||
package com.naef.jnlua.script;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.script.AbstractScriptEngine;
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import com.naef.jnlua.LuaException;
|
||||
import com.naef.jnlua.LuaState;
|
||||
|
||||
/**
|
||||
* Lua script engine implementation conforming to JSR 223: Scripting for the
|
||||
* Java Platform.
|
||||
*/
|
||||
class LuaScriptEngine extends AbstractScriptEngine implements Compilable,
|
||||
Invocable {
|
||||
// -- Static
|
||||
private static final String READER = "reader";
|
||||
private static final String WRITER = "writer";
|
||||
private static final String ERROR_WRITER = "errorWriter";
|
||||
private static final Pattern LUA_ERROR_MESSAGE = Pattern
|
||||
.compile("^(.+):(\\d+):");
|
||||
|
||||
// -- State
|
||||
private LuaScriptEngineFactory factory;
|
||||
private LuaState luaState;
|
||||
|
||||
// -- Construction
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*/
|
||||
LuaScriptEngine(LuaScriptEngineFactory factory) {
|
||||
super();
|
||||
this.factory = factory;
|
||||
luaState = new LuaState();
|
||||
|
||||
// Configuration
|
||||
context.setBindings(createBindings(), ScriptContext.ENGINE_SCOPE);
|
||||
luaState.openLibs();
|
||||
}
|
||||
|
||||
// -- ScriptEngine methods
|
||||
@Override
|
||||
public Bindings createBindings() {
|
||||
return new LuaBindings(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(String script, ScriptContext context)
|
||||
throws ScriptException {
|
||||
synchronized (luaState) {
|
||||
loadChunk(script, context);
|
||||
return callChunk(context);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(Reader reader, ScriptContext context)
|
||||
throws ScriptException {
|
||||
synchronized (luaState) {
|
||||
loadChunk(reader, context);
|
||||
return callChunk(context);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngineFactory getFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
// -- Compilable method
|
||||
@Override
|
||||
public CompiledScript compile(String script) throws ScriptException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
synchronized (luaState) {
|
||||
loadChunk(script, null);
|
||||
try {
|
||||
dumpChunk(out);
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
}
|
||||
return new CompiledLuaScript(this, out.toByteArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompiledScript compile(Reader script) throws ScriptException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
synchronized (luaState) {
|
||||
loadChunk(script, null);
|
||||
try {
|
||||
dumpChunk(out);
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
}
|
||||
return new CompiledLuaScript(this, out.toByteArray());
|
||||
}
|
||||
|
||||
// -- Invocable methods
|
||||
@Override
|
||||
public <T> T getInterface(Class<T> clasz) {
|
||||
synchronized (luaState) {
|
||||
getLuaState().rawGet(LuaState.REGISTRYINDEX, LuaState.RIDX_GLOBALS);
|
||||
try {
|
||||
return luaState.getProxy(-1, clasz);
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
||||
synchronized (luaState) {
|
||||
luaState.pushJavaObject(thiz);
|
||||
try {
|
||||
if (!luaState.isTable(-1)) {
|
||||
throw new IllegalArgumentException("object is not a table");
|
||||
}
|
||||
return luaState.getProxy(-1, clasz);
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeFunction(String name, Object... args)
|
||||
throws ScriptException, NoSuchMethodException {
|
||||
synchronized (luaState) {
|
||||
luaState.getGlobal(name);
|
||||
if (!luaState.isFunction(-1)) {
|
||||
luaState.pop(1);
|
||||
throw new NoSuchMethodException(String.format(
|
||||
"function '%s' is undefined", name));
|
||||
}
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
luaState.pushJavaObject(args[i]);
|
||||
}
|
||||
luaState.call(args.length, 1);
|
||||
try {
|
||||
return luaState.toJavaObject(-1, Object.class);
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeMethod(Object thiz, String name, Object... args)
|
||||
throws ScriptException, NoSuchMethodException {
|
||||
synchronized (luaState) {
|
||||
luaState.pushJavaObject(thiz);
|
||||
try {
|
||||
if (!luaState.isTable(-1)) {
|
||||
throw new IllegalArgumentException("object is not a table");
|
||||
}
|
||||
luaState.getField(-1, name);
|
||||
if (!luaState.isFunction(-1)) {
|
||||
luaState.pop(1);
|
||||
throw new NoSuchMethodException(String.format(
|
||||
"method '%s' is undefined", name));
|
||||
}
|
||||
luaState.pushValue(-2);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
luaState.pushJavaObject(args[i]);
|
||||
}
|
||||
luaState.call(args.length + 1, 1);
|
||||
try {
|
||||
return luaState.toJavaObject(-1, Object.class);
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -- Package private methods
|
||||
/**
|
||||
* Returns the Lua state.
|
||||
*/
|
||||
LuaState getLuaState() {
|
||||
return luaState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a chunk from a string.
|
||||
*/
|
||||
void loadChunk(String string, ScriptContext scriptContext)
|
||||
throws ScriptException {
|
||||
try {
|
||||
luaState.load(string, getChunkName(scriptContext));
|
||||
} catch (LuaException e) {
|
||||
throw getScriptException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a chunk from a reader.
|
||||
*/
|
||||
void loadChunk(Reader reader, ScriptContext scriptContext)
|
||||
throws ScriptException {
|
||||
loadChunk(new ReaderInputStream(reader), scriptContext, "t");
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a chunk from an input stream.
|
||||
*/
|
||||
void loadChunk(InputStream inputStream, ScriptContext scriptContext,
|
||||
String mode) throws ScriptException {
|
||||
try {
|
||||
luaState.load(inputStream, getChunkName(scriptContext), mode);
|
||||
} catch (LuaException e) {
|
||||
throw getScriptException(e);
|
||||
} catch (IOException e) {
|
||||
throw new ScriptException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a loaded chunk.
|
||||
*/
|
||||
Object callChunk(ScriptContext context) throws ScriptException {
|
||||
try {
|
||||
// Apply context
|
||||
Object[] argv;
|
||||
if (context != null) {
|
||||
// Global bindings
|
||||
Bindings bindings;
|
||||
bindings = context.getBindings(ScriptContext.GLOBAL_SCOPE);
|
||||
if (bindings != null) {
|
||||
applyBindings(bindings);
|
||||
}
|
||||
|
||||
// Engine bindings
|
||||
bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
if (bindings != null) {
|
||||
if (bindings instanceof LuaBindings
|
||||
&& ((LuaBindings) bindings).getScriptEngine() == this) {
|
||||
// No need to apply our own live bindings
|
||||
} else {
|
||||
applyBindings(bindings);
|
||||
}
|
||||
}
|
||||
|
||||
// Readers and writers
|
||||
put(READER, context.getReader());
|
||||
put(WRITER, context.getWriter());
|
||||
put(ERROR_WRITER, context.getErrorWriter());
|
||||
|
||||
// Arguments
|
||||
argv = (Object[]) context.getAttribute(ARGV);
|
||||
} else {
|
||||
argv = null;
|
||||
}
|
||||
|
||||
// Push arguments
|
||||
int argCount = argv != null ? argv.length : 0;
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
luaState.pushJavaObject(argv[i]);
|
||||
}
|
||||
|
||||
// Call
|
||||
luaState.call(argCount, 1);
|
||||
|
||||
// Return
|
||||
try {
|
||||
return luaState.toJavaObject(1, Object.class);
|
||||
} finally {
|
||||
luaState.pop(1);
|
||||
}
|
||||
} catch (LuaException e) {
|
||||
throw getScriptException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps a loaded chunk into an output stream. The chunk is left on the
|
||||
* stack.
|
||||
*/
|
||||
void dumpChunk(OutputStream out) throws ScriptException {
|
||||
try {
|
||||
luaState.dump(out);
|
||||
} catch (LuaException e) {
|
||||
throw new ScriptException(e);
|
||||
} catch (IOException e) {
|
||||
throw new ScriptException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// -- Private methods
|
||||
/**
|
||||
* Sets a single binding in a Lua state.
|
||||
*/
|
||||
private void applyBindings(Bindings bindings) {
|
||||
for (Map.Entry<String, Object> binding : bindings.entrySet()) {
|
||||
luaState.pushJavaObject(binding.getValue());
|
||||
String variableName = binding.getKey();
|
||||
int lastDotIndex = variableName.lastIndexOf('.');
|
||||
if (lastDotIndex >= 0) {
|
||||
variableName = variableName.substring(lastDotIndex + 1);
|
||||
}
|
||||
luaState.setGlobal(variableName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Lua chunk name from a script context.
|
||||
*/
|
||||
private String getChunkName(ScriptContext context) {
|
||||
if (context != null) {
|
||||
Object fileName = context.getAttribute(FILENAME);
|
||||
if (fileName != null) {
|
||||
return "@" + fileName.toString();
|
||||
}
|
||||
}
|
||||
return "=null";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a script exception for a Lua exception.
|
||||
*/
|
||||
private ScriptException getScriptException(LuaException e) {
|
||||
Matcher matcher = LUA_ERROR_MESSAGE.matcher(e.getMessage());
|
||||
if (matcher.find()) {
|
||||
String fileName = matcher.group(1);
|
||||
int lineNumber = Integer.parseInt(matcher.group(2));
|
||||
return new ScriptException(e.getMessage(), fileName, lineNumber);
|
||||
} else {
|
||||
return new ScriptException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// -- Private classes
|
||||
/**
|
||||
* Provides an UTF-8 input stream based on a reader.
|
||||
*/
|
||||
private static class ReaderInputStream extends InputStream {
|
||||
// -- Static
|
||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||
|
||||
// -- State
|
||||
private Reader reader;
|
||||
private CharsetEncoder encoder;
|
||||
private boolean flushed;
|
||||
private CharBuffer charBuffer = CharBuffer.allocate(1024);
|
||||
private ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*/
|
||||
public ReaderInputStream(Reader reader) {
|
||||
this.reader = reader;
|
||||
encoder = UTF8.newEncoder();
|
||||
charBuffer.limit(0);
|
||||
byteBuffer.limit(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (!byteBuffer.hasRemaining()) {
|
||||
if (!charBuffer.hasRemaining()) {
|
||||
charBuffer.clear();
|
||||
reader.read(charBuffer);
|
||||
charBuffer.flip();
|
||||
}
|
||||
byteBuffer.clear();
|
||||
if (charBuffer.hasRemaining()) {
|
||||
if (encoder.encode(charBuffer, byteBuffer, false).isError()) {
|
||||
throw new IOException("Encoding error");
|
||||
}
|
||||
} else {
|
||||
if (!flushed) {
|
||||
if (encoder.encode(charBuffer, byteBuffer, true)
|
||||
.isError()) {
|
||||
throw new IOException("Encoding error");
|
||||
}
|
||||
if (encoder.flush(byteBuffer).isError()) {
|
||||
throw new IOException("Encoding error");
|
||||
}
|
||||
flushed = true;
|
||||
}
|
||||
}
|
||||
byteBuffer.flip();
|
||||
if (!byteBuffer.hasRemaining()) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return byteBuffer.get();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,197 +0,0 @@
|
||||
/*
|
||||
* $Id: LuaScriptEngineFactory.java 38 2012-01-04 22:44:15Z andre@naef.com $
|
||||
* See LICENSE.txt for license terms.
|
||||
*/
|
||||
|
||||
package com.naef.jnlua.script;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
|
||||
import com.naef.jnlua.LuaState;
|
||||
|
||||
/**
|
||||
* Lua script engine factory implementation conforming to JSR 223: Scripting for
|
||||
* the Java Platform.
|
||||
*/
|
||||
public class LuaScriptEngineFactory implements ScriptEngineFactory {
|
||||
// -- Static
|
||||
private static final String ENGINE_NAME = "JNLua";
|
||||
private static final String LANGUAGE_NAME = "Lua";
|
||||
private static final List<String> EXTENSIONS;
|
||||
private static final List<String> MIME_TYPES;
|
||||
private static final List<String> NAMES;
|
||||
static {
|
||||
// Extensions
|
||||
List<String> extensions = new ArrayList<String>();
|
||||
extensions.add("lua");
|
||||
EXTENSIONS = Collections.unmodifiableList(extensions);
|
||||
|
||||
// MIME types
|
||||
List<String> mimeTypes = new ArrayList<String>();
|
||||
mimeTypes.add("application/x-lua");
|
||||
mimeTypes.add("text/x-lua");
|
||||
MIME_TYPES = Collections.unmodifiableList(mimeTypes);
|
||||
|
||||
// Names
|
||||
List<String> names = new ArrayList<String>();
|
||||
names.add("lua");
|
||||
names.add("Lua");
|
||||
names.add("jnlua");
|
||||
names.add("JNLua");
|
||||
NAMES = Collections.unmodifiableList(names);
|
||||
}
|
||||
|
||||
// -- Construction
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*/
|
||||
public LuaScriptEngineFactory() {
|
||||
}
|
||||
|
||||
// -- ScriptEngineFactory methods
|
||||
@Override
|
||||
public String getEngineName() {
|
||||
return ENGINE_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEngineVersion() {
|
||||
return LuaState.VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getExtensions() {
|
||||
return EXTENSIONS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getMimeTypes() {
|
||||
return MIME_TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNames() {
|
||||
return NAMES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguageName() {
|
||||
return LANGUAGE_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguageVersion() {
|
||||
return LuaState.LUA_VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(String key) {
|
||||
if (key.equals(ScriptEngine.ENGINE)) {
|
||||
return getEngineName();
|
||||
}
|
||||
if (key.equals(ScriptEngine.ENGINE_VERSION)) {
|
||||
return getEngineVersion();
|
||||
}
|
||||
if (key.equals(ScriptEngine.NAME)) {
|
||||
return getNames().get(0);
|
||||
}
|
||||
if (key.equals(ScriptEngine.LANGUAGE)) {
|
||||
return getLanguageName();
|
||||
}
|
||||
if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
|
||||
return getLanguageVersion();
|
||||
}
|
||||
if (key.equals("THREADING")) {
|
||||
return "MULTITHREADED";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodCallSyntax(String obj, String m, String... args) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(obj);
|
||||
sb.append(':');
|
||||
sb.append(m);
|
||||
sb.append('(');
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (i > 0) {
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append(args[i]);
|
||||
}
|
||||
sb.append(')');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOutputStatement(String toDisplay) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("print(");
|
||||
quoteString(sb, toDisplay);
|
||||
sb.append(')');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProgram(String... statements) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < statements.length; i++) {
|
||||
sb.append(statements[i]);
|
||||
sb.append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptEngine getScriptEngine() {
|
||||
return new LuaScriptEngine(this);
|
||||
}
|
||||
|
||||
// --Private methods
|
||||
/**
|
||||
* Quotes a string in double quotes.
|
||||
*/
|
||||
private void quoteString(StringBuffer sb, String s) {
|
||||
sb.append('"');
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
switch (s.charAt(i)) {
|
||||
case '\u0007':
|
||||
sb.append("\\a");
|
||||
break;
|
||||
case '\b':
|
||||
sb.append("\\b");
|
||||
break;
|
||||
case '\f':
|
||||
sb.append("\\f");
|
||||
break;
|
||||
case '\n':
|
||||
sb.append("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
sb.append("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
sb.append("\\t");
|
||||
break;
|
||||
case '\u000b':
|
||||
sb.append("\\v");
|
||||
break;
|
||||
case '\\':
|
||||
sb.append("\\\\");
|
||||
break;
|
||||
case '"':
|
||||
sb.append("\\\"");
|
||||
break;
|
||||
default:
|
||||
sb.append(s.charAt(i));
|
||||
}
|
||||
}
|
||||
sb.append('"');
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<body>
|
||||
|
||||
<p>Provides the JNLua provider for JSR 223: Scripting for the Java platform.</p>
|
||||
</body>
|
||||
</html>
|
@ -1,158 +0,0 @@
|
||||
/**
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.turtle.api;
|
||||
import dan200.computer.api.*;
|
||||
|
||||
/**
|
||||
* The interface passed to upgrades by turtles, providing methods that they can call.
|
||||
* This should not be implemented by your classes. Do not interact with turtles except via this interface and ITurtleUpgrade.
|
||||
*/
|
||||
public interface ITurtleAccess
|
||||
{
|
||||
/**
|
||||
* Returns the world in which the turtle resides.
|
||||
* @return the world in which the turtle resides.
|
||||
*/
|
||||
public net.minecraft.world.World getWorld();
|
||||
|
||||
/**
|
||||
* Returns a vector containing the integer block co-ordinates at which the turtle resides.
|
||||
* @return a vector containing the integer block co-ordinates at which the turtle resides.
|
||||
*/
|
||||
public net.minecraft.util.Vec3 getPosition();
|
||||
|
||||
/**
|
||||
* Returns a vector containing the co-ordinates at which the turtle is rendered.
|
||||
* This will shift when the turtle is moving.
|
||||
* @param f The subframe fraction
|
||||
* @return a vector containing the integer block co-ordinates at which the turtle resides.
|
||||
*/
|
||||
public net.minecraft.util.Vec3 getVisualPosition( float f );
|
||||
|
||||
/**
|
||||
* Returns the world direction the turtle is currently facing.
|
||||
* @return the world direction the turtle is currently facing.
|
||||
*/
|
||||
public int getFacingDir();
|
||||
|
||||
/**
|
||||
* Returns the size of the turtles inventory, in number of slots. This will currently always be 16.
|
||||
* @return the size of the turtles inventory, in number of slots. This will currently always be 16.
|
||||
*/
|
||||
public int getInventorySize();
|
||||
|
||||
/**
|
||||
* Returns which slot the turtle currently has selected in its inventory using turtle.select().
|
||||
* Unlike the 1-based lua representation, this will be between 0 and getInventorySize() - 1.
|
||||
* @return which slot the turtle currently has selected in its inventory
|
||||
*/
|
||||
public int getSelectedSlot();
|
||||
|
||||
/**
|
||||
* Returns the item stack that the turtle has in one of its inventory slots.
|
||||
* @param index which inventory slot to retreive, should be between 0 and getInventorySize() - 1
|
||||
* @return the item stack that the turtle has in one of its inventory slots. May be null.
|
||||
*/
|
||||
public net.minecraft.item.ItemStack getSlotContents( int index );
|
||||
|
||||
/**
|
||||
* Changes the item stack that the turtle has in one of its inventory slots.
|
||||
* @param index which inventory slot to change, should be between 0 and getInventorySize() - 1
|
||||
* @param stack an item stack to put in the slot. May be null.
|
||||
*/
|
||||
public void setSlotContents( int index, net.minecraft.item.ItemStack stack );
|
||||
|
||||
/**
|
||||
* Tries to store an item stack into the turtles current inventory, starting from the turtles
|
||||
* currently selected inventory slot.
|
||||
* @param stack The item stack to try and store.
|
||||
* @return true if the stack was completely stored in the inventory, false if
|
||||
* it was only partially stored, or could not fit at all. If false is returned
|
||||
* and the stack was partially stored, the ItemStack passed into "stack" will now
|
||||
* represent the stack of items that is left over.
|
||||
*/
|
||||
public boolean storeItemStack( net.minecraft.item.ItemStack stack );
|
||||
|
||||
/**
|
||||
* Drops an item stack from the turtle onto the floor, or into an inventory is there is one
|
||||
* adjacent to the turtle in the direction specified.
|
||||
* @param stack The item stack to drop.
|
||||
* @param dir The world direction to drop the item
|
||||
* @return true if the stack was dropped, or completely stored in the adjacent inventory, false if
|
||||
* it was only partially stored in the adjacent inventory, or could not fit at all. If false is returned
|
||||
* and the stack was partially stored, the ItemStack passed into "stack" will now
|
||||
* represent the stack of items that is left over.
|
||||
*/
|
||||
public boolean dropItemStack( net.minecraft.item.ItemStack stack, int dir );
|
||||
|
||||
/**
|
||||
* "Deploys" an item stack in the direction specified. This simulates a player right clicking, and calls onItemUse() on the Item class.
|
||||
* Will return true if some kind of deployment happened, and may modify the item stack. For block item types, this can be
|
||||
* used to place blocks. Some kinds of items (such as shears when facing a sheep) may modify the turtles inventory during this call.
|
||||
* @param stack The item stack to deploy
|
||||
* @param dir The world direction to deploy the item
|
||||
* @return true if the stack was deployed, false if it was not.
|
||||
*/
|
||||
public boolean deployWithItemStack( net.minecraft.item.ItemStack stack, int dir );
|
||||
|
||||
/**
|
||||
* Tries to "attack" entities with an item stack in the direction specified. This simulates a player left clicking, but will
|
||||
* not affect blocks. If an entity is attacked and killed during this call, its dropped items will end up in the turtles
|
||||
* inventory.
|
||||
* @param stack The item stack to attack with
|
||||
* @param dir The world direction to attack with the item
|
||||
* @return true if something was attacked, false if it was not
|
||||
*/
|
||||
public boolean attackWithItemStack( net.minecraft.item.ItemStack stack, int dir, float damageMultiplier );
|
||||
|
||||
/**
|
||||
* Returns the current fuel level of the turtle, this is the same integer returned by turtle.getFuelLevel(),
|
||||
* that decreases by 1 every time the turtle moves. Can be used to have your tool or peripheral require or supply
|
||||
* fuel to the turtle.
|
||||
* @return the fuel level
|
||||
*/
|
||||
public int getFuelLevel();
|
||||
|
||||
/**
|
||||
* Tries to increase the fuel level of a turtle by burning an item stack. If the item passed in is a fuel source, fuel
|
||||
* will increase and true will be returned. Otherwise, nothing will happen and false will be returned.
|
||||
* @param stack The stack to try to refuel with
|
||||
* @return Whether the turtle was refueled
|
||||
*/
|
||||
public boolean refuelWithItemStack( net.minecraft.item.ItemStack stack );
|
||||
|
||||
/**
|
||||
* Removes some fuel from the turtles fuel supply. Negative numbers can be passed in to INCREASE the fuel level of the turtle.
|
||||
* @return Whether the turtle was able to consume the ammount of fuel specified. Will return false if you supply a number
|
||||
* greater than the current fuel level of the turtle.
|
||||
*/
|
||||
public boolean consumeFuel( int fuel );
|
||||
|
||||
/**
|
||||
* Adds a custom command to the turtles command queue. Unlike peripheral methods, these custom commands will be executed
|
||||
* on the main thread, so are guaranteed to be able to access Minecraft objects safely, and will be queued up
|
||||
* with the turtles standard movement and tool commands. An issued command will return an unique integer, which will
|
||||
* be supplied as a parameter to a "turtle_response" event issued to the turtle after the command has completed. Look at the
|
||||
* lua source code for "rom/apis/turtle" for how to build a lua wrapper around this functionality.
|
||||
* @param handler an object which will execute the custom command when its point in the queue is reached
|
||||
* @return the unique command identifier described above
|
||||
* @see ITurtleCommandHandler
|
||||
*/
|
||||
public int issueCommand( ITurtleCommandHandler handler );
|
||||
|
||||
/**
|
||||
* Returns the upgrade on the specified side of the turtle, if there is one.
|
||||
* @return the upgrade on the specified side of the turtle, if there is one.
|
||||
*/
|
||||
public ITurtleUpgrade getUpgrade( TurtleSide side );
|
||||
|
||||
/**
|
||||
* Returns the peripheral created by the upgrade on the specified side of the turtle, if there is one.
|
||||
* @return the peripheral created by the upgrade on the specified side of the turtle, if there is one.
|
||||
*/
|
||||
public IHostedPeripheral getPeripheral( TurtleSide side );
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
/**
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.turtle.api;
|
||||
|
||||
/**
|
||||
* An interface for objects executing custom turtle commands, used with ITurtleAccess.issueCommand
|
||||
* @see ITurtleAccess#issueCommand( ITurtleCommandHandler )
|
||||
*/
|
||||
public interface ITurtleCommandHandler
|
||||
{
|
||||
/**
|
||||
* Will be called by the turtle on the main thread when it is time to execute the custom command.
|
||||
* The handler should either perform the work of the command, and return true for success, or return
|
||||
* false to indicate failure if the command cannot be executed at this time.
|
||||
* @param turtle access to the turtle for whom the command was issued
|
||||
* @return true for success, false for failure. If true is returned, the turtle will wait 0.4 seconds
|
||||
* before executing the next command in its queue, as it does for the standard turtle commands.
|
||||
* @see ITurtleAccess#issueCommand( ITurtleCommandHandler )
|
||||
*/
|
||||
public boolean handleCommand( ITurtleAccess turtle );
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
/**
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.turtle.api;
|
||||
import net.minecraft.util.Icon;
|
||||
import dan200.computer.api.*;
|
||||
|
||||
/**
|
||||
* The primary interface for defining an upgrade for Turtles. A turtle upgrade
|
||||
* can either be a new tool, or a new peripheral.
|
||||
* @see TurtleAPI#registerUpgrade( ITurtleUpgrade )
|
||||
*/
|
||||
public interface ITurtleUpgrade
|
||||
{
|
||||
/**
|
||||
* Gets a unique numerical identifier representing this type of turtle upgrade.
|
||||
* Like Minecraft block and item IDs, you should strive to make this number unique
|
||||
* among all turtle upgrades that have been released for ComputerCraft.
|
||||
* The ID must be in the range 64 to 255, as the ID is stored as an 8-bit value,
|
||||
* and 0-64 is reserved for future use by ComputerCraft. The upgrade will
|
||||
* fail registration if an already used ID is specified.
|
||||
* @see TurtleAPI#registerUpgrade( ITurtleUpgrade )
|
||||
*/
|
||||
public int getUpgradeID();
|
||||
|
||||
/**
|
||||
* Return a String to describe this type of upgrade in turtle item names.
|
||||
* Examples of built-in adjectives are "Wireless", "Mining" and "Crafty".
|
||||
*/
|
||||
public String getAdjective();
|
||||
|
||||
/**
|
||||
* Return whether this upgrade adds a tool or a peripheral to the turtle.
|
||||
* Currently, turtle crafting is restricted to one tool & one peripheral per turtle.
|
||||
* @see TurtleUpgradeType for the differences between the two.
|
||||
*/
|
||||
public TurtleUpgradeType getType();
|
||||
|
||||
/**
|
||||
* Return an item stack representing the type of item that a turtle must be crafted
|
||||
* with to create a turtle which holds this upgrade.
|
||||
* Currently, turtle crafting is restricted to one tool & one peripheral per turtle.
|
||||
*/
|
||||
public net.minecraft.item.ItemStack getCraftingItem();
|
||||
|
||||
/**
|
||||
* Return whether this turtle upgrade is an easter egg, and should be attempted to be hidden
|
||||
* from the creative mode inventory and recipe book plugins.
|
||||
*/
|
||||
public boolean isSecret();
|
||||
|
||||
/**
|
||||
* Will only be called for Peripheral upgrades. Creates a peripheral for a turtle
|
||||
* being placed using this upgrade. The peripheral created will be stored
|
||||
* for the lifetime of the turtle, will have update() called once-per-tick, and will be
|
||||
* attach'd detach'd and have methods called in the same manner as a Computer peripheral.
|
||||
* @param turtle Access to the turtle that the peripheral is being created for.
|
||||
* @param side Which side of the turtle (left or right) that the upgrade resides on.
|
||||
* @returns The newly created peripheral. You may return null if this upgrade is a Tool
|
||||
* and this method is not expected to be called.
|
||||
*/
|
||||
public IHostedPeripheral createPeripheral( ITurtleAccess turtle, TurtleSide side );
|
||||
|
||||
/**
|
||||
* Will only be called for Tool upgrades. Called when turtle.dig() or turtle.attack() is called
|
||||
* by the turtle, and the tool is required to do some work.
|
||||
* @param turtle Access to the turtle that the tool resides on.
|
||||
* @param side Which side of the turtle (left or right) the tool resides on.
|
||||
* @param verb Which action (dig or attack) the turtle is being called on to perform.
|
||||
* @param direction Which world direction the action should be performed in, relative to the turtles
|
||||
* position. This will either be up, down, or the direction the turtle is facing, depending on
|
||||
* whether dig, digUp or digDown was called.
|
||||
* @return Whether the turtle was able to perform the action, and hence whether the turtle.dig()
|
||||
* or turtle.attack() lua method should return true. If true is returned, the tool will perform
|
||||
* a swinging animation. You may return false if this upgrade is a Peripheral
|
||||
* and this method is not expected to be called.
|
||||
*/
|
||||
public boolean useTool( ITurtleAccess turtle, TurtleSide side, TurtleVerb verb, int direction );
|
||||
|
||||
/**
|
||||
* Called to obtain the Icon to be used when rendering a turtle peripheral. Needs to be a "block"
|
||||
* type Icon for now, as there is no way to determine which texture sheet an Icon is from by the
|
||||
* Icon itself.
|
||||
* @param turtle Access to the turtle that the peripheral resides on.
|
||||
* @param side Which side of the turtle (left or right) the peripheral resides on.
|
||||
* @return The Icon that you wish to be used to render your turtle peripheral.
|
||||
*/
|
||||
public Icon getIcon( ITurtleAccess turtle, TurtleSide side );
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
/**
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.turtle.api;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* The static entry point to the ComputerCraft Turtle Upgrade API.
|
||||
* Members in this class must be called after mod_CCTurtle has been initialised,
|
||||
* but may be called before it is fully loaded.
|
||||
*/
|
||||
public class TurtleAPI
|
||||
{
|
||||
/**
|
||||
* Registers a new turtle upgrade for use in ComputerCraft. After calling this,
|
||||
* users should be able to craft Turtles with your new upgrade. It is recommended to call
|
||||
* this during the load() method of your mod.
|
||||
* @throws Exception if you try to register an upgrade with an already used or reserved upgradeID
|
||||
* @see ITurtleUpgrade
|
||||
*/
|
||||
public static void registerUpgrade( ITurtleUpgrade upgrade )
|
||||
{
|
||||
if( upgrade != null )
|
||||
{
|
||||
findCCTurtle();
|
||||
if( ccTurtle_registerTurtleUpgrade != null )
|
||||
{
|
||||
try {
|
||||
ccTurtle_registerTurtleUpgrade.invoke( null, new Object[]{ upgrade } );
|
||||
} catch( Exception e ) {
|
||||
// It failed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The functions below here are private, and are used to interface with the non-API ComputerCraft classes.
|
||||
// Reflection is used here so you can develop your mod in MCP without decompiling ComputerCraft and including
|
||||
// it in your solution.
|
||||
|
||||
private static void findCCTurtle()
|
||||
{
|
||||
if( !ccTurtleSearched ) {
|
||||
// Search for CCTurtle
|
||||
try {
|
||||
ccTurtle = Class.forName( "dan200.CCTurtle" );
|
||||
ccTurtle_registerTurtleUpgrade = findCCTurtleMethod( "registerTurtleUpgrade", new Class[] {
|
||||
ITurtleUpgrade.class
|
||||
} );
|
||||
|
||||
} catch( ClassNotFoundException e ) {
|
||||
System.out.println("ComputerCraftAPI: CCTurtle not found.");
|
||||
|
||||
} finally {
|
||||
ccTurtleSearched = true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Method findCCTurtleMethod( String name, Class[] args )
|
||||
{
|
||||
try {
|
||||
return ccTurtle.getMethod( name, args );
|
||||
|
||||
} catch( NoSuchMethodException e ) {
|
||||
System.out.println("ComputerCraftAPI: CCTurtle method " + name + " not found.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean ccTurtleSearched = false;
|
||||
private static Class ccTurtle = null;
|
||||
private static Method ccTurtle_registerTurtleUpgrade = null;
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
/**
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.turtle.api;
|
||||
|
||||
/**
|
||||
* An enum representing the two sides of the turtle that a turtle upgrade might reside.
|
||||
*/
|
||||
public enum TurtleSide
|
||||
{
|
||||
/**
|
||||
* The turtles left side (where the pickaxe usually is on a Wireless Mining Turtle)
|
||||
*/
|
||||
Left,
|
||||
|
||||
/**
|
||||
* The turtles right side (where the modem usually is on a Wireless Mining Turtle)
|
||||
*/
|
||||
Right,
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/**
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.turtle.api;
|
||||
|
||||
/**
|
||||
* An enum representing the two different types of upgrades that an ITurtleUpgrade
|
||||
* implementation can add to a turtle.
|
||||
* @see ITurtleUpgrade
|
||||
*/
|
||||
public enum TurtleUpgradeType
|
||||
{
|
||||
/**
|
||||
* A tool is rendered as an item on the side of the turtle, and responds to the turtle.dig()
|
||||
* and turtle.attack() methods (Such as pickaxe or sword on Mining and Melee turtles).
|
||||
*/
|
||||
Tool,
|
||||
|
||||
/**
|
||||
* A peripheral adds a special peripheral which is attached to the side of the turtle,
|
||||
* and can be interacted with the peripheral API (Such as the modem on Wireless Turtles).
|
||||
*/
|
||||
Peripheral,
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
/**
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2013. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.turtle.api;
|
||||
|
||||
/**
|
||||
* An enum representing the two different actions that an ITurtleUpgrade of type
|
||||
* Tool may be called on to perform by a turtle.
|
||||
* @see ITurtleUpgrade
|
||||
* @see ITurtleUpgrade#useTool
|
||||
*/
|
||||
public enum TurtleVerb
|
||||
{
|
||||
/**
|
||||
* The turtle called turtle.dig(), turtle.digUp() or turtle.digDown()
|
||||
*/
|
||||
Dig,
|
||||
|
||||
/**
|
||||
* The turtle called turtle.attack(), turtle.attackUp() or turtle.attackDown()
|
||||
*/
|
||||
Attack,
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user