Skip to content

Instantly share code, notes, and snippets.

@DenWav
Created September 5, 2016 07:14
Show Gist options
  • Select an option

  • Save DenWav/4788b307737c78b13f310c3bf273aa25 to your computer and use it in GitHub Desktop.

Select an option

Save DenWav/4788b307737c78b13f310c3bf273aa25 to your computer and use it in GitHub Desktop.
override fun getPlayerId(uuid: UUID) =
synchronized(this) {
if (playerMap.contains(uuid)) {
return@synchronized playerMap[uuid]!!
}
// We don't have this UUID cached, so check the database
var result: Int? = null
// byte array representation of the UUID
val array = uuid.toByte()
connection.use {
result = getFirstColumn("SELECT p.id FROM players p WHERE p.uuid = ?", array)
if (result == null) {
// This uuid isn't in the database yet, so add it
val name = plugin.getPlayerName(uuid)
// Only add it to the database if we can actually get a player name
if (name != null) {
executeUpdate("INSERT INTO players (uuid, name) VALUES (?, ?)", array, name)
result = getFirstColumn("SELECT p.id FROM players p WHERE p.uuid = ?", array)
}
}
}
if (result != null) {
playerMap[uuid] = result!!
}
return@synchronized result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment