test
parent
e887182a54
commit
34fb92dbcb
6
pom.xml
6
pom.xml
|
@ -97,6 +97,12 @@
|
||||||
<repository>
|
<repository>
|
||||||
<id>krakatoa-earth</id>
|
<id>krakatoa-earth</id>
|
||||||
<url>https://repo.krakatoa.umbach.dev/repository/krakatoa-earth/</url>
|
<url>https://repo.krakatoa.umbach.dev/repository/krakatoa-earth/</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</releases>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
</repository>
|
</repository>
|
||||||
<!-- This adds the Spigot Maven repository to the build -->
|
<!-- This adds the Spigot Maven repository to the build -->
|
||||||
<repository>
|
<repository>
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
package net.krakatoaapi;
|
package net.krakatoaapi;
|
||||||
|
|
||||||
import de.dytanic.cloudnet.driver.CloudNetDriver;
|
import de.dytanic.cloudnet.CloudNet;
|
||||||
|
import earth.krakatao.KraSocketClient;
|
||||||
|
import earth.krakatao.KraSocketClientConfig;
|
||||||
|
import earth.krakatao.events.KraSocketClientEventInitiater;
|
||||||
|
import earth.krakatao.events.KraSocketClientEventInterface;
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocol;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.krakatoaapi.config.ConfigHandler;
|
import net.krakatoaapi.config.ConfigHandler;
|
||||||
import net.krakatoaapi.listener.PlayerJoinListener;
|
import net.krakatoaapi.listener.PlayerJoinListener;
|
||||||
|
import net.krakatoaapi.listener.SocketClientMessageListener;
|
||||||
import net.krakatoaapi.mongo.MongoManager;
|
import net.krakatoaapi.mongo.MongoManager;
|
||||||
import net.krakatoaapi.protocol.KraProtocol;
|
|
||||||
import net.krakatoaapi.socket.SocketClient;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
@ -24,40 +27,60 @@ public class KrakatoaAPI extends JavaPlugin {
|
||||||
|
|
||||||
private ConfigHandler configHandler;
|
private ConfigHandler configHandler;
|
||||||
private MongoManager mongoManager;
|
private MongoManager mongoManager;
|
||||||
private KraProtocol kraProtocol;
|
private KraSocketClientProtocol kraProtocol;
|
||||||
private SocketClient socketClient;
|
private KraSocketClient kraSocketClient;
|
||||||
|
private KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
||||||
|
private KraSocketClientEventInterface kraSocketClientEventInterface;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
super.onEnable();
|
super.onEnable();
|
||||||
|
|
||||||
instance = this;
|
instance = this;
|
||||||
configHandler = new ConfigHandler();
|
this.configHandler = new ConfigHandler();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
configHandler.load();
|
this.configHandler.load();
|
||||||
} catch (IOException | InvalidConfigurationException e) {
|
} catch (IOException | InvalidConfigurationException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
kraProtocol = new KraProtocol();
|
this.kraProtocol = new KraSocketClientProtocol();
|
||||||
|
|
||||||
mongoManager = new MongoManager(configHandler.getMongodbHost(),
|
KraSocketClientConfig kraSocketClientConfig = new KraSocketClientConfig(
|
||||||
configHandler.getMongodbPort(), configHandler.getMongodbUsername(),
|
this.configHandler.getWebSocketProtocol(), this.configHandler.getWebSocketHost(),
|
||||||
configHandler.getMongodbPassword());
|
this.configHandler.getWebSocketPort(), this.configHandler.getWebSocketAccessKey(),
|
||||||
mongoManager.connect(configHandler.getMongodbDatabase());
|
CloudNet.getInstance().getComponentName());
|
||||||
|
|
||||||
|
this.kraSocketClientEventInitiater = new KraSocketClientEventInitiater();
|
||||||
|
this.kraSocketClientEventInterface = new SocketClientMessageListener();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socketClient = new SocketClient(
|
this.kraSocketClient = new KraSocketClient(kraSocketClientConfig,
|
||||||
new URI(configHandler.getWebSocketProtocol() + "://" + configHandler.getWebSocketHost()
|
this.kraSocketClientEventInterface, this.kraProtocol);
|
||||||
+ ":" + configHandler.getWebSocketPort() + "/ws?t="
|
|
||||||
+ configHandler.getWebSocketAccessKey() + "&s=" + CloudNetDriver.getInstance()
|
|
||||||
.getComponentName()));
|
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
socketClient.connect();
|
this.mongoManager = new MongoManager(this.configHandler.getMongodbHost(),
|
||||||
|
this.configHandler.getMongodbPort(), this.configHandler.getMongodbUsername(),
|
||||||
|
this.configHandler.getMongodbPassword());
|
||||||
|
this.mongoManager.connect(this.configHandler.getMongodbDatabase());
|
||||||
|
|
||||||
|
this.kraSocketClient.sendTest();
|
||||||
|
|
||||||
|
/*
|
||||||
|
try {
|
||||||
|
socketClient = new SocketClient(
|
||||||
|
new URI(configHandler.getWebSocketProtocol() + "://" + configHandler.getWebSocketHost()
|
||||||
|
+ ":" + configHandler.getWebSocketPort() + "/ws?t="
|
||||||
|
+ configHandler.getWebSocketAccessKey() + "&s=" + CloudNetDriver.getInstance()
|
||||||
|
.getComponentName()));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
socketClient.connect(); */
|
||||||
|
|
||||||
loadListeners();
|
loadListeners();
|
||||||
}
|
}
|
||||||
|
@ -66,8 +89,8 @@ public class KrakatoaAPI extends JavaPlugin {
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
super.onDisable();
|
super.onDisable();
|
||||||
|
|
||||||
socketClient.getConnection().close();
|
//socketClient.getConnection().close();
|
||||||
socketClient.getConnection().closeConnection(1, "");
|
//socketClient.getConnection().closeConnection(1, "");
|
||||||
|
|
||||||
instance = null;
|
instance = null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package net.krakatoaapi.listener;
|
||||||
|
|
||||||
|
import earth.krakatao.events.KraSocketClientEventInterface;
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
||||||
|
import org.java_websocket.handshake.ServerHandshake;
|
||||||
|
|
||||||
|
public class SocketClientMessageListener implements KraSocketClientEventInterface {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kraSocketClientOnMessage(
|
||||||
|
KraSocketClientProtocolMessage kraSocketClientProtocolMessage) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kraSocketClientOnOpen(ServerHandshake serverHandshake) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kraSocketClientOnClose(int i, String s, boolean b) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,96 +0,0 @@
|
||||||
package net.krakatoaapi.protocol;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import net.krakatoaapi.KrakatoaAPI;
|
|
||||||
|
|
||||||
public class KraProtocol {
|
|
||||||
|
|
||||||
public void SendMessage(KraProtocolMessage kraProtocolMessage) {
|
|
||||||
SendMessage(kraProtocolMessage.getStatus(), kraProtocolMessage.getCmdID(),
|
|
||||||
kraProtocolMessage.getDest(), kraProtocolMessage.getUuid(),
|
|
||||||
kraProtocolMessage.getCmdNumber(), kraProtocolMessage.getArgs());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SendMessage(int status, int cmdID, int dest, String uuid, int cmdNumber,
|
|
||||||
String args) {
|
|
||||||
System.out.println("sendMessage: " + status + " " + cmdID + " " + dest + " " + cmdNumber);
|
|
||||||
|
|
||||||
int argLen = args.length();
|
|
||||||
|
|
||||||
byte[] raw = new byte[40 + argLen];
|
|
||||||
|
|
||||||
raw[0] = (byte) status;
|
|
||||||
|
|
||||||
raw[1] = (byte) cmdID;
|
|
||||||
raw[2] = (byte) (cmdID >> 8);
|
|
||||||
raw[3] = (byte) (cmdID >> 16);
|
|
||||||
raw[4] = (byte) (cmdID >> 24);
|
|
||||||
|
|
||||||
raw[5] = (byte) dest;
|
|
||||||
|
|
||||||
byte[] uuidBytes = uuid.getBytes();
|
|
||||||
|
|
||||||
/* for (int i = 0; i < 32; i++) {
|
|
||||||
raw[6 + i] = uuidBytes[i];
|
|
||||||
}*/
|
|
||||||
|
|
||||||
System.arraycopy(uuidBytes, 0, raw, 6, 32);
|
|
||||||
|
|
||||||
raw[38] = (byte) cmdNumber;
|
|
||||||
raw[39] = (byte) (cmdNumber >> 8);
|
|
||||||
|
|
||||||
if (argLen > 0) {
|
|
||||||
for (int i = 0; i < argLen; i++) {
|
|
||||||
raw[40 + i] = (byte) args.charAt(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("raw: " + Arrays.toString(raw));
|
|
||||||
|
|
||||||
KrakatoaAPI.getInstance().getSocketClient().SendMessage(raw);
|
|
||||||
}
|
|
||||||
|
|
||||||
public KraProtocolMessage DecodeMessage(byte[] data) {
|
|
||||||
System.out.println("start decode");
|
|
||||||
|
|
||||||
byte status = data[0];
|
|
||||||
|
|
||||||
int cmdID = Byte.toUnsignedInt(data[1]);
|
|
||||||
|
|
||||||
cmdID += Byte.toUnsignedInt(data[2]) * 256;
|
|
||||||
cmdID += Byte.toUnsignedInt(data[3]) * 256 * 256;
|
|
||||||
cmdID += Byte.toUnsignedInt(data[4]) * 256 * 256 * 256;
|
|
||||||
|
|
||||||
int dest = Byte.toUnsignedInt(data[5]);
|
|
||||||
|
|
||||||
byte[] playerUuidBytes = new byte[32];
|
|
||||||
|
|
||||||
System.arraycopy(data, 6, playerUuidBytes, 0, 32);
|
|
||||||
|
|
||||||
/* for (int i = 0; i < 32; i++) {
|
|
||||||
playerUuidBytes[i] = data[6 + i];
|
|
||||||
} */
|
|
||||||
|
|
||||||
String playerUuid = new String(playerUuidBytes, StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
short cmdNumber = (short) (Byte.toUnsignedInt(data[38]) + (Byte.toUnsignedInt(data[39]) * 256));
|
|
||||||
|
|
||||||
int argLen = data.length - 40;
|
|
||||||
byte[] argsBytes = new byte[argLen];
|
|
||||||
|
|
||||||
/* for (int i = 0; i < argLen; i++) {
|
|
||||||
argsBytes[i] = data[40 + i];
|
|
||||||
} */
|
|
||||||
|
|
||||||
System.arraycopy(data, 40, argsBytes, 0, argLen);
|
|
||||||
|
|
||||||
String args = new String(argsBytes, StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
System.out.println(
|
|
||||||
"decoded message " + status + " " + cmdID + " " + dest + " " + playerUuid + " "
|
|
||||||
+ cmdNumber + " " + args + " ");
|
|
||||||
|
|
||||||
return new KraProtocolMessage(status, cmdID, dest, playerUuid, cmdNumber, args);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
package net.krakatoaapi.protocol;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Getter
|
|
||||||
public class KraProtocolMessage {
|
|
||||||
|
|
||||||
private final byte status;
|
|
||||||
private final int cmdID; // long
|
|
||||||
private final int dest;
|
|
||||||
private final String uuid;
|
|
||||||
private final short cmdNumber;
|
|
||||||
private final String args;
|
|
||||||
|
|
||||||
public int getStatus() {
|
|
||||||
return Byte.toUnsignedInt(status);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCmdNumber() {
|
|
||||||
return Short.toUnsignedInt(cmdNumber);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,69 +0,0 @@
|
||||||
package net.krakatoaapi.socket;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import net.krakatoaapi.KrakatoaAPI;
|
|
||||||
import net.krakatoaapi.protocol.KraProtocolMessage;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.java_websocket.client.WebSocketClient;
|
|
||||||
import org.java_websocket.handshake.ServerHandshake;
|
|
||||||
|
|
||||||
public class SocketClient extends WebSocketClient {
|
|
||||||
|
|
||||||
public SocketClient(URI uri) {
|
|
||||||
super(uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SendMessage(byte[] message) {
|
|
||||||
if (isOpen()) {
|
|
||||||
send(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onOpen(ServerHandshake serverHandshake) {
|
|
||||||
System.out.println("Opened connection!");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMessage(String s) {
|
|
||||||
System.out.println("recv s: " + s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMessage(ByteBuffer byteBuffer) {
|
|
||||||
System.out.println("recv b: " + Arrays.toString(byteBuffer.array()));
|
|
||||||
|
|
||||||
KraProtocolMessage kraProtocolMessage = KrakatoaAPI.getInstance().getKraProtocol()
|
|
||||||
.DecodeMessage(byteBuffer.array());
|
|
||||||
|
|
||||||
Bukkit.getScheduler().runTask(KrakatoaAPI.getInstance(),
|
|
||||||
() -> Bukkit.getServer().getPluginManager()
|
|
||||||
.callEvent(new SocketMessageEvent(kraProtocolMessage.getStatus(),
|
|
||||||
kraProtocolMessage.getCmdID(), kraProtocolMessage.getDest(),
|
|
||||||
kraProtocolMessage.getUuid(), kraProtocolMessage.getCmdNumber(),
|
|
||||||
kraProtocolMessage.getArgs())));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClose(int code, String reason, boolean remote) {
|
|
||||||
System.out.println(
|
|
||||||
"Connection! closed by " + (remote ? "remote peer" : "us") + " Code: " + code + " Reason: "
|
|
||||||
+ reason);
|
|
||||||
|
|
||||||
/* 1 called on plugin disable */
|
|
||||||
if (code != 1) {
|
|
||||||
Bukkit.getScheduler().runTaskLater(KrakatoaAPI.getInstance(), () -> {
|
|
||||||
System.out.println("try reconnect");
|
|
||||||
reconnect();
|
|
||||||
}, 20L * 3L);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Exception e) {
|
|
||||||
System.out.println("Error: " + e);
|
|
||||||
//e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package net.krakatoaapi.socket;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.bukkit.event.Event;
|
|
||||||
import org.bukkit.event.HandlerList;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
@Getter
|
|
||||||
public class SocketMessageEvent extends Event {
|
|
||||||
|
|
||||||
private static final HandlerList handlers = new HandlerList();
|
|
||||||
|
|
||||||
@Setter
|
|
||||||
private boolean cancelled;
|
|
||||||
|
|
||||||
private final int status;
|
|
||||||
private final int cmdID;
|
|
||||||
private final int dest;
|
|
||||||
private final String uuid;
|
|
||||||
private final int commandNumber;
|
|
||||||
private final String args;
|
|
||||||
|
|
||||||
public static HandlerList getHandlerList() {
|
|
||||||
return handlers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull HandlerList getHandlers() {
|
|
||||||
return handlers;
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,5 +0,0 @@
|
||||||
#Generated by Maven
|
|
||||||
#Sat Oct 16 15:56:37 CEST 2021
|
|
||||||
groupId=net.krakatoaapi
|
|
||||||
artifactId=KrakatoaAPI
|
|
||||||
version=1.0-SNAPSHOT
|
|
|
@ -1,9 +1,3 @@
|
||||||
net/krakatoaapi/listener/PlayerJoinListener.class
|
net/krakatoaapi/listener/PlayerJoinListener.class
|
||||||
net/krakatoaapi/mongo/MongoManager.class
|
net/krakatoaapi/listener/SocketClientMessageListener.class
|
||||||
net/krakatoaapi/protocol/KraProtocolMessage.class
|
|
||||||
net/krakatoaapi/socket/SocketClient.class
|
|
||||||
net/krakatoaapi/builder/ItemBuilder.class
|
net/krakatoaapi/builder/ItemBuilder.class
|
||||||
net/krakatoaapi/KrakatoaAPI.class
|
|
||||||
net/krakatoaapi/config/ConfigHandler.class
|
|
||||||
net/krakatoaapi/socket/SocketMessageEvent.class
|
|
||||||
net/krakatoaapi/protocol/KraProtocol.class
|
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/listener/PlayerJoinListener.java
|
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/listener/PlayerJoinListener.java
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/socket/SocketMessageEvent.java
|
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/listener/SocketClientMessageListener.java
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/builder/ItemBuilder.java
|
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/builder/ItemBuilder.java
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/protocol/KraProtocol.java
|
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/KrakatoaAPI.java
|
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/KrakatoaAPI.java
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/mongo/MongoManager.java
|
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/mongo/MongoManager.java
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/socket/SocketClient.java
|
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/config/ConfigHandler.java
|
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/config/ConfigHandler.java
|
||||||
/home/alex/IdeaProjects/KrakatoaAPI/src/main/java/net/krakatoaapi/protocol/KraProtocolMessage.java
|
|
||||||
|
|
Loading…
Reference in New Issue