Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgorge committed Nov 24, 2010
0 parents commit 048b567
Show file tree
Hide file tree
Showing 21 changed files with 4,491 additions and 0 deletions.
35 changes: 35 additions & 0 deletions APIException.java
@@ -0,0 +1,35 @@
// $Id$
/*
* CraftAPI
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

//import org.apache.xmlrpc.XmlRpcException;

/**
*
* @author sk89q
*/
public class APIException extends Exception {
/**
*
*/
private static final long serialVersionUID = -3014431928944408933L;

public APIException(int code, String msg) {
super("CODE: "+code+", "+msg);
}
}
71 changes: 71 additions & 0 deletions HttpStream.java
@@ -0,0 +1,71 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.json.simple.JSONObject;


public class HttpStream extends InputStream {
private String type = "";
private int next = 0;
public static ArrayList<String[]> chatStack = new ArrayList<String[]>();
public static ArrayList<String[]> consoleStack = new ArrayList<String[]>();
public static ArrayList<String[]> commandStack = new ArrayList<String[]>();
public static ArrayList<String[]> connectionsStack = new ArrayList<String[]>();
public ArrayList<String[]> stack = null;

public HttpStream (String s) {
type = s;

if(type.equals("chat"))
stack = chatStack;
else if(type.equals("commands"))
stack = commandStack;
else if(type.equals("connections"))
stack = connectionsStack;
else if(type.equals("console"))
stack = consoleStack;

next = stack.size();
}

public String getNext () {
while(next >= stack.size()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

JSONObject r = new JSONObject();
//JSONObject q = new JSONObject();

if(type.equals("chat")) {
r.put("player", stack.get(next)[0]);
r.put("message", stack.get(next)[1]);
//r.put("chat", q);
}
else if(type.equals("commands")) {
r.put("player", stack.get(next)[0]);
r.put("command", stack.get(next)[1]);
}
else if(type.equals("connections")) {
r.put("action", stack.get(next)[0]);
r.put("player", stack.get(next)[1]);
//r.put(", value)
}
else if(type.equals("console")) {
r.put("line", stack.get(next)[0]);
}

next++;
return r.toJSONString().concat("\r\n");
}

@Override
public int read() throws IOException {
return -1;
}
}
146 changes: 146 additions & 0 deletions JSONApi.java
@@ -0,0 +1,146 @@
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.logging.Logger;

/**
*
* @author alecgorge
*/
public class JSONApi extends Plugin {
private Listener l = new Listener(this);
protected static final Logger log = Logger.getLogger("Minecraft");
private String name = "JSONApi";
private JSONServer server = null;
private String version = "rev 1";
public static boolean logging = false;
public static int port = 0;


public void enable() {
try {
Hashtable<String, String> auth = new Hashtable<String, String>();

PropertiesFile options = new PropertiesFile("JSONApi.properties");
logging = options.getBoolean("logToConsole", true);
port = options.getInt("port", 20059);


PropertiesFile pf = new PropertiesFile("JSONApiAuthentcation.txt");
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream;
try {
fstream = new FileInputStream("JSONApiAuthentcation.txt");
}
catch (FileNotFoundException e) {
File f = new File("JSONApiAuthentcation.txt");
f.createNewFile();
fstream = new FileInputStream("JSONApiAuthentcation.txt");
}
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;

//Read File Line By Line
while ((line = br.readLine()) != null) {
// Print the content on the console
if(!line.startsWith("#")) {
String[] parts = line.trim().split(":");
if(parts.length == 2) {
auth.put(parts[0], parts[1]);
}
}
//System.out.println (strLine);
}
// Close the input stream
in.close();
} catch (Exception e){//Catch exception if any
e.printStackTrace();
}
if(auth.size() == 0) {
log.severe("No valid logins for JSONApi. Check JSONApiAuthentication.txt");
return;
}
server = new JSONServer(auth);
}
catch( IOException ioe ) {
log.severe( "Couldn't start server!\n");
ioe.printStackTrace();
//System.exit( -1 );
}
}

public void disable() {
if(server != null) {
server.stop();
}
}

public void initialize() {
log.info("JSONApi is active and listening for requests.");
// Uncomment as needed.
//etc.getLoader().addListener( PluginLoader.Hook.ARM_SWING, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.BLOCK_CREATED, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.BLOCK_DESTROYED, l, this, PluginListener.Priority.MEDIUM);
etc.getLoader().addListener( PluginLoader.Hook.CHAT, l, this, PluginListener.Priority.MEDIUM);
etc.getLoader().addListener( PluginLoader.Hook.COMMAND, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.COMPLEX_BLOCK_CHANGE, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.COMPLEX_BLOCK_SEND, l, this, PluginListener.Priority.MEDIUM);
etc.getLoader().addListener( PluginLoader.Hook.DISCONNECT, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.INVENTORY_CHANGE, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.IPBAN, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.KICK, l, this, PluginListener.Priority.MEDIUM);
etc.getLoader().addListener( PluginLoader.Hook.LOGIN, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.LOGINCHECK, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.NUM_HOOKS, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.PLAYER_MOVE, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.SERVERCOMMAND, l, this, PluginListener.Priority.MEDIUM);
//etc.getLoader().addListener( PluginLoader.Hook.TELEPORT, l, this, PluginListener.Priority.MEDIUM);
}

public class Listener extends PluginListener {
JSONApi p;

public String join(String[] strings, String separator) {
StringBuffer sb = new StringBuffer();
for (int i=0; i < strings.length; i++) {
if (i != 0) sb.append(separator);
sb.append(strings[i]);
}
return sb.toString();
}

// This controls the accessability of functions / variables from the main class.
public Listener(JSONApi plugin) {
p = plugin;
}

public boolean onChat(Player player, String message) {
HttpStream.chatStack.add(new String[] {player.getName(),message});

return false;
}

public void onDisconnect (Player player) {
HttpStream.connectionsStack.add(new String[] {"disconnect", player.getName()});
}

public void onLogin (Player player) {
HttpStream.connectionsStack.add(new String[] {"connect", player.getName()});
}

public boolean onCommand (Player player, String[] split) {
HttpStream.commandStack.add(new String[] {player.getName(), join(split, " ")});

return false;
}
}
}

0 comments on commit 048b567

Please sign in to comment.