-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configs, chat commands. TODO: rework keybinding system.
- Loading branch information
1 parent
2202f83
commit eb13024
Showing
19 changed files
with
393 additions
and
8 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/dev/blend/mixin/client/gui/screen/ChatScreenMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} | ||
|
||
} |
7 changes: 6 additions & 1 deletion
7
...n/kotlin/dev/blend/event/impl/KeyEvent.kt → ...otlin/dev/blend/event/impl/InputEvents.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.