Add keybinds and use fabric's mixins for the :common gradle project.
This commit is contained in:
parent
d14c7c94aa
commit
48116b81fd
|
@ -1,13 +1,5 @@
|
|||
architectury {
|
||||
common()
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name = "SpongePowered"
|
||||
url = uri("https://repo.spongepowered.org/repository/maven-public/")
|
||||
}
|
||||
}
|
||||
architectury { common() }
|
||||
dependencies {
|
||||
compileOnly("org.spongepowered:mixin:0.8-SNAPSHOT")
|
||||
modImplementation("net.fabricmc:fabric-loader:${rootProject.property("fabric_loader_version")}")
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package fr.username404.snowygui.mixins;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.Options;
|
||||
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.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Mixin(Options.class)
|
||||
class KeyMappings {
|
||||
private static final List<KeyMapping> keysToAdd = AddKeyMaps.INSTANCE.getList();
|
||||
|
||||
@Mutable @Shadow @Final public KeyMapping[] keyMappings;
|
||||
|
||||
@Inject(method = "<init>*", at = @At("RETURN"))
|
||||
private void onSettingsConstructed(Minecraft minecraft, File file, CallbackInfo ci) {
|
||||
Map<String, Integer> categoryMap = keysAccessor.getSortedCategoryMap();
|
||||
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);
|
||||
categoryMap.put(key.getCategory(), biggestInt + 1);
|
||||
}
|
||||
newKeys.remove(key);
|
||||
newKeys.add(key);
|
||||
}
|
||||
keyMappings = newKeys.toArray(new KeyMapping[0]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package fr.username404.snowygui.mixins;
|
||||
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Mixin(KeyMapping.class)
|
||||
public class keysAccessor {
|
||||
@Accessor("CATEGORY_SORT_ORDER")
|
||||
static Map<String, Integer> getSortedCategoryMap() { throw new AssertionError(); }
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package fr.username404.snowygui.config
|
||||
|
||||
import com.mojang.blaze3d.platform.InputConstants
|
||||
import net.minecraft.client.KeyMapping
|
||||
import org.lwjgl.glfw.GLFW.GLFW_KEY_U
|
||||
import org.lwjgl.glfw.GLFW.GLFW_KEY_Y
|
||||
|
||||
object AddKeyMaps {
|
||||
private const val prefix = "snowy"
|
||||
private fun mkMap(translationSuffix: String, key: Int, category: String = "keycategory"): KeyMapping {
|
||||
return KeyMapping(
|
||||
"$prefix.$translationSuffix", InputConstants.Type.KEYSYM,
|
||||
key, category
|
||||
)
|
||||
}
|
||||
fun getList(): List<KeyMapping> {
|
||||
return listOf(
|
||||
mkMap("opengui", GLFW_KEY_Y),
|
||||
mkMap("configkey", GLFW_KEY_U)
|
||||
)
|
||||
}
|
||||
}
|
|
@ -2,7 +2,10 @@
|
|||
"required": true,
|
||||
"package": "fr.username404.snowygui.mixins",
|
||||
"compatibilityLevel": "JAVA_8",
|
||||
"client": [],
|
||||
"client": [
|
||||
"keysAccessor",
|
||||
"KeyMappings"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue