Skip to content

Commit

Permalink
Merge pull request #134 from rlqu/revamp/chat-tab-module
Browse files Browse the repository at this point in the history
  • Loading branch information
FllipEis committed Aug 13, 2023
2 parents 4e9ab05 + 2c47460 commit 4ade668
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ package eu.thesimplecloud.module.prefix.manager

import eu.thesimplecloud.api.CloudAPI
import eu.thesimplecloud.api.external.ICloudModule
import eu.thesimplecloud.jsonlib.JsonLib
import eu.thesimplecloud.module.prefix.config.ConfigLoader
import java.io.File
import eu.thesimplecloud.launcher.startup.Launcher
import eu.thesimplecloud.module.prefix.manager.config.ChatTabModuleConfigPersistence
import eu.thesimplecloud.module.prefix.manager.command.ChatTabCommand

/**
* Created by IntelliJ IDEA.
Expand All @@ -37,21 +37,14 @@ import java.io.File
class PrefixModule : ICloudModule {

override fun onEnable() {
migrateOldConfigFile()
val configLoader = ConfigLoader()
val config = configLoader.loadConfig()
CloudAPI.instance.getGlobalPropertyHolder().setProperty("prefix-config", config)
}
val chatTabConfig = ChatTabModuleConfigPersistence.load()
ChatTabModuleConfigPersistence.save(chatTabConfig)

override fun onDisable() {}
CloudAPI.instance.getGlobalPropertyHolder().setProperty("prefix-config", chatTabConfig)
Launcher.instance.commandManager.registerCommand(this, ChatTabCommand())
}

private fun migrateOldConfigFile() {
val path = "modules/chat+tablist/config.json"
val jsonLib = JsonLib.fromJsonFile(File(path)) ?: return
if (jsonLib.getObject("disabledServerGroups", List::class.java) != null)
return
jsonLib.append("disabledServerGroups", ArrayList<String>())
jsonLib.saveAsFile(path)
override fun onDisable() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package eu.thesimplecloud.module.prefix.manager.command

import eu.thesimplecloud.api.CloudAPI
import eu.thesimplecloud.api.command.ICommandSender
import eu.thesimplecloud.api.player.ICloudPlayer
import eu.thesimplecloud.launcher.console.command.CommandType
import eu.thesimplecloud.launcher.console.command.ICommandHandler
import eu.thesimplecloud.launcher.console.command.annotations.Command
import eu.thesimplecloud.launcher.console.command.annotations.CommandArgument
import eu.thesimplecloud.launcher.console.command.annotations.CommandSubPath
import eu.thesimplecloud.launcher.console.command.provider.ServiceGroupCommandSuggestionProvider
import eu.thesimplecloud.module.prefix.manager.config.ChatTabConfig
import java.util.*

@Command(name = "chat-tab", CommandType.CONSOLE_AND_INGAME, "cloud.module.chat-tab")
class ChatTabCommand : ICommandHandler {

@CommandSubPath("set <group> <delay>", "Sets the delay for the given group")
fun executeSetDelayWithGroup(
commandSender: ICommandSender,
@CommandArgument("group", ServiceGroupCommandSuggestionProvider::class) group: String,
@CommandArgument("delay") delay: String
) {

if (CloudAPI.instance.getCloudServiceGroupManager().getServiceGroupByName(group) == null) {
commandSender.sendProperty("module.chat-tab.command.chat-tab.group-not-exist")
return
}

val config = CloudAPI.instance.getGlobalPropertyHolder().getProperty<ChatTabConfig>("prefix-config")?.getValue() ?: return
config.delay[group] = delay.toLongOrNull() ?: 0L
config.update()

commandSender.sendProperty("module.chat-tab.command.chat-tab.delay-set-success", group, delay)
}

@CommandSubPath("set <delay>", "Sets the delay for the current group you're online on")
fun executeSetDelayWithPlayerGroup(
commandSender: ICommandSender,
@CommandArgument("delay") delay: String
) {

if (commandSender !is ICloudPlayer) {
commandSender.sendProperty("module.chat-tab.command.chat-tab.no-player")
return
}

executeSetDelayWithGroup(commandSender, commandSender.getConnectedServer()!!.getGroupName(), delay)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,41 @@
* DEALINGS IN THE SOFTWARE.
*/

package eu.thesimplecloud.module.prefix.config
package eu.thesimplecloud.module.prefix.manager.config

import eu.thesimplecloud.api.CloudAPI
import eu.thesimplecloud.api.property.IProperty

/**
* Created by IntelliJ IDEA.
* User: Philipp.Eistrach
* Date: 19.12.2020
* Time: 13:36
* Date: 10.10.2020
* Time: 17:01
* @author Frederick Baier
*/
data class Config(
val disabledServerGroups: List<String> = emptyList(),
val chatFormat: String = "%PLAYER% §8» §7%MESSAGE%",
val informationList: List<TablistInformation> = listOf(TablistInformation())
class ChatTabConfig(
val chatFormat: String = "%PLAYER% §8» §7%MESSAGE%",
val informationList: List<TablistInformation> = listOf(TablistInformation()),
val disabledServerGroups: List<String> = emptyList(),
val delay: MutableMap<String, Long> = mutableMapOf(
Pair("Lobby", 0L)
)
) {

fun update() {
property = CloudAPI.instance.getGlobalPropertyHolder().setProperty("prefix-config", this)
ChatTabModuleConfigPersistence.save(this)
}

companion object {

@Volatile
private var property: IProperty<Config>? = null
private var property: IProperty<ChatTabConfig>? = null

fun getConfig(): Config {
fun getConfig(): ChatTabConfig {
if (this.property == null) {
this.property =
CloudAPI.instance.getGlobalPropertyHolder().requestProperty<Config>("prefix-config").getBlocking()
CloudAPI.instance.getGlobalPropertyHolder().requestProperty<ChatTabConfig>("prefix-config")
.getBlocking()
}
return this.property!!.getValue()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,32 @@
* DEALINGS IN THE SOFTWARE.
*/

package eu.thesimplecloud.module.prefix.config
package eu.thesimplecloud.module.prefix.manager.config

import eu.thesimplecloud.api.config.AbstractJsonLibConfigLoader
import eu.thesimplecloud.jsonlib.JsonLib
import java.io.File

/**
* Created by IntelliJ IDEA.
* User: Philipp.Eistrach
* Date: 19.12.2020
* Time: 13:36
* Date: 10.10.2020
* Time: 17:11
* @author Frederick Baier
*/
class ConfigLoader : AbstractJsonLibConfigLoader<Config>(
Config::class.java,
File("modules/chat+tablist/config.json"),
{
Config()
},
true
)
object ChatTabModuleConfigPersistence {

private val configFile = File("modules/chat+tablist/config.json")

fun save(chatTabConfig: ChatTabConfig) {
JsonLib.fromObject(chatTabConfig).saveAsFile(configFile)
}

fun load(): ChatTabConfig {
if (!configFile.exists()) return createDefaultConfig()
return JsonLib.fromJsonFile(configFile)!!.getObject(ChatTabConfig::class.java)
}

private fun createDefaultConfig(): ChatTabConfig {
return ChatTabConfig()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* DEALINGS IN THE SOFTWARE.
*/

package eu.thesimplecloud.module.prefix.config
package eu.thesimplecloud.module.prefix.manager.config

/**
* Created by IntelliJ IDEA.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package eu.thesimplecloud.module.prefix.service.spigot

import eu.thesimplecloud.api.CloudAPI
import eu.thesimplecloud.module.prefix.config.Config
import eu.thesimplecloud.module.prefix.manager.config.ChatTabConfig
import eu.thesimplecloud.module.prefix.service.spigot.listener.ChatListener
import eu.thesimplecloud.module.prefix.service.spigot.listener.CloudListener
import eu.thesimplecloud.module.prefix.service.spigot.listener.JoinListener
Expand All @@ -41,25 +41,21 @@ import org.bukkit.plugin.java.JavaPlugin
class BukkitPluginMain : JavaPlugin() {

override fun onEnable() {
instance = this

val groupName = CloudPlugin.instance.thisService().getGroupName()
if (Config.getConfig().disabledServerGroups.contains(groupName)) {
if (ChatTabConfig.getConfig().disabledServerGroups.contains(groupName)) {
Bukkit.getLogger().info("[SimpleCloud] The Chat+Tab module is deactivated on this service!")
Bukkit.getPluginManager().disablePlugin(this)
return
}

TablistHelper.load()
Bukkit.getPluginManager().registerEvents(JoinListener(), this)

Bukkit.getPluginManager().registerEvents(JoinListener(this), this)
Bukkit.getPluginManager().registerEvents(ChatListener(), this)

CloudAPI.instance.getEventManager()
.registerListener(CloudAPI.instance.getThisSidesCloudModule(), CloudListener())
}

companion object {
lateinit var instance: BukkitPluginMain
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
package eu.thesimplecloud.module.prefix.service.spigot.listener

import eu.thesimplecloud.module.permission.PermissionPool
import eu.thesimplecloud.module.prefix.config.Config
import eu.thesimplecloud.module.prefix.config.TablistInformation
import eu.thesimplecloud.module.prefix.manager.config.ChatTabConfig
import eu.thesimplecloud.module.prefix.manager.config.TablistInformation
import eu.thesimplecloud.module.prefix.service.tablist.TablistHelper
import org.bukkit.ChatColor
import org.bukkit.event.EventHandler
Expand All @@ -48,7 +48,7 @@ class ChatListener : Listener {
PermissionPool.instance.getPermissionPlayerManager().getCachedPermissionPlayer(player.uniqueId) ?: return
val canWriteColored = event.player.hasPermission("cloud.module.chat.color")
val tablistInformation = TablistHelper.getTablistInformationByUUID(player.uniqueId) ?: return
val format = ChatColor.translateAlternateColorCodes('&', Config.getConfig().chatFormat)
val format = ChatColor.translateAlternateColorCodes('&', ChatTabConfig.getConfig().chatFormat)
.replace("%PLAYER%", buildPrompt(tablistInformation))
.replace("%NAME%", event.player.name)
.replace("%PRIORITY%", tablistInformation.priority.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

package eu.thesimplecloud.module.prefix.service.spigot.listener

import eu.thesimplecloud.module.prefix.manager.config.ChatTabConfig
import eu.thesimplecloud.module.prefix.service.spigot.BukkitPluginMain
import eu.thesimplecloud.module.prefix.service.tablist.TablistHelper
import eu.thesimplecloud.plugin.startup.CloudPlugin
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
Expand All @@ -33,13 +37,15 @@ import org.bukkit.event.player.PlayerJoinEvent
* Date: 19.12.2020
* Time: 15:36
*/
class JoinListener : Listener {
class JoinListener(val plugin: BukkitPluginMain) : Listener {

@EventHandler
fun handleJoin(event: PlayerJoinEvent) {
val player = event.player

TablistHelper.updateScoreboardForAllPlayers()
Bukkit.getScheduler().runTaskLater(plugin, Runnable {
TablistHelper.updateScoreboardForAllPlayers()
}, ChatTabConfig.getConfig().delay[CloudPlugin.instance.thisService().getGroupName()] ?: 0L)

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
package eu.thesimplecloud.module.prefix.service.tablist

import eu.thesimplecloud.module.permission.PermissionPool
import eu.thesimplecloud.module.prefix.config.Config
import eu.thesimplecloud.module.prefix.config.TablistInformation
import eu.thesimplecloud.module.prefix.manager.config.ChatTabConfig
import eu.thesimplecloud.module.prefix.manager.config.TablistInformation
import java.util.*

/**
Expand All @@ -36,7 +36,7 @@ object ProxyTablistHelper {
val permissionPlayer =
PermissionPool.instance.getPermissionPlayerManager().getCachedPermissionPlayer(uuid) ?: return null

val informationList = Config.getConfig().informationList
val informationList = ChatTabConfig.getConfig().informationList
val tablistInformation = informationList.sortedBy { it.priority }.first {
permissionPlayer.hasPermissionGroup(it.groupName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
package eu.thesimplecloud.module.prefix.service.tablist

import eu.thesimplecloud.module.permission.PermissionPool
import eu.thesimplecloud.module.prefix.config.Config
import eu.thesimplecloud.module.prefix.config.TablistInformation
import eu.thesimplecloud.module.prefix.manager.config.ChatTabConfig
import eu.thesimplecloud.module.prefix.manager.config.TablistInformation
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.entity.Player
Expand All @@ -50,7 +50,7 @@ object TablistHelper {
}

private fun initScoreboard(scoreboard: Scoreboard) {
Config.getConfig().informationList.forEach {
ChatTabConfig.getConfig().informationList.forEach {
val team = scoreboard.getTeam(it.priority.toString()) ?: scoreboard.registerNewTeam(it.priority.toString())

val chatColor = ChatColor.valueOf(it.color)
Expand Down Expand Up @@ -100,7 +100,7 @@ object TablistHelper {
PermissionPool.instance.getPermissionPlayerManager().getCachedPermissionPlayer(uuid)
?: return null

val informationList = Config.getConfig().informationList
val informationList = ChatTabConfig.getConfig().informationList
val tablistInformation = informationList.sortedBy { it.priority }.first {
permissionPlayer.hasPermissionGroup(it.groupName)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"module.chat-tab.command.chat-tab.group-not-exist": "§cDie Gruppe wurde nicht gefunden.",
"module.chat-tab.command.chat-tab.delay-set-success": "§7Setze die Verzögerung für %SERVICE% auf %DELAY% ticks.",
"module.chat-tab.command.chat-tab.no-player": "§cHierfür musst du ein Spieler sein.",
"module.chat-tab.command.chat-tab.get-group-failed": "§cKonnte die Gruppe nicht finden, auf dem du online bist."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"module.chat-tab.command.chat-tab.group-not-exist": "§cGroup not found.",
"module.chat-tab.command.chat-tab.delay-set-success": "§7Set the delay for %SERVICE% to %DELAY% ticks.",
"module.chat-tab.command.chat-tab.no-player": "§cYou have to be a player to do this.",
"module.chat-tab.command.chat-tab.get-group-failed": "§cCouldn't find the group you're online on."
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import eu.thesimplecloud.api.property.IProperty
import eu.thesimplecloud.api.property.Property
import eu.thesimplecloud.api.service.ICloudService
import eu.thesimplecloud.module.permission.PermissionPool
import eu.thesimplecloud.module.prefix.config.TablistInformation
import eu.thesimplecloud.module.prefix.manager.config.TablistInformation
import eu.thesimplecloud.module.prefix.service.tablist.ProxyTablistHelper
import eu.thesimplecloud.module.proxy.config.Config
import eu.thesimplecloud.module.proxy.config.DefaultConfig
Expand Down

0 comments on commit 4ade668

Please sign in to comment.