Skip to content

Commit

Permalink
feat(database): Dropped MineSQL with original easysql registry
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Dec 19, 2023
1 parent df799a7 commit 3308158
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 28 deletions.
17 changes: 12 additions & 5 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>artcore-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand All @@ -20,9 +20,16 @@

<dependencies>
<dependency>
<groupId>cc.carm.plugin</groupId>
<artifactId>minesql-api</artifactId>
<version>${deps.minesql.version}</version>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-api</artifactId>
<version>${deps.easysql.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>de.themoep</groupId>
<artifactId>minedown</artifactId>
<version>${deps.minedown.version}</version>
<scope>compile</scope>
</dependency>

Expand Down Expand Up @@ -123,7 +130,7 @@
<version>1.6</version>
<scope>compile</scope>
</dependency>

</dependencies>

<build>
Expand Down
9 changes: 7 additions & 2 deletions api/src/main/java/com/artformgames/core/ArtCore.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.artformgames.core;

import cc.carm.lib.easysql.api.SQLManager;
import com.artformgames.core.user.manager.UserManager;
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
import org.bukkit.Bukkit;
Expand All @@ -24,12 +25,16 @@ private static ArtCorePlugin plugin() {
return core;
}

public static SQLManager getSQLManager() {
return plugin().getSQLManager();
}

public static UserManager<?> getUserManager() {
throw new UnsupportedOperationException("Not implemented");
return plugin().getUserManager();
}

public static BungeeChannelApi getBungeeAPI() {
throw new UnsupportedOperationException("Not implemented");
return plugin().getBungeeAPI();
}


Expand Down
6 changes: 5 additions & 1 deletion api/src/main/java/com/artformgames/core/ArtCorePlugin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.artformgames.core;

import cc.carm.lib.easysql.api.SQLManager;
import com.artformgames.core.user.manager.UserManager;
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
import org.jetbrains.annotations.NotNull;

interface ArtCorePlugin {

@NotNull UserManager<?> getUserManager();
@NotNull SQLManager getSQLManager();

@NotNull UserManager<?> getUserManager();

@NotNull BungeeChannelApi getBungeeAPI();


}
47 changes: 47 additions & 0 deletions api/src/main/java/com/artformgames/core/conf/MessagesRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.artformgames.core.conf;

import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageListBuilder;
import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageValueBuilder;
import cc.carm.lib.mineconfiguration.bukkit.builder.title.TitleConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle;
import de.themoep.minedown.MineDown;
import me.clip.placeholderapi.PlaceholderAPI;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.function.BiFunction;

public abstract class MessagesRoot extends ConfigurationRoot {

protected static @NotNull CraftMessageListBuilder<BaseComponent[]> list() {
return ConfiguredMessageList.create(getParser())
.whenSend((sender, message) -> message.forEach(m -> sender.spigot().sendMessage(m)));
}

protected static @NotNull CraftMessageValueBuilder<BaseComponent[]> value() {
return ConfiguredMessage.create(getParser())
.whenSend((sender, message) -> sender.spigot().sendMessage(message));
}

protected static @NotNull TitleConfigBuilder title() {
return ConfiguredTitle.create();
}

private static @NotNull BiFunction<CommandSender, String, BaseComponent[]> getParser() {
return (sender, message) -> {
if (sender == null) return MineDown.parse(message);
if (sender instanceof Player player) {
return MineDown.parse(PlaceholderAPI.setPlaceholders(player, message));
} else {
return MineDown.parse(message);
}
};
}


}
8 changes: 4 additions & 4 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>artcore-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand All @@ -27,9 +27,9 @@
</dependency>

<dependency>
<groupId>cc.carm.plugin</groupId>
<artifactId>minesql-api</artifactId>
<version>${deps.minesql.version}</version>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-beecp</artifactId>
<version>${deps.easysql.version}</version>
<scope>provided</scope>
</dependency>

Expand Down
44 changes: 33 additions & 11 deletions plugin/src/main/java/com/artformgames/core/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.mineconfiguration.bukkit.MineConfiguration;
import cc.carm.plugin.minesql.MineSQL;
import com.artformgames.core.conf.PluginConfig;
import com.artformgames.core.conf.PluginMessages;
import com.artformgames.core.data.DataTables;
import com.artformgames.core.data.DataManager;
import com.artformgames.core.function.settings.UserSettingsLoader;
import com.artformgames.core.listener.PluginListener;
import com.artformgames.core.listener.UserListener;
import com.artformgames.core.user.BukkitUserManager;
import com.artformgames.core.utils.GHUpdateChecker;
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,7 +26,9 @@ public Main() {
}

protected MineConfiguration configuration;
protected DataManager dataManager;
protected BukkitUserManager usersManager;
protected BungeeChannelApi bungeeChannel;

@Override
protected void load() {
Expand All @@ -38,36 +40,40 @@ protected void load() {


log("Loading database...");
SQLManager sqlManager = MineSQL.getRegistry().get(PluginConfig.DATASOURCE_ID.getNotNull());
if (sqlManager == null) {
error("Datasource not configured or exists! Please check the configuration!");
this.dataManager = new DataManager(getLogger());
if (!dataManager.initialize()) {
dataManager.shutdown();
setEnabled(false);
return;
}
DataTables.initializeTables(sqlManager);

log("Initialize users manager...");
log("Initializing users manager...");
this.usersManager = new BukkitUserManager(getLogger());
if (!Bukkit.getOnlinePlayers().isEmpty()) {
this.usersManager.loadAll();
}


log("Initializing channels...");
this.bungeeChannel = new BungeeChannelApi(this);

}

@Override
protected boolean initialize() {

log("Register listeners...");
log("Registering listeners...");
GUI.initialize(this);
registerListener(new UserListener());
registerListener(new PluginListener());

log("Register commands...");
log("Registering commands...");


log("Enable user settings data...");
log("Enabling user settings data...");
this.usersManager.registerHandler(new UserSettingsLoader(this));


if (PluginConfig.METRICS.getNotNull()) {
log("Initializing bStats...");
new Metrics(this, 20501);
Expand All @@ -86,20 +92,26 @@ protected boolean initialize() {
@Override
protected void shutdown() {

log("Close channels...");
this.bungeeChannel.unregister();

log("Shutting down UserManager...");
try {
this.usersManager.unloadAll();
} catch (Exception e) {
e.printStackTrace();
}

log("Shutting down DataManager...");
this.dataManager.shutdown();

}

@Override
public boolean isDebugging() {
return PluginConfig.DEBUG.getNotNull();
}

public static void info(String... messages) {
getInstance().log(messages);
}
Expand All @@ -116,9 +128,19 @@ public static Main getInstance() {
return instance;
}

@Override
public @NotNull SQLManager getSQLManager() {
return this.dataManager.getSQLManager();
}

@Override
public @NotNull BukkitUserManager getUserManager() {
return this.usersManager;
}

@Override
public @NotNull BungeeChannelApi getBungeeAPI() {
return this.bungeeChannel;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.artformgames.core.data;

import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.annotation.ConfigPath;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;

public class DBConfiguration extends ConfigurationRoot {

@ConfigPath("driver")
@HeaderComment({
"数据库驱动配置,请根据数据库类型设置。",
"默认为MySQL: com.mysql.cj.jdbc.Driver",
})
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
String.class, "com.mysql.cj.jdbc.Driver"
);

protected static final ConfigValue<String> HOST = ConfiguredValue.of(String.class, "127.0.0.1");
protected static final ConfigValue<Integer> PORT = ConfiguredValue.of(Integer.class, 3306);
protected static final ConfigValue<String> DATABASE = ConfiguredValue.of(String.class, "minecraft");
protected static final ConfigValue<String> USERNAME = ConfiguredValue.of(String.class, "root");
protected static final ConfigValue<String> PASSWORD = ConfiguredValue.of(String.class, "password");
protected static final ConfigValue<String> EXTRA = ConfiguredValue.of(String.class, "?useSSL=false");

protected static String buildJDBC() {
return String.format("jdbc:mysql://%s:%s/%s%s",
HOST.getNotNull(), PORT.getNotNull(), DATABASE.getNotNull(), EXTRA.getNotNull()
);
}


}
54 changes: 54 additions & 0 deletions plugin/src/main/java/com/artformgames/core/data/DataManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.artformgames.core.data;

import cc.carm.lib.easysql.EasySQL;
import cc.carm.lib.easysql.api.SQLManager;
import com.artformgames.core.Main;

import java.util.logging.Logger;


public class DataManager {


private final Logger logger;
private SQLManager sqlManager;

public DataManager(Logger logger) {
this.logger = logger;
}

public Logger getLogger() {
return logger;
}

public boolean initialize() {
try {
getLogger().info(" Connecting to database...");
this.sqlManager = EasySQL.createManager(
DBConfiguration.DRIVER_NAME.getNotNull(), DBConfiguration.buildJDBC(),
DBConfiguration.USERNAME.getNotNull(), DBConfiguration.PASSWORD.getNotNull()
);
this.sqlManager.setDebugMode(() -> Main.getInstance().isDebugging());
} catch (Exception exception) {
getLogger().severe("Error connecting to database, please check the configuration.");
exception.printStackTrace();
return false;
}

getLogger().info(" Initializing tables...");
DataTables.initializeTables(this.sqlManager);

return true;
}

public void shutdown() {
EasySQL.shutdownManager(getSQLManager());
this.sqlManager = null;
}

public SQLManager getSQLManager() {
return sqlManager;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public enum DataTables implements SQLTable {

USERS("users", (table) -> {
USERS("users", table -> {
table.addAutoIncrementColumn(UserKey.KeyType.ID.getColumnName());
table.addColumn(UserKey.KeyType.UUID.getColumnName(), "CHAR(36) NOT NULL");
table.addColumn(UserKey.KeyType.NAME.getColumnName(), "VARCHAR(20)");
Expand All @@ -25,7 +25,7 @@ public enum DataTables implements SQLTable {
table.setIndex(IndexType.UNIQUE_KEY, "idx_user_uuid", UserKey.KeyType.UUID.getColumnName());
}),

USER_SETTINGS("user_settings", (builder) -> {
USER_SETTINGS("user_settings", builder -> {
builder.addColumn("uid", "INT UNSIGNED NOT NULL");
builder.addColumn("type", "INT(11) UNSIGNED NOT NULL");
builder.addColumn("value", "TEXT");
Expand Down
Loading

0 comments on commit 3308158

Please sign in to comment.