Skip to content

Commit 1ffbf66

Browse files
Merge remote-tracking branch 'origin/dev/2.0' into dev/2.0
# Conflicts: # build.gradle
2 parents 4282d58 + e970f1c commit 1ffbf66

7 files changed

Lines changed: 146 additions & 125 deletions

File tree

simplecloud-base/src/main/kotlin/eu/thesimplecloud/base/manager/database/SQLOfflineCloudPlayerHandler.kt

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ package eu.thesimplecloud.base.manager.database
2525
import eu.thesimplecloud.api.player.IOfflineCloudPlayer
2626
import eu.thesimplecloud.api.player.OfflineCloudPlayer
2727
import eu.thesimplecloud.jsonlib.JsonLib
28-
import eu.thesimplecloud.launcher.startup.Launcher
2928
import java.sql.*
3029
import java.util.*
31-
import java.util.concurrent.TimeUnit
3230

3331

3432
/**
@@ -40,42 +38,36 @@ import java.util.concurrent.TimeUnit
4038
class SQLOfflineCloudPlayerHandler(private val databaseConnectionInformation: DatabaseConnectionInformation) :
4139
AbstractOfflineCloudPlayerHandler() {
4240

43-
var connection: Connection? = null
44-
private set
45-
4641
private val playerCollectionName = databaseConnectionInformation.collectionPrefix + "players"
4742

4843
init {
49-
runReconnectLoop()
5044
createDatabaseAndIndicesIfNotExist()
5145
}
5246

5347
private fun createDatabaseAndIndicesIfNotExist() {
5448
if (!doesTableExist()) {
55-
val statement =
56-
connection!!.prepareStatement("CREATE TABLE IF NOT EXISTS `$playerCollectionName` (`uniqueId` varchar(36), `name` varchar(16), `data` LONGBLOB)")
57-
statement.executeUpdate()
58-
createIndex("uniqueId")
59-
createIndex("name")
49+
getConnection().use { connection ->
50+
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `$playerCollectionName` (`uniqueId` varchar(36), `name` varchar(16), `data` LONGBLOB)")
51+
.use { statement ->
52+
statement.executeUpdate()
53+
createIndex("uniqueId")
54+
createIndex("name")
55+
}
56+
}
6057
}
6158
}
6259

63-
private fun runReconnectLoop() {
64-
reconnect()
65-
Launcher.instance.scheduler.scheduleAtFixedRate({
66-
reconnect()
67-
}, 1, 1, TimeUnit.HOURS)
68-
}
69-
70-
private fun reconnect() = synchronized(this) {
71-
closeConnection()
72-
this.connection =
73-
DriverManager.getConnection("jdbc:mysql://${databaseConnectionInformation.host}:${databaseConnectionInformation.port}/${databaseConnectionInformation.databaseName}?user=${databaseConnectionInformation.userName}&password=${databaseConnectionInformation.password}&serverTimezone=UTC&autoReconnect=true")
60+
private fun getConnection(): Connection {
61+
return DriverManager.getConnection("jdbc:mysql://${databaseConnectionInformation.host}:${databaseConnectionInformation.port}/${databaseConnectionInformation.databaseName}?user=${databaseConnectionInformation.userName}&password=${databaseConnectionInformation.password}&serverTimezone=UTC&autoReconnect=true")
7462
}
7563

7664
private fun createIndex(columnName: String) {
77-
val statement = connection!!.prepareStatement("ALTER TABLE $playerCollectionName ADD INDEX ($columnName)")
78-
statement.executeUpdate()
65+
getConnection().use { connection ->
66+
connection.prepareStatement("ALTER TABLE $playerCollectionName ADD INDEX ($columnName)")
67+
.use { statement ->
68+
statement.executeUpdate()
69+
}
70+
}
7971
}
8072

8173
override fun getOfflinePlayer(playerUniqueId: UUID): IOfflineCloudPlayer? {
@@ -88,13 +80,17 @@ class SQLOfflineCloudPlayerHandler(private val databaseConnectionInformation: Da
8880

8981
private fun loadPlayer(value: String, fieldName: String): IOfflineCloudPlayer? = synchronized(this) {
9082
if (!exist(value, fieldName)) return null
91-
val statement =
92-
connection!!.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
93-
statement.setString(1, value)
94-
val resultSet = statement.executeQuery()
95-
val allDataStrings = getAllDataStringsFromResultSet(resultSet)
96-
val players = allDataStrings.mapNotNull { loadPlayerFromJsonString(it) }
97-
return getPlayerWithLatestLogin(players)
83+
84+
getConnection().use { conn ->
85+
conn.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
86+
.use { statement ->
87+
statement.setString(1, value)
88+
val resultSet = statement.executeQuery()
89+
val allDataStrings = getAllDataStringsFromResultSet(resultSet)
90+
val players = allDataStrings.mapNotNull { loadPlayerFromJsonString(it) }
91+
return getPlayerWithLatestLogin(players)
92+
}
93+
}
9894
}
9995

10096
private fun getAllDataStringsFromResultSet(resultSet: ResultSet): List<String> {
@@ -117,50 +113,62 @@ class SQLOfflineCloudPlayerHandler(private val databaseConnectionInformation: Da
117113

118114
override fun saveCloudPlayer(offlineCloudPlayer: OfflineCloudPlayer): Unit = synchronized(this) {
119115
val newData = JsonLib.fromObject(offlineCloudPlayer, databaseGson).getAsJsonString()
120-
if (!exist(offlineCloudPlayer.getUniqueId().toString(), "uniqueId")) {
121-
val statement =
122-
connection!!.prepareStatement("INSERT INTO `$playerCollectionName` (`uniqueId`, `name`, `data`) VALUES (?, ?, ?)")
123-
statement.setString(1, offlineCloudPlayer.getUniqueId().toString())
124-
statement.setString(2, offlineCloudPlayer.getName())
125-
statement.setString(3, newData)
126-
statement.executeUpdate()
127-
} else {
128-
val statement =
129-
connection!!.prepareStatement("UPDATE `$playerCollectionName` SET `data` = ?, `name` = ? WHERE `uniqueId` = ?")
130-
statement.setString(1, newData)
131-
statement.setString(2, offlineCloudPlayer.getName())
132-
statement.setString(3, offlineCloudPlayer.getUniqueId().toString())
133-
statement.executeUpdate()
116+
117+
getConnection().use { connection ->
118+
if (!exist(offlineCloudPlayer.getUniqueId().toString(), "uniqueId")) {
119+
connection.prepareStatement("INSERT INTO `$playerCollectionName` (`uniqueId`, `name`, `data`) VALUES (?, ?, ?)")
120+
.use { statement ->
121+
statement.setString(1, offlineCloudPlayer.getUniqueId().toString())
122+
statement.setString(2, offlineCloudPlayer.getName())
123+
statement.setString(3, newData)
124+
statement.executeUpdate()
125+
}
126+
} else {
127+
connection.prepareStatement("UPDATE `$playerCollectionName` SET `data` = ?, `name` = ? WHERE `uniqueId` = ?")
128+
.use { statement ->
129+
statement.setString(1, newData)
130+
statement.setString(2, offlineCloudPlayer.getName())
131+
statement.setString(3, offlineCloudPlayer.getUniqueId().toString())
132+
statement.executeUpdate()
133+
}
134+
}
134135
}
135136
}
136137

137138
override fun getRegisteredPlayerCount(): Int {
138-
val statement = connection!!.prepareStatement("SELECT COUNT(*) FROM `$playerCollectionName`")
139-
val resultSet = statement.executeQuery()
140-
return if (!resultSet.next()) {
141-
0
142-
} else {
143-
resultSet.getInt(1)
139+
getConnection().use { connection ->
140+
connection.prepareStatement("SELECT COUNT(*) FROM `$playerCollectionName`").use { statement ->
141+
val resultSet = statement.executeQuery()
142+
return if (!resultSet.next()) {
143+
0
144+
} else {
145+
resultSet.getInt(1)
146+
}
147+
}
144148
}
145149
}
146150

147151
override fun closeConnection() {
148-
connection?.close()
152+
//nothing to do
149153
}
150154

151155
private fun exist(searchValue: String, fieldName: String): Boolean {
152-
val prepareStatement =
153-
connection!!.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
154-
prepareStatement.setString(1, searchValue)
155-
val resultSet = prepareStatement.executeQuery()
156-
return resultSet.next()
156+
getConnection().use { connection ->
157+
connection.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
158+
.use { statement ->
159+
statement.setString(1, searchValue)
160+
val resultSet = statement.executeQuery()
161+
return resultSet.next()
162+
}
163+
}
157164
}
158165

159166
private fun doesTableExist(): Boolean {
160-
val meta: DatabaseMetaData = connection!!.metaData
161-
val res = meta.getTables(null, null, this.playerCollectionName, arrayOf("TABLE"))
162-
return res.next()
167+
getConnection().use { connection ->
168+
val meta: DatabaseMetaData = connection.metaData
169+
val res = meta.getTables(null, null, this.playerCollectionName, arrayOf("TABLE"))
170+
return res.next()
171+
}
163172
}
164173

165-
166174
}

simplecloud-base/src/main/kotlin/eu/thesimplecloud/base/manager/database/SQLiteOfflineCloudPlayerHandler.kt

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import java.util.concurrent.TimeUnit
1212
class SQLiteOfflineCloudPlayerHandler(private val databaseConnectionInformation: DatabaseConnectionInformation) :
1313
AbstractOfflineCloudPlayerHandler() {
1414

15-
var connection: Connection? = null
16-
private set
17-
1815
private val databaseFile = File("storage/database.db")
1916

2017
private val playerCollectionName = databaseConnectionInformation.collectionPrefix + "players"
@@ -26,35 +23,33 @@ class SQLiteOfflineCloudPlayerHandler(private val databaseConnectionInformation:
2623

2724
//Class.forName("org.sqlite.JDBC");
2825

29-
runReconnectLoop()
3026
createDatabaseAndIndicesIfNotExist()
3127
}
3228

3329
private fun createDatabaseAndIndicesIfNotExist() {
3430
if (!doesTableExist()) {
35-
val statement =
36-
connection!!.prepareStatement("CREATE TABLE IF NOT EXISTS `$playerCollectionName` (`uniqueId` varchar(36), `name` varchar(16), `data` LONGBLOB)")
37-
statement.executeUpdate()
38-
createIndex("uniqueId")
39-
createIndex("name")
31+
getConnection().use { connection ->
32+
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `$playerCollectionName` (`uniqueId` varchar(36), `name` varchar(16), `data` LONGBLOB)")
33+
.use { statement ->
34+
statement.executeUpdate()
35+
createIndex("uniqueId")
36+
createIndex("name")
37+
}
38+
}
4039
}
4140
}
4241

43-
private fun runReconnectLoop() {
44-
reconnect()
45-
Launcher.instance.scheduler.scheduleAtFixedRate({
46-
reconnect()
47-
}, 1, 1, TimeUnit.HOURS)
48-
}
49-
50-
private fun reconnect() = synchronized(this) {
51-
closeConnection()
52-
this.connection = DriverManager.getConnection("jdbc:sqlite:${databaseFile.path}")
42+
private fun getConnection(): Connection {
43+
return DriverManager.getConnection("jdbc:sqlite:${databaseFile.path}")
5344
}
5445

5546
private fun createIndex(columnName: String) {
56-
val statement = connection!!.prepareStatement("CREATE INDEX ${columnName} ON $playerCollectionName ($columnName)")
57-
statement.executeUpdate()
47+
getConnection().use { connection ->
48+
connection.prepareStatement("CREATE INDEX ${columnName} ON $playerCollectionName ($columnName)")
49+
.use { statement ->
50+
statement.executeUpdate()
51+
}
52+
}
5853
}
5954

6055
override fun getOfflinePlayer(playerUniqueId: UUID): IOfflineCloudPlayer? {
@@ -67,13 +62,17 @@ class SQLiteOfflineCloudPlayerHandler(private val databaseConnectionInformation:
6762

6863
private fun loadPlayer(value: String, fieldName: String): IOfflineCloudPlayer? = synchronized(this) {
6964
if (!exist(value, fieldName)) return null
70-
val statement =
71-
connection!!.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
72-
statement.setString(1, value)
73-
val resultSet = statement.executeQuery()
74-
val allDataStrings = getAllDataStringsFromResultSet(resultSet)
75-
val players = allDataStrings.mapNotNull { loadPlayerFromJsonString(it) }
76-
return getPlayerWithLatestLogin(players)
65+
66+
getConnection().use { conn ->
67+
conn.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
68+
.use { statement ->
69+
statement.setString(1, value)
70+
val resultSet = statement.executeQuery()
71+
val allDataStrings = getAllDataStringsFromResultSet(resultSet)
72+
val players = allDataStrings.mapNotNull { loadPlayerFromJsonString(it) }
73+
return getPlayerWithLatestLogin(players)
74+
}
75+
}
7776
}
7877

7978
private fun getAllDataStringsFromResultSet(resultSet: ResultSet): List<String> {
@@ -96,48 +95,61 @@ class SQLiteOfflineCloudPlayerHandler(private val databaseConnectionInformation:
9695

9796
override fun saveCloudPlayer(offlineCloudPlayer: OfflineCloudPlayer): Unit = synchronized(this) {
9897
val newData = JsonLib.fromObject(offlineCloudPlayer, databaseGson).getAsJsonString()
99-
if (!exist(offlineCloudPlayer.getUniqueId().toString(), "uniqueId")) {
100-
val statement =
101-
connection!!.prepareStatement("INSERT INTO `$playerCollectionName` (`uniqueId`, `name`, `data`) VALUES (?, ?, ?)")
102-
statement.setString(1, offlineCloudPlayer.getUniqueId().toString())
103-
statement.setString(2, offlineCloudPlayer.getName())
104-
statement.setString(3, newData)
105-
statement.executeUpdate()
106-
} else {
107-
val statement =
108-
connection!!.prepareStatement("UPDATE `$playerCollectionName` SET `data` = ?, `name` = ? WHERE `uniqueId` = ?")
109-
statement.setString(1, newData)
110-
statement.setString(2, offlineCloudPlayer.getName())
111-
statement.setString(3, offlineCloudPlayer.getUniqueId().toString())
112-
statement.executeUpdate()
98+
99+
getConnection().use { connection ->
100+
if (!exist(offlineCloudPlayer.getUniqueId().toString(), "uniqueId")) {
101+
connection.prepareStatement("INSERT INTO `$playerCollectionName` (`uniqueId`, `name`, `data`) VALUES (?, ?, ?)")
102+
.use { statement ->
103+
statement.setString(1, offlineCloudPlayer.getUniqueId().toString())
104+
statement.setString(2, offlineCloudPlayer.getName())
105+
statement.setString(3, newData)
106+
statement.executeUpdate()
107+
}
108+
} else {
109+
connection.prepareStatement("UPDATE `$playerCollectionName` SET `data` = ?, `name` = ? WHERE `uniqueId` = ?")
110+
.use { statement ->
111+
statement.setString(1, newData)
112+
statement.setString(2, offlineCloudPlayer.getName())
113+
statement.setString(3, offlineCloudPlayer.getUniqueId().toString())
114+
statement.executeUpdate()
115+
}
116+
}
113117
}
114118
}
115119

116120
override fun getRegisteredPlayerCount(): Int {
117-
val statement = connection!!.prepareStatement("SELECT COUNT(*) FROM `$playerCollectionName`")
118-
val resultSet = statement.executeQuery()
119-
return if (!resultSet.next()) {
120-
0
121-
} else {
122-
resultSet.getInt(1)
121+
getConnection().use { connection ->
122+
connection.prepareStatement("SELECT COUNT(*) FROM `$playerCollectionName`").use { statement ->
123+
val resultSet = statement.executeQuery()
124+
return if (!resultSet.next()) {
125+
0
126+
} else {
127+
resultSet.getInt(1)
128+
}
129+
}
123130
}
124131
}
125132

126133
override fun closeConnection() {
127-
connection?.close()
134+
//nothing to do
128135
}
129136

130137
private fun exist(searchValue: String, fieldName: String): Boolean {
131-
val prepareStatement =
132-
connection!!.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
133-
prepareStatement.setString(1, searchValue)
134-
val resultSet = prepareStatement.executeQuery()
135-
return resultSet.next()
138+
getConnection().use { connection ->
139+
connection.prepareStatement("SELECT `data` FROM `$playerCollectionName` WHERE `$fieldName` = ?")
140+
.use { statement ->
141+
statement.setString(1, searchValue)
142+
val resultSet = statement.executeQuery()
143+
return resultSet.next()
144+
}
145+
}
136146
}
137147

138148
private fun doesTableExist(): Boolean {
139-
val meta: DatabaseMetaData = connection!!.metaData
140-
val res = meta.getTables(null, null, this.playerCollectionName, arrayOf("TABLE"))
141-
return res.next()
149+
getConnection().use { connection ->
150+
val meta: DatabaseMetaData = connection.metaData
151+
val res = meta.getTables(null, null, this.playerCollectionName, arrayOf("TABLE"))
152+
return res.next()
153+
}
142154
}
143155
}

simplecloud-base/src/main/kotlin/eu/thesimplecloud/base/manager/update/converter/Converter_2_7_To_2_8.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Converter_2_7_To_2_8 : IVersionConverter {
1919

2020
changeProxyGroupServiceVersion("VELOCITY", "VELOCITY_1_1_9")
2121
changeProxyGroupServiceVersion("VELOCITY_3", "VELOCITY_3_4_0")
22+
changeProxyGroupServiceVersion("WATERFALL", "WATERFALL_1_21")
2223
}
2324

2425
private fun changeProxyGroupServiceVersion(from: String, to: String) {

simplecloud-modules/simplecloud-module-npc/src/main/resources/plugin.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ author: D151l
44

55
main: eu.thesimplecloud.module.npc.plugin.NPCPlugin
66

7-
depend: [ SimpleCloud-Plugin ]
7+
depend: [SimpleCloud-Plugin]
88

9-
api-version: 1.21
9+
api-version: 1.13

simplecloud-modules/simplecloud-module-permission/src/main/resources/plugin.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ author: Wetterbericht
44

55
main: eu.thesimplecloud.module.permission.service.spigot.SpigotPluginMain
66

7-
depend: [ SimpleCloud-Plugin ]
7+
depend: [SimpleCloud-Plugin]
88

9-
api-version: 1.21
9+
api-version: 1.13
1010

1111
commands:

0 commit comments

Comments
 (0)