Skip to content

Commit 6ed5b52

Browse files
committed
mysql対応
1 parent 8eabb3f commit 6ed5b52

File tree

8 files changed

+433
-135
lines changed

8 files changed

+433
-135
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,10 @@
153153
<artifactId>particle</artifactId>
154154
<version>1.8.1</version>
155155
</dependency>
156+
<dependency>
157+
<groupId>com.zaxxer</groupId>
158+
<artifactId>HikariCP</artifactId>
159+
<version>5.0.1</version>
160+
</dependency>
156161
</dependencies>
157162
</project>

src/main/java/com/github/elic0de/thejpspit/TheJpsPit.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.github.elic0de.thejpspit.config.Settings;
1010
import com.github.elic0de.thejpspit.cosmetics.CosmeticManager;
1111
import com.github.elic0de.thejpspit.database.Database;
12+
import com.github.elic0de.thejpspit.database.MySqlDatabase;
1213
import com.github.elic0de.thejpspit.database.SqLiteDatabase;
1314
import com.github.elic0de.thejpspit.game.Game;
1415
import com.github.elic0de.thejpspit.hook.EconomyHook;
@@ -84,21 +85,19 @@ public void reload() {
8485
@Override
8586
public void onEnable() {
8687
// Initialize TheJpsPit
87-
this.loadConfig();
88+
loadConfig();
89+
// todo:これいる?↓
8890
saveConfig();
89-
final AtomicBoolean initialized = new AtomicBoolean(true);
9091
game = new Game();
9192

9293
this.hooks = new ArrayList<>();
9394

94-
this.database = new SqLiteDatabase(this);
95-
96-
initialized.set(this.database.initialize());
97-
if (initialized.get()) {
98-
getLogger().log(Level.INFO, "Successfully established a connection to the database");
99-
} else {
100-
throw new RuntimeException("Failed to establish a connection to the database. " +
101-
"Please check the supplied database credentials in the config file");
95+
// Prepare the database and networking system
96+
this.database = this.loadDatabase();
97+
if (!database.hasLoaded()) {
98+
Bukkit.getLogger().log(Level.SEVERE, "Failed to load database! Please check your credentials! Disabling plugin...");
99+
Bukkit.getPluginManager().disablePlugin(this);
100+
return;
102101
}
103102

104103
cosmeticManager = new CosmeticManager();
@@ -140,6 +139,16 @@ public void onEnable() {
140139
});
141140
}
142141

142+
private Database loadDatabase() throws RuntimeException {
143+
final Database database = switch (getSettings().getDatabaseType()) {
144+
case MYSQL -> new MySqlDatabase(this);
145+
case SQLITE -> new SqLiteDatabase(this);
146+
};
147+
database.initialize();
148+
Bukkit.getLogger().log(Level.INFO, "Successfully initialized the " + getSettings().getDatabaseType().getDisplayName() + " database");
149+
return database;
150+
}
151+
143152

144153
private void setPreferences() {
145154
final Optional<PitPreferences> preferences = database.getPitPreferences();
@@ -198,7 +207,7 @@ private void createNPCs() {
198207
public void onDisable() {
199208
// Plugin shutdown logic
200209
if (database != null) {
201-
database.terminate();
210+
getDatabase().close();
202211
}
203212
game.getTask().stop();
204213

src/main/java/com/github/elic0de/thejpspit/config/Settings.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.github.elic0de.thejpspit.config;
22

3+
import com.github.elic0de.thejpspit.database.Database;
4+
import com.github.elic0de.thejpspit.database.Database.Type;
35
import java.util.Arrays;
46
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Optional;
59
import net.william278.annotaml.YamlComment;
610
import net.william278.annotaml.YamlFile;
711
import net.william278.annotaml.YamlKey;
@@ -14,6 +18,50 @@ public class Settings {
1418
@YamlComment("アクセストークンが漏洩すると、被害が広範囲に及ぶことになりますので公開しないでください")
1519
private String githubToken = "";
1620

21+
// Database settings
22+
@YamlComment("Database connection settings")
23+
@YamlKey("database.type")
24+
private Database.Type databaseType = Database.Type.SQLITE;
25+
26+
@YamlKey("database.mysql.credentials.host")
27+
private String mySqlHost = "localhost";
28+
29+
@YamlKey("database.mysql.credentials.port")
30+
private int mySqlPort = 3306;
31+
32+
@YamlKey("database.mysql.credentials.database")
33+
private String mySqlDatabase = "HuskTowns";
34+
35+
@YamlKey("database.mysql.credentials.username")
36+
private String mySqlUsername = "root";
37+
38+
@YamlKey("database.mysql.credentials.password")
39+
private String mySqlPassword = "pa55w0rd";
40+
41+
@YamlKey("database.mysql.credentials.parameters")
42+
private String mySqlConnectionParameters = "?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8";
43+
44+
@YamlComment("MySQL connection pool properties")
45+
@YamlKey("database.mysql.connection_pool.size")
46+
private int mySqlConnectionPoolSize = 10;
47+
48+
@YamlKey("database.mysql.connection_pool.idle")
49+
private int mySqlConnectionPoolIdle = 10;
50+
51+
@YamlKey("database.mysql.connection_pool.lifetime")
52+
private long mySqlConnectionPoolLifetime = 1800000;
53+
54+
@YamlKey("database.mysql.connection_pool.keepalive")
55+
private long mySqlConnectionPoolKeepAlive = 30000;
56+
57+
@YamlKey("database.mysql.connection_pool.timeout")
58+
private long mySqlConnectionPoolTimeout = 20000;
59+
60+
@YamlKey("database.table_names")
61+
private Map<String, String> tableNames = Map.of(
62+
Database.Table.USER_DATA.name().toLowerCase(), Database.Table.USER_DATA.getDefaultName(),
63+
Database.Table.PIT_DATA.name().toLowerCase(), Database.Table.PIT_DATA.getDefaultName()
64+
);
1765

1866
@YamlKey("scoreboard")
1967
private List<String> scoreboard = Arrays.asList(
@@ -50,6 +98,58 @@ public String getGithubToken() {
5098
return githubToken;
5199
}
52100

101+
public Type getDatabaseType() {
102+
return databaseType;
103+
}
104+
105+
public String getMySqlHost() {
106+
return mySqlHost;
107+
}
108+
109+
public int getMySqlPort() {
110+
return mySqlPort;
111+
}
112+
113+
public String getMySqlDatabase() {
114+
return mySqlDatabase;
115+
}
116+
117+
public String getMySqlUsername() {
118+
return mySqlUsername;
119+
}
120+
121+
public String getMySqlPassword() {
122+
return mySqlPassword;
123+
}
124+
125+
public String getMySqlConnectionParameters() {
126+
return mySqlConnectionParameters;
127+
}
128+
129+
public int getMySqlConnectionPoolSize() {
130+
return mySqlConnectionPoolSize;
131+
}
132+
133+
public int getMySqlConnectionPoolIdle() {
134+
return mySqlConnectionPoolIdle;
135+
}
136+
137+
public long getMySqlConnectionPoolLifetime() {
138+
return mySqlConnectionPoolLifetime;
139+
}
140+
141+
public long getMySqlConnectionPoolKeepAlive() {
142+
return mySqlConnectionPoolKeepAlive;
143+
}
144+
145+
public long getMySqlConnectionPoolTimeout() {
146+
return mySqlConnectionPoolTimeout;
147+
}
148+
149+
public String getTableName(Database.Table tableName) {
150+
return Optional.ofNullable(tableNames.get(tableName.name().toLowerCase())).orElse(tableName.getDefaultName());
151+
}
152+
53153
public List<String> getScoreboard() {
54154
return scoreboard;
55155
}

src/main/java/com/github/elic0de/thejpspit/database/Database.java

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,48 @@
1212
import java.util.Optional;
1313
import java.util.UUID;
1414
import java.util.concurrent.CompletableFuture;
15+
import java.util.logging.Level;
1516
import java.util.logging.Logger;
17+
import java.util.regex.Matcher;
18+
import java.util.regex.Pattern;
19+
import org.bukkit.Bukkit;
1620
import org.bukkit.entity.Player;
1721

1822
public abstract class Database {
1923

20-
protected final String playerTableName;
21-
protected final String preferencesTableName;
2224

2325
protected final TheJpsPit plugin;
26+
private final String schemaFile;
27+
private boolean loaded;
2428

25-
private final Logger logger;
26-
27-
protected Database(TheJpsPit implementor) {
28-
this.plugin = implementor;
29-
this.playerTableName = "player";
30-
this.preferencesTableName = "preferences";
31-
this.logger = implementor.getLogger();
32-
}
33-
34-
protected Logger getLogger() {
35-
return logger;
29+
protected Database(TheJpsPit plugin, String schemaFile) {
30+
this.plugin = plugin;
31+
this.schemaFile = "database/" + schemaFile;
3632
}
3733

38-
protected final String[] getSchemaStatements() throws IOException {
39-
return formatStatementTables(
40-
new String(
41-
Objects.requireNonNull(plugin.getResource("database/sqlite_schema.sql"))
42-
.readAllBytes(),
43-
StandardCharsets.UTF_8))
44-
.split(";");
34+
protected final String[] getSchema() {
35+
try (InputStream schemaStream = Objects.requireNonNull(plugin.getResource(schemaFile))) {
36+
final String schema = new String(schemaStream.readAllBytes(), StandardCharsets.UTF_8);
37+
return format(schema).split(";");
38+
} catch (IOException e) {
39+
Bukkit.getLogger().log(Level.SEVERE, "Failed to load database schema", e);
40+
}
41+
return new String[0];
4542
}
4643

47-
protected final String formatStatementTables(String sql) {
48-
return sql
49-
.replaceAll("%pit_preferences%", preferencesTableName)
50-
.replaceAll("%players_table%", playerTableName);
44+
protected final String format( String statement) {
45+
final Pattern pattern = Pattern.compile("%(\\w+)%");
46+
final Matcher matcher = pattern.matcher(statement);
47+
final StringBuilder sb = new StringBuilder();
48+
while (matcher.find()) {
49+
final Table table = Table.match(matcher.group(1));
50+
matcher.appendReplacement(sb, plugin.getSettings().getTableName(table));
51+
}
52+
matcher.appendTail(sb);
53+
return sb.toString();
5154
}
5255

53-
public abstract boolean initialize();
54-
55-
public abstract CompletableFuture<Void> runScript(InputStream inputStream,
56-
Map<String, String> replacements);
56+
public abstract void initialize() throws RuntimeException;
5757

5858
public abstract void createPitPlayer(Player Player);
5959

@@ -78,12 +78,52 @@ public abstract CompletableFuture<Optional<Integer>> getPlayerRanking(PitPlayer
7878

7979
public abstract void deletePlayerData();
8080

81-
public abstract void terminate();
81+
public abstract void close();
82+
83+
public boolean hasLoaded() {
84+
return loaded;
85+
}
86+
87+
protected void setLoaded(boolean loaded) {
88+
this.loaded = loaded;
89+
}
90+
91+
public enum Type {
92+
MYSQL("MySQL"),
93+
SQLITE("SQLite");
94+
private final String displayName;
95+
96+
Type(String displayName) {
97+
this.displayName = displayName;
98+
}
99+
100+
public String getDisplayName() {
101+
return displayName;
102+
}
103+
}
104+
105+
public enum Table {
106+
USER_DATA("pit_users"),
107+
PIT_DATA("pit_data");
108+
109+
private final String defaultName;
110+
111+
Table(String defaultName) {
112+
this.defaultName = defaultName;
113+
}
114+
115+
public static Database.Table match(String placeholder) throws IllegalArgumentException {
116+
return Table.valueOf(placeholder.toUpperCase());
117+
}
118+
119+
public String getDefaultName() {
120+
return defaultName;
121+
}
122+
}
82123

83124
public enum RankType {
84125
KILLS,
85126
DEATHS,
86127
RATING
87128
}
88-
89129
}

0 commit comments

Comments
 (0)