Skip to content

Commit

Permalink
configs, chat commands. TODO: rework keybinding system.
Browse files Browse the repository at this point in the history
  • Loading branch information
twenty48lol committed Dec 13, 2024
1 parent 2202f83 commit eb13024
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.blend.mixin.client.gui.screen;

import dev.blend.event.impl.ChatSendEvent;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ChatScreen;
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;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ChatScreen.class)
public class ChatScreenMixin {

@Inject(
method = "keyPressed",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/screen/ChatScreen;sendMessage(Ljava/lang/String;Z)V"
)
)
private void setScreenToNullBeforeTriggeringEventSoICanToggleClickGUIAndNotHaveItClosedByThis(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
MinecraftClient.getInstance().setScreen(null);
}

@Inject(
method = "sendMessage",
at = @At(
value = "HEAD"
),
cancellable = true
)
private void onChatSend(String chatText, boolean addToHistory, CallbackInfo ci) {
final ChatSendEvent event = new ChatSendEvent(chatText);
event.call();
if (event.isCancelled()) {
ci.cancel();
}
}

}
3 changes: 3 additions & 0 deletions src/main/kotlin/dev/blend/Client.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dev.blend

import dev.blend.command.api.CommandManager
import dev.blend.config.ConfigManager
import dev.blend.handler.api.HandlerManager
import dev.blend.module.api.ModuleManager
import org.slf4j.LoggerFactory

object Client {

val name = "Blend"
val version = "5.0.0"
val logger = LoggerFactory.getLogger(name)

fun initialize() {
Expand All @@ -16,6 +18,7 @@ object Client {
ModuleManager.initialize()
CommandManager.initialize()
HandlerManager.initialize()
ConfigManager.initialize()

Runtime.getRuntime().addShutdownHook(Thread(this::shutdown, "Shutdown"))
logger.info("Initialized $name in ${System.currentTimeMillis() - initTime}ms")
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/dev/blend/command/Command.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package dev.blend.command

import dev.blend.command.api.CommandInfo
import dev.blend.util.player.ChatUtil.format
import dev.blend.util.player.ChatUtil.info

abstract class Command {

val commandInfo: CommandInfo
val name get() = commandInfo.names.first()

init {
if (this::class.java.isAnnotationPresent(CommandInfo::class.java)) {
Expand All @@ -16,8 +19,11 @@ abstract class Command {

abstract fun execute(args: List<String>)

fun getName(): String {
return commandInfo.names[0]
protected fun usage() {
info("Usage of ${format("@$name!")}:")
commandInfo.syntax.forEach { syntax ->
info(format(syntax), false)
}
}

}
11 changes: 9 additions & 2 deletions src/main/kotlin/dev/blend/command/api/CommandManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dev.blend.command.api

import dev.blend.command.Command
import dev.blend.command.impl.ConfigCommand
import dev.blend.command.impl.HelpCommand
import dev.blend.command.impl.ListCommand
import dev.blend.command.impl.UsageCommand
import dev.blend.util.interfaces.IManager

object CommandManager: IManager {
Expand All @@ -9,10 +13,13 @@ object CommandManager: IManager {

override fun initialize() {
commands.addAll(arrayOf(

HelpCommand(),
ListCommand(),
ConfigCommand(),
UsageCommand,
))
commands.sortBy {
it.getName()
it.name
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/main/kotlin/dev/blend/command/impl/ConfigCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dev.blend.command.impl

import dev.blend.Client
import dev.blend.command.Command
import dev.blend.command.api.CommandInfo
import dev.blend.config.ConfigManager
import dev.blend.util.player.ChatUtil
import java.awt.Desktop

@CommandInfo(
names = ["config", "profile", "configs", "profiles", "c", "f"],
description = "Save the current configuration to a file.",
syntax = [".config @save! _name", ".config @load! _name", ".config @folder!", ".config @list!"],
)
class ConfigCommand: Command() {
override fun execute(args: List<String>) {
with(ChatUtil) {
if (args.size > 1) {
val argument = args[1].lowercase()
// save, load
if (args.size > 2) {
val file = args[2].lowercase().replace(".json", "")
when (argument) {
"load" -> {
if (ConfigManager.load(file)) {
info("Loaded config ${format("@$file!")} successfully.")
} else {
error("Failed to load config ${format("%$file!")}.")
}
}
"save" -> {
if (ConfigManager.save(file)) {
info("Saved config ${format("@$file!")} successfully.")
} else {
warn("Failed to save config ${format("%$file!")}.")
}
}
else -> {
usage()
}
}
} else {
when (argument) {
"folder" -> {
try {
Desktop.getDesktop().open(ConfigManager.configDir)
info("Opened config folder.")
} catch (hi: Exception) {
error("Error opening config directory ${format("%:(")}")
}
}
"list" -> {
ConfigManager.listConfigs()
}
else -> usage()
}
}

} else {
usage()
}
}
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/dev/blend/command/impl/HelpCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.blend.command.impl

import dev.blend.command.Command
import dev.blend.command.api.CommandInfo
import dev.blend.util.player.ChatUtil

@CommandInfo(
names = ["help", "wtf", "h"],
description = "Helps users with chat commands.",
syntax = [".help", ".help @{command}"]
)
class HelpCommand: Command() {
override fun execute(args: List<String>) {
with(ChatUtil) {
if (args.size > 1) {
UsageCommand.execute(args)
} else {
info("Help: ")
info("${format("@.list!")} for a list of all available commands.", false)
info("${format("@.usage!")} for proper usage of a command.", false)
}
}
}
}
22 changes: 22 additions & 0 deletions src/main/kotlin/dev/blend/command/impl/ListCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.blend.command.impl

import dev.blend.command.Command
import dev.blend.command.api.CommandInfo
import dev.blend.command.api.CommandManager
import dev.blend.util.player.ChatUtil

@CommandInfo(
names = ["list", "l"],
description = "Lists all available commands.",
syntax = [".list"]
)
class ListCommand: Command() {
override fun execute(args: List<String>) {
with(ChatUtil) {
info("List of all available commands: ")
CommandManager.commands.forEach { command ->
info("${aquafy(command.name)}: ${command.commandInfo.description}", false)
}
}
}
}
34 changes: 34 additions & 0 deletions src/main/kotlin/dev/blend/command/impl/UsageCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.blend.command.impl

import dev.blend.command.Command
import dev.blend.command.api.CommandInfo
import dev.blend.command.api.CommandManager
import dev.blend.util.player.ChatUtil

@CommandInfo(
names = ["usage", "how", "?"],
description = "Displays the proper usage of a command.",
syntax = [".usage @{command}!"]
)
object UsageCommand: Command() {
override fun execute(args: List<String>) {
with(ChatUtil) {
if (args.size > 1) {
val commandName = args[1]
CommandManager.commands
.forEach { command ->
command.commandInfo.names.forEach { name ->
if (name.equals(commandName, ignoreCase = true)) {
info("Usage of ${format("@${commandName}!")}:")
command.commandInfo.syntax.forEach { syntax ->
info(format(syntax), false)
}
}
}
}
} else {
error(this@UsageCommand.commandInfo.syntax.first())
}
}
}
}
90 changes: 90 additions & 0 deletions src/main/kotlin/dev/blend/config/ConfigManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package dev.blend.config

import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import dev.blend.Client
import dev.blend.module.api.ModuleManager
import dev.blend.util.IAccessor
import dev.blend.util.interfaces.IManager
import dev.blend.util.player.ChatUtil
import org.lwjgl.util.tinyfd.TinyFileDialogs
import java.io.File
import java.io.FileReader
import java.io.FileWriter

object ConfigManager: IAccessor, IManager {

val configDir get() = File(clientDir, "configs")
val gson = GsonBuilder()
.setPrettyPrinting()
.create()

override fun initialize() {
clientDir.mkdir()
configDir.mkdir()
load()
}

fun save(name: String = "default"): Boolean {
try {
val config = File(configDir, "$name.json")
if (!config.exists()) {
config.createNewFile()
}
FileWriter(config).use {
val root = JsonObject()
root.addProperty("name", Client.name.lowercase())
root.addProperty("version", Client.version.lowercase())
val modules = JsonArray()
ModuleManager.modules.forEach { module ->
modules.add(module.getJsonObject())
}
root.add("modules", modules)
it.write(gson.toJson(root))
}
return true
} catch (e: Exception) {
Client.logger.error("Failed to save config \"${name}\"", e)
return false
}
}

fun load(name: String = "default"): Boolean {
val config = File(configDir, "$name.json")
try {
FileReader(config).use { reader ->
val root = gson.fromJson(reader, JsonObject::class.java)
val configVersion = root.get("version").asString.substring(2).toDouble()
if (configVersion > Client.version.substring(2).toDouble()) {
with(ChatUtil) {
warn("Config ${aquafy(name)} saved on a newer version, Please update your client.")
}
}
val modules = root.getAsJsonArray("modules")
modules.forEach { obj ->
val moduleName = obj.asJsonObject?.get("name")?.asString
ModuleManager.getModule(moduleName!!)?.useJsonObject(obj.asJsonObject)
}
}
return true
} catch (e: Exception) {
Client.logger.error("Failed to load config \"${name}\"", e)
}
return false
}

fun listConfigs() {
try {
ChatUtil.info("List of available configs:")
configDir.listFiles()?.forEach { file ->
if (file.isFile) {
ChatUtil.info(file.name.replace(".json", ""), false)
}
}
} catch (_: Exception) {
ChatUtil.error("Error listing configs.")
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package dev.blend.event.impl

import best.azura.eventbus.core.Event
import best.azura.eventbus.events.CancellableEvent

class KeyEvent(
val window: Long,
val key: Int,
val scanCode: Int,
val action: Int,
val modifiers: Int
): Event
): Event

class ChatSendEvent(
val message: String
): CancellableEvent()
3 changes: 2 additions & 1 deletion src/main/kotlin/dev/blend/handler/Handler.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package dev.blend.handler

import dev.blend.event.api.EventBus
import dev.blend.util.IAccessor

interface Handler {
interface Handler: IAccessor {
fun register() {
EventBus.subscribe(this)
}
Expand Down
Loading

0 comments on commit eb13024

Please sign in to comment.