-
Import
PEWSServer-lite.jar
(You can download it in the Release page.). -
Define you listener factory, this factory creates a listener for every WebSocket connection.
class MyFactory extends MCListenerFactory {
@Override
public MCListener create(WebSocket conn) {
return new MyListener();
}
}
class MyListener implements MCListener {
@Override
public void onCreate(MCClient client) {
//when finishing connecting
}
@Override
public void onDestroy(MCClient client) {
//when disconnecting
}
@Override
public void onError(MCClient client, MCError error) {
//when some error occurs in Minecraft Client
}
}
- Create a WebSocket server:
WSServer server = new WSServer(new InetSocketAddress(19131), new MyFactory());
- Run your server:
server.start();
- Now you can connect to this server by using command '/connect your_ip:port' on your client.
-
Extend
com.xero.mcpews.event.Event
. -
Add a public static field to store the
EventType
of this customEvent
like this and override methodgetType()
, making it return theEventType
field before.
public static final EventType TYPE = EventType.registerEventType("CustomEvent", CustomEvent.class);
@Override
public EventType getType() {
return TYPE;
}
- If you do not want to use Gson to serialize this type of event, you should add a public static method with the following name and signature:
public static CustomEvent fromJson(JsonElement json, Gson gson) {...}
otherwise, WSServer will use {@code gson.fromJson} to serialize it when receiving event with the specific name.
- Finally, you can use
registerReceiver(CustomEvent.TYPE, myReceiver)
to subscribe the custom event.