Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to use more hex patterns. Each can be toggled #3

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.frcsty.actions.action

import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin

interface Action {
val id: String
fun run(player: Player, data: String, cache: PlaceholderCache? = null) {}
fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache? = null) {}
fun run(plugin: Plugin, player: Player, data: String, cache: PlaceholderCache? = null) {}
fun run(plugin: Plugin, player: Player, data: String, settings: Settings, cache: PlaceholderCache? = null) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.color
import com.github.frcsty.actions.util.getTranslatedMessage
import org.bukkit.Bukkit
Expand All @@ -10,7 +11,7 @@ import org.bukkit.entity.Player
object ActionbarBroadcastAction : Action {
override val id = "ACTIONBARBROADCAST"

override fun run(player: Player, data: String, cache: PlaceholderCache?) {
Bukkit.getServer().onlinePlayers.forEach { it.sendActionBar(data.getTranslatedMessage(player, cache).color()) }
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
Bukkit.getServer().onlinePlayers.forEach { it.sendActionBar(data.getTranslatedMessage(player, cache).color(settings.hexPatterns)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package com.github.frcsty.actions.action.broadcast

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.sendTranslatedMessage
import org.bukkit.Bukkit
import org.bukkit.entity.Player

object AudienceBroadcastAction : Action {
override val id = "AUDIENCEBROADCAST"

override fun run(player: Player, data: String, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach {
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach {
if (player == it) return@forEach
it.sendTranslatedMessage(data, player, cache)
it.sendTranslatedMessage(data, player, settings.hexPatterns, cache)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package com.github.frcsty.actions.action.broadcast

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.sendTranslatedMessage
import org.bukkit.Bukkit
import org.bukkit.entity.Player

object BroadcastAction : Action {
override val id = "BROADCAST"

override fun run(player: Player, data: String, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach {
it.sendTranslatedMessage(data, player, cache)
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach {
it.sendTranslatedMessage(data, player, settings.hexPatterns, cache)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.sendPersistentActionbarMessage
import org.bukkit.Bukkit
import org.bukkit.entity.Player
Expand All @@ -10,7 +11,7 @@ import org.bukkit.plugin.Plugin
object PersistentActionbarBroadcastAction : Action {
override val id = "PERSISTENTACTIONBARBROADCAST"

override fun run(plugin: Plugin, player: Player, data: String, cache: PlaceholderCache?) {
sendPersistentActionbarMessage(plugin, data, cache, player, Bukkit.getServer().onlinePlayers)
override fun run(plugin: Plugin, player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
sendPersistentActionbarMessage(plugin, data, cache, player, Bukkit.getServer().onlinePlayers, settings.hexPatterns)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.color
import com.github.frcsty.actions.util.getTranslatedMessage
import org.bukkit.Bukkit
Expand All @@ -10,7 +11,7 @@ import org.bukkit.entity.Player
object TitleBroadcastAction : Action {
override val id = "TITLEBROADCAST"

override fun run(player: Player, data: String, cache: PlaceholderCache?) {
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
val args = data.split(";")

val (title, subtitle) = args
Expand All @@ -20,8 +21,8 @@ object TitleBroadcastAction : Action {

Bukkit.getServer().onlinePlayers.forEach {
it.sendTitle(
title.getTranslatedMessage(player, cache).color(),
subtitle.getTranslatedMessage(player, cache).color(),
title.getTranslatedMessage(player, cache).color(settings.hexPatterns),
subtitle.getTranslatedMessage(player, cache).color(settings.hexPatterns),
fadeIn,
stay,
fadeOut
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package com.github.frcsty.actions.action.player

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.color
import com.github.frcsty.actions.util.getTranslatedMessage
import org.bukkit.entity.Player

object ActionbarMessageAction : Action {
override val id = "ACTIONBARMESSAGE"

override fun run(player: Player, data: String, cache: PlaceholderCache?) {
player.sendActionBar(data.getTranslatedMessage(player, cache).color())
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
player.sendActionBar(data.getTranslatedMessage(player, cache).color(settings.hexPatterns))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.DefaultFontInfo
import com.github.frcsty.actions.util.color
import com.github.frcsty.actions.util.getTranslatedMessage
Expand All @@ -13,8 +14,8 @@ object CenterMessageAction : Action {
private const val MAX_PX = 250
override val id = "CENTERMESSAGE"

override fun run(player: Player, data: String, cache: PlaceholderCache?) {
var message = data.getTranslatedMessage(player, cache).color()
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
var message = data.getTranslatedMessage(player, cache).color(settings.hexPatterns)
var messagePxSize = 0

var previousCode = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.color
import com.github.frcsty.actions.util.getTranslatedMessage
import org.bukkit.Material
Expand All @@ -12,7 +13,7 @@ object EquipItemAction : Action {

override val id = "EQUIP"

override fun run(player: Player, data: String, cache: PlaceholderCache?) {
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
val args = data.split(";")
val vars: List<String> = args[0].split(",")
val useData = vars.size == 2
Expand All @@ -32,9 +33,9 @@ object EquipItemAction : Action {
}

val meta = item.itemMeta
meta.setDisplayName(display.getTranslatedMessage(player, cache).color())
meta.setDisplayName(display.getTranslatedMessage(player, cache).color(settings.hexPatterns))
if (lore.isNotEmpty()) {
meta.lore = lore.map { it.getTranslatedMessage(player, cache).color() }
meta.lore = lore.map { it.getTranslatedMessage(player, cache).color(settings.hexPatterns) }
}

item.itemMeta = meta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package com.github.frcsty.actions.action.player

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.sendTranslatedMessage
import org.bukkit.entity.Player

object MessageAction : Action {
override val id = "MESSAGE"

override fun run(player: Player, data: String, cache: PlaceholderCache?) =
player.sendTranslatedMessage(data, player, cache)
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) =
player.sendTranslatedMessage(data, player, settings.hexPatterns, cache)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package com.github.frcsty.actions.action.player

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.sendPersistentActionbarMessage
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin

object PersistentActionbarMessageAction : Action {
override val id = "PERSISTENTACTIONBARMESSAGE"

override fun run(plugin: Plugin, player: Player, data: String, cache: PlaceholderCache?) {
sendPersistentActionbarMessage(plugin, data, cache, player, listOf(player))
override fun run(plugin: Plugin, player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
sendPersistentActionbarMessage(plugin, data, cache, player, listOf(player), settings.hexPatterns)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player

import com.github.frcsty.actions.action.Action
import com.github.frcsty.actions.cache.PlaceholderCache
import com.github.frcsty.actions.load.Settings
import com.github.frcsty.actions.util.color
import com.github.frcsty.actions.util.getTranslatedMessage
import org.bukkit.entity.Player
Expand All @@ -10,7 +11,7 @@ object TitleMessageAction : Action {

override val id = "TITLEMESSAGE"

override fun run(player: Player, data: String, cache: PlaceholderCache?) {
override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) {
val args = data.split(";")

val (title, subtitle) = args
Expand All @@ -19,8 +20,8 @@ object TitleMessageAction : Action {
val fadeOut = args.getOrNull(5)?.toIntOrNull() ?: DEFAULT_FADE_OUT

player.sendTitle(
title.getTranslatedMessage(player, cache).color(),
subtitle.getTranslatedMessage(player, cache).color(),
title.getTranslatedMessage(player, cache).color(settings.hexPatterns),
subtitle.getTranslatedMessage(player, cache).color(settings.hexPatterns),
fadeIn,
stay,
fadeOut
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.frcsty.actions.load

import java.util.logging.Logger
import java.util.regex.Pattern

interface Settings {
val hexPatterns: LinkedHashSet<Pattern>
val cacheUpdateInterval: Long
var cachedPlaceholders: List<String>
val debug: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.frcsty.actions.cache.PlaceholderCache
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitRunnable
import java.util.regex.Pattern

fun getPersistentActionbarMessageAndTime(data: String): Pair<Long, String> {
val split = data.split(";", limit = 2)
Expand All @@ -18,7 +19,8 @@ fun sendPersistentActionbarMessage(
data: String,
cache: PlaceholderCache?,
actionPlayer: Player,
recipientPlayers: Collection<Player>
recipientPlayers: Collection<Player>,
hexPatterns: LinkedHashSet<Pattern>
) {
val (time, message) = getPersistentActionbarMessageAndTime(data)

Expand All @@ -31,7 +33,7 @@ fun sendPersistentActionbarMessage(
}

for (recipientPlayer in recipientPlayers) {
recipientPlayer.sendActionBar(message.getTranslatedMessage(actionPlayer, cache).color())
recipientPlayer.sendActionBar(message.getTranslatedMessage(actionPlayer, cache).color(hexPatterns))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,21 @@ import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin

private val LEGACY_HEX_PATTERN: Pattern = Pattern.compile("#<([A-Fa-f0-9]){6}>")
private val HEX_PATTERN: Pattern = Pattern.compile("#([A-Fa-f0-9]){6}")

fun String.color(): String {
var translation = this.colorLegacy()

var matcher = HEX_PATTERN.matcher(translation)

while (matcher.find()) {
val hex: ChatColor = ChatColor.of(matcher.group())
val before = translation.substring(0, matcher.start())
val after = translation.substring(matcher.end())

translation = before + hex + after
matcher = HEX_PATTERN.matcher(translation)
}

return ChatColor.translateAlternateColorCodes('&', translation)
}

fun String.colorLegacy(): String {
fun String.color(hexPatterns: LinkedHashSet<Pattern>): String {
var translation = this

var matcher = LEGACY_HEX_PATTERN.matcher(translation)

while (matcher.find()) {
var hexString = matcher.group()

hexString = "#" + hexString.substring(2, hexString.length - 1)
val hex: ChatColor = ChatColor.of(hexString)
val before = translation.substring(0, matcher.start())
val after = translation.substring(matcher.end())

translation = before + hex + after
matcher = LEGACY_HEX_PATTERN.matcher(translation)
for (hexPattern in hexPatterns) {
var matcher = hexPattern.matcher(translation)

while (matcher.find()) {
val hex: ChatColor = ChatColor.of("#" + matcher.group("hex"))
val before = translation.substring(0, matcher.start())
val after = translation.substring(matcher.end())
translation = before + hex + after
matcher = hexPattern.matcher(translation)
}
}

return translation
return ChatColor.translateAlternateColorCodes('&', translation)
}

fun String.setPAPIPlaceholders(player: Player, cache: PlaceholderCache? = null): String {
Expand All @@ -58,16 +35,16 @@ fun String.replacePlaceholder(placeholder: String, value: String): String {
return this.replace(placeholder, value)
}

fun Player.sendTranslatedMessage(message: String, cache: PlaceholderCache? = null) {
this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(this, cache).color()))
fun Player.sendTranslatedMessage(message: String, hexPatterns: LinkedHashSet<Pattern>, cache: PlaceholderCache? = null) {
this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(this, cache).color(hexPatterns)))
}

fun Player.sendTranslatedMessage(message: String, player: Player, cache: PlaceholderCache? = null) {
this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color()))
fun Player.sendTranslatedMessage(message: String, player: Player, hexPatterns: LinkedHashSet<Pattern>, cache: PlaceholderCache? = null) {
this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color(hexPatterns)))
}

fun CommandSender.sendTranslatedMessage(player: Player, message: String, cache: PlaceholderCache? = null) {
this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color()))
fun CommandSender.sendTranslatedMessage(player: Player, message: String, hexPatterns: LinkedHashSet<Pattern>, cache: PlaceholderCache? = null) {
this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color(hexPatterns)))
}

fun String.getTranslatedMessage(player: Player, cache: PlaceholderCache? = null): String {
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/com/github/frcsty/command/FormatCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.frcsty.actions.util.replacePlaceholder
import com.github.frcsty.actions.util.sendTranslatedMessage
import com.github.frcsty.configuration.MessageLoader
import com.github.frcsty.load.Loader
import com.github.frcsty.load.Settings
import com.github.frcsty.util.removeCustomMessage
import com.github.frcsty.util.setCustomMessage
import me.mattstudios.mf.annotations.Alias
Expand Down Expand Up @@ -51,7 +52,8 @@ class FormatCommand(private val messageLoader: MessageLoader, private val loader
player,
messageLoader.getMessage("customMessageSetTargetMessage")
.replacePlaceholder("%type%", argument.lowercase())
.replacePlaceholder("%message%", message.color()),
.replacePlaceholder("%message%", message.color(loader.settings.hexPatterns)),
loader.settings.hexPatterns,
loader.placeholderCache
)
}
Expand All @@ -76,6 +78,6 @@ class FormatCommand(private val messageLoader: MessageLoader, private val loader
}

sender.sendTranslatedMessage(player, messageLoader.getMessage("customMessageRemoveTargetMessage")
.replacePlaceholder("%type%", argument.lowercase()), loader.placeholderCache)
.replacePlaceholder("%type%", argument.lowercase()), loader.settings.hexPatterns, loader.placeholderCache)
}
}
Loading