Skip to content

Instantly share code, notes, and snippets.

@rmichela
Created January 21, 2011 08:46
Show Gist options
  • Save rmichela/789432 to your computer and use it in GitHub Desktop.
Save rmichela/789432 to your computer and use it in GitHub Desktop.
Receiver example for the IPC bukkit plugin
package bukkit.deltahat.IPC.TestB;
import java.io.File;
import java.util.Random;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin;
import bukkit.deltahat.IPC.IpcEvent;
import bukkit.deltahat.IPC.IpcEventListener;
import bukkit.deltahat.IPC.IpcMethod;
import bukkit.deltahat.IPC.IpcMethodResult;
import bukkit.deltahat.IPC.IpcRegistry;
public class TestB extends JavaPlugin {
public TestB(PluginLoader pluginLoader, Server instance,
PluginDescriptionFile desc, File folder, File plugin,
ClassLoader cLoader) {
super(pluginLoader, instance, desc, folder, plugin, cLoader);
// TODO Auto-generated constructor stub
}
@Override
public void onDisable() {
// TODO Auto-generated method stub
}
@Override
public void onEnable() {
IpcRegistry.publishIpcMethod(new DivideMethod());
IpcRegistry.publishIpcMethod(new Die1());
IpcRegistry.publishIpcMethod(new Die2());
(new ShoutEventListener()).register();
System.out.println("[IPC Test B] Loaded.");
}
private class DivideMethod extends IpcMethod
{
public DivideMethod() {
super(TestB.this, "Divide");
}
@Override
protected IpcMethodResult invokeImpl(Object[] args) {
try
{
int r = (Integer)args[0] / (Integer)args[1];
return new IpcMethodResult(r);
}
catch(Exception e)
{
return new IpcMethodResult(e);
}
}
}
private class Die1 extends IpcMethod
{
public Die1()
{
super(TestB.this, "Roll");
}
@Override
protected IpcMethodResult invokeImpl(Object[] args)
{
return new IpcMethodResult("Die 1: " + (new Random()).nextInt());
}
}
private class Die2 extends IpcMethod
{
public Die2()
{
super(TestB.this, "Roll");
}
@Override
protected IpcMethodResult invokeImpl(Object[] args)
{
return new IpcMethodResult("Die 2: " + (new Random()).nextInt());
}
}
private class ShoutEventListener extends IpcEventListener
{
public ShoutEventListener() {
super("shoutEvent", Priority.Normal, TestB.this);
}
@Override
protected void onEventImpl(IpcEvent event) {
System.out.println("[IPC Test B] Shouted");
Player p = event.getPlayer();
String s = event.getData();
p.sendMessage("You shouted: " + s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment