|
1 | 1 | package org.bsrserver; |
2 | 2 |
|
| 3 | +import java.sql.*; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.HashMap; |
| 6 | + |
| 7 | +import org.slf4j.Logger; |
3 | 8 | import com.google.inject.Inject; |
4 | 9 | import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; |
5 | 10 | import com.velocitypowered.api.plugin.Plugin; |
6 | 11 | import com.velocitypowered.api.event.Subscribe; |
7 | 12 | import com.velocitypowered.api.proxy.ProxyServer; |
| 13 | +import com.velocitypowered.api.plugin.annotation.DataDirectory; |
8 | 14 |
|
9 | 15 | @Plugin( |
10 | 16 | id = "bsrgreeter", |
|
16 | 22 | ) |
17 | 23 | public class Main { |
18 | 24 | private final ProxyServer proxyServer; |
| 25 | + private final Logger logger; |
| 26 | + private final Path dataDirectory; |
| 27 | + private final HashMap<String, ServerInfo> serverInfoHashMap = new HashMap<>(); |
19 | 28 |
|
20 | 29 | @Inject |
21 | | - public Main(ProxyServer proxyServer) { |
| 30 | + public Main(ProxyServer proxyServer, Logger logger, @DataDirectory Path dataDirectory) { |
22 | 31 | this.proxyServer = proxyServer; |
| 32 | + this.logger = logger; |
| 33 | + this.dataDirectory = dataDirectory; |
23 | 34 | } |
24 | 35 |
|
25 | 36 | @Subscribe |
26 | 37 | public void onInitialize(ProxyInitializeEvent event) { |
27 | | - proxyServer.getEventManager().register(this, new EventListener(proxyServer)); |
| 38 | + // load config |
| 39 | + Config.getInstance().loadConfig(dataDirectory); |
| 40 | + |
| 41 | + // read database |
| 42 | + try { |
| 43 | + // connect |
| 44 | + Class.forName("org.postgresql.Driver"); |
| 45 | + Connection connection = DriverManager.getConnection( |
| 46 | + Config.getInstance().getDatabaseUrl(), |
| 47 | + Config.getInstance().getDatabaseUser(), |
| 48 | + Config.getInstance().getDatabasePassword() |
| 49 | + ); |
| 50 | + logger.info("Successfully connected to database"); |
| 51 | + |
| 52 | + // select |
| 53 | + String tableName = Config.getInstance().getDatabaseTable(); |
| 54 | + Statement statement = connection.createStatement(); |
| 55 | + ResultSet resultSet = statement.executeQuery("SELECT * FROM " + tableName); |
| 56 | + while (resultSet.next()) { |
| 57 | + String serverName = resultSet.getString("server_name"); |
| 58 | + ServerInfo serverInfo = new ServerInfo( |
| 59 | + serverName, |
| 60 | + resultSet.getString("named_name"), |
| 61 | + resultSet.getDate("foundation_time") |
| 62 | + ); |
| 63 | + serverInfoHashMap.put(serverName, serverInfo); |
| 64 | + } |
| 65 | + resultSet.close(); |
| 66 | + statement.close(); |
| 67 | + connection.close(); |
| 68 | + logger.info("Loaded servers: " + serverInfoHashMap.keySet()); |
| 69 | + } catch (Exception e) { |
| 70 | + e.printStackTrace(); |
| 71 | + logger.error("Fail connect to database"); |
| 72 | + } |
| 73 | + |
| 74 | + // register command |
| 75 | + proxyServer.getEventManager().register(this, new EventListener(this)); |
| 76 | + } |
| 77 | + |
| 78 | + public ProxyServer getProxyServer() { |
| 79 | + return proxyServer; |
| 80 | + } |
| 81 | + |
| 82 | + public HashMap<String, ServerInfo> getServerInfoHashMap() { |
| 83 | + return serverInfoHashMap; |
28 | 84 | } |
29 | 85 | } |
0 commit comments