Skip to content

Commit

Permalink
Add more code
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Nov 9, 2015
1 parent 70e006a commit cc5ec0a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<artifactId>google-http-client-gson</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.*
class Configuration(val plugin: DiscourseGroupSync) {

var DISCOURSE_URL = ""
var GROUPS: HashSet<Group> = HashSet<Group>()
var GROUPS: MutableMap<Int, Group> = HashMap<Int, Group>()

fun load() {
plugin.reloadConfig()
Expand All @@ -29,7 +29,7 @@ class Configuration(val plugin: DiscourseGroupSync) {
if (discourseGroup == null || minecraftGroup == null) return

val group = Group(discourseGroup, minecraftGroup)
GROUPS.add(group)
GROUPS.put(discourseGroup, group)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gg.obsidian.discoursegroupsync

import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.*

class DiscourseGroupSync : JavaPlugin(), Listener {

val config = Configuration(this)
val userManager = UserManager(this)

override fun onEnable() {
val configFile = File(dataFolder, "config.yml")
Expand All @@ -21,7 +23,8 @@ class DiscourseGroupSync : JavaPlugin(), Listener {
server.pluginManager.registerEvents(this, this)
}

fun getUser(username: String): List<Int> {
return ArrayList<Int>();
@EventHandler
fun onJoin(e: PlayerJoinEvent) {
userManager.syncGroups(e.player)
}
}
22 changes: 22 additions & 0 deletions src/main/kotlin/gg/obsidian/discoursegroupsync/UUIDHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gg.obsidian.discoursegroupsync

import com.mashape.unirest.http.HttpResponse
import com.mashape.unirest.http.JsonNode
import com.mashape.unirest.http.Unirest
import java.util.*

object UUIDHelper {
val PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"

fun uuidToUsername(uuid: UUID): String {
val url = PROFILE_URL + uuid.toString().replace("-", "")
val jsonResponse: HttpResponse<JsonNode> = Unirest.get(url).asJson()
val body = jsonResponse.body.`object`

if (body.has("error")) {
return ""
}

return body.getString("name")
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/gg/obsidian/discoursegroupsync/User.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package gg.obsidian.discoursegroupsync

data class User(val username: String, val minecraftGroups: Set<String>)
import java.util.*

data class User(val username: String = "", val minecraftGroups: Set<String> = HashSet(), val exists: Boolean = true)
43 changes: 43 additions & 0 deletions src/main/kotlin/gg/obsidian/discoursegroupsync/UserManger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gg.obsidian.discoursegroupsync

import com.mashape.unirest.http.HttpResponse
import com.mashape.unirest.http.JsonNode
import com.mashape.unirest.http.Unirest
import org.bukkit.entity.Player
import java.util.*

class UserManager(val plugin: DiscourseGroupSync) {

fun getDiscourseUser(username: String): User {
val url = plugin.config.DISCOURSE_URL + "/users/" + username + ".json"
val jsonResponse: HttpResponse<JsonNode> = Unirest.get(url).asJson()

if (jsonResponse.status != 200) {
return User(exists = false)
}

val body = jsonResponse.body.`object`

val minecraftGroups = HashSet<String>()

val customGroups = body.getJSONArray("custom_groups")

for (i in (0..customGroups.length() - 1)) {
val group = customGroups.get(i) as JsonNode
val groupId = group.getObject().getInt("id")

if (plugin.config.GROUPS.containsKey(groupId)) {
minecraftGroups.add(plugin.config.GROUPS[groupId].minecraftGroup)
}
}

return User(username = username, minecraftGroups = HashSet<String>())
}

fun syncGroups(player: Player) {
val username = UUIDHelper.uuidToUsername(player.uniqueId)
if (username == "") return

val discourseUser = getDiscourseUser(username)
}
}

0 comments on commit cc5ec0a

Please sign in to comment.