mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 14:38:49 +00:00
Initial MySQL work
This commit is contained in:
1 parent
bbb7b99dd2
commit
400bdd44a3
16 files changed
+400
-14
No files matched your search
@@ -43,12 +43,14 @@ import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver
|
||||
import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.JSONUserManager;
|
||||
import net.draycia.carbon.common.users.db.MariaDBUserManager;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.Moonshine;
|
||||
import net.kyori.moonshine.exception.scan.UnscannableMethodException;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
@@ -61,6 +63,17 @@ public final class CarbonCommonModule extends AbstractModule {
|
||||
final Injector injector
|
||||
) {
|
||||
switch (Objects.requireNonNull(configFactory.primaryConfig()).storageType()) {
|
||||
case MYSQL -> {
|
||||
final @Nullable UserManager<CarbonPlayerCommon> userManager = MariaDBUserManager.manager(
|
||||
configFactory.primaryConfig().databaseSettings(), this.getClass().getClassLoader());
|
||||
|
||||
if (userManager == null) {
|
||||
throw new IllegalStateException("Failure connecting to database.");
|
||||
}
|
||||
|
||||
return userManager;
|
||||
}
|
||||
case PSQL -> throw new UnsupportedOperationException("PostgreSQL is not supported yet.");
|
||||
default -> {
|
||||
return injector.getInstance(JSONUserManager.class);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.draycia.carbon.common.config;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
|
||||
@DefaultQualifier(Nullable.class)
|
||||
@ConfigSerializable
|
||||
public class DatabaseSettings {
|
||||
|
||||
@Comment("JDBC URL")
|
||||
private String url = "jdbc:mysql://localhost:3306/carbon";
|
||||
|
||||
@Comment("")
|
||||
private String username = "username";
|
||||
|
||||
@Comment("")
|
||||
private String password = "password";
|
||||
|
||||
public String url() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public String username() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public String password() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,6 +43,9 @@ public class PrimaryConfig {
|
||||
@Comment("The service that will be used to store and load player information.")
|
||||
private StorageType storageType = StorageType.JSON;
|
||||
|
||||
@Comment("")
|
||||
private DatabaseSettings databaseSettings = new DatabaseSettings();
|
||||
|
||||
@Comment("Various ClearChat command settings.")
|
||||
private ClearChatSettings clearChatSettings = new ClearChatSettings();
|
||||
|
||||
@@ -70,6 +73,10 @@ public class PrimaryConfig {
|
||||
return this.storageType;
|
||||
}
|
||||
|
||||
public DatabaseSettings databaseSettings() {
|
||||
return this.databaseSettings;
|
||||
}
|
||||
|
||||
public ClearChatSettings clearChatSettings() {
|
||||
return this.clearChatSettings;
|
||||
}
|
||||
|
||||
@@ -64,6 +64,28 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
// Punishments
|
||||
protected List<UUID> ignoredPlayers = new ArrayList<>();
|
||||
|
||||
public CarbonPlayerCommon(
|
||||
final boolean muted,
|
||||
final boolean deafened,
|
||||
final Key selectedChannel,
|
||||
final String username,
|
||||
final UUID uuid,
|
||||
final Component displayName,
|
||||
final UUID lastWhisperTarget,
|
||||
final UUID whisperReplyTarget,
|
||||
final boolean spying
|
||||
) {
|
||||
this.muted = muted;
|
||||
this.deafened = deafened;
|
||||
this.selectedChannel = selectedChannel;
|
||||
this.username = username;
|
||||
this.uuid = uuid;
|
||||
this.displayName = displayName;
|
||||
this.lastWhisperTarget = lastWhisperTarget;
|
||||
this.whisperReplyTarget = whisperReplyTarget;
|
||||
this.spying = spying;
|
||||
}
|
||||
|
||||
public CarbonPlayerCommon(
|
||||
final String username,
|
||||
final UUID uuid
|
||||
@@ -130,19 +152,29 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoring(final CarbonPlayer sender) {
|
||||
return this.ignoredPlayers.contains(sender.uuid());
|
||||
public boolean ignoring(final UUID player) {
|
||||
return this.ignoredPlayers.contains(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoring(final CarbonPlayer player) {
|
||||
return this.ignoring(player.uuid());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ignoring(final UUID player, final boolean nowIgnoring) {
|
||||
if (nowIgnoring) {
|
||||
if (!this.ignoredPlayers.contains(player)) {
|
||||
this.ignoredPlayers.add(player);
|
||||
}
|
||||
} else {
|
||||
this.ignoredPlayers.remove(player);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ignoring(final CarbonPlayer player, final boolean nowIgnoring) {
|
||||
if (nowIgnoring) {
|
||||
if (!this.ignoredPlayers.contains(player.uuid())) {
|
||||
this.ignoredPlayers.add(player.uuid());
|
||||
}
|
||||
} else {
|
||||
this.ignoredPlayers.remove(player.uuid());
|
||||
}
|
||||
this.ignoring(player.uuid(), nowIgnoring);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -55,8 +55,18 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoring(final CarbonPlayer sender) {
|
||||
return this.carbonPlayerCommon().ignoring(sender);
|
||||
public boolean ignoring(final UUID player) {
|
||||
return this.carbonPlayerCommon().ignoring(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoring(final CarbonPlayer player) {
|
||||
return this.carbonPlayerCommon().ignoring(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ignoring(final UUID player, final boolean nowIgnoring) {
|
||||
this.carbonPlayerCommon().ignoring(player, nowIgnoring);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package net.draycia.carbon.common.users.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.jdbi.v3.core.mapper.RowMapper;
|
||||
import org.jdbi.v3.core.statement.StatementContext;
|
||||
|
||||
public class CarbonPlayerCommonRowMapper implements RowMapper<CarbonPlayerCommon> {
|
||||
|
||||
@Override
|
||||
public CarbonPlayerCommon map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||
return new CarbonPlayerCommon(
|
||||
rs.getBoolean("muted"),
|
||||
rs.getBoolean("deafened"),
|
||||
rs.getObject("selectedchannel", Key.class),
|
||||
rs.getString("username"),
|
||||
rs.getObject("uuid", UUID.class),
|
||||
rs.getObject("displayname", Component.class),
|
||||
rs.getObject("lastwhispertarget", UUID.class),
|
||||
rs.getObject("whisperreplytarget", UUID.class),
|
||||
rs.getBoolean("spying")
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package net.draycia.carbon.common.users.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.jdbi.v3.core.mapper.ColumnMapper;
|
||||
import org.jdbi.v3.core.statement.StatementContext;
|
||||
|
||||
public class ComponentMapper implements ColumnMapper<Component> {
|
||||
|
||||
final GsonComponentSerializer serializer = GsonComponentSerializer.gson();
|
||||
|
||||
@Override
|
||||
public Component map(final ResultSet r, final int columnNumber, final StatementContext ctx) throws SQLException {
|
||||
return this.serializer.deserialize(r.getString(columnNumber));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component map(final ResultSet r, final String columnLabel, final StatementContext ctx) throws SQLException {
|
||||
return this.serializer.deserialize(r.getString(columnLabel));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package net.draycia.carbon.common.users.db;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.sql.DataSource;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.config.DatabaseSettings;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class MariaDBUserManager implements UserManager<CarbonPlayerCommon> {
|
||||
|
||||
private final Logger logger;
|
||||
private final Jdbi jdbi;
|
||||
|
||||
private final Map<UUID, CarbonPlayerCommon> userCache = Collections.synchronizedMap(new HashMap<>());
|
||||
private final QueriesLocator locator = new QueriesLocator();
|
||||
|
||||
private MariaDBUserManager(
|
||||
final Logger logger,
|
||||
final Jdbi jdbi
|
||||
) {
|
||||
this.logger = logger;
|
||||
this.jdbi = jdbi;
|
||||
}
|
||||
|
||||
public static @Nullable MariaDBUserManager manager(
|
||||
final Logger logger,
|
||||
final DatabaseSettings databaseSettings,
|
||||
final ClassLoader classLoader
|
||||
) {
|
||||
final HikariConfig hikariConfig = new HikariConfig();
|
||||
hikariConfig.setJdbcUrl(databaseSettings.url());
|
||||
hikariConfig.setUsername(databaseSettings.username());
|
||||
hikariConfig.setPassword(databaseSettings.password());
|
||||
|
||||
final DataSource dataSource = new HikariDataSource(hikariConfig);
|
||||
|
||||
Flyway.configure(classLoader)
|
||||
.baselineVersion("0")
|
||||
.baselineOnMigrate(true)
|
||||
.locations("queries/migrations")
|
||||
.dataSource(dataSource)
|
||||
.validateOnMigrate(true)
|
||||
.load()
|
||||
.migrate();
|
||||
|
||||
final Jdbi jdbi = Jdbi.create(dataSource)
|
||||
.registerArrayType(UUID.class, "uuid")
|
||||
.registerArgument(new UUIDArgumentFactory())
|
||||
.registerColumnMapper(Component.class, new ComponentMapper())
|
||||
.registerRowMapper(new CarbonPlayerCommonRowMapper());
|
||||
|
||||
return new MariaDBUserManager(logger, jdbi);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> carbonPlayer(final UUID uuid) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
final @Nullable CarbonPlayerCommon cachedPlayer = this.userCache.get(uuid);
|
||||
|
||||
if (cachedPlayer != null) {
|
||||
return new ComponentPlayerResult<>(cachedPlayer, empty());
|
||||
}
|
||||
|
||||
return this.jdbi.withHandle(handle -> {
|
||||
final CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||
.bind("uuid", uuid)
|
||||
.mapTo(CarbonPlayerCommon.class)
|
||||
.first();
|
||||
|
||||
handle.createQuery(this.locator.query("select-ignores"))
|
||||
.bind("uuid", uuid)
|
||||
.mapTo(UUID.class)
|
||||
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true));
|
||||
|
||||
return new ComponentPlayerResult<>(carbonPlayerCommon, empty());
|
||||
});
|
||||
}).completeOnTimeout(new ComponentPlayerResult<>(null, text("Timed out loading data of UUID [" + uuid + " ]")), 30, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> savePlayer(final CarbonPlayerCommon player) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) {
|
||||
return this.savePlayer(player).thenApply(result -> {
|
||||
this.userCache.remove(player.uuid());
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.draycia.carbon.common.users.db;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import java.util.List;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.jdbi.v3.core.locator.ClasspathSqlLocator;
|
||||
|
||||
public final class QueriesLocator {
|
||||
|
||||
private static final Splitter SPLITTER = Splitter.on(';');
|
||||
private final ClasspathSqlLocator locator = ClasspathSqlLocator.create();
|
||||
|
||||
public @NonNull String query(final @NonNull String name) {
|
||||
return this.locator.locate("queries/" + name);
|
||||
}
|
||||
|
||||
public @NonNull List<@NonNull String> queries(final @NonNull String name) {
|
||||
return SPLITTER.splitToList(this.locator.locate("queries/" + name));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2021 Josh Taylor (broccolai)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.users.db;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.UUID;
|
||||
import org.jdbi.v3.core.argument.AbstractArgumentFactory;
|
||||
import org.jdbi.v3.core.argument.Argument;
|
||||
import org.jdbi.v3.core.config.ConfigRegistry;
|
||||
|
||||
// https://github.com/broccolai/tickets/blob/55a23b5fcfdc8e4b4bfa861ea7ec620506bd0dfa/core/src/main/java/broccolai/tickets/core/storage/factory/UUIDArgumentFactory.java
|
||||
public final class UUIDArgumentFactory extends AbstractArgumentFactory<UUID> {
|
||||
|
||||
public UUIDArgumentFactory() {
|
||||
super(Types.VARCHAR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Argument build(final UUID value, final ConfigRegistry config) {
|
||||
return (position, statement, ctx) -> statement.setString(position, value.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
CREATE TABLE carbon_users
|
||||
(
|
||||
`uuid` UUID NOT NULL PRIMARY KEY,
|
||||
`muted` BOOLEAN,
|
||||
`deafened` BOOLEAN,
|
||||
`selectedchannel` varchar(256),
|
||||
`username` varchar(20),
|
||||
`displayname` varchar(1024),
|
||||
`lastwhispertarget` UUID,
|
||||
`whisperreplytarget` UUID,
|
||||
`spying` BOOLEAN
|
||||
);
|
||||
|
||||
CREATE TABLE carbon_ignores
|
||||
(
|
||||
`uuid` UUID NOT NULL,
|
||||
`ignoredplayer` UUID NOT NULL
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
SELECT ignoredplayer FROM carbon_ignores WHERE (uuid = <uuid>);
|
||||
@@ -0,0 +1 @@
|
||||
SELECT * from carbon_users WHERE (uuid = <uuid>);
|
||||
Reference in new issue
Block a user