mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
Don't save empty player data into database
This commit is contained in:
4 files changed
+33
-53
No files matched your search
@@ -101,7 +101,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CarbonPlayerCommon(
|
public CarbonPlayerCommon(
|
||||||
final String username,
|
final @Nullable String username, // will be resolved when requested
|
||||||
final UUID uuid
|
final UUID uuid
|
||||||
) {
|
) {
|
||||||
this.muted = PersistentUserProperty.of(false);
|
this.muted = PersistentUserProperty.of(false);
|
||||||
|
|||||||
+3
-12
@@ -24,7 +24,6 @@ import com.google.inject.MembersInjector;
|
|||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
@@ -54,7 +53,6 @@ import org.jdbi.v3.core.Jdbi;
|
|||||||
import org.jdbi.v3.core.statement.Update;
|
import org.jdbi.v3.core.statement.Update;
|
||||||
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
||||||
|
|
||||||
// TODO: Dispatch updates using messaging system when users are modified
|
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class MySQLUserManager extends DatabaseUserManager {
|
public final class MySQLUserManager extends DatabaseUserManager {
|
||||||
|
|
||||||
@@ -86,13 +84,7 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
.findOne();
|
.findOne();
|
||||||
|
|
||||||
if (carbonPlayerCommon.isEmpty()) {
|
if (carbonPlayerCommon.isEmpty()) {
|
||||||
// Player doesn't exist in the DB, create them!
|
return new CarbonPlayerCommon(null, uuid);
|
||||||
final String name = Objects.requireNonNull(this.profileResolver.resolveName(uuid).join());
|
|
||||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
|
|
||||||
|
|
||||||
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player).execute();
|
|
||||||
|
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handle.createQuery(this.locator.query("select-ignores"))
|
handle.createQuery(this.locator.query("select-ignores"))
|
||||||
@@ -157,11 +149,10 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
|
|
||||||
public MySQLUserManager create() {
|
public MySQLUserManager create() {
|
||||||
try {
|
try {
|
||||||
//Class.forName("org.postgresql.Driver");
|
|
||||||
Class.forName("org.mariadb.jdbc.Driver");
|
Class.forName("org.mariadb.jdbc.Driver");
|
||||||
Class.forName("com.mysql.cj.jdbc.Driver"); // Manually loading this might not be necessary
|
Class.forName("com.mysql.cj.jdbc.Driver"); // Manually loading this might not be necessary
|
||||||
} catch (final Exception exception) {
|
} catch (final ClassNotFoundException exception) {
|
||||||
exception.printStackTrace();
|
throw new RuntimeException("Could not find required class", exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
final HikariConfig hikariConfig = new HikariConfig();
|
final HikariConfig hikariConfig = new HikariConfig();
|
||||||
|
|||||||
+28
-39
@@ -24,7 +24,6 @@ import com.google.inject.MembersInjector;
|
|||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
@@ -56,7 +55,6 @@ import org.jdbi.v3.core.statement.Update;
|
|||||||
import org.jdbi.v3.postgres.PostgresPlugin;
|
import org.jdbi.v3.postgres.PostgresPlugin;
|
||||||
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
||||||
|
|
||||||
// TODO: Dispatch updates using messaging system when users are modified
|
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class PostgreSQLUserManager extends DatabaseUserManager {
|
public final class PostgreSQLUserManager extends DatabaseUserManager {
|
||||||
|
|
||||||
@@ -82,42 +80,33 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
@Override
|
@Override
|
||||||
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||||
return this.jdbi.withHandle(handle -> {
|
return this.jdbi.withHandle(handle -> {
|
||||||
try {
|
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||||
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
.bind("id", uuid)
|
||||||
.bind("id", uuid)
|
.mapTo(CarbonPlayerCommon.class)
|
||||||
.mapTo(CarbonPlayerCommon.class)
|
.findOne()
|
||||||
.first();
|
.orElse(null);
|
||||||
|
if (carbonPlayerCommon == null) {
|
||||||
handle.createQuery(this.locator.query("select-ignores"))
|
return new CarbonPlayerCommon(null, uuid);
|
||||||
.bind("id", uuid)
|
|
||||||
.mapTo(UUID.class)
|
|
||||||
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true, true));
|
|
||||||
|
|
||||||
handle.createQuery(this.locator.query("select-leftchannels"))
|
|
||||||
.bind("id", uuid)
|
|
||||||
.mapTo(Key.class)
|
|
||||||
.forEach(channel -> {
|
|
||||||
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
|
|
||||||
.channelRegistry()
|
|
||||||
.get(channel);
|
|
||||||
if (chatChannel == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
carbonPlayerCommon.leaveChannel(chatChannel, true);
|
|
||||||
});
|
|
||||||
return carbonPlayerCommon;
|
|
||||||
} catch (final IllegalStateException exception) {
|
|
||||||
// Player doesn't exist in the DB, create them!
|
|
||||||
final String name = Objects.requireNonNull(
|
|
||||||
this.profileResolver.resolveName(uuid).join());
|
|
||||||
|
|
||||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
|
|
||||||
|
|
||||||
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handle.createQuery(this.locator.query("select-ignores"))
|
||||||
|
.bind("id", uuid)
|
||||||
|
.mapTo(UUID.class)
|
||||||
|
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true, true));
|
||||||
|
|
||||||
|
handle.createQuery(this.locator.query("select-leftchannels"))
|
||||||
|
.bind("id", uuid)
|
||||||
|
.mapTo(Key.class)
|
||||||
|
.forEach(channel -> {
|
||||||
|
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
|
||||||
|
.channelRegistry()
|
||||||
|
.get(channel);
|
||||||
|
if (chatChannel == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
carbonPlayerCommon.leaveChannel(chatChannel, true);
|
||||||
|
});
|
||||||
|
return carbonPlayerCommon;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,8 +154,8 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
try {
|
try {
|
||||||
Class.forName("org.postgresql.Driver");
|
Class.forName("org.postgresql.Driver");
|
||||||
PluginRegister.REGISTERED_PLUGINS.add(new PostgreSQLDatabaseType());
|
PluginRegister.REGISTERED_PLUGINS.add(new PostgreSQLDatabaseType());
|
||||||
} catch (final Exception exception) {
|
} catch (final ClassNotFoundException exception) {
|
||||||
exception.printStackTrace();
|
throw new RuntimeException("Could not find required class", exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
final HikariConfig hikariConfig = new HikariConfig();
|
final HikariConfig hikariConfig = new HikariConfig();
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ public class JSONUserManager extends CachingUserManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CarbonPlayerCommon(null /* Username will be resolved when requested */, uuid);
|
return new CarbonPlayerCommon(null, uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path userFile(final UUID id) {
|
private Path userFile(final UUID id) {
|
||||||
|
|||||||
Reference in new issue
Block a user