Skip to content

Commit

Permalink
Add display name template
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Nov 29, 2015
1 parent b34aaf3 commit c1c9ee8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/main/kotlin/gg/obsidian/obsidianpermissions/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Configuration(val plugin: Plugin) {
for (rawDefinition in plugin.config.getMapList("groups")) {
val definition = rawDefinition as Map<String, Any>
var name: String? = null
var displayName: String? = null
var rank = 10;
val permissions = HashSet<String>()

Expand All @@ -27,14 +28,23 @@ class Configuration(val plugin: Plugin) {
rank = definition["rank"] as Int
}

if (definition.contains("display_name")) {
displayName = definition["display_name"] as String
}

if (definition.contains("permissions")) {
val hashmap = definition["permissions"] as ArrayList<String>
permissions.addAll(hashmap)
}

if (name == null) continue

val group = Group(name, rank, permissions)
val group = Group(
name = name,
rank = rank,
permissions = permissions,
displayNameTemplate = displayName
)
GROUPS.add(group)
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/gg/obsidian/obsidianpermissions/Manager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.obsidian.obsidianpermissions

import gg.obsidian.obsidianpermissions.models.Group
import org.bukkit.ChatColor
import org.bukkit.entity.Player
import org.bukkit.metadata.FixedMetadataValue
import org.bukkit.permissions.Permission
Expand Down Expand Up @@ -47,6 +48,8 @@ class Manager(val plugin: Plugin) {
player.addAttachment(plugin, perm.name, true);
}

setDisplayName(player)

return true
}

Expand Down Expand Up @@ -92,6 +95,15 @@ class Manager(val plugin: Plugin) {
return plugin.configuration.getGroup(name)
}

fun setDisplayName(player: Player) {
var template = getDisplayNameTemplate(player)
if (template.contains("%p")) {
template = template.replace("%p", player.name)
}
val colorizedName = ChatColor.translateAlternateColorCodes('&', template)
player.displayName = colorizedName
}

// Private functions

private fun getPlayerState(player: Player): PlayerState? {
Expand All @@ -115,4 +127,12 @@ class Manager(val plugin: Plugin) {
}
return finalMap
}

private fun getDisplayNameTemplate(player: Player): String {
val groups = getPlayerGroups(player)
for (group in groups) {
if (group.displayNameTemplate != null) return group.displayNameTemplate
}
return "%p"
}
}
8 changes: 6 additions & 2 deletions src/main/kotlin/gg/obsidian/obsidianpermissions/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.bukkit.event.player.PlayerLoginEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.plugin.ServicePriority
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.*
import javax.persistence.PersistenceException

Expand Down Expand Up @@ -77,8 +78,11 @@ class Plugin : JavaPlugin(), Listener {
// Utilities

fun loadConfig(version: String) {
this.saveDefaultConfig()
config.options().copyDefaults(true)
val configFile = File(dataFolder, "config.yml")
if (!configFile.exists()) {
config.options().copyDefaults(true)
saveConfig()
}
config.set("version", version)
saveConfig()
configuration.load()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package gg.obsidian.obsidianpermissions.models
data class Group(
val name: String,
val rank: Int,
val permissions: Set<String>
val permissions: Set<String>,
val displayNameTemplate: String? = null
) {}

0 comments on commit c1c9ee8

Please sign in to comment.