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;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.name.Named;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -36,22 +38,26 @@ import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
|
||||
import org.yaml.snakeyaml.introspector.BeanAccess;
|
||||
|
||||
public class DatabaseConfigLoader {
|
||||
private final Path dataDirectory;
|
||||
private final String name;
|
||||
private final ClassLoader classLoader;
|
||||
private final JsonObject initData;
|
||||
private final Yaml yaml;
|
||||
private Yaml yaml;
|
||||
|
||||
public DatabaseConfigLoader(
|
||||
Path dataDirectory,
|
||||
String databaseName,
|
||||
ClassLoader classLoader,
|
||||
JsonObject initData) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.name = databaseName;
|
||||
this.classLoader = classLoader;
|
||||
this.initData = initData;
|
||||
@Inject
|
||||
@Named("dataDirectory")
|
||||
private Path dataDirectory;
|
||||
|
||||
@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.setBeanAccess(BeanAccess.FIELD);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import org.geysermc.floodgate.api.logger.FloodgateLogger;
|
||||
import org.geysermc.floodgate.config.FloodgateConfig;
|
||||
import org.geysermc.floodgate.database.config.DatabaseConfig;
|
||||
import org.geysermc.floodgate.database.config.DatabaseConfigLoader;
|
||||
import org.geysermc.floodgate.util.InjectorHolder;
|
||||
|
||||
public abstract class CommonPlayerLink implements PlayerLink {
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
@@ -57,7 +58,7 @@ public abstract class CommonPlayerLink implements PlayerLink {
|
||||
private FloodgateApi api;
|
||||
|
||||
@Inject
|
||||
private DatabaseConfigLoader configLoader;
|
||||
private InjectorHolder injectorHolder;
|
||||
|
||||
@Inject
|
||||
private void init(FloodgateConfig config) {
|
||||
@@ -86,8 +87,8 @@ public abstract class CommonPlayerLink implements PlayerLink {
|
||||
*/
|
||||
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
|
||||
// DatabaseConfig every time you run this method.
|
||||
return configLoader.loadAs(configClass);
|
||||
// DatabaseConfigLoader and DatabaseConfig every time you run this method.
|
||||
return injectorHolder.get().getInstance(DatabaseConfigLoader.class).loadAs(configClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.google.gson.JsonObject;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.name.Names;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -45,8 +46,8 @@ import javax.inject.Named;
|
||||
import org.geysermc.floodgate.api.link.PlayerLink;
|
||||
import org.geysermc.floodgate.api.logger.FloodgateLogger;
|
||||
import org.geysermc.floodgate.config.FloodgateConfig;
|
||||
import org.geysermc.floodgate.database.config.DatabaseConfigLoader;
|
||||
import org.geysermc.floodgate.util.Constants;
|
||||
import org.geysermc.floodgate.util.InjectorHolder;
|
||||
import org.geysermc.floodgate.util.Utils;
|
||||
|
||||
@Singleton
|
||||
@@ -155,12 +156,20 @@ public final class PlayerLinkLoader {
|
||||
|
||||
init = false;
|
||||
|
||||
InjectorHolder injectorHolder = new InjectorHolder();
|
||||
Injector linkInjector = injector.createChildInjector(binder -> {
|
||||
DatabaseConfigLoader configLoader = new DatabaseConfigLoader(
|
||||
dataDirectory, databaseName, classLoader, linkConfig
|
||||
);
|
||||
binder.bind(DatabaseConfigLoader.class).toInstance(configLoader);
|
||||
binder.bind(String.class)
|
||||
.annotatedWith(Names.named("databaseName"))
|
||||
.toInstance(databaseName);
|
||||
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);
|
||||
|
||||
|
||||
@@ -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 {
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
MysqlConfig databaseconfig = getConfig(MysqlConfig.class);
|
||||
pool = new MariaDbPoolDataSource(
|
||||
"jdbc:mariadb://" + databaseconfig.getHostname() + "/" +
|
||||
databaseconfig.getDatabase() +
|
||||
"?user=" + databaseconfig.getUsername() + "&password=" +
|
||||
databaseconfig.getPassword() +
|
||||
"&minPoolSize=2&maxPoolSize=10"
|
||||
);
|
||||
|
||||
pool = new MariaDbPoolDataSource();
|
||||
|
||||
String hostname = databaseconfig.getHostname();
|
||||
if (hostname.contains(":")) {
|
||||
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 (Statement statement = connection.createStatement()) {
|
||||
statement.setQueryTimeout(25); // set timeout to 30 sec.
|
||||
statement.executeUpdate(
|
||||
"CREATE TABLE IF NOT EXISTS `LinkedPlayers` ( " +
|
||||
"`bedrockId` BINARY(16) NOT NULL , " +
|
||||
@@ -150,7 +164,9 @@ public class MysqlDatabase extends CommonPlayerLink {
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public CompletableFuture<Void> linkPlayer(@NonNull UUID bedrockId, @NonNull UUID javaId,
|
||||
public CompletableFuture<Void> linkPlayer(
|
||||
@NonNull UUID bedrockId,
|
||||
@NonNull UUID javaId,
|
||||
@NonNull String javaUsername) {
|
||||
return CompletableFuture.runAsync(
|
||||
() -> linkPlayer0(bedrockId, javaId, javaUsername),
|
||||
@@ -198,7 +214,8 @@ public class MysqlDatabase extends CommonPlayerLink {
|
||||
@Override
|
||||
@NonNull
|
||||
public CompletableFuture<String> createLinkRequest(
|
||||
@NonNull UUID javaId, @NonNull String javaUsername,
|
||||
@NonNull UUID javaId,
|
||||
@NonNull String javaUsername,
|
||||
@NonNull String bedrockUsername) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
String linkCode = createCode();
|
||||
@@ -209,7 +226,10 @@ public class MysqlDatabase extends CommonPlayerLink {
|
||||
}, getExecutorService());
|
||||
}
|
||||
|
||||
private void createLinkRequest0(String javaUsername, UUID javaId, String linkCode,
|
||||
private void createLinkRequest0(
|
||||
String javaUsername,
|
||||
UUID javaId,
|
||||
String linkCode,
|
||||
String bedrockUsername) {
|
||||
try (Connection connection = pool.getConnection()) {
|
||||
try (PreparedStatement query = connection.prepareStatement(
|
||||
|
||||
@@ -61,7 +61,6 @@ public class SqliteDatabase extends CommonPlayerLink {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + databasePath.toString());
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.setQueryTimeout(30); // set timeout to 30 sec.
|
||||
statement.executeUpdate(
|
||||
"create table if not exists LinkedPlayers (bedrockId string, javaUniqueId string, javaUsername string)"
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user