mirror of
https://github.com/GeyserMC/Floodgate.git
synced 2025-12-19 14:59:20 +00:00
Some more changes
This commit is contained in:
@@ -26,6 +26,8 @@
|
|||||||
package org.geysermc.floodgate.database.config;
|
package org.geysermc.floodgate.database.config;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.name.Named;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -36,22 +38,26 @@ import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
|
|||||||
import org.yaml.snakeyaml.introspector.BeanAccess;
|
import org.yaml.snakeyaml.introspector.BeanAccess;
|
||||||
|
|
||||||
public class DatabaseConfigLoader {
|
public class DatabaseConfigLoader {
|
||||||
private final Path dataDirectory;
|
private Yaml yaml;
|
||||||
private final String name;
|
|
||||||
private final ClassLoader classLoader;
|
|
||||||
private final JsonObject initData;
|
|
||||||
private final Yaml yaml;
|
|
||||||
|
|
||||||
public DatabaseConfigLoader(
|
@Inject
|
||||||
Path dataDirectory,
|
@Named("dataDirectory")
|
||||||
String databaseName,
|
private Path dataDirectory;
|
||||||
ClassLoader classLoader,
|
|
||||||
JsonObject initData) {
|
|
||||||
this.dataDirectory = dataDirectory;
|
|
||||||
this.name = databaseName;
|
|
||||||
this.classLoader = classLoader;
|
|
||||||
this.initData = initData;
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Named("databaseName")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Named("databaseClassLoader")
|
||||||
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Named("databaseInitData")
|
||||||
|
private JsonObject initData;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public void init() {
|
||||||
yaml = new Yaml(new CustomClassLoaderConstructor(classLoader));
|
yaml = new Yaml(new CustomClassLoaderConstructor(classLoader));
|
||||||
yaml.setBeanAccess(BeanAccess.FIELD);
|
yaml.setBeanAccess(BeanAccess.FIELD);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import org.geysermc.floodgate.api.logger.FloodgateLogger;
|
|||||||
import org.geysermc.floodgate.config.FloodgateConfig;
|
import org.geysermc.floodgate.config.FloodgateConfig;
|
||||||
import org.geysermc.floodgate.database.config.DatabaseConfig;
|
import org.geysermc.floodgate.database.config.DatabaseConfig;
|
||||||
import org.geysermc.floodgate.database.config.DatabaseConfigLoader;
|
import org.geysermc.floodgate.database.config.DatabaseConfigLoader;
|
||||||
|
import org.geysermc.floodgate.util.InjectorHolder;
|
||||||
|
|
||||||
public abstract class CommonPlayerLink implements PlayerLink {
|
public abstract class CommonPlayerLink implements PlayerLink {
|
||||||
@Getter(AccessLevel.PROTECTED)
|
@Getter(AccessLevel.PROTECTED)
|
||||||
@@ -57,7 +58,7 @@ public abstract class CommonPlayerLink implements PlayerLink {
|
|||||||
private FloodgateApi api;
|
private FloodgateApi api;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private DatabaseConfigLoader configLoader;
|
private InjectorHolder injectorHolder;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private void init(FloodgateConfig config) {
|
private void init(FloodgateConfig config) {
|
||||||
@@ -86,8 +87,8 @@ public abstract class CommonPlayerLink implements PlayerLink {
|
|||||||
*/
|
*/
|
||||||
public <T extends DatabaseConfig> T getConfig(Class<T> configClass) {
|
public <T extends DatabaseConfig> T getConfig(Class<T> configClass) {
|
||||||
// this method is not intended to be used more than once. It'll make a new instance of
|
// this method is not intended to be used more than once. It'll make a new instance of
|
||||||
// DatabaseConfig every time you run this method.
|
// DatabaseConfigLoader and DatabaseConfig every time you run this method.
|
||||||
return configLoader.loadAs(configClass);
|
return injectorHolder.get().getInstance(DatabaseConfigLoader.class).loadAs(configClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import com.google.gson.JsonObject;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
import com.google.inject.name.Names;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@@ -45,8 +46,8 @@ import javax.inject.Named;
|
|||||||
import org.geysermc.floodgate.api.link.PlayerLink;
|
import org.geysermc.floodgate.api.link.PlayerLink;
|
||||||
import org.geysermc.floodgate.api.logger.FloodgateLogger;
|
import org.geysermc.floodgate.api.logger.FloodgateLogger;
|
||||||
import org.geysermc.floodgate.config.FloodgateConfig;
|
import org.geysermc.floodgate.config.FloodgateConfig;
|
||||||
import org.geysermc.floodgate.database.config.DatabaseConfigLoader;
|
|
||||||
import org.geysermc.floodgate.util.Constants;
|
import org.geysermc.floodgate.util.Constants;
|
||||||
|
import org.geysermc.floodgate.util.InjectorHolder;
|
||||||
import org.geysermc.floodgate.util.Utils;
|
import org.geysermc.floodgate.util.Utils;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@@ -155,12 +156,20 @@ public final class PlayerLinkLoader {
|
|||||||
|
|
||||||
init = false;
|
init = false;
|
||||||
|
|
||||||
|
InjectorHolder injectorHolder = new InjectorHolder();
|
||||||
Injector linkInjector = injector.createChildInjector(binder -> {
|
Injector linkInjector = injector.createChildInjector(binder -> {
|
||||||
DatabaseConfigLoader configLoader = new DatabaseConfigLoader(
|
binder.bind(String.class)
|
||||||
dataDirectory, databaseName, classLoader, linkConfig
|
.annotatedWith(Names.named("databaseName"))
|
||||||
);
|
.toInstance(databaseName);
|
||||||
binder.bind(DatabaseConfigLoader.class).toInstance(configLoader);
|
binder.bind(ClassLoader.class).annotatedWith(
|
||||||
|
Names.named("databaseClassLoader")).toInstance(classLoader);
|
||||||
|
binder.bind(JsonObject.class)
|
||||||
|
.annotatedWith(Names.named("databaseInitData"))
|
||||||
|
.toInstance(linkConfig);
|
||||||
|
binder.bind(InjectorHolder.class)
|
||||||
|
.toInstance(injectorHolder);
|
||||||
});
|
});
|
||||||
|
injectorHolder.set(linkInjector);
|
||||||
|
|
||||||
PlayerLink instance = linkInjector.getInstance(mainClass);
|
PlayerLink instance = linkInjector.getInstance(mainClass);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author GeyserMC
|
||||||
|
* @link https://github.com/GeyserMC/Floodgate
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.geysermc.floodgate.util;
|
||||||
|
|
||||||
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
|
public class InjectorHolder {
|
||||||
|
private Injector injector;
|
||||||
|
|
||||||
|
public Injector get() {
|
||||||
|
return injector;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(Injector injector) {
|
||||||
|
this.injector = injector;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,17 +54,31 @@ public class MysqlDatabase extends CommonPlayerLink {
|
|||||||
try {
|
try {
|
||||||
Class.forName("org.mariadb.jdbc.Driver");
|
Class.forName("org.mariadb.jdbc.Driver");
|
||||||
MysqlConfig databaseconfig = getConfig(MysqlConfig.class);
|
MysqlConfig databaseconfig = getConfig(MysqlConfig.class);
|
||||||
pool = new MariaDbPoolDataSource(
|
|
||||||
"jdbc:mariadb://" + databaseconfig.getHostname() + "/" +
|
pool = new MariaDbPoolDataSource();
|
||||||
databaseconfig.getDatabase() +
|
|
||||||
"?user=" + databaseconfig.getUsername() + "&password=" +
|
String hostname = databaseconfig.getHostname();
|
||||||
databaseconfig.getPassword() +
|
if (hostname.contains(":")) {
|
||||||
"&minPoolSize=2&maxPoolSize=10"
|
String[] split = hostname.split(":");
|
||||||
);
|
|
||||||
|
pool.setServerName(split[0]);
|
||||||
|
try {
|
||||||
|
pool.setPortNumber(Integer.parseInt(split[1]));
|
||||||
|
} catch (NumberFormatException exception) {
|
||||||
|
getLogger().info("{} is not a valid port! Will use the default port", split[1]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pool.setServerName(hostname);
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.setUser(databaseconfig.getUsername());
|
||||||
|
pool.setPassword(databaseconfig.getPassword());
|
||||||
|
pool.setDatabaseName(databaseconfig.getDatabase());
|
||||||
|
pool.setMinPoolSize(2);
|
||||||
|
pool.setMaxPoolSize(10);
|
||||||
|
|
||||||
try (Connection connection = pool.getConnection()) {
|
try (Connection connection = pool.getConnection()) {
|
||||||
try (Statement statement = connection.createStatement()) {
|
try (Statement statement = connection.createStatement()) {
|
||||||
statement.setQueryTimeout(25); // set timeout to 30 sec.
|
|
||||||
statement.executeUpdate(
|
statement.executeUpdate(
|
||||||
"CREATE TABLE IF NOT EXISTS `LinkedPlayers` ( " +
|
"CREATE TABLE IF NOT EXISTS `LinkedPlayers` ( " +
|
||||||
"`bedrockId` BINARY(16) NOT NULL , " +
|
"`bedrockId` BINARY(16) NOT NULL , " +
|
||||||
@@ -150,8 +164,10 @@ public class MysqlDatabase extends CommonPlayerLink {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NonNull
|
@NonNull
|
||||||
public CompletableFuture<Void> linkPlayer(@NonNull UUID bedrockId, @NonNull UUID javaId,
|
public CompletableFuture<Void> linkPlayer(
|
||||||
@NonNull String javaUsername) {
|
@NonNull UUID bedrockId,
|
||||||
|
@NonNull UUID javaId,
|
||||||
|
@NonNull String javaUsername) {
|
||||||
return CompletableFuture.runAsync(
|
return CompletableFuture.runAsync(
|
||||||
() -> linkPlayer0(bedrockId, javaId, javaUsername),
|
() -> linkPlayer0(bedrockId, javaId, javaUsername),
|
||||||
getExecutorService());
|
getExecutorService());
|
||||||
@@ -198,7 +214,8 @@ public class MysqlDatabase extends CommonPlayerLink {
|
|||||||
@Override
|
@Override
|
||||||
@NonNull
|
@NonNull
|
||||||
public CompletableFuture<String> createLinkRequest(
|
public CompletableFuture<String> createLinkRequest(
|
||||||
@NonNull UUID javaId, @NonNull String javaUsername,
|
@NonNull UUID javaId,
|
||||||
|
@NonNull String javaUsername,
|
||||||
@NonNull String bedrockUsername) {
|
@NonNull String bedrockUsername) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
String linkCode = createCode();
|
String linkCode = createCode();
|
||||||
@@ -209,8 +226,11 @@ public class MysqlDatabase extends CommonPlayerLink {
|
|||||||
}, getExecutorService());
|
}, getExecutorService());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createLinkRequest0(String javaUsername, UUID javaId, String linkCode,
|
private void createLinkRequest0(
|
||||||
String bedrockUsername) {
|
String javaUsername,
|
||||||
|
UUID javaId,
|
||||||
|
String linkCode,
|
||||||
|
String bedrockUsername) {
|
||||||
try (Connection connection = pool.getConnection()) {
|
try (Connection connection = pool.getConnection()) {
|
||||||
try (PreparedStatement query = connection.prepareStatement(
|
try (PreparedStatement query = connection.prepareStatement(
|
||||||
"INSERT INTO `LinkedPlayersRequest` VALUES (?, ?, ?, ?, ?) " +
|
"INSERT INTO `LinkedPlayersRequest` VALUES (?, ?, ?, ?, ?) " +
|
||||||
|
|||||||
@@ -61,7 +61,6 @@ public class SqliteDatabase extends CommonPlayerLink {
|
|||||||
Class.forName("org.sqlite.JDBC");
|
Class.forName("org.sqlite.JDBC");
|
||||||
connection = DriverManager.getConnection("jdbc:sqlite:" + databasePath.toString());
|
connection = DriverManager.getConnection("jdbc:sqlite:" + databasePath.toString());
|
||||||
try (Statement statement = connection.createStatement()) {
|
try (Statement statement = connection.createStatement()) {
|
||||||
statement.setQueryTimeout(30); // set timeout to 30 sec.
|
|
||||||
statement.executeUpdate(
|
statement.executeUpdate(
|
||||||
"create table if not exists LinkedPlayers (bedrockId string, javaUniqueId string, javaUsername string)"
|
"create table if not exists LinkedPlayers (bedrockId string, javaUniqueId string, javaUsername string)"
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user