Skip to content

Commit

Permalink
0.2.3 - Fabric 1.20.1, i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed Jun 22, 2023
1 parent be997f1 commit 8861826
Show file tree
Hide file tree
Showing 39 changed files with 536 additions and 132 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/ya-ilya/progreso?color=royalblue)
![Build Status](https://img.shields.io/github/actions/workflow/status/ya-ilya/progreso/build.yml?branch=main&logo=gradle)
![Fabric Version](https://img.shields.io/badge/Fabric-1.20-informational)
![Fabric Version](https://img.shields.io/badge/Fabric-1.20.1-informational)

</div>

Expand All @@ -16,10 +16,10 @@ This utility mod not ready to use. So for now it's just an experiment

### Installation

- Install [fabric 1.20](https://fabricmc.net/use/installer/)
- Install [fabric 1.20.1](https://fabricmc.net/use/installer/)
- Download the latest release from [releases](https://github.com/ya-ilya/progreso/releases) or dev build from [actions](https://github.com/ya-ilya/progreso/actions)
- Put the jar to `.minecraft/mods` folder
- Run [fabric 1.20]() from your launcher
- Run [fabric 1.20.1]() from your launcher

### Development

Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ kotlin.code.style=official
kotlinVersion=1.8.21

# progreso
progresoVersion=0.2.2
progresoVersion=0.2.3

# fabric
minecraftVersion=1.20
fabricVersion=0.83.0+1.20
minecraftVersion=1.20.1
fabricVersion=0.84.0+1.20.1
fabricLoomVersion=1.2.7
fabricKotlinVersion=1.9.4+kotlin.1.8.21
yarnMappings=1.20+build.1
yarnMappings=1.20.1+build.5
loaderVersion=0.14.21
17 changes: 16 additions & 1 deletion progreso-api/src/main/kotlin/org/progreso/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.progreso.api.accessor.ChatAccessor
import org.progreso.api.accessor.EventAccessor
import org.progreso.api.accessor.LoggerAccessor
import org.progreso.api.event.EventBus
import org.progreso.api.i18n.I18n
import org.progreso.api.managers.*
import kotlin.properties.Delegates

Expand All @@ -21,11 +22,19 @@ object Api {
.setPrettyPrinting()
.create()

fun initialize(event: EventAccessor, chat: ChatAccessor, logger: LoggerAccessor) {
fun initialize(
event: EventAccessor,
chat: ChatAccessor,
logger: LoggerAccessor,
configuration: Configuration = Configuration.EMPTY
) {
if (initialized) {
throw RuntimeException("Api already initialized")
}

logger.info("Setting internationalization...")
I18n.initialize(configuration.locales, configuration.locale)

logger.info("Setting accessors...")
EVENT = event
CHAT = chat
Expand Down Expand Up @@ -58,4 +67,10 @@ object Api {

initialized = true
}

data class Configuration(val locales: List<I18n.Locale>, val locale: String) {
companion object {
val EMPTY = Configuration(emptyList(), "unknown")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.progreso.api.accessor

import org.progreso.api.i18n.I18n.i18n

/**
* Provides access to the minecraft client chat
* ```java
Expand Down Expand Up @@ -39,21 +41,49 @@ interface ChatAccessor {
*/
fun send(message: Any)

/**
* Send localized message to the chat
*/
fun sendLocalized(key: String, vararg replacements: Pair<String, Any>) {
send(i18n(key, *replacements))
}

/**
* Sends info message to the chat
*/
fun info(message: Any)

/**
* Send localized info message to the chat
*/
fun infoLocalized(key: String, vararg replacements: Pair<String, Any>) {
info(i18n(key, *replacements))
}

/**
* Sends warn message to the chat
*/
fun warn(message: Any)

/**
* Send localized warn message to the chat
*/
fun warnLocalized(key: String, vararg replacements: Pair<String, Any>) {
warn(i18n(key, *replacements))
}

/**
* Sends error message to the chat
*/
fun error(message: Any)

/**
* Send localized error message to the chat
*/
fun errorLocalized(key: String, vararg replacements: Pair<String, Any>) {
error(i18n(key, *replacements))
}

/**
* Add message to sent messages
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@ abstract class AbstractCommand(
) : LiteralBuilder(name) {
protected companion object {
fun send(message: Any) = Api.CHAT.send(message)

fun sendLocalized(key: String, vararg replacements: Pair<String, Any>) =
Api.CHAT.sendLocalized(key, *replacements)

fun info(message: Any) = Api.CHAT.info(message)

fun infoLocalized(key: String, vararg replacements: Pair<String, Any>) =
Api.CHAT.infoLocalized(key, *replacements)

fun warn(message: Any) = Api.CHAT.warn(message)

fun warnLocalized(key: String, vararg replacements: Pair<String, Any>) =
Api.CHAT.warnLocalized(key, *replacements)

fun error(message: Any) = Api.CHAT.error(message)

fun errorLocalized(key: String, vararg replacements: Pair<String, Any>) =
Api.CHAT.errorLocalized(key, *replacements)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class ConfigHelperArgumentType : ArgumentType<AbstractConfigHelper<*>?> {
override fun parse(reader: StringReader): AbstractConfigHelper<*>? {
val helperName = reader.readString()
val helper = ConfigManager.getHelperByNameOrNull(helperName)
if (helper == null) Api.CHAT.error("Helper $helperName not found")
if (helper == null) Api.CHAT.errorLocalized(
"argument.config_helper.error",
"helper" to helperName
)
return helper
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class FriendArgumentType : ArgumentType<FriendManager.Friend?> {
override fun parse(reader: StringReader): FriendManager.Friend? {
val friendName = reader.readString()
val friend = FriendManager.getFriendByNameOrNull(friendName)
if (friend == null) Api.CHAT.error("Friend $friendName not found")
if (friend == null) Api.CHAT.errorLocalized(
"argument.friend.error",
"friend" to friendName
)
return friend
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.progreso.api.command.argument.arguments

import org.progreso.api.Api
import org.progreso.api.command.argument.ArgumentType
import org.progreso.api.command.reader.StringReader
import org.progreso.api.i18n.I18n

class LocaleArgumentType : ArgumentType<I18n.Locale?> {
companion object {
fun create() = LocaleArgumentType()
}

override val name = "locale"

override fun parse(reader: StringReader): I18n.Locale? {
val localeName = reader.readString()
val locale = I18n.getLocaleByNameOrNull(localeName)
if (locale == null) {
Api.CHAT.errorLocalized(
"argument.locale.error",
"locale" to localeName
)
}
return locale
}

override fun check(reader: StringReader): Boolean {
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class ModuleArgumentType : ArgumentType<AbstractModule?> {
override fun parse(reader: StringReader): AbstractModule? {
val moduleName = reader.readString()
val module = ModuleManager.getModuleByNameOrNull(moduleName)
if (module == null) Api.CHAT.error("Module $moduleName not found")
if (module == null) Api.CHAT.errorLocalized(
"argument.module.error",
"module" to moduleName
)
return module
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class PluginArgumentType : ArgumentType<AbstractPlugin?> {
override fun parse(reader: StringReader): AbstractPlugin? {
val pluginName = reader.readString()
val plugin = PluginManager.getPluginByNameOrNull(pluginName)
if (plugin == null) Api.CHAT.error("Plugin $pluginName not found")
if (plugin == null) Api.CHAT.errorLocalized(
"argument.plugin.error",
"plugin" to pluginName
)
return plugin
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ class CommandDispatcher {

private fun predict(builder: AbstractBuilder<*>, reader: StringReader): LiteralBuilder? {
if (!reader.hasNext()) {
return builder as LiteralBuilder?
return try {
builder as LiteralBuilder?
} catch (ex: ClassCastException) {
null
}
}

val string = reader.peek()
Expand Down
48 changes: 48 additions & 0 deletions progreso-api/src/main/kotlin/org/progreso/api/i18n/I18n.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.progreso.api.i18n

import org.progreso.api.Api.GSON
import org.progreso.api.i18n.exceptions.I18nException
import java.io.InputStream

object I18n {
lateinit var locales: List<Locale>
lateinit var locale: Locale

fun initialize(locales: List<Locale>, locale: String) {
this.locales = locales
this.locale = locales.first { it.name == locale }
}

fun getLocaleByNameOrNull(name: String): Locale? {
return locales.firstOrNull { it.name == name }
}

@JvmStatic
fun i18n(key: String, vararg replacements: Pair<String, Any>): String {
val value = locale[key]
?: throw I18nException("Internationalization of $key not found in ${locale.name} dictionary")

return replace(value, *replacements)
}

private fun replace(value: String, vararg replacements: Pair<String, Any>): String {
var result = value
replacements.forEach { result = result.replace("{{${it.first}}}", it.second.toString()) }
return result
}

data class Locale(
val name: String,
private val dictionary: Map<String, String>
) {
companion object {
fun fromStream(name: String, stream: InputStream): Locale {
return Locale(name, GSON.fromJson<Map<String, String>>(stream.reader(), Map::class.java))
}
}

operator fun get(key: String): String? {
return dictionary[key]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.progreso.api.i18n.exceptions

class I18nException(message: String) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ object CommandManager : CommandContainer {
if (message.startsWith(PREFIX)) {
try {
if (!DISPATCHER.dispatch(message.removePrefix(PREFIX.toString()))) {
Api.CHAT.error("Command not found")
Api.CHAT.errorLocalized("command.command_not_found")
}
} catch (ex: SyntaxException) {
Api.CHAT.error("Invalid syntax. ${ex.message}")
Api.CHAT.errorLocalized("command.invalid_syntax", "message" to ex.message!!)
ex.printStackTrace()
} catch (ex: Exception) {
Api.CHAT.error("Failed to execute command")
Api.CHAT.errorLocalized("command.failed_to_execute")
ex.printStackTrace()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import org.progreso.api.i18n.I18n;
import org.progreso.api.managers.AltManager;
import org.progreso.api.managers.PluginManager;
import org.progreso.client.gui.minecraft.ProgresoAltsScreen;
import org.progreso.client.gui.minecraft.ProgresoPluginsScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@SuppressWarnings("unchecked")
@Mixin(TitleScreen.class)
public abstract class MixinTitleScreen extends Screen {
protected MixinTitleScreen(Text title) {
Expand All @@ -23,14 +27,25 @@ protected MixinTitleScreen(Text title) {
)
public void initHook(CallbackInfo callbackInfo) {
addDrawableChild(
ButtonWidget.builder(Text.of("Alts"), b -> showAltsScreen())
ButtonWidget.builder(Text.of(I18n.i18n("gui.alts.title")), b -> showAltsScreen())
.dimensions(5, 5, 70, 20)
.build()
);

addDrawableChild(
ButtonWidget.builder(Text.of(I18n.i18n("gui.plugins.title")), b -> showPluginsScreen())
.dimensions(5, 28, 70, 20)
.build()
);
}

private void showAltsScreen() {
if (client == null) return;
client.setScreen(new ProgresoAltsScreen(AltManager.INSTANCE.getAlts()));
}

private void showPluginsScreen() {
if (client == null) return;
client.setScreen(new ProgresoPluginsScreen(PluginManager.INSTANCE.getPlugins()));
}
}
Loading

0 comments on commit 8861826

Please sign in to comment.