Skip to content

Commit

Permalink
Code improvement, added a few comments, renamed some classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed Mar 23, 2024
1 parent a08ac9a commit 81d4c02
Show file tree
Hide file tree
Showing 30 changed files with 185 additions and 141 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ progresoVersion=0.3

# fabric
minecraftVersion=1.20.4
fabricVersion=0.91.3+1.20.4
fabricLoomVersion=1.4-SNAPSHOT
fabricVersion=0.96.11+1.20.4
fabricLoomVersion=1.5-SNAPSHOT
yarnMappings=1.20.4+build.3
loaderVersion=0.15.2
loaderVersion=0.15.7

# other
gsonVersion=2.10.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,47 +40,73 @@ interface ChatAccessor {

/**
* Send message to the chat
*
* @param message Message
*/
fun send(message: Any)

/**
* Send localized message to the chat
*
* @param args Internationalization key
* @param args Internationalization args
* @see [TextAccessor.i18n]
*/
fun sendLocalized(key: String, vararg args: Any) {
send(Api.TEXT.i18n(key, *args))
}

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

/**
* Send localized info message to the chat
*
* @param args Internationalization key
* @param args Internationalization args
* @see [TextAccessor.i18n]
*/
fun infoLocalized(key: String, vararg args: Any) {
info(Api.TEXT.i18n(key, *args))
}

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

/**
* Send localized warn message to the chat
*
*
* @param args Internationalization key
* @param args Internationalization args
* @see [TextAccessor.i18n]
*/
fun warnLocalized(key: String, vararg args: Any) {
warn(Api.TEXT.i18n(key, *args))
}

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

/**
* Send localized error message to the chat
*
*
* @param args Internationalization key
* @param args Internationalization args
* @see [TextAccessor.i18n]
*/
fun errorLocalized(key: String, vararg args: Any) {
error(Api.TEXT.i18n(key, *args))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ interface CommandAccessor {
}
}

/**
* Create command source for [com.mojang.brigadier]
*/
fun createCommandSource(): Any

/**
* Get suggestions for [com.mojang.brigadier]
*
* @param candidates Candidates
* @param builder Builder
*/
fun suggestMatching(candidates: Iterable<String>, builder: SuggestionsBuilder): CompletableFuture<Suggestions>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.progreso.api.accessor
import com.mojang.brigadier.Message

/**
* Provides access to the minecraft text class
* Provides access to the internationalization system
* ```java
* import org.progreso.api.accessor.TextAccessor;
*
Expand All @@ -24,6 +24,7 @@ interface TextAccessor {
/**
* Get internationalization by [key]
*
* @param key Key
* @param args Arguments
* @see [String.format]
*/
Expand All @@ -32,6 +33,7 @@ interface TextAccessor {
/**
* Get internationalization by [key]
*
* @param key Key
* @param args Arguments
* @see [String.format]
*/
Expand Down
18 changes: 10 additions & 8 deletions progreso-api/src/main/kotlin/org/progreso/api/alt/AltAccount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,22 @@ sealed class AltAccount(
const val XBOX_PRE_AUTH_URL =
"https://login.live.com/oauth20_authorize.srf?client_id=<client_id>&redirect_uri=<redirect_uri>&response_type=code&display=touch&scope=<scope>&prompt=select_account"
const val XBOX_AUTH_URL = "https://login.live.com/oauth20_token.srf"
const val XBOX_XBL_URL = "https://user.auth.xboxlive.com/user/authenticate"
const val XBOX_XSTS_URL = "https://xsts.auth.xboxlive.com/xsts/authorize"
const val MC_AUTH_URL = "https://api.minecraftservices.com/authentication/login_with_xbox"
const val MC_PROFILE_URL = "https://api.minecraftservices.com/minecraft/profile"

private const val XBOX_XBL_URL = "https://user.auth.xboxlive.com/user/authenticate"
private const val XBOX_XSTS_URL = "https://xsts.auth.xboxlive.com/xsts/authorize"
private const val MC_AUTH_URL = "https://api.minecraftservices.com/authentication/login_with_xbox"
private const val MC_PROFILE_URL = "https://api.minecraftservices.com/minecraft/profile"

const val XBOX_AUTH_DATA =
"client_id=<client_id>&redirect_uri=<redirect_uri>&grant_type=authorization_code&code="
const val XBOX_REFRESH_DATA =

private const val XBOX_REFRESH_DATA =
"client_id=<client_id>&scope=<scope>&grant_type=refresh_token&redirect_uri=<redirect_uri>&refresh_token="
const val XBOX_XBL_DATA =
private const val XBOX_XBL_DATA =
"""{"Properties":{"AuthMethod":"RPS","SiteName":"user.auth.xboxlive.com","RpsTicket":"<rps_ticket>"},"RelyingParty":"http://auth.xboxlive.com","TokenType":"JWT"}"""
const val XBOX_XSTS_DATA =
private const val XBOX_XSTS_DATA =
"""{"Properties":{"SandboxId":"RETAIL","UserTokens":["<xbl_token>"]},"RelyingParty":"rp://api.minecraftservices.com/","TokenType":"JWT"}"""
const val MC_AUTH_DATA =
private const val MC_AUTH_DATA =
"""{"identityToken":"XBL3.0 x=<userhash>;<xsts_token>"}"""

fun String.replaceAuthKeys() = this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package org.progreso.api.common

/**
* Interfaces for containers
*/
interface Container
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ModuleConfigCategory(
override fun create(name: String): ModuleConfig {
return ModuleConfig(
name,
container.modules.map { ModuleConfig.ModuleConfigData.create(it) }
container.modules.map { ModuleConfig.ModuleConfigData(it) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,17 @@ class ModuleConfig(name: String, var modules: List<ModuleConfigData>) : Abstract
val name: String,
var settings: List<SettingConfigData>
) {
companion object {
fun create(module: AbstractModule): ModuleConfigData {
return ModuleConfigData(
module.name,
module.settings.map { SettingConfigData.create(it) }
)
}
}
constructor(module: AbstractModule) : this(module.name, module.settings.map { SettingConfigData(it) })
}

data class SettingConfigData(
val name: String,
var value: Any
) {
companion object {
fun create(setting: AbstractSetting<*>): SettingConfigData {
return when (setting) {
is GroupSetting -> SettingConfigData(setting.name, setting.settings.map { create(it) })
else -> SettingConfigData(setting.name, setting.value)
}
}
}
constructor(setting: AbstractSetting<*>) : this(
setting.name,
if (setting is GroupSetting) setting.settings.map { SettingConfigData(it) }
else setting.value
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.progreso.api.config.container
import org.progreso.api.common.Container
import org.progreso.api.config.AbstractConfigCategory

/**
* Interface for config category containers
*/
interface ConfigCategoryContainer : Container {
val categories: MutableSet<AbstractConfigCategory<*, *>>

Expand Down
71 changes: 42 additions & 29 deletions progreso-api/src/main/kotlin/org/progreso/api/event/EventBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType
import java.util.concurrent.ConcurrentHashMap

class EventBus {
class EventBus(
private val registerMethods: Boolean = true,
private val registerFields: Boolean = true
) {
companion object {
private val CACHE = ConcurrentHashMap<Class<*>, MutableList<ListenerInvoker>>()
}
Expand All @@ -17,43 +20,53 @@ class EventBus {
if (!CACHE.containsKey(instanceClass)) {
val listeners = mutableListOf<ListenerInvoker>()

for (method in instanceClass.declaredMethods) {
val annotation = method.getAnnotation(EventHandler::class.java)
if (registerMethods) {
for (method in instanceClass.declaredMethods) {
val annotation = method.getAnnotation(EventHandler::class.java)

if (annotation != null) {
val parameterType = method.parameterTypes[0]
if (annotation != null) {
val parameterType = method.parameterTypes[0]

if (!Event::class.java.isAssignableFrom(parameterType)) {
throw RuntimeException("Method: $method")
}
if (!Event::class.java.isAssignableFrom(parameterType)) {
throw RuntimeException("Method: $method")
}

listeners.add(ListenerInvoker(annotation.priority, parameterType, method))
listeners.add(
ListenerInvoker(
annotation.priority,
parameterType,
method
)
)
}
}
}

for (field in instanceClass.declaredFields) {
field.isAccessible = true
if (registerFields) {
for (field in instanceClass.declaredFields) {
field.isAccessible = true

val annotation = field.getAnnotation(EventHandler::class.java)
val annotation = field.getAnnotation(EventHandler::class.java)

if (annotation != null) {
val type = field.genericType as ParameterizedType
if (annotation != null) {
val type = field.genericType as ParameterizedType

if (!EventListener::class.java.isAssignableFrom(type.rawType as Class<*>)) {
throw RuntimeException("Field: $field")
}
if (!EventListener::class.java.isAssignableFrom(type.rawType as Class<*>)) {
throw RuntimeException("Field: $field")
}

listeners.add(
ListenerInvoker(
annotation.priority,
type.actualTypeArguments[0] as Class<*>,
EventListener.INVOKE_METHOD,
field.get(instance)
listeners.add(
ListenerInvoker(
annotation.priority,
type.actualTypeArguments[0] as Class<*>,
EventListener.INVOKE_METHOD,
field.get(instance)
)
)
)
}
}

field.isAccessible = false
field.isAccessible = false
}
}

sortListeners(listeners)
Expand All @@ -68,11 +81,11 @@ class EventBus {
}
}

fun registerListener(
fun <T : Event> registerListener(
instanceClass: Class<*>,
eventClass: Class<*>,
eventClass: Class<T>,
priority: EventPriority,
listener: EventListener<*>
listener: EventListener<T>
) {
val listenerInvoker = ListenerInvoker(
priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class MixinMinecraftClient {
cancellable = true
)
public void setScreenHook(Screen screen, CallbackInfo callbackInfo) {
if (Client.EVENT_BUS.post(new ScreenEvent.Open(screen))) {
if (Client.EVENT_BUS.post(new ScreenEvent.Set(screen))) {
callbackInfo.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.util.concurrent.CompletableFuture

object CommandAccessor : CommandAccessor {
override fun createCommandSource(): Any {
return ClientCommandSource(null, mc.client)
return ClientCommandSource(mc.networkHandler, mc.client)
}

override fun suggestMatching(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import org.progreso.api.managers.AltManager
object AltCommand : AbstractCommand() {
init {
builder.then(
literal("add")
.then(
literal("offline").then(
argument("name", string()).execute { context ->
val name = StringArgumentType.getString(context, "name")
literal("add").then(
literal("offline").then(
argument("name", string()).execute { context ->
val name = StringArgumentType.getString(context, "name")

AltManager.addAlt(AltAccount.Offline(name))
infoLocalized("command.alt.add", name)
}
)
AltManager.addAlt(AltAccount.Offline(name))
infoLocalized("command.alt.add", name)
}
)
)
)

builder.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import org.progreso.client.Client.Companion.config
import org.progreso.client.commands.arguments.FontArgumentType
import org.progreso.client.gui.createDefaultTextRenderer
import org.progreso.client.gui.customTextRenderer
import org.progreso.client.managers.minecraft.ProgresoResourceManager
import org.progreso.client.managers.minecraft.exceptions.ProgresoResourceManagerException
import org.progreso.client.managers.ProgresoResourceManager
import org.progreso.client.util.render.createTextRendererFromProgresoResource
import java.io.FileNotFoundException

@AbstractCommand.Register("font")
object FontCommand : AbstractCommand() {
Expand Down Expand Up @@ -71,7 +71,7 @@ object FontCommand : AbstractCommand() {
"command.font.load",
fontName
)
} catch (ex: ProgresoResourceManagerException) {
} catch (ex: FileNotFoundException) {
errorLocalized(
"argument.font.error",
fontName
Expand Down
Loading

0 comments on commit 81d4c02

Please sign in to comment.