diff --git a/build.gradle b/build.gradle index ac630a7..fbf4064 100644 --- a/build.gradle +++ b/build.gradle @@ -4,10 +4,10 @@ plugins { } group = "com.github.frcsty" -version = "2.1.2" +version = "2.1.3" def path = "com.github.frcsty.frozenjoin" -def version = JavaVersion.VERSION_1_8 +def javaVersion = JavaVersion.VERSION_1_8 repositories { mavenCentral() @@ -19,15 +19,9 @@ repositories { } dependencies { - testImplementation "org.junit.jupiter:junit-jupiter:5.6.2" - testImplementation "io.kotest:kotest-runner-junit5-jvm:4.0.6" - testImplementation "io.kotest:kotest-assertions-core-jvm:4.0.6" implementation "me.mattstudios.utils:matt-framework:1.4" implementation "org.bstats:bstats-bukkit:1.7" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72" - implementation("com.github.frcsty:FrozenActions:1.0.0") { - exclude group: "com.github.frcsty.libs.kotlin" - } compileOnly "net.luckperms:api:5.1" compileOnly "com.destroystokyo.paper:paper-api:1.16.1-R0.1-SNAPSHOT" compileOnly "me.clip:placeholderapi:2.10.6" @@ -41,13 +35,15 @@ processResources { shadowJar { relocate "org.bstats", "${path}.bstats" - relocate "me.mattstudios.utils", "${path}.utils" + relocate "me.mattstudios.mf", "${path}.mf-utils" relocate "kotlin", "${path}.kotlin" + + archiveFileName.set("${project.name}-${version}.jar") } -sourceCompatibility = version -targetCompatibility = version +sourceCompatibility = javaVersion +targetCompatibility = javaVersion compileKotlin { - kotlinOptions.jvmTarget = version + kotlinOptions.jvmTarget = javaVersion } diff --git a/src/main/java/com/github/frcsty/Handler.java b/src/main/java/com/github/frcsty/Handler.java deleted file mode 100644 index f96a036..0000000 --- a/src/main/java/com/github/frcsty/Handler.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.github.frcsty; - -import com.github.frcsty.frozenactions.wrapper.ActionHandler; -import org.bukkit.entity.Player; - -import java.util.Arrays; -import java.util.List; - -public final class Handler { - - private final ActionHandler handler = new ActionHandler(); - - public void execute(final Player player, final String... actions) { - handler.execute(player, Arrays.asList(actions)); - } - - public void execute(final Player player, final List actions) { - handler.execute(player, actions); - } - -} diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/FrozenJoinPlugin.kt b/src/main/kotlin/com/github/frcsty/FrozenJoinPlugin.kt similarity index 75% rename from src/main/kotlin/com/github/frcsty/frozenjoin/FrozenJoinPlugin.kt rename to src/main/kotlin/com/github/frcsty/FrozenJoinPlugin.kt index 5c38cd9..a9b53ea 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/FrozenJoinPlugin.kt +++ b/src/main/kotlin/com/github/frcsty/FrozenJoinPlugin.kt @@ -1,6 +1,6 @@ -package com.github.frcsty.frozenjoin +package com.github.frcsty -import com.github.frcsty.frozenjoin.load.Loader +import com.github.frcsty.load.Loader import org.bukkit.plugin.java.JavaPlugin class FrozenJoinPlugin : JavaPlugin() { diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/command/ConvertCommand.kt b/src/main/kotlin/com/github/frcsty/command/ConvertCommand.kt similarity index 88% rename from src/main/kotlin/com/github/frcsty/frozenjoin/command/ConvertCommand.kt rename to src/main/kotlin/com/github/frcsty/command/ConvertCommand.kt index fee6dcb..d73c1a8 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/command/ConvertCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/ConvertCommand.kt @@ -1,11 +1,11 @@ -package com.github.frcsty.frozenjoin.command - -import com.github.frcsty.frozenjoin.FrozenJoinPlugin -import com.github.frcsty.frozenjoin.configuration.MessageLoader -import com.github.frcsty.frozenjoin.convert.FileConverter -import com.github.frcsty.frozenjoin.load.Settings -import com.github.frcsty.frozenjoin.load.logInfo -import com.github.frcsty.frozenjoin.util.replacePlaceholder +package com.github.frcsty.command + +import com.github.frcsty.FrozenJoinPlugin +import com.github.frcsty.configuration.MessageLoader +import com.github.frcsty.convert.FileConverter +import com.github.frcsty.load.Settings +import com.github.frcsty.load.logInfo +import com.github.frcsty.util.replacePlaceholder import me.mattstudios.mf.annotations.Command import me.mattstudios.mf.annotations.Permission import me.mattstudios.mf.annotations.SubCommand diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/command/FormatCommand.kt b/src/main/kotlin/com/github/frcsty/command/FormatCommand.kt similarity index 89% rename from src/main/kotlin/com/github/frcsty/frozenjoin/command/FormatCommand.kt rename to src/main/kotlin/com/github/frcsty/command/FormatCommand.kt index 87d7bf5..5f89964 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/command/FormatCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/FormatCommand.kt @@ -1,7 +1,7 @@ -package com.github.frcsty.frozenjoin.command +package com.github.frcsty.command -import com.github.frcsty.frozenjoin.configuration.MessageLoader -import com.github.frcsty.frozenjoin.util.* +import com.github.frcsty.configuration.MessageLoader +import com.github.frcsty.util.* import me.mattstudios.mf.annotations.Command import me.mattstudios.mf.annotations.Permission import me.mattstudios.mf.annotations.SubCommand @@ -34,8 +34,7 @@ class FormatCommand(private val messageLoader: MessageLoader) : CommandBase() { when (argument) { JOIN_COMMAND, QUIT_COMMAND -> { - player.setCustomMessage(JOIN_COMMAND, message = msg) - player.setCustomMessage(QUIT_COMMAND, message = msg) + player.setCustomMessage(argument, message = msg) } else -> { sender.sendMessage(messageLoader.getMessage("customMessageInvalidArgumentMessage")) diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/command/HelpCommand.kt b/src/main/kotlin/com/github/frcsty/command/HelpCommand.kt similarity index 73% rename from src/main/kotlin/com/github/frcsty/frozenjoin/command/HelpCommand.kt rename to src/main/kotlin/com/github/frcsty/command/HelpCommand.kt index b896d62..29e8c76 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/command/HelpCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/HelpCommand.kt @@ -1,10 +1,10 @@ -package com.github.frcsty.frozenjoin.command +package com.github.frcsty.command -import com.github.frcsty.frozenjoin.configuration.MessageLoader -import com.github.frcsty.frozenjoin.util.color -import com.github.frcsty.frozenjoin.util.replacePlaceholder -import com.github.frcsty.frozenjoin.load.Settings -import com.github.frcsty.frozenjoin.load.logInfo +import com.github.frcsty.configuration.MessageLoader +import com.github.frcsty.load.Settings +import com.github.frcsty.load.logInfo +import com.github.frcsty.util.color +import com.github.frcsty.util.replacePlaceholder import me.mattstudios.mf.annotations.Command import me.mattstudios.mf.annotations.Permission import me.mattstudios.mf.annotations.SubCommand diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/command/InfoCommand.kt b/src/main/kotlin/com/github/frcsty/command/InfoCommand.kt similarity index 71% rename from src/main/kotlin/com/github/frcsty/frozenjoin/command/InfoCommand.kt rename to src/main/kotlin/com/github/frcsty/command/InfoCommand.kt index c42ab2e..3c3b5ac 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/command/InfoCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/InfoCommand.kt @@ -1,11 +1,11 @@ -package com.github.frcsty.frozenjoin.command +package com.github.frcsty.command -import com.github.frcsty.frozenjoin.FrozenJoinPlugin -import com.github.frcsty.frozenjoin.configuration.MessageLoader -import com.github.frcsty.frozenjoin.load.Settings -import com.github.frcsty.frozenjoin.load.logInfo -import com.github.frcsty.frozenjoin.util.color -import com.github.frcsty.frozenjoin.util.replacePlaceholder +import com.github.frcsty.FrozenJoinPlugin +import com.github.frcsty.configuration.MessageLoader +import com.github.frcsty.load.Settings +import com.github.frcsty.load.logInfo +import com.github.frcsty.util.color +import com.github.frcsty.util.replacePlaceholder import me.mattstudios.mf.annotations.Command import me.mattstudios.mf.annotations.Default import me.mattstudios.mf.annotations.Permission diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/command/MotdCommand.kt b/src/main/kotlin/com/github/frcsty/command/MotdCommand.kt similarity index 76% rename from src/main/kotlin/com/github/frcsty/frozenjoin/command/MotdCommand.kt rename to src/main/kotlin/com/github/frcsty/command/MotdCommand.kt index 45d6581..d176adc 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/command/MotdCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/MotdCommand.kt @@ -1,8 +1,8 @@ -package com.github.frcsty.frozenjoin.command +package com.github.frcsty.command -import com.github.frcsty.frozenjoin.configuration.MessageLoader -import com.github.frcsty.frozenjoin.load.Loader -import com.github.frcsty.frozenjoin.message.MessageFormatter +import com.github.frcsty.configuration.MessageLoader +import com.github.frcsty.load.Loader +import com.github.frcsty.message.MessageFormatter import me.mattstudios.mf.annotations.Command import me.mattstudios.mf.annotations.Default import me.mattstudios.mf.annotations.Permission diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/command/ReloadCommand.kt b/src/main/kotlin/com/github/frcsty/command/ReloadCommand.kt similarity index 79% rename from src/main/kotlin/com/github/frcsty/frozenjoin/command/ReloadCommand.kt rename to src/main/kotlin/com/github/frcsty/command/ReloadCommand.kt index 04ed9dd..2de560b 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/command/ReloadCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/ReloadCommand.kt @@ -1,11 +1,11 @@ -package com.github.frcsty.frozenjoin.command - -import com.github.frcsty.frozenjoin.FrozenJoinPlugin -import com.github.frcsty.frozenjoin.configuration.MessageLoader -import com.github.frcsty.frozenjoin.util.color -import com.github.frcsty.frozenjoin.load.Loader -import com.github.frcsty.frozenjoin.load.Settings -import com.github.frcsty.frozenjoin.load.logInfo +package com.github.frcsty.command + +import com.github.frcsty.FrozenJoinPlugin +import com.github.frcsty.configuration.MessageLoader +import com.github.frcsty.load.Loader +import com.github.frcsty.load.Settings +import com.github.frcsty.load.logInfo +import com.github.frcsty.util.color import me.mattstudios.mf.annotations.Command import me.mattstudios.mf.annotations.Permission import me.mattstudios.mf.annotations.SubCommand diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/configuration/MessageLoader.kt b/src/main/kotlin/com/github/frcsty/configuration/MessageLoader.kt similarity index 85% rename from src/main/kotlin/com/github/frcsty/frozenjoin/configuration/MessageLoader.kt rename to src/main/kotlin/com/github/frcsty/configuration/MessageLoader.kt index 1560858..72f1b71 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/configuration/MessageLoader.kt +++ b/src/main/kotlin/com/github/frcsty/configuration/MessageLoader.kt @@ -1,7 +1,7 @@ -package com.github.frcsty.frozenjoin.configuration +package com.github.frcsty.configuration -import com.github.frcsty.frozenjoin.FrozenJoinPlugin -import com.github.frcsty.frozenjoin.util.color +import com.github.frcsty.FrozenJoinPlugin +import com.github.frcsty.util.color import org.bukkit.configuration.ConfigurationSection class MessageLoader(private val plugin: FrozenJoinPlugin) { diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/convert/FileConverter.kt b/src/main/kotlin/com/github/frcsty/convert/FileConverter.kt similarity index 97% rename from src/main/kotlin/com/github/frcsty/frozenjoin/convert/FileConverter.kt rename to src/main/kotlin/com/github/frcsty/convert/FileConverter.kt index 70a8894..939467c 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/convert/FileConverter.kt +++ b/src/main/kotlin/com/github/frcsty/convert/FileConverter.kt @@ -1,6 +1,6 @@ -package com.github.frcsty.frozenjoin.convert +package com.github.frcsty.convert -import com.github.frcsty.frozenjoin.load.Settings +import com.github.frcsty.load.Settings import org.bukkit.configuration.file.YamlConfiguration import java.io.File import java.util.logging.Level diff --git a/src/main/kotlin/com/github/frcsty/library/ActionHandler.kt b/src/main/kotlin/com/github/frcsty/library/ActionHandler.kt new file mode 100644 index 0000000..9fea0ae --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/ActionHandler.kt @@ -0,0 +1,112 @@ +package com.github.frcsty.library + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.actions.broadcast.* +import com.github.frcsty.library.actions.player.* +import com.github.frcsty.library.time.parseTime +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import org.bukkit.plugin.Plugin +import java.util.* +import java.util.concurrent.TimeUnit + +private val ACTION_PATTERN = Regex("(.*) ?\\[(?[A-Z]+?)] ?(?.+)", RegexOption.IGNORE_CASE) +private val DELAY_PATTERN = Regex("\\[DELAY=(?\\d+[a-z])]", RegexOption.IGNORE_CASE) +private val CHANCE_PATTERN = Regex("\\[CHANCE=(?\\d+)]", RegexOption.IGNORE_CASE) +private val RANDOM = SplittableRandom() + +class ActionHandler(private val plugin: Plugin) { + + val actions = mutableMapOf() + + fun loadDefault() { + setOf( + ActionbarBroadcastAction, + ActionbarMessageAction, + BroadcastAction, + BungeeAction, + CenterBroadcastAction, + CenterMessageAction, + ConsoleCommandAction, + EquipItemAction, + JsonBroadcastAction, + JsonMessageAction, + MessageAction, + PlayerCommandAction, + SoundAction, + SoundBroadcastAction, + TeleportAction, + TitleBroadcastAction, + TitleMessageAction + ).forEach { this.actions[it.id] = it } + } + + fun setAction(identifier: String, action: Action) { + this.actions[identifier] = action + } + + fun execute(player: Player, input: List) { + input.forEach { execute(player, it) } + } + + private fun execute(player: Player, input: String) { + val actionHolder = getDelayAction(hasChanceAction(input) ?: return) + val inputAction = actionHolder.action + val match = ACTION_PATTERN.matchEntire(inputAction) + + if (match == null) { + println("Action does not match regex $inputAction") + return + } + + val arguments = match.groups["arguments"]?.value ?: return + val delay = actionHolder.delay + + val actionName = match.groups["action"]?.value?.toUpperCase() + + val action = actions[actionName] ?: return + + Bukkit.getScheduler().runTaskLater( + plugin, + Runnable { + action.run(player, arguments) + action.run(plugin, player, arguments) + }, + delay + ) + } + + private fun hasChanceAction(input: String): String? { + val match = CHANCE_PATTERN.matchEntire(input) ?: return input + val chanceGroup = match.groups["chance"] ?: return null + val chance = chanceGroup.value.toInt() + + val randomValue = RANDOM.nextInt(100) + 1 + + return if (randomValue <= chance) { + input.replace(chanceGroup.value, "") + } else null + } + + private fun getDelayAction(input: String): ActionHolder { + val match = DELAY_PATTERN.matchEntire(input) ?: return ActionHolder(input, 0L) + + val delayGroup = match.groups["delay"] ?: return ActionHolder(input, 0L) + val delay = delayGroup.value + + return try { + val time = delay.parseTime() + ActionHolder( + action = input.replace(delayGroup.value, ""), + delay = time.to(TimeUnit.SECONDS) * 20L + ) + } catch (ex: IllegalArgumentException) { + ex.printStackTrace() + ActionHolder(input, 0L) + } + } + + init { + Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/ActionHolder.kt b/src/main/kotlin/com/github/frcsty/library/ActionHolder.kt new file mode 100644 index 0000000..2241deb --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/ActionHolder.kt @@ -0,0 +1,6 @@ +package com.github.frcsty.library + +class ActionHolder( + val action: String, + val delay: Long +) \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/Action.kt b/src/main/kotlin/com/github/frcsty/library/actions/Action.kt new file mode 100644 index 0000000..50a4840 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/Action.kt @@ -0,0 +1,10 @@ +package com.github.frcsty.library.actions + +import org.bukkit.entity.Player +import org.bukkit.plugin.Plugin + +interface Action { + val id: String + fun run(player: Player, data: String) {} + fun run(plugin: Plugin, player: Player, data: String) {} +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/broadcast/ActionbarBroadcastAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/ActionbarBroadcastAction.kt new file mode 100644 index 0000000..f25ba50 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/ActionbarBroadcastAction.kt @@ -0,0 +1,15 @@ +package com.github.frcsty.library.actions.broadcast + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.getTranslatedMessage +import com.github.frcsty.library.util.sendActionBarMessage +import org.bukkit.Bukkit +import org.bukkit.entity.Player + +object ActionbarBroadcastAction : Action { + override val id = "ACTIONBARBROADCAST" + + override fun run(player: Player, data: String) { + Bukkit.getServer().onlinePlayers.forEach { it.sendActionBarMessage(data.getTranslatedMessage(player)) } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/broadcast/BroadcastAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/BroadcastAction.kt new file mode 100644 index 0000000..5f1a305 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/BroadcastAction.kt @@ -0,0 +1,14 @@ +package com.github.frcsty.library.actions.broadcast + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.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) = Bukkit.getServer().onlinePlayers.forEach { + it.sendTranslatedMessage(data, player) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/broadcast/CenterBroadcastAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/CenterBroadcastAction.kt new file mode 100644 index 0000000..29b81aa --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/CenterBroadcastAction.kt @@ -0,0 +1,16 @@ +package com.github.frcsty.library.actions.broadcast + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.actions.player.CenterMessageAction +import com.github.frcsty.library.util.getTranslatedMessage +import org.bukkit.Bukkit +import org.bukkit.entity.Player + +object CenterBroadcastAction : Action { + + override val id = "CENTERBROADCAST" + + override fun run(player: Player, data: String) { + Bukkit.getServer().onlinePlayers.forEach { CenterMessageAction.run(it, data.getTranslatedMessage(player)) } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/broadcast/JsonBroadcastAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/JsonBroadcastAction.kt new file mode 100644 index 0000000..3b38858 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/JsonBroadcastAction.kt @@ -0,0 +1,15 @@ +package com.github.frcsty.library.actions.broadcast + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.getTranslatedMessage +import org.bukkit.Bukkit +import org.bukkit.entity.Player + +object JsonBroadcastAction : Action { + + override val id = "JSONBROADCAST" + + override fun run(player: Player, data: String) { + Bukkit.getServer().onlinePlayers.forEach { Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw ${it.name} ${data.getTranslatedMessage(player)}") } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/broadcast/SoundBroadcastAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/SoundBroadcastAction.kt new file mode 100644 index 0000000..7075744 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/SoundBroadcastAction.kt @@ -0,0 +1,20 @@ +package com.github.frcsty.library.actions.broadcast + +import com.github.frcsty.library.actions.Action +import org.bukkit.Bukkit +import org.bukkit.Sound +import org.bukkit.entity.Player + +object SoundBroadcastAction : Action { + + override val id = "SOUNDBROADCAST" + + override fun run(player: Player, data: String) { + val args = data.split(";") + val sound = Sound.valueOf(args[0]) + val volume: Float = args[1].toFloat() + val pitch: Float = args[2].toFloat() + + Bukkit.getServer().onlinePlayers.forEach { it.playSound(it.location, sound, volume, pitch) } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/broadcast/TitleBroadcastAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/TitleBroadcastAction.kt new file mode 100644 index 0000000..2412fba --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/broadcast/TitleBroadcastAction.kt @@ -0,0 +1,27 @@ +package com.github.frcsty.library.actions.broadcast + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.sendPlayerTitle +import org.bukkit.Bukkit +import org.bukkit.entity.Player + +object TitleBroadcastAction : Action { + override val id = "TITLEBROADCAST" + + override fun run(player: Player, data: String) { + val args = data.split(";") + + val (title, subtitle) = args + val fadeIn = args.getOrNull(3)?.toIntOrNull() ?: DEFAULT_FADE_IN + val stay = args.getOrNull(4)?.toIntOrNull() ?: DEFAULT_STAY + val fadeOut = args.getOrNull(5)?.toIntOrNull() ?: DEFAULT_FADE_OUT + + Bukkit.getServer().onlinePlayers.forEach { + it.sendPlayerTitle(title, subtitle, fadeIn, stay, fadeOut) + } + } + + private const val DEFAULT_FADE_IN = 5 + private const val DEFAULT_STAY = 10 + private const val DEFAULT_FADE_OUT = 5 +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/ActionbarMessageAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/ActionbarMessageAction.kt new file mode 100644 index 0000000..6c38dd3 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/ActionbarMessageAction.kt @@ -0,0 +1,13 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.sendActionBarMessage +import org.bukkit.entity.Player + +object ActionbarMessageAction : Action { + override val id = "ACTIONBARMESSAGE" + + override fun run(player: Player, data: String) { + player.sendActionBarMessage(data) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/BungeeAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/BungeeAction.kt new file mode 100644 index 0000000..ca0037e --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/BungeeAction.kt @@ -0,0 +1,21 @@ +@file:Suppress("UnstableApiUsage") + +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.google.common.io.ByteStreams +import org.bukkit.entity.Player +import org.bukkit.plugin.Plugin +import java.util.* + +object BungeeAction : Action { + + override val id = "BUNGEE" + + override fun run(plugin: Plugin, player: Player, data: String) { + val output = ByteStreams.newDataOutput() + Arrays.stream(arrayOf("Connect", data)).forEach { s: String -> output.writeUTF(s) } + + player.sendPluginMessage(plugin, "BungeeCord", output.toByteArray()) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/CenterMessageAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/CenterMessageAction.kt new file mode 100644 index 0000000..d1fc70d --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/CenterMessageAction.kt @@ -0,0 +1,70 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.DefaultFontInfo +import com.github.frcsty.library.util.color +import org.bukkit.entity.Player + +object CenterMessageAction : Action { + + private const val CENTER_PX = 154 + private const val MAX_PX = 250 + override val id = "CENTERMESSAGE" + + override fun run(player: Player, data: String) { + var message = data.color() + var messagePxSize = 0 + + var previousCode = false + var isBold = false + + var charIndex = 0 + var lastSpaceIndex = 0 + var toSendAfter: String? = null + var recentColorCode = "" + + for (c in message.toCharArray()) { + if (c == '§') { + previousCode = true + continue + } else if (previousCode) { + previousCode = false + recentColorCode = "§$c" + if (c == 'l' || c == 'L') { + isBold = true + continue + } else { + isBold = false + } + } else if (c == ' ') { + lastSpaceIndex = charIndex + } else { + val dfi: DefaultFontInfo = DefaultFontInfo.getDefaultFontInfo(c) + messagePxSize += if (isBold) dfi.boldLength else dfi.length + messagePxSize++ + } + if (messagePxSize >= MAX_PX) { + toSendAfter = recentColorCode + message.substring(lastSpaceIndex + 1) + message = message.substring(0, lastSpaceIndex + 1) + break + } + charIndex++ + } + + val halvedMessageSize = messagePxSize / 2 + val toCompensate: Int = CENTER_PX - halvedMessageSize + val spaceLength: Int = DefaultFontInfo.SPACE.length + 1 + var compensated = 0 + + val sb = StringBuilder() + while (compensated < toCompensate) { + sb.append(" ") + compensated += spaceLength + } + + player.sendMessage(sb.toString() + message) + if (toSendAfter != null) { + run(player, toSendAfter) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/ConsoleCommandAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/ConsoleCommandAction.kt new file mode 100644 index 0000000..74e72d3 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/ConsoleCommandAction.kt @@ -0,0 +1,15 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.getTranslatedMessage +import org.bukkit.Bukkit +import org.bukkit.entity.Player + +object ConsoleCommandAction : Action { + + override val id = "CONSOLECOMMAND" + + override fun run(player: Player, data: String) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), data.getTranslatedMessage(player)) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/EquipItemAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/EquipItemAction.kt new file mode 100644 index 0000000..21a374a --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/EquipItemAction.kt @@ -0,0 +1,47 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.color +import org.bukkit.Material +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack + +object EquipItemAction : Action { + + override val id = "EQUIP" + + override fun run(player: Player, data: String) { + val args = data.split(";") + val vars: List = args[0].split(",") + val useData = vars.size == 2 + val material: Material = + if (useData) Material.getMaterial(vars[1]) ?: return + else Material.getMaterial(args[0]) ?: return + val data = if (useData) Integer.valueOf(vars[1]) else 0 + val amount = args[3].toInt() + val display = args[1] + val slot = args[4].toInt() + val lore = args[2].split("~") + + val item: ItemStack + item = if (useData) { + ItemStack(material, amount, data.toShort()) + } else { + ItemStack(material, amount) + } + + val meta = item.itemMeta + meta.setDisplayName(display.color()) + if (lore.isNotEmpty()) { + meta.lore = lore + } + + item.itemMeta = meta + + if (player.inventory.getItem(slot) == null) { + player.inventory.setItem(slot, item) + } else { + player.inventory.addItem(item) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/JsonMessageAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/JsonMessageAction.kt new file mode 100644 index 0000000..11c6def --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/JsonMessageAction.kt @@ -0,0 +1,15 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.getTranslatedMessage +import org.bukkit.Bukkit +import org.bukkit.entity.Player + +object JsonMessageAction : Action { + + override val id = "JSONMESSAGE" + + override fun run(player: Player, data: String) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw ${player.name} ${data.getTranslatedMessage(player)}") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/MessageAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/MessageAction.kt new file mode 100644 index 0000000..e5e52d2 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/MessageAction.kt @@ -0,0 +1,11 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.sendTranslatedMessage +import org.bukkit.entity.Player + +object MessageAction : Action { + override val id = "MESSAGE" + + override fun run(player: Player, data: String) = player.sendTranslatedMessage(data, player) +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/PlayerCommandAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/PlayerCommandAction.kt new file mode 100644 index 0000000..76d9b74 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/PlayerCommandAction.kt @@ -0,0 +1,14 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.util.getTranslatedMessage +import org.bukkit.entity.Player + +object PlayerCommandAction : Action { + + override val id = "PLAYERCOMMAND" + + override fun run(player: Player, data: String) { + player.performCommand(data.getTranslatedMessage(player)) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/SoundAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/SoundAction.kt new file mode 100644 index 0000000..08afdb4 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/SoundAction.kt @@ -0,0 +1,19 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import org.bukkit.Sound +import org.bukkit.entity.Player + +object SoundAction : Action { + + override val id = "SOUND" + + override fun run(player: Player, data: String) { + val args = data.split(";") + val sound = Sound.valueOf(args[0]) + val volume: Float = args[1].toFloat() + val pitch: Float = args[2].toFloat() + + player.playSound(player.location, sound, volume, pitch) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/TeleportAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/TeleportAction.kt new file mode 100644 index 0000000..40cca7a --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/TeleportAction.kt @@ -0,0 +1,25 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import org.bukkit.Bukkit +import org.bukkit.Location +import org.bukkit.entity.Player + +object TeleportAction : Action { + + override val id = "TELEPORT" + + override fun run(player: Player, data: String) { + val args = data.split(";") + val location = Location( + Bukkit.getWorld(args[0]), + args[1].toDouble(), + args[2].toDouble(), + args[3].toDouble(), + args[4].toFloat(), + args[5].toFloat() + ) + + player.teleport(location) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/actions/player/TitleMessageAction.kt b/src/main/kotlin/com/github/frcsty/library/actions/player/TitleMessageAction.kt new file mode 100644 index 0000000..b79bcc8 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/actions/player/TitleMessageAction.kt @@ -0,0 +1,16 @@ +package com.github.frcsty.library.actions.player + +import com.github.frcsty.library.actions.Action +import com.github.frcsty.library.actions.broadcast.TitleBroadcastAction +import org.bukkit.entity.Player + +object TitleMessageAction : Action { + + override val id = "TITLEMESSAGE" + + override fun run(player: Player, data: String) { + val args = data.split(";") + + TitleBroadcastAction.run(player, (listOf("PLAYER") + args.drop(1)).joinToString(";")) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/time/TimeAPI.kt b/src/main/kotlin/com/github/frcsty/library/time/TimeAPI.kt new file mode 100644 index 0000000..f8b8dd0 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/time/TimeAPI.kt @@ -0,0 +1,47 @@ +package com.github.frcsty.library.time + +import java.util.concurrent.TimeUnit + +private const val DAYS_IN_WEEK = 7 +private const val DAYS_IN_MONTH = 30 +private const val DAYS_IN_YEAR = 365 + +/** + * Parses a String in a "time" format (eg 3m2s) and returns the actual time value in seconds + */ +fun String.parseTime(): TimeResult { + var seconds = 0L + val scanner = TimeScanner( + this + .replace(" ", "") + .replace("and", "") + .replace(",", "") + .toLowerCase() + ) + var next: Long + while (scanner.hasNext()) { + next = scanner.nextLong() + seconds += when (scanner.nextString()) { + "s", "sec", "secs", "second", "seconds" -> next + "m", "min", "mins", "minute", "minutes" -> TimeUnit.MINUTES.toSeconds(next) + "h", "hr", "hrs", "hour", "hours" -> TimeUnit.HOURS.toSeconds(next) + "d", "dy", "dys", "day", "days" -> TimeUnit.DAYS.toSeconds(next) + "w", "week", "weeks" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_WEEK) + "mo", "mon", "mnth", "month", "months" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_MONTH) + "y", "yr", "yrs", "year", "years" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_YEAR) + else -> throw IllegalArgumentException() + } + } + return TimeResult(seconds) +} + +/** + * Possibly over-engineering, but this allows us to keep a meaningful return value from [parseTime] while not basically copying [TimeUnit] + */ +@Suppress("EXPERIMENTAL_FEATURE_WARNING") +inline class TimeResult(private val seconds: Long) { + + fun to(unit: TimeUnit): Long { + return unit.convert(seconds, TimeUnit.SECONDS) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/time/TimeScanner.kt b/src/main/kotlin/com/github/frcsty/library/time/TimeScanner.kt new file mode 100644 index 0000000..4294f27 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/time/TimeScanner.kt @@ -0,0 +1,27 @@ +package com.github.frcsty.library.time + +internal class TimeScanner(time: String) { + private val time = time.toCharArray() + private var index = 0 + + operator fun hasNext(): Boolean { + return index < time.size - 1 + } + + fun nextLong(): Long { + return String(next(Char::isDigit)).toLong() + } + + fun nextString(): String { + return String(next { codePoint -> + Character.isAlphabetic(codePoint.toInt()) + }) + } + + private fun next(condition: (Char) -> Boolean): CharArray { + val startIndex = index + while (++index < time.size && condition(time[index])); + return time.copyOfRange(startIndex, index) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/util/DefaultFontInfo.kt b/src/main/kotlin/com/github/frcsty/library/util/DefaultFontInfo.kt new file mode 100644 index 0000000..d351ba0 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/util/DefaultFontInfo.kt @@ -0,0 +1,18 @@ +package com.github.frcsty.library.util + +enum class DefaultFontInfo(val character: Char, val length: Int) { + A('A', 5), a('a', 5), B('B', 5), b('b', 5), C('C', 5), c('c', 5), D('D', 5), d('d', 5), E('E', 5), e('e', 5), F('F', 5), f('f', 4), G('G', 5), g('g', 5), H('H', 5), h('h', 5), I('I', 3), i('i', 1), J('J', 5), j('j', 5), K('K', 5), k('k', 4), L('L', 5), l('l', 1), M('M', 5), m('m', 5), N('N', 5), n('n', 5), O('O', 5), o('o', 5), P('P', 5), p('p', 5), Q('Q', 5), q('q', 5), R('R', 5), r('r', 5), S('S', 5), s('s', 5), T('T', 5), t('t', 4), U('U', 5), u('u', 5), V('V', 5), v('v', 5), W('W', 5), w('w', 5), X('X', 5), x('x', 5), Y('Y', 5), y('y', 5), Z('Z', 5), z('z', 5), NUM_1('1', 5), NUM_2('2', 5), NUM_3('3', 5), NUM_4('4', 5), NUM_5('5', 5), NUM_6('6', 5), NUM_7('7', 5), NUM_8('8', 5), NUM_9('9', 5), NUM_0('0', 5), EXCLAMATION_POINT('!', 1), AT_SYMBOL('@', 6), NUM_SIGN('#', 5), DOLLAR_SIGN('$', 5), PERCENT('%', 5), UP_ARROW('^', 5), AMPERSAND('&', 5), ASTERISK('*', 5), LEFT_PARENTHESIS('(', 4), RIGHT_PERENTHESIS(')', 4), MINUS('-', 5), UNDERSCORE('_', 5), PLUS_SIGN('+', 5), EQUALS_SIGN('=', 5), LEFT_CURL_BRACE('{', 4), RIGHT_CURL_BRACE('}', 4), LEFT_BRACKET('[', 3), RIGHT_BRACKET(']', 3), COLON(':', 1), SEMI_COLON(';', 1), DOUBLE_QUOTE('"', 3), SINGLE_QUOTE('\'', 1), LEFT_ARROW('<', 4), RIGHT_ARROW('>', 4), QUESTION_MARK('?', 5), SLASH('/', 5), BACK_SLASH('\\', 5), LINE('|', 1), TILDE('~', 5), TICK('`', 2), PERIOD('.', 1), COMMA(',', 1), SPACE(' ', 3), DEFAULT('a', 4); + + val boldLength: Int + get() = if (this == SPACE) length else length + 1 + + companion object { + fun getDefaultFontInfo(c: Char): DefaultFontInfo { + for (dFI in values()) { + if (dFI.character == c) return dFI + } + return DEFAULT + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/util/Extensions.kt b/src/main/kotlin/com/github/frcsty/library/util/Extensions.kt new file mode 100644 index 0000000..dbed469 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/util/Extensions.kt @@ -0,0 +1,55 @@ +package com.github.frcsty.library.util + +import me.clip.placeholderapi.PlaceholderAPI +import net.md_5.bungee.api.ChatColor +import net.md_5.bungee.api.chat.TextComponent +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import org.bukkit.plugin.Plugin +import java.util.regex.Pattern + +private val HEX_PATTERN: Pattern = Pattern.compile("#<([A-Fa-f0-9]){6}>") + +fun String.color(): String { + var translation = this + + if (Reflection.is16) { + var matcher = 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 = HEX_PATTERN.matcher(translation) + } + } + + return ChatColor.translateAlternateColorCodes('&', translation) +} + +fun Player.sendTranslatedMessage(msg: String, player: Player) { + val message = msg.getTranslatedMessage(player) + + if (Reflection.is16) { + this.spigot().sendMessage(*TextComponent.fromLegacyText(message.color())) + return + } + + this.sendMessage(message.color()) +} + +fun String.getTranslatedMessage(player: Player): String { + var message = this + val daddy: Plugin? = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") + + if (daddy != null && daddy.isEnabled) { + message = PlaceholderAPI.setPlaceholders(player, message) + } + + return message +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/util/Reflection.kt b/src/main/kotlin/com/github/frcsty/library/util/Reflection.kt new file mode 100644 index 0000000..d5bfa25 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/util/Reflection.kt @@ -0,0 +1,23 @@ +package com.github.frcsty.library.util + +import org.bukkit.Bukkit + +object Reflection { + private val version by lazy { + Bukkit.getServer().javaClass.`package`.name.split(".")[3] + } + + val is1_8: Boolean + get() = "v1_8" in version + + val is16: Boolean + get() = "v1_16" in version + + fun getNMSClass(className: String): Class<*> { + return Class.forName("net.minecraft.server.$version.$className") + } + + fun getCraftBukkitClass(className: String): Class<*> { + return Class.forName("org.bukkit.craftbukkit.$version.$className") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/library/util/Titles.kt b/src/main/kotlin/com/github/frcsty/library/util/Titles.kt new file mode 100644 index 0000000..5c01458 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/library/util/Titles.kt @@ -0,0 +1,104 @@ +package com.github.frcsty.library.util + +import org.bukkit.entity.Player + +private val entityPlayerClass by lazy { + Reflection.getNMSClass("EntityPlayer") +} + +private val playerConnectionClass by lazy { + Reflection.getNMSClass("PlayerConnection") +} + +private val playerConnectionField by lazy { + entityPlayerClass.getField("playerConnection") +} + +private val craftPlayerClass by lazy { + Reflection.getCraftBukkitClass("entity.CraftPlayer") +} + +private val packetClass by lazy { + Reflection.getNMSClass("Packet") +} + +private val sendPacketMethod by lazy { + playerConnectionClass.getMethod("sendPacket", packetClass) +} + +private val getHandleMethod by lazy { + craftPlayerClass.getMethod("getHandle") +} + +private val chatSerializerMethod by lazy { + Reflection.getNMSClass("IChatBaseComponent\$ChatSerializer").getMethod("a", String::class.java) +} + +private val enumTitleActionClass by lazy { + Reflection.getNMSClass("PacketPlayOutTitle\$EnumTitleAction") +} + +private val iChatBaseComponentClass by lazy { + Reflection.getNMSClass("IChatBaseComponent") +} + +private val packetPlayOutTitleConstructor by lazy { + Reflection.getNMSClass("PacketPlayOutTitle") + .getConstructor(enumTitleActionClass, iChatBaseComponentClass, Int::class.java, Int::class.java, Int::class.java) +} + +private val packetPlayOutChatClass by lazy { + Reflection.getNMSClass("PacketPlayOutChat") +} + +private val packetPlayOutChatConstructor by lazy { + Reflection.getNMSClass("PacketPlayOutChat") + .getConstructor(packetPlayOutChatClass, iChatBaseComponentClass, Byte::class.java) +} + +private val titleEnum by lazy { + enumTitleActionClass.getField("TITLE").get(null) +} +private val subtitleEnum by lazy { + enumTitleActionClass.getField("SUBTITLE").get(null) +} + +fun Player.sendPlayerTitle(title: String, subtitle: String?, fadeIn: Int, stay: Int, fadeOut: Int) { + if (Reflection.is1_8) { + val titleBaseComponent = createIChatBaseComponent(title) + val titlePacket: Any = packetPlayOutTitleConstructor.newInstance(titleEnum, titleBaseComponent, fadeIn, stay, fadeOut) + + sendPacket(titlePacket) + + if (subtitle != null) { + val subtitleBaseComponent: Any = createIChatBaseComponent(subtitle) + val subtitlePacket = packetPlayOutTitleConstructor.newInstance(subtitleEnum, subtitleBaseComponent, fadeIn, stay, fadeOut) + sendPacket(subtitlePacket) + } + + } else { + this.sendTitle(title, subtitle, fadeIn, stay, fadeOut) + } +} + +fun Player.sendActionBarMessage(message: String) { + if (Reflection.is1_8) { + val actionBarBaseComponent = createIChatBaseComponent(message) + val actionBarPacket: Any = packetPlayOutChatConstructor.newInstance(actionBarBaseComponent) + + sendPacket(actionBarPacket) + } else { + this.sendActionBar(message.color()) + } +} + +private fun Player.sendPacket(packet: Any) { + val entityPlayer = entityPlayerClass.cast(getHandleMethod.invoke(this)) + val playerConnection = playerConnectionField.get(entityPlayer) + + sendPacketMethod.invoke(playerConnection, packet) +} + +private fun createIChatBaseComponent(text: String): Any { + return chatSerializerMethod(null, """{"text":"$text"}""") +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/base/PlayerJoinListener.kt b/src/main/kotlin/com/github/frcsty/listener/base/PlayerJoinListener.kt similarity index 75% rename from src/main/kotlin/com/github/frcsty/frozenjoin/listener/base/PlayerJoinListener.kt rename to src/main/kotlin/com/github/frcsty/listener/base/PlayerJoinListener.kt index d802b22..0c55b64 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/base/PlayerJoinListener.kt +++ b/src/main/kotlin/com/github/frcsty/listener/base/PlayerJoinListener.kt @@ -1,8 +1,8 @@ -package com.github.frcsty.frozenjoin.listener.base +package com.github.frcsty.listener.base -import com.github.frcsty.frozenjoin.listener.event.FrozenJoinEvent -import com.github.frcsty.frozenjoin.load.Loader -import com.github.frcsty.frozenjoin.message.MessageFormatter +import com.github.frcsty.listener.event.FrozenJoinEvent +import com.github.frcsty.load.Loader +import com.github.frcsty.message.MessageFormatter import org.bukkit.Bukkit import org.bukkit.event.EventHandler import org.bukkit.event.Listener @@ -23,6 +23,10 @@ class PlayerJoinListener(private val loader: Loader) : Listener { val manager = loader.formatManager val player = event.player + if (loader.positionStorage.positions[player.uniqueId] == null) { + loader.positionStorage.positions[player.uniqueId] = (loader.positionStorage.positions.size + 1).toLong() + } + if (!player.hasPlayedBefore()) { val motdObject = manager.motdsMap[FIRST_JOIN] ?: return val actions = motdObject.message diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/base/PlayerQuitListener.kt b/src/main/kotlin/com/github/frcsty/listener/base/PlayerQuitListener.kt similarity index 68% rename from src/main/kotlin/com/github/frcsty/frozenjoin/listener/base/PlayerQuitListener.kt rename to src/main/kotlin/com/github/frcsty/listener/base/PlayerQuitListener.kt index b6fa9f2..fa5df14 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/base/PlayerQuitListener.kt +++ b/src/main/kotlin/com/github/frcsty/listener/base/PlayerQuitListener.kt @@ -1,14 +1,14 @@ -package com.github.frcsty.frozenjoin.listener.base +package com.github.frcsty.listener.base -import com.github.frcsty.frozenjoin.listener.event.FrozenQuitEvent -import com.github.frcsty.frozenjoin.load.Loader -import com.github.frcsty.frozenjoin.message.MessageFormatter +import com.github.frcsty.listener.event.FrozenQuitEvent +import com.github.frcsty.load.Loader +import com.github.frcsty.message.MessageFormatter import org.bukkit.Bukkit import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerQuitEvent -class PlayerQuitListener(private val loader: Loader) : Listener { +class PlayerQuitListener(private val loader:Loader) : Listener { companion object { private const val ACTION = "quit" diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/event/FrozenJoinEvent.kt b/src/main/kotlin/com/github/frcsty/listener/event/FrozenJoinEvent.kt similarity index 90% rename from src/main/kotlin/com/github/frcsty/frozenjoin/listener/event/FrozenJoinEvent.kt rename to src/main/kotlin/com/github/frcsty/listener/event/FrozenJoinEvent.kt index 5ce2a1b..62f201f 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/event/FrozenJoinEvent.kt +++ b/src/main/kotlin/com/github/frcsty/listener/event/FrozenJoinEvent.kt @@ -1,4 +1,4 @@ -package com.github.frcsty.frozenjoin.listener.event +package com.github.frcsty.listener.event import org.bukkit.entity.Player import org.bukkit.event.Event diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/event/FrozenQuitEvent.kt b/src/main/kotlin/com/github/frcsty/listener/event/FrozenQuitEvent.kt similarity index 90% rename from src/main/kotlin/com/github/frcsty/frozenjoin/listener/event/FrozenQuitEvent.kt rename to src/main/kotlin/com/github/frcsty/listener/event/FrozenQuitEvent.kt index 8bc22b2..df056cd 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/listener/event/FrozenQuitEvent.kt +++ b/src/main/kotlin/com/github/frcsty/listener/event/FrozenQuitEvent.kt @@ -1,4 +1,4 @@ -package com.github.frcsty.frozenjoin.listener.event +package com.github.frcsty.listener.event import org.bukkit.entity.Player import org.bukkit.event.Event diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/load/Loader.kt b/src/main/kotlin/com/github/frcsty/load/Loader.kt similarity index 77% rename from src/main/kotlin/com/github/frcsty/frozenjoin/load/Loader.kt rename to src/main/kotlin/com/github/frcsty/load/Loader.kt index 11b9d86..e1ce185 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/load/Loader.kt +++ b/src/main/kotlin/com/github/frcsty/load/Loader.kt @@ -1,13 +1,15 @@ -package com.github.frcsty.frozenjoin.load +package com.github.frcsty.load -import com.github.frcsty.Handler -import com.github.frcsty.frozenjoin.FrozenJoinPlugin -import com.github.frcsty.frozenjoin.`object`.FormatManager -import com.github.frcsty.frozenjoin.command.* -import com.github.frcsty.frozenjoin.configuration.MessageLoader -import com.github.frcsty.frozenjoin.listener.base.PlayerJoinListener -import com.github.frcsty.frozenjoin.listener.base.PlayerQuitListener -import com.github.frcsty.frozenjoin.util.color +import com.github.frcsty.FrozenJoinPlugin +import com.github.frcsty.`object`.FormatManager +import com.github.frcsty.command.* +import com.github.frcsty.configuration.MessageLoader +import com.github.frcsty.library.ActionHandler +import com.github.frcsty.listener.base.PlayerJoinListener +import com.github.frcsty.listener.base.PlayerQuitListener +import com.github.frcsty.placeholder.PositionPlaceholder +import com.github.frcsty.position.PositionStorage +import com.github.frcsty.util.color import me.mattstudios.mf.base.CommandManager import org.bstats.bukkit.Metrics import org.bukkit.Bukkit.getServer @@ -17,12 +19,14 @@ import java.util.logging.Level class Loader(private val plugin: FrozenJoinPlugin) { - val actionHandler = Handler() + val actionHandler = ActionHandler(plugin) val formatManager = FormatManager(plugin) + val positionStorage = PositionStorage private val messageLoader = MessageLoader(plugin) fun initialize() { plugin.saveDefaultConfig() + positionStorage.initialize(plugin) messageLoader.load() if (Settings.METRICS) { @@ -47,6 +51,7 @@ class Loader(private val plugin: FrozenJoinPlugin) { ConvertCommand(plugin, messageLoader) ) + actionHandler.loadDefault() if (Settings.VERSION >= 15) { manager.register(FormatCommand(messageLoader)) } @@ -55,6 +60,8 @@ class Loader(private val plugin: FrozenJoinPlugin) { registerMessages(manager, messageLoader) registerListeners(PlayerJoinListener(this), PlayerQuitListener(this)) + + PositionPlaceholder(this).register() } private fun registerMessages(manager: CommandManager, messages: MessageLoader) { @@ -84,5 +91,6 @@ class Loader(private val plugin: FrozenJoinPlugin) { fun terminate() { plugin.reloadConfig() + positionStorage.terminate(plugin) } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/load/Settings.kt b/src/main/kotlin/com/github/frcsty/load/Settings.kt similarity index 83% rename from src/main/kotlin/com/github/frcsty/frozenjoin/load/Settings.kt rename to src/main/kotlin/com/github/frcsty/load/Settings.kt index 03c0718..2abcc45 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/load/Settings.kt +++ b/src/main/kotlin/com/github/frcsty/load/Settings.kt @@ -1,7 +1,6 @@ -package com.github.frcsty.frozenjoin.load +package com.github.frcsty.load -import com.github.frcsty.frozenjoin.FrozenJoinPlugin -import net.luckperms.api.LuckPerms +import com.github.frcsty.FrozenJoinPlugin import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin import java.util.logging.Level @@ -14,7 +13,7 @@ object Settings { val LOGGER = plugin.logger val DEBUG: Boolean = config.getString("consoleMessages", "ENABLED").equals("ENABLED", ignoreCase = true) val METRICS: Boolean = config.getBoolean("stonks", true) - val FULL_VERSION = server.javaClass.`package`.name + private val FULL_VERSION = server.javaClass.`package`.name val VERSION: Int = Integer.valueOf(FULL_VERSION.split("_")[1]) val HEX_USE: Boolean = VERSION == 16 val PLUGIN_VERSION = plugin.description.version diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/message/MessageFormatter.kt b/src/main/kotlin/com/github/frcsty/message/MessageFormatter.kt similarity index 85% rename from src/main/kotlin/com/github/frcsty/frozenjoin/message/MessageFormatter.kt rename to src/main/kotlin/com/github/frcsty/message/MessageFormatter.kt index bd0fff4..01e4bae 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/message/MessageFormatter.kt +++ b/src/main/kotlin/com/github/frcsty/message/MessageFormatter.kt @@ -1,12 +1,12 @@ -package com.github.frcsty.frozenjoin.message - -import com.github.frcsty.Handler -import com.github.frcsty.frozenjoin.`object`.FormatManager -import com.github.frcsty.frozenjoin.load.Settings -import com.github.frcsty.frozenjoin.load.logInfo -import com.github.frcsty.frozenjoin.util.getCustomMessage -import com.github.frcsty.frozenjoin.util.luckPermsCheck -import com.github.frcsty.frozenjoin.util.sendTranslatedMessage +package com.github.frcsty.message + +import com.github.frcsty.`object`.FormatManager +import com.github.frcsty.library.ActionHandler +import com.github.frcsty.load.Settings +import com.github.frcsty.load.logInfo +import com.github.frcsty.util.getCustomMessage +import com.github.frcsty.util.luckPermsCheck +import com.github.frcsty.util.sendTranslatedMessage import net.luckperms.api.LuckPerms import org.bukkit.Bukkit import org.bukkit.entity.Player @@ -17,7 +17,7 @@ object MessageFormatter { private val provider = getProvider() - fun executeMotd(player: Player, manager: FormatManager, actionHandler: Handler, command: Boolean, message: String) { + fun executeMotd(player: Player, manager: FormatManager, actionHandler: ActionHandler, command: Boolean, message: String) { //This is not the most readable thing in the world, but I'm kind of scared to change anything since there's no test suite val motds = manager.motdsMap.filter { (key, value) -> !("firstJoin".equals(key, true)) && player.hasEffectivePermission(value.permission, provider) @@ -40,11 +40,11 @@ object MessageFormatter { if (Settings.DEBUG) logInfo("Executing '${motdObject.identifier}' motd for user ${player.name} (${player.uniqueId})") } - fun executeFormat(player: Player, manager: FormatManager, actionHandler: Handler, action: String): List { + fun executeFormat(player: Player, manager: FormatManager, actionHandler: ActionHandler, action: String): List { if (Settings.VERSION >= 15) { val customMessage = player.getCustomMessage(action) if (customMessage != "emptyValue") { - actionHandler.execute(player, "[BROADCAST] $customMessage") + actionHandler.execute(player, listOf("[BROADCAST] $customMessage")) if (Settings.DEBUG) logInfo("Executing custom message for user ${player.name} (${player.uniqueId})") return listOf(customMessage) } @@ -75,7 +75,7 @@ object MessageFormatter { actionHandler.execute(player, actions) } "RANDOM" -> { - actionHandler.execute(player, actions.random()) + actionHandler.execute(player, listOf(actions.random())) } "VANISH" -> { val inverted = formatObject.isInverted diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/object/Format.kt b/src/main/kotlin/com/github/frcsty/object/Format.kt similarity index 86% rename from src/main/kotlin/com/github/frcsty/frozenjoin/object/Format.kt rename to src/main/kotlin/com/github/frcsty/object/Format.kt index 8390dee..f0a9b15 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/object/Format.kt +++ b/src/main/kotlin/com/github/frcsty/object/Format.kt @@ -1,4 +1,4 @@ -package com.github.frcsty.frozenjoin.`object` +package com.github.frcsty.`object` class Format( val identifier: String = "", diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/object/FormatManager.kt b/src/main/kotlin/com/github/frcsty/object/FormatManager.kt similarity index 92% rename from src/main/kotlin/com/github/frcsty/frozenjoin/object/FormatManager.kt rename to src/main/kotlin/com/github/frcsty/object/FormatManager.kt index bf88b27..097b05e 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/object/FormatManager.kt +++ b/src/main/kotlin/com/github/frcsty/object/FormatManager.kt @@ -1,7 +1,7 @@ -package com.github.frcsty.frozenjoin.`object` +package com.github.frcsty.`object` -import com.github.frcsty.frozenjoin.FrozenJoinPlugin -import com.github.frcsty.frozenjoin.load.logError +import com.github.frcsty.FrozenJoinPlugin +import com.github.frcsty.load.logError class FormatManager(private val plugin: FrozenJoinPlugin) { diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/object/MOTD.kt b/src/main/kotlin/com/github/frcsty/object/MOTD.kt similarity index 78% rename from src/main/kotlin/com/github/frcsty/frozenjoin/object/MOTD.kt rename to src/main/kotlin/com/github/frcsty/object/MOTD.kt index 3c9bad9..c26bd0a 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/object/MOTD.kt +++ b/src/main/kotlin/com/github/frcsty/object/MOTD.kt @@ -1,4 +1,4 @@ -package com.github.frcsty.frozenjoin.`object` +package com.github.frcsty.`object` class MOTD( val identifier: String = "", diff --git a/src/main/kotlin/com/github/frcsty/placeholder/PositionPlaceholder.kt b/src/main/kotlin/com/github/frcsty/placeholder/PositionPlaceholder.kt new file mode 100644 index 0000000..b0741d8 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/placeholder/PositionPlaceholder.kt @@ -0,0 +1,43 @@ +package com.github.frcsty.placeholder + +import com.github.frcsty.load.Loader +import me.clip.placeholderapi.expansion.PlaceholderExpansion +import org.bukkit.entity.Player +import java.text.DecimalFormat + +class PositionPlaceholder(private val loader: Loader) : PlaceholderExpansion() { + + companion object { + private val FORMAT = DecimalFormat("###,##") + } + + override fun getIdentifier(): String { + return "frozenjoin" + } + + override fun getVersion(): String { + return "2.1.2" + } + + override fun getAuthor(): String { + return "Frcsty" + } + + override fun canRegister(): Boolean { + return true + } + + override fun persist(): Boolean { + return true + } + + override fun onPlaceholderRequest(player: Player, params: String): String? { + when (params) { + "player-position" -> { + return FORMAT.format(loader.positionStorage.positions[player.uniqueId]) + } + } + + return null + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/position/PositionStorage.kt b/src/main/kotlin/com/github/frcsty/position/PositionStorage.kt new file mode 100644 index 0000000..74c6989 --- /dev/null +++ b/src/main/kotlin/com/github/frcsty/position/PositionStorage.kt @@ -0,0 +1,51 @@ +package com.github.frcsty.position + +import com.github.frcsty.FrozenJoinPlugin +import org.bukkit.configuration.file.YamlConfiguration +import java.io.File +import java.util.* + +object PositionStorage { + + val positions: SortedMap = sortedMapOf() + + fun initialize(plugin: FrozenJoinPlugin) { + val file = File("${plugin.dataFolder}/positions.yml") + + if (file.exists().not()) { + file.createNewFile() + return + } + + val fileConfiguration = YamlConfiguration.loadConfiguration(file) + val section = fileConfiguration.getConfigurationSection("positions")?: return + + for (position in section.getKeys(false)) { + positions[UUID.fromString(position)] = section.getLong(position) + } + + fileConfiguration.save(file) + } + + fun terminate(plugin: FrozenJoinPlugin) { + val file = File("${plugin.dataFolder}/positions.yml") + + if (file.exists().not()) { + return + } + + val fileConfiguration = YamlConfiguration.loadConfiguration(file) + + if (positions.isEmpty()) { + return + } + + for (position in positions.keys) { + fileConfiguration.set("positions.$position", positions[position]) + } + + fileConfiguration.save(file) + positions.clear() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/util/Extensions.kt b/src/main/kotlin/com/github/frcsty/util/Extensions.kt similarity index 95% rename from src/main/kotlin/com/github/frcsty/frozenjoin/util/Extensions.kt rename to src/main/kotlin/com/github/frcsty/util/Extensions.kt index dc22d71..561cd21 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/util/Extensions.kt +++ b/src/main/kotlin/com/github/frcsty/util/Extensions.kt @@ -1,6 +1,6 @@ -package com.github.frcsty.frozenjoin.util +package com.github.frcsty.util -import com.github.frcsty.frozenjoin.load.Settings +import com.github.frcsty.load.Settings import me.clip.placeholderapi.PlaceholderAPI import net.md_5.bungee.api.ChatColor import net.md_5.bungee.api.chat.TextComponent diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/util/Permission.kt b/src/main/kotlin/com/github/frcsty/util/Permission.kt similarity index 89% rename from src/main/kotlin/com/github/frcsty/frozenjoin/util/Permission.kt rename to src/main/kotlin/com/github/frcsty/util/Permission.kt index 296fad6..8969d5c 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/util/Permission.kt +++ b/src/main/kotlin/com/github/frcsty/util/Permission.kt @@ -1,6 +1,6 @@ -package com.github.frcsty.frozenjoin.util +package com.github.frcsty.util -import com.github.frcsty.frozenjoin.load.Settings +import com.github.frcsty.load.Settings import net.luckperms.api.LuckPerms import net.luckperms.api.model.user.User import org.bukkit.entity.Player diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/util/PersistantData.kt b/src/main/kotlin/com/github/frcsty/util/PersistantData.kt similarity index 89% rename from src/main/kotlin/com/github/frcsty/frozenjoin/util/PersistantData.kt rename to src/main/kotlin/com/github/frcsty/util/PersistantData.kt index 61cca19..5f2e08d 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/util/PersistantData.kt +++ b/src/main/kotlin/com/github/frcsty/util/PersistantData.kt @@ -1,6 +1,6 @@ -package com.github.frcsty.frozenjoin.util +package com.github.frcsty.util -import com.github.frcsty.frozenjoin.FrozenJoinPlugin +import com.github.frcsty.FrozenJoinPlugin import org.bukkit.NamespacedKey import org.bukkit.entity.Player import org.bukkit.persistence.PersistentDataType diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2ce8c41..b02c24b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -108,6 +108,7 @@ settings: # firstJoinMessage: - '[MESSAGE] &8[&aWelcome&8] &7Please welcome &f%player_name% &7to the server!' + - '[BROADCAST] &8[&bWelcome&8] &f%player_name% &8(&7%frozenjoin_player-position%&8)' # # # » Message of The Day @@ -196,6 +197,7 @@ formats: priority: 3 type: 'NORMAL' join: + - '[BROADCAST] &8[&a+&8] &7Everyone welcome &f%player_name% &8(&7%frozenjoin_player-position%&8)&7!' - '[BROADCAST] &8[&a+&8] &b%player_name%' - '[CENTERMESSAGE] #<0cf797>Smexy' - '[DELAY=5s][MESSAGE] &aYou had a message sent to you, with a delay of &f5 seconds&a.' diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8a3a4d8..0738a9b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: FrozenJoin version: @version@ -main: com.github.frcsty.frozenjoin.FrozenJoinPlugin +main: com.github.frcsty.FrozenJoinPlugin author: Frcsty api-version: 1.13 softdepend: ["PlaceholderAPI", "LuckPerms"] diff --git a/src/main/resources/positions.yml b/src/main/resources/positions.yml new file mode 100644 index 0000000..44a67b1 --- /dev/null +++ b/src/main/resources/positions.yml @@ -0,0 +1,2 @@ +# Please for the love of god do not corrupt all data in here by manually editing it during runtime. Much Thanks. +positions: \ No newline at end of file diff --git a/src/test/java/Test.kt b/src/test/java/Test.kt deleted file mode 100644 index 645ca9a..0000000 --- a/src/test/java/Test.kt +++ /dev/null @@ -1,22 +0,0 @@ -import org.junit.jupiter.api.Test - -class Test { - - init { - testRegex() - } - - @Test - fun testRegex() { - val messages = listOf( - "[BROADCAST] &8[&a+&8] &b%player_name%", - "[CENTERMESSAGE] #<0cf797>Smexy", - "[DELAY=5s][MESSAGE] &aYou had a message sent to you, with a delay of &f5 seconds&a.", - "[CHANCE=30][DELAY=5s][MESSAGE] &aYou had a &f30% &achance of getting this message, with a &f3s &adelay!" - ) - - for (message in messages) { - - } - } -} \ No newline at end of file