139 lines
5.1 KiB
Java
Executable File
139 lines
5.1 KiB
Java
Executable File
package net.krakatoaapi;
|
|
|
|
import de.dytanic.cloudnet.driver.CloudNetDriver;
|
|
import earth.krakatao.KraSocketClient;
|
|
import earth.krakatao.KraSocketClientConfig;
|
|
import earth.krakatao.events.KraSocketClientEventInitiater;
|
|
import earth.krakatao.events.KraSocketClientEventInterface;
|
|
import earth.krakatao.protocol.KraSocketClientProtocol;
|
|
import earth.krakatao.protocol.KraSocketClientProtocolDest;
|
|
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
|
import earth.krakatao.protocol.KraSocketClientProtocolStatus;
|
|
import earth.krakatoa.core.config.MongoConfig;
|
|
import earth.krakatoa.core.config.RedisConfig;
|
|
import earth.krakatoa.core.mongo.MongoManager;
|
|
import earth.krakatoa.core.redis.RedisManager;
|
|
import earth.krakatoa.core.util.Formatter;
|
|
import lombok.Getter;
|
|
import net.krakatoaapi.command.KrakataoCommand;
|
|
import net.krakatoaapi.config.ConfigHandler;
|
|
import net.krakatoaapi.listener.PlayerJoinListener;
|
|
import net.krakatoaapi.listener.PlayerQuitListener;
|
|
import net.krakatoaapi.listener.SocketClientMessageListener;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
import org.bukkit.plugin.PluginManager;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URISyntaxException;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
|
|
@Getter
|
|
public class KrakatoaAPI extends JavaPlugin {
|
|
|
|
@Getter
|
|
private static KrakatoaAPI instance;
|
|
|
|
private ConfigHandler configHandler;
|
|
private KraSocketClientProtocol kraSocketClientProtocol;
|
|
private KraSocketClient kraSocketClient;
|
|
private KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
|
private KraSocketClientEventInterface kraSocketClientEventInterface;
|
|
|
|
private MongoManager mongoManager;
|
|
private RedisManager redisManager;
|
|
|
|
private Formatter formatter;
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
super.onEnable();
|
|
|
|
instance = this;
|
|
this.configHandler = new ConfigHandler();
|
|
|
|
try {
|
|
this.configHandler.load();
|
|
} catch (IOException | InvalidConfigurationException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
this.kraSocketClientProtocol = new KraSocketClientProtocol();
|
|
|
|
KraSocketClientConfig kraSocketClientConfig = new KraSocketClientConfig(
|
|
this.configHandler.getWebSocketProtocol(), this.configHandler.getWebSocketHost(),
|
|
this.configHandler.getWebSocketPort(), this.configHandler.getWebSocketAccessKey(),
|
|
CloudNetDriver.getInstance().getComponentName(),
|
|
this.configHandler.getWebSocketAckTimeouts());
|
|
|
|
this.kraSocketClientEventInitiater = new KraSocketClientEventInitiater();
|
|
this.kraSocketClientEventInterface = new SocketClientMessageListener();
|
|
|
|
try {
|
|
this.kraSocketClient = new KraSocketClient(kraSocketClientConfig,
|
|
this.kraSocketClientEventInterface, this.kraSocketClientProtocol);
|
|
} catch (URISyntaxException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
this.kraSocketClient.getSocketClient().connect();
|
|
|
|
this.getLogger().info("testaa lol");
|
|
|
|
this.mongoManager = new MongoManager(new MongoConfig(this.configHandler.getMongodbHost(),
|
|
this.configHandler.getMongodbPort(), this.configHandler.getMongodbUsername(),
|
|
this.configHandler.getMongodbPassword(),
|
|
this.configHandler.getMongodbDatabase()));
|
|
this.mongoManager.connect();
|
|
|
|
this.redisManager = new RedisManager(
|
|
new RedisConfig(this.configHandler.getRedisHost(), this.getConfigHandler().getRedisPort()));
|
|
this.redisManager.connect();
|
|
|
|
this.formatter = new Formatter();
|
|
|
|
loadListeners();
|
|
loadCommands();
|
|
|
|
// call to proxy that the server is only for the auto restart system
|
|
Bukkit.getScheduler().runTaskLaterAsynchronously(this, () -> {
|
|
KraSocketClientProtocolMessage kraSocketClientProtocolMessage = new KraSocketClientProtocolMessage(
|
|
KraSocketClientProtocolStatus.SEND.getStatus(), 0,
|
|
KraSocketClientProtocolDest.PROXY.getStatus(),
|
|
UUID.fromString("c7b023ac-0303-4275-923d-3a1028e887a0"),
|
|
(short) 59739,
|
|
CloudNetDriver.getInstance().getComponentName(), null);
|
|
|
|
this.kraSocketClient.getSocketClient().SendMessage(kraSocketClientProtocolMessage);
|
|
}, 3 * 20);
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
super.onDisable();
|
|
|
|
this.mongoManager.shutdown();
|
|
this.redisManager.shutdown();
|
|
|
|
this.kraSocketClient.getSocketClient().getConnection().close();
|
|
this.kraSocketClient.getSocketClient().getConnection().closeConnection(1, "");
|
|
|
|
instance = null;
|
|
}
|
|
|
|
public void loadListeners() {
|
|
PluginManager pluginManager = Bukkit.getPluginManager();
|
|
List.of(new PlayerJoinListener(), new PlayerQuitListener()).forEach(listener -> {
|
|
pluginManager.registerEvents(listener, this);
|
|
});
|
|
}
|
|
|
|
public void loadCommands() {
|
|
Objects.requireNonNull(getCommand("krakatoa")).setExecutor(new KrakataoCommand());
|
|
}
|
|
|
|
}
|