Skip to content

Commit

Permalink
First release in github
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkiller committed Apr 8, 2011
1 parent 13b8e82 commit d2e1170
Show file tree
Hide file tree
Showing 9 changed files with 1,972 additions and 0 deletions.
207 changes: 207 additions & 0 deletions src/com/bukkit/sharkiller/milkAdmin/McRKit/RTKInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package com.bukkit.sharkiller.milkAdmin.McRKit;
//RTK UDP API, Revision 4
//(C) Nick Stones-Havas 2011
//Revision date: February 18th, 2011
/*-------------------
CHANGELOG
--Revision 4--
* Added support for hold and start API functions
--Revision 3--
* Added enum javadoc descriptions
--Revision 2--
* Added Javadoc comments
* Made getReference() static.
--Revision 1--
* Made the singleton reference have private accesss.
* Added an accessor method to retrieve the singleton reference
--Revision 0--
* Initial release
-------------------*/

import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.SocketTimeoutException;

import java.io.IOException;

import java.util.LinkedList;


/**
* The <em>RTKInterface</em> handles all of the UDP API calls to the RemoteToolkit Minecraft wrapper.
*
* @author <a href="mailto:nick@drdanick.com">Nick Stones-Havas</a>
* @version 2, 09/02/2011
*/
public class RTKInterface{
//singleton reference
private static RTKInterface singleton = null;

//hostname
private String host;

//port
private int port;

//username
private String user;

//password
private String password;

//registered listeners
private LinkedList<RTKListener> listeners;

/**
* Defines the type of action to perform.
*/
public enum CommandType{
/**
* Requests a restart
*/
RESTART,
/**
* Recquests a forceful restart
*/
FORCE_RESTART,
/**
* Requests a forceful stop
*/
FORCE_STOP,
/**
* Requests the wrapper version
*/
VERSION,
/**
* Disables wrapper restarts <b>WARNING: As of now this does not stop scheduled events, only the restart-on-stop event.</b>
*/
DISABLE_RESTARTS,
/**
* Enables wrapper restarts
*/
ENABLE_RESTARTS,

/**
* Stops the server until START_SERVER is called
*/
HOLD_SERVER,

/**
* Starts a held server
*/
START_SERVER
}

private RTKInterface(int port, String host, String user, String password){
this.port=port;
this.host=host;
singleton=this;
this.user=user.toLowerCase();
this.password=password.toLowerCase();
listeners = new LinkedList<RTKListener>();
}
/**
* Instantiates an RTKInterface singleton.
* @param port The port to send API events to.
* @param host The host name to send API events to.
* @param user The wrapper username to send API calls as.
* @param password The wrapper password that is tied to username.
* @throws RTKInterfaceException if RTKInterface has already been instantiated.
*/
public static RTKInterface createRTKInterface(int port,String host,String user,String password)throws RTKInterfaceException{
if(singleton==null){
return new RTKInterface(port,host,user,password);
}else{
throw new RTKInterfaceException("RTKInterface already instantiated.");
}
}

/**
* Returns the singleton reference
*/
public static RTKInterface getReference(){
return singleton;
}

/**
* Registers an RTKListener to dispatch inbound events to.
* @param listener The listener that is to be registered.
*/
public void registerRTKListener(RTKListener listener){
listeners.add(listener);
}
/**
* Deregisters an RTKListener.
* @param listener The listener that is to be deregistered.
*/
public void deregisterRTKListener(RTKListener listener){
listeners.remove(listener);
}

private void despatchStringToListeners(String s){
for(RTKListener listener:listeners)
listener.onRTKStringReceived(s);
}

/**
* Executes an api function.
* @param type The type of wrapper function to perform.
* @throws IOException
*/
public void executeCommand(CommandType type)throws IOException{
String packet = null;
String suffix = ":"+user+":"+password;
if(type==CommandType.RESTART)
packet = "restart"+suffix;
else if(type==CommandType.FORCE_RESTART)
packet = "forcerestart"+suffix;
else if(type==CommandType.FORCE_STOP)
packet = "forcerestop"+suffix;
else if(type==CommandType.VERSION)
packet = "version"+suffix;
else if(type==CommandType.DISABLE_RESTARTS)
packet = "disable"+suffix;
else if(type==CommandType.ENABLE_RESTARTS)
packet = "enable"+suffix;
else if(type==CommandType.HOLD_SERVER)
packet = "hold"+suffix;
else if(type==CommandType.START_SERVER)
packet = "start"+suffix;
dispatchUDPPacket(packet,host,port);
}

private void dispatchUDPPacket(String packet, String host, int port)throws IOException{
try{
final DatagramSocket ds = new DatagramSocket();
DatagramPacket dp = new DatagramPacket(packet.getBytes(), packet.getBytes().length,InetAddress.getByName(host),port);
ds.send(dp);
new Thread(){
public void run(){
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
try{
ds.setSoTimeout(5000);
ds.receive(incoming);
String temp = new String(incoming.getData());
despatchStringToListeners(temp.trim());
}catch(SocketTimeoutException e){
despatchStringToListeners("RTK_TIMEOUT");
}catch(Exception e){
System.err.println("Unexpected Socket error: "+e);
e.printStackTrace();
}
}
}.start();
}catch(IOException e){
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bukkit.sharkiller.milkAdmin.McRKit;

public class RTKInterfaceException extends Exception {
private static final long serialVersionUID = 1L;

public RTKInterfaceException(String msg) {
super(msg);
}
}
5 changes: 5 additions & 0 deletions src/com/bukkit/sharkiller/milkAdmin/McRKit/RTKListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bukkit.sharkiller.milkAdmin.McRKit;

public interface RTKListener{
public void onRTKStringReceived(String message);
}
94 changes: 94 additions & 0 deletions src/com/bukkit/sharkiller/milkAdmin/MilkAdmin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.bukkit.sharkiller.milkAdmin;

import java.io.*;

import java.util.HashMap;
import java.util.logging.Logger;

import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.config.Configuration;

import com.bukkit.sharkiller.milkAdmin.McRKit.RTKInterface;
import com.bukkit.sharkiller.milkAdmin.McRKit.RTKInterfaceException;
import com.bukkit.sharkiller.milkAdmin.McRKit.RTKListener;

public class MilkAdmin extends org.bukkit.plugin.java.JavaPlugin implements RTKListener{
private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
private final MilkAdminPlayerListener playerListener = new MilkAdminPlayerListener(this);
RTKInterface api = null;
Configuration Settings = new Configuration(new File("milkAdmin/settings.yml"));

public MilkAdmin(org.bukkit.plugin.PluginLoader pluginLoader, org.bukkit.Server serverInstance, org.bukkit.plugin.PluginDescriptionFile descFile, java.io.File folder, java.io.File pluginFile, java.lang.ClassLoader cLoader) {

}
public MilkAdmin() {

}
public void setup() {
try{
new File("milkAdmin").mkdir();
new File("milkAdmin", "banlist.ini").createNewFile();
new File("milkAdmin", "settings.yml").createNewFile();
} catch (IOException ex) {
System.out.println("Could not create milkAdmin files.");
}
eraseLoggedIn();
try{
Settings.load();
String username = Settings.getString("RTK.Username", "user");
String password = Settings.getString("RTK.Password", "pass");
int port = Settings.getInt("RTK.Port", 25561);
api = RTKInterface.createRTKInterface(port,"localhost",username,password);
}catch(RTKInterfaceException e){
e.printStackTrace();
}
api.registerRTKListener(this);
}

public void onRTKStringReceived(String s){
System.out.println("From wrapper: "+s);
}

public void eraseLoggedIn(){
File myFile = new File("milkAdmin/loggedin.ini");
myFile.delete();
try{
new File("milkAdmin/loggedin.ini").createNewFile();
} catch (IOException ex) {
System.out.println("Could not create milkAdmin files!");
}
}

@SuppressWarnings("unused")
public void onEnable() {
Logger logger = Logger.getLogger("Minecraft");
setup();
PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);

PluginDescriptionFile pdfFile = this.getDescription();
System.out.println("[milkAdmin] v"+pdfFile.getVersion()+" is enabled!" );
System.out.println("[milkAdmin] Developed by: "+pdfFile.getAuthors());
new WebServer(this);
}

public void onDisable() {
System.out.println("[milkAdmin] Disabled!");
}

public boolean isDebugging(final Player player) {
if (debugees.containsKey(player)) {
return debugees.get(player);
} else {
return false;
}
}

public void setDebugging(final Player player, final boolean value) {
debugees.put(player, value);
}
}
35 changes: 35 additions & 0 deletions src/com/bukkit/sharkiller/milkAdmin/MilkAdminPlayerListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.bukkit.sharkiller.milkAdmin;

import java.io.File;
import java.io.IOException;

import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.util.config.Configuration;
/**
* Handle events for all Player related events
* @author Snowl
*/
public class MilkAdminPlayerListener extends PlayerListener {
//private final milkBukkit plugin;
Configuration Settings = new Configuration(new File("milkAdmin/settings.yml"));
PropertiesFile banList = new PropertiesFile("milkAdmin/banlist.ini");
String BannedString = Settings.getString("Strings.Banned", "Banned from this server");

public MilkAdminPlayerListener(MilkAdmin instance) {
//plugin = instance;
Settings.load();
}

@Override
public void onPlayerJoin(PlayerJoinEvent event) {
try {
banList.load();
} catch (IOException ioe) {}
String PlayerBanned = banList.getString(event.getPlayer().getName(), "false");
String PlayerBannedIP = banList.getString(event.getPlayer().getAddress().getAddress().getHostAddress(), "false");
if(PlayerBannedIP.contentEquals("true") || PlayerBanned.contentEquals("true")){
event.getPlayer().kickPlayer(BannedString);
}
}
}
Loading

0 comments on commit d2e1170

Please sign in to comment.