1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-10 01:18:07 +01:00

feat: more storage providers [WIP]

This commit is contained in:
Jozef Steinhübl 2023-08-28 11:24:05 +02:00
parent 85c876e87f
commit c4c6671c7c
12 changed files with 169 additions and 19 deletions

View file

@ -6,9 +6,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
public interface StorageProvider {
@NonNull String[] getIdentifiers();
@NonNull User loadUser(@NonNull UUID uniqueId);
@NonNull User loadUser(@NonNull UUID uniqueId) throws Exception;
// todo: look at this
void saveUser(@NonNull User user);
}

View file

@ -27,4 +27,15 @@ dependencies {
tasks.shadowJar {
relocate("dev.jorel.commandapi", "dev.xhyrom.lighteco.libraries.commandapi")
// common
relocate("eu.okaeri.configs", "dev.xhyrom.lighteco.libraries.okaeri.configs")
relocate("eu.okaeri.validator", "dev.xhyrom.lighteco.libraries.okaeri.validator")
relocate("net.kyori.adventure", "dev.xhyrom.lighteco.libraries.net.kyori.adventure")
relocate("net.kyori.examination", "dev.xhyrom.lighteco.libraries.net.kyori.examination")
relocate("org.yaml.snakeyaml", "dev.xhyrom.lighteco.libraries.org.yaml.snakeyaml")
relocate("com.zaxxer.hikari", "dev.xhyrom.lighteco.libraries.com.zaxxer.hikari")
}

View file

@ -24,13 +24,13 @@ public class BalanceCommand implements Command {
.withPermission(permissionBase + "balance.others")
.withArguments(new OfflinePlayerArgument("target"))
.executes((sender, args) -> {
this.handleBalance(sender, args, currency);
}),
this.handleBalance(sender, args, currency);
}),
new CommandAPICommand(name)
.withPermission(permissionBase + "balance")
.executesPlayer((sender, args) -> {
this.handleBalance(sender, args, currency);
})
this.handleBalance(sender, args, currency);
})
};
}

View file

@ -50,16 +50,13 @@ public class BukkitCommandManager extends AbstractCommandManager {
String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command.";
// Dont expose set, give, take directly - only through main command
new CommandAPICommand(currency.getIdentifier())
.withPermission("lighteco.currency." + currency.getIdentifier() + ".command")
.withSubcommand(new SetCommand(this, currency, permissionBase).build())
.withSubcommand(new GiveCommand(this, currency, permissionBase).build())
.withSubcommand(new TakeCommand(this, currency, permissionBase).build())
.register();
// Register main command
registerCurrencyCommand(currency);
// Expose pay as main command
new PayCommand(this, currency, permissionBase).build().register();
// Expose balance as main command
for (CommandAPICommand cmd : new BalanceCommand(
this,
"balance",

View file

@ -14,6 +14,8 @@ dependencies {
implementation("eu.okaeri:okaeri-configs-yaml-snakeyaml:5.0.0-beta.5")
implementation("eu.okaeri:okaeri-configs-validator-okaeri:5.0.0-beta.5")
implementation("com.zaxxer:HikariCP:5.0.1")
compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28")

View file

@ -16,11 +16,6 @@ public class MemoryStorageProvider implements StorageProvider {
this.plugin = plugin;
}
@Override
public @NonNull String[] getIdentifiers() {
return new String[] {"memory"};
}
@Override
public @NonNull User loadUser(@NonNull UUID uniqueId) {
this.simulateSlowDatabaseQuery();

View file

@ -0,0 +1,34 @@
package dev.xhyrom.lighteco.common.storage.provider.sql;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.api.storage.StorageProvider;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.sql.Connection;
import java.util.UUID;
public class SqlStorageProvider implements StorageProvider {
private final LightEcoPlugin plugin;
private final ConnectionFactory connectionFactory;
public SqlStorageProvider(LightEcoPlugin plugin, ConnectionFactory connectionFactory) {
this.plugin = plugin;
this.connectionFactory = connectionFactory;
}
@Override
public @NonNull User loadUser(@NonNull UUID uniqueId) throws Exception {
try (Connection c = this.connectionFactory.getConnection()) {
}
return null;
}
@Override
public void saveUser(@NonNull User user) {
}
}

View file

@ -0,0 +1,12 @@
package dev.xhyrom.lighteco.common.storage.provider.sql.connection;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import java.sql.Connection;
public interface ConnectionFactory {
void init(LightEcoPlugin plugin);
void shutdown() throws Exception;
Connection getConnection() throws Exception;
}

View file

@ -0,0 +1,36 @@
package dev.xhyrom.lighteco.common.storage.provider.sql.connection.file;
import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
abstract class FileConnectionFactory implements ConnectionFactory {
private Connection connection;
private final File file;
public FileConnectionFactory(File file) {
this.file = file;
}
protected abstract Connection createConnection(File file) throws SQLException;
@Override
public void shutdown() throws Exception {
if (this.connection == null) return;
this.connection.close();
}
@Override
public Connection getConnection() throws SQLException {
Connection connection = this.connection;
if (connection == null || connection.isClosed()) {
connection = createConnection(this.file);
this.connection = connection;
}
return connection;
}
}

View file

@ -0,0 +1,39 @@
package dev.xhyrom.lighteco.common.storage.provider.sql.connection.file;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import java.io.File;
import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class H2ConnectionFactory extends FileConnectionFactory {
private Constructor<?> connectionConstructor;
public H2ConnectionFactory(File file) {
super(file);
}
@Override
public void init(LightEcoPlugin plugin) {
//ClassLoader classLoader = plugin
}
@Override
protected Connection createConnection(File file) throws SQLException {
try {
return (Connection) this.connectionConstructor.newInstance(
"jdbc:h2:" + file.getAbsolutePath(),
new Properties(),
null, null, false
);
} catch (Exception e) {
if (e.getCause() instanceof SQLException) {
throw (SQLException) e.getCause();
}
throw new SQLException("Failed to create connection", e);
}
}
}

View file

@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS `{prefix}_users` (
`uuid` VARCHAR(36) NOT NULL,
`currency_identifier` VARCHAR(255) NOT NULL,
`balance` DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (`uuid`, `currency_identifier`)
) DEFAULT CHARSET = utf8mb4;
CREATE TABLE IF NOT EXISTS `{prefix}_{context}_users` (
`uuid` VARCHAR(36) NOT NULL,
`currency_identifier` VARCHAR(255) NOT NULL,
`balance` DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (`uuid`, `currency_identifier`)
) DEFAULT CHARSET = utf8mb4;

View file

@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS `{prefix}_users` (
`uuid` VARCHAR(36) NOT NULL,
`currency_identifier` VARCHAR(255) NOT NULL,
`balance` DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (`uuid`, `currency_identifier`)
);
CREATE TABLE IF NOT EXISTS `{prefix}_{context}_users` (
`uuid` VARCHAR(36) NOT NULL,
`currency_identifier` VARCHAR(255) NOT NULL,
`balance` DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (`uuid`, `currency_identifier`)
);