Add an Events system, a mixin for ends of ticks, and ConfigScreen.kt.

This commit is contained in:
Username404-59 2021-04-09 20:48:01 +02:00
parent de806a742c
commit 7416bd0ee9
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
10 changed files with 75 additions and 7 deletions

View File

@ -0,0 +1,20 @@
package fr.username404.snowygui.mixins;
import fr.username404.snowygui.EventSnowy;
import net.minecraft.client.Minecraft;
import org.jetbrains.annotations.NotNull;
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;
@Mixin(Minecraft.class)
public class EndTickMixin implements EventSnowy {
@NotNull
@Override
public String getType() {
return "EndTick";
}
@Inject(method = "tick", at = @At("RETURN"))
public void endTick(CallbackInfo ci) { fire(); }
}

View File

@ -1,6 +1,7 @@
package fr.username404.snowygui.mixins;
import com.google.common.collect.Lists;
import fr.username404.snowygui.config.AddKeyMaps;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
@ -8,13 +9,15 @@ import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import fr.username404.snowygui.config.AddKeyMaps;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.io.File;
import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Mixin(Options.class)
class KeyMappings {
@ -28,7 +31,8 @@ class KeyMappings {
List<KeyMapping> newKeys = Lists.newArrayList(keyMappings);
for (KeyMapping key : keysToAdd) {
if (!categoryMap.containsKey(key.getCategory())) {
Optional<Integer> biggest = categoryMap.values().stream().max(Integer::compareTo); int biggestInt = biggest.orElse(0);
Optional<Integer> biggest = categoryMap.values().stream().max(Integer::compareTo);
int biggestInt = biggest.orElse(0);
categoryMap.put(key.getCategory(), biggestInt + 1);
}
newKeys.remove(key);

View File

@ -0,0 +1,16 @@
package fr.username404.snowygui
interface EventSnowy {
val type: String
fun <T> fire(vararg args: T?) {
useKey(type).forEach {
it(args)
}
}
companion object {
fun useKey(key: String): MutableSet<argsLambda> { return eventMap.getOrPut(key, ::mutableSetOf) }
private val eventMap: MutableMap<String, MutableSet<argsLambda>> = mutableMapOf()
}
}
internal typealias argsLambda = (args: Array<*>) -> Unit

View File

@ -1,14 +1,29 @@
package fr.username404.snowygui
import fr.username404.snowygui.EventSnowy.Companion.useKey
import fr.username404.snowygui.config.AddKeyMaps
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
abstract class Snowy {
companion object {
fun onEvent(e: String, lambda: argsLambda) {
useKey(e).add(lambda)
}
@JvmStatic
val logs: Logger = LogManager.getLogger()
}
private fun eventsInit() {
onEvent("EndTick") {
for (Key in AddKeyMaps.list.keys) {
if (Key.isDown) {
AddKeyMaps.list[Key]?.invoke()
}
}
}
}
fun atInit() {
logs.info("Init point of SnowyGUI hit.")
eventsInit()
}
}

View File

@ -0,0 +1,8 @@
package fr.username404.snowygui
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.TranslatableComponent
internal class SnowyScreen: Screen(TranslatableComponent("screen.snowy.gui")) {
}

View File

@ -1,7 +1,9 @@
package fr.username404.snowygui.config
import com.mojang.blaze3d.platform.InputConstants
import fr.username404.snowygui.SnowyScreen
import net.minecraft.client.KeyMapping
import net.minecraft.client.Minecraft
import org.lwjgl.glfw.GLFW.GLFW_KEY_U
import org.lwjgl.glfw.GLFW.GLFW_KEY_Y
@ -15,7 +17,7 @@ object AddKeyMaps {
) to lambda
}
val list: MutableMap<KeyMapping, (() -> Unit)?> = mutableMapOf(
mkMap("opengui", GLFW_KEY_Y),
mkMap("configkey", GLFW_KEY_U)
mkMap("opengui", GLFW_KEY_Y) { Minecraft.getInstance().setScreen(SnowyScreen()) },
mkMap("configkey", GLFW_KEY_U) { Minecraft.getInstance().setScreen(SnowyConfigScreen()) }
)
}

View File

@ -3,6 +3,6 @@ package fr.username404.snowygui.config
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.TranslatableComponent
abstract class SnowyConfigScreen: Screen(TranslatableComponent("screen.snowygui.config")) { // TODO Actual config screen
open class SnowyConfigScreen: Screen(TranslatableComponent("screen.snowygui.config")) { // TODO Actual config screen
}

View File

@ -1,4 +1,5 @@
{
"screen.snowy.gui": "Snowy Interface",
"screen.snowy.config": "Snowy configuration screen",
"category.snowy.keycategory": "SnowyGUI",
"key.snowy.opengui": "Open the snowy gui",

View File

@ -1,4 +1,5 @@
{
"screen.snowy.gui": "Interface de Snowy",
"screen.snowy.config": "Configuration de snowy",
"category.snowy.keycategory": "SnowyGUI",
"key.snowy.opengui": "Ouvrir l'interface de snowy",

View File

@ -4,7 +4,8 @@
"compatibilityLevel": "JAVA_8",
"client": [
"KeysAccessor",
"KeyMappings"
"KeyMappings",
"EndTickMixin"
],
"injectors": {
"defaultRequire": 1