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

I18n Translation implementation #33

Merged
merged 7 commits into from
Jan 30, 2024
Merged
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
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import de.chojo.Repo
import io.papermc.hangarpublishplugin.model.Platforms
import net.minecrell.pluginyml.bukkit.BukkitPluginDescription
import net.minecrell.pluginyml.paper.PaperPluginDescription
import xyz.jpenilla.runpaper.task.RunServer

plugins {
kotlin("jvm") version "1.9.22"
Expand Down Expand Up @@ -47,6 +48,13 @@ tasks {
runServer {
minecraftVersion("1.20.4")
}
register<RunServer>("runServer2") {
group = "run paper"
minecraftVersion("1.20.4")
runDirectory.set(File("run-2"))
pluginJars(*rootProject.getTasksByName("shadowJar", false).map { (it as Jar).archiveFile }
.toTypedArray())
}
shadowJar {
relocate("org.bstats", "net.onelitefeather.clipboardconnect.org.bstats")
}
Expand Down
8 changes: 8 additions & 0 deletions docker/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'
services:
keydb:
image: eqalpha/keydb
container_name: clipboard-connect-keydb
restart: unless-stopped
ports:
- "127.0.0.1:6379:6379"
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,34 @@ import dev.derklaro.aerogel.util.Qualifiers
import dev.derklaro.aerogel.util.Scopes
import jakarta.inject.Inject
import jakarta.inject.Singleton
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.translation.GlobalTranslator
import net.kyori.adventure.translation.TranslationRegistry
import net.kyori.adventure.util.UTF8ResourceBundleControl
import net.onelitefeather.clipboardconnect.command.ClipboardPlayer
import net.onelitefeather.clipboardconnect.command.ClipboardSender
import net.onelitefeather.clipboardconnect.commands.LoadCommand
import net.onelitefeather.clipboardconnect.commands.SaveCommand
import net.onelitefeather.clipboardconnect.commands.SetupCommand
import net.onelitefeather.clipboardconnect.conversation.ConversationContext
import net.onelitefeather.clipboardconnect.listener.PlayerJoinListener
import net.onelitefeather.clipboardconnect.listener.PlayerQuitListener
import net.onelitefeather.clipboardconnect.listener.SetupListener
import net.onelitefeather.clipboardconnect.services.SetupService
import net.onelitefeather.clipboardconnect.translations.PluginTranslationRegistry
import net.onelitefeather.clipboardconnect.utils.RawTypeMatcher
import org.bstats.bukkit.Metrics
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import java.io.IOException
import java.net.URLClassLoader
import java.nio.file.Files
import java.util.*
import kotlin.io.path.Path

/**
Expand Down Expand Up @@ -82,6 +91,52 @@ class ClipboardConnect : JavaPlugin() {
))
}

@Inject
private fun registerTranslations() {
val translationRegistry: TranslationRegistry = PluginTranslationRegistry(
TranslationRegistry.create(
Key.key(
"clipboardconnect",
"translations"
)
)
)
translationRegistry.defaultLocale(Locale.US)
val langFolder = dataFolder.toPath().resolve("lang")
if (Files.exists(langFolder)) {
try {
URLClassLoader(arrayOf(langFolder.toUri().toURL())).use { urlClassLoader ->
config.getStringList("translations").stream().map { languageTag: String ->
Locale.forLanguageTag(
languageTag
)
}.forEach { r: Locale ->
val bundle = ResourceBundle.getBundle(
"clipboardconnect",
r,
urlClassLoader,
UTF8ResourceBundleControl.get()
)
translationRegistry.registerAll(r, bundle, false)
}
}
} catch (e: IOException) {
throw RuntimeException(e)
}
} else {
config.getStringList("translations").stream().map { languageTag: String ->
Locale.forLanguageTag(
languageTag
)
}.forEach { r: Locale ->
val bundle =
ResourceBundle.getBundle("clipboardconnect", r, UTF8ResourceBundleControl.get())
translationRegistry.registerAll(r, bundle, false)
}
}
GlobalTranslator.translator().addSource(translationRegistry)
}

@Inject
private fun bsStats() {
Metrics(this, 20460)
Expand Down Expand Up @@ -138,6 +193,7 @@ class ClipboardConnect : JavaPlugin() {
val redisFile = Path(dataFolder.toString(), "redis.yml")
if (Files.exists(redisFile)) {
server.pluginManager.registerEvents(injector.instance(PlayerQuitListener::class.java), this)
server.pluginManager.registerEvents(injector.instance(PlayerJoinListener::class.java), this)
injector.instance(AnnotationParser::class.java).parse(injector.instance(SaveCommand::class.java))
injector.instance(AnnotationParser::class.java).parse(injector.instance(LoadCommand::class.java))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,9 @@ class LoadCommand @Inject constructor(private val syncService: SyncService, @Nam
fun execute(clipboardPlayer: ClipboardPlayer) {
val run = Runnable {
if (syncService.syncPull(clipboardPlayer.getWorldEditPlayer())) {
clipboardPlayer.sendMessage(
MiniMessage.miniMessage().deserialize(
"<prefix><green>Clipboard was successfully loaded",
Placeholder.component("prefix", prefix)
)
)
clipboardPlayer.sendMessage(Component.translatable("command.clipboard.successfully.loaded").arguments(prefix))
} else {
clipboardPlayer.sendMessage(
MiniMessage.miniMessage().deserialize(
"<prefix><red>Clipboard has some issues to load",
Placeholder.component("prefix", prefix)
)
)
clipboardPlayer.sendMessage(Component.translatable("command.clipboard.failed.to.loaded").arguments(prefix))
}
}
if (faweSupport) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class SaveCommand @Inject constructor(private val syncService: SyncService, @Nam
@CommandDescription("Saves a clipboard global")
fun execute(clipboardPlayer: ClipboardPlayer) {
if (syncService.syncPush(clipboardPlayer.getWorldEditPlayer(), false)) {
clipboardPlayer.sendMessage(MiniMessage.miniMessage().deserialize("<prefix><green>Clipboard was successfully uploaded", Placeholder.component("prefix",prefix)))
clipboardPlayer.sendMessage(Component.translatable("command.clipboard.successfully.uploaded").arguments(prefix))
} else {
clipboardPlayer.sendMessage(MiniMessage.miniMessage().deserialize("<prefix><red>Clipboard has some issues to upload", Placeholder.component("prefix",prefix)))
clipboardPlayer.sendMessage(Component.translatable("command.clipboard.failed.to.uploaded").arguments(prefix))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.onelitefeather.clipboardconnect.listener

import com.sk89q.worldedit.bukkit.BukkitAdapter
import jakarta.inject.Inject
import jakarta.inject.Named
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import net.onelitefeather.clipboardconnect.ClipboardConnect
import net.onelitefeather.clipboardconnect.services.SyncService
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent

/**
* Listener class that handles the 'PlayerJoinEvent' when a player joins the server.
*
* @property syncService The SyncService instance used for syncing player data.
* @property plugin The ClipboardConnect plugin instance.
*/
class PlayerJoinListener
@Inject constructor(
private val syncService: SyncService,
private val plugin: ClipboardConnect,
@Named("prefix") private val prefix: Component,
) : Listener {

/**
* Handles the 'PlayerQuitEvent' when a player quits the server.
*
* @param event The PlayerQuitEvent object.
*/
@EventHandler(priority = EventPriority.LOWEST)
fun playerJoin(event: PlayerJoinEvent) {
val player = event.player
plugin.componentLogger.debug(
MiniMessage.miniMessage()
.deserialize("<player> is logging in", Placeholder.component("player", player.name()))
)
if (!player.hasPermission("clipboardconnect.service.load")) return
plugin.componentLogger.debug(
MiniMessage.miniMessage()
.deserialize("<player> permission check was successful", Placeholder.component("player", player.name()))
)
val worldEditPlayer = BukkitAdapter.adapt(player)
plugin.componentLogger.debug(
MiniMessage.miniMessage()
.deserialize("Try to pull clipboard for <player>", Placeholder.component("player", player.name()))
)
if (syncService.syncPull(worldEditPlayer)) {
plugin.componentLogger.debug(
MiniMessage.miniMessage().deserialize(
"<green>Clipboard from <actor> was successful written into actor",
Placeholder.unparsed("actor", worldEditPlayer.name)
)
)
player.sendMessage(
MiniMessage.miniMessage().deserialize(
"<prefix><green>Clipboard <green>was successfully transfered to this server",
Placeholder.component("prefix", prefix)
)
)
}
}

}
Loading
Loading