Skip to content

Commit

Permalink
Finish 1.21 port and fix Quilt compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
MikolajKolek committed Jun 24, 2024
1 parent d6e4a0b commit deeefee
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 140 deletions.
8 changes: 0 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ indent_style = tab
indent_style = space
indent_size = 2

[quilt.mod.json]
indent_style = tab
tab_width = 2

[*.toml]
indent_style = tab
tab_width = 2

[*.properties]
indent_style = space
indent_size = 2
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
# against bad commits.

name: build
on: [pull_request, push]
on:
workflow_dispatch:
branches: [ "1.21" ]
push:
branches: [ "1.21" ]
pull_request:
branches: [ "1.21" ]

jobs:
build:
Expand Down Expand Up @@ -33,8 +39,8 @@ jobs:
- name: build
run: ./gradlew build
- name: capture build artifacts
if: ${{ runner.os == 'Linux' && matrix.java == '21' }} # Only upload artifacts built from latest java on one OS
if: ${{ runner.os == 'Linux' }} # Only upload artifacts built from latest java on one OS
uses: actions/upload-artifact@v4
with:
name: Artifacts
path: build/libs/
path: build/libs/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<img src="src/main/resources/assets/fixaltgr/icon.png" width="128">

# Fix Alt Gr
A simple Minecraft mod that (mostly) fixes the [MC-127862](https://bugs.mojang.com/browse/MC-127862) bug, which sometimes makes the Alt Gr key lock your left control key in a pressed state. It prevents the bug from happening in text fields in most situations.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ loom {
splitEnvironmentSourceSets()

mods {
"fixaltgr" {
"fix-alt-gr" {
sourceSet sourceSets.main
sourceSet sourceSets.client
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ loader_version=0.15.11
# Mod Properties
mod_version=3.0.0+1.21
maven_group=com.mikolajkolek
archives_base_name=fixaltgr
archives_base_name=fix-alt-gr

# Dependencies
jnativehook_version=2.2.2
10 changes: 8 additions & 2 deletions src/client/java/com/mikolajkolek/fixaltgr/FixAltGrClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import net.fabricmc.api.ClientModInitializer;
import net.minecraft.client.MinecraftClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FixAltGrClient implements ClientModInitializer {
public static final String MODID = "fixaltgr";
public static final String MODID = "fix-alt-gr";
public static final Logger LOGGER = LoggerFactory.getLogger(MODID);
public static final GlobalKeyboardListener listener = new GlobalKeyboardListener();

@Override public void onInitializeClient() {
try {
//FixAltGrLibraryLocator.setAaDefaultLocator();
if(StringUtils.containsIgnoreCase(MinecraftClient.getInstance().getVersionType(), "quilt")) {
FixAltGrClient.LOGGER.info("FixAltGr detected running on Quilt, correcting library locator...");
QuiltLibraryLocator.setAaDefaultLocator();
}

GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,107 +1,114 @@
package com.mikolajkolek.fixaltgr;

import com.github.kwhat.jnativehook.NativeLibraryLocator;

import java.io.File;
import java.util.Iterator;

public class FixAltGrLibraryLocator implements NativeLibraryLocator {
public static void setAaDefaultLocator() {
System.setProperty("jnativehook.lib.locator", FixAltGrLibraryLocator.class.getCanonicalName());
}

// This code is based on the JNativeHook class DefaultLibraryLocator
// You can find the class together with the whole source code at https://github.com/kwhat/jnativehook/
@Override
public Iterator<File> getLibraries() {
/*List<File> libraries = new ArrayList<>(1);
String libName = System.getProperty("jnativehook.lib.name", "JNativeHook");
// Get the package name for the GlobalScreen.
String basePackage = GlobalScreen.class.getPackage().getName().replace('.', '/');
String libNativeArch = NativeSystem.getArchitecture().toString().toLowerCase();
String libNativeName = System
.mapLibraryName(libName) // Get what the system "thinks" the library name should be.
.replaceAll("\\.jnilib$", "\\.dylib"); // Hack for OS X JRE 1.6 and earlier.
// Resource path for the native library.
String libResourcePath = "/" + basePackage + "/lib/" +
NativeSystem.getFamily().toString().toLowerCase() +
'/' + libNativeArch + '/' + libNativeName;
String classLocation;
if(FabricLoader.getInstance().isDevelopmentEnvironment())
classLocation = GlobalScreen.class.getProtectionDomain().getCodeSource().getLocation().toString();
else
classLocation = FabricLoader.getInstance().getModContainer(FixAltGrClient.MODID).get().get().get(0).get(0).toString();
File classFile;
try {
classFile = new File(new URI(classLocation));
}
catch (URISyntaxException e) {
FixAltGr.LOGGER.warn(e.getMessage());
classFile = new File(classLocation);
}
File libFile;
if (classFile.isFile()) {
// Jar Archive
String libPath = System.getProperty("jnativehook.lib.path", classFile.getParentFile().getPath());
InputStream resourceInputStream = GlobalScreen.class.getResourceAsStream(libResourcePath);
if (resourceInputStream == null) {
throw new RuntimeException("Unable to extract the native library " + libResourcePath + "!\n");
}
String version = GlobalScreen.class.getPackage().getImplementationVersion();
if (version != null) {
version = '-' + version;
} else {
version = "";
}
libFile = new File(
libPath,
libNativeName.replaceAll("^(.*)\\.(.*)$", "$1" + version + '.' + libNativeArch + ".$2")
);
if (!libFile.exists()) {
try {
// Check and see if a copy of the native lib already exists.
FileOutputStream libOutputStream = new FileOutputStream(libFile);
// Read from the digest stream and write to the file steam.
int size;
byte[] buffer = new byte[4 * 1024];
while ((size = resourceInputStream.read(buffer)) != -1) {
libOutputStream.write(buffer, 0, size);
}
// Close all the streams.
resourceInputStream.close();
libOutputStream.close();
}
catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
FixAltGr.LOGGER.info("Extracted library: " + libFile.getPath() + ".\n");
}
} else {
// Loose Classes
libFile = Paths.get(classFile.getAbsolutePath(), libResourcePath).toFile();
}
if (!libFile.exists()) {
throw new RuntimeException("Unable to locate JNI library at " + libFile.getPath() + "!\n");
}
FixAltGr.LOGGER.info("Loading library: " + libFile.getPath() + ".\n");
libraries.add(libFile);
return libraries.iterator();*/
return null;
}
}
package com.mikolajkolek.fixaltgr;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeLibraryLocator;
import com.github.kwhat.jnativehook.NativeSystem;
import net.fabricmc.loader.api.FabricLoader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class QuiltLibraryLocator implements NativeLibraryLocator {
public static void setAaDefaultLocator() {
System.setProperty("jnativehook.lib.locator", QuiltLibraryLocator.class.getCanonicalName());
}

// This code is based on the JNativeHook class DefaultLibraryLocator
// You can find the class together with the whole source code at https://github.com/kwhat/jnativehook/
@Override
public Iterator<File> getLibraries() {
List<File> libraries = new ArrayList<>(1);

String libName = System.getProperty("jnativehook.lib.name", "JNativeHook");

// Get the package name for the GlobalScreen.
String basePackage = GlobalScreen.class.getPackage().getName().replace('.', '/');

String libNativeArch = NativeSystem.getArchitecture().toString().toLowerCase();
String libNativeName = System
.mapLibraryName(libName) // Get what the system "thinks" the library name should be.
.replaceAll("\\.jnilib$", "\\.dylib"); // Hack for OS X JRE 1.6 and earlier.

// Resource path for the native library.
String libResourcePath = "/" + basePackage + "/lib/" +
NativeSystem.getFamily().toString().toLowerCase() +
'/' + libNativeArch + '/' + libNativeName;

// classLocation change required by the Quilt Loader
String classLocation = FabricLoader.getInstance().getModContainer(FixAltGrClient.MODID).get().getOrigin().getPaths().get(0).toString();

File classFile;
try {
classFile = new File(new URI(classLocation));
}
catch (URISyntaxException e) {
FixAltGrClient.LOGGER.warn(e.getMessage());
classFile = new File(classLocation);
}

File libFile;
if (classFile.isFile()) {
// Jar Archive
String libPath = System.getProperty("jnativehook.lib.path", classFile.getParentFile().getPath());

InputStream resourceInputStream = GlobalScreen.class.getResourceAsStream(libResourcePath);
if (resourceInputStream == null) {
throw new RuntimeException("Unable to extract the native library " + libResourcePath + "!\n");
}

String version = GlobalScreen.class.getPackage().getImplementationVersion();
if (version != null) {
version = '-' + version;
} else {
version = "";
}

libFile = new File(
libPath,
libNativeName.replaceAll("^(.*)\\.(.*)$", "$1" + version + '.' + libNativeArch + ".$2")
);
if (!libFile.exists()) {
try {
// Check and see if a copy of the native lib already exists.
FileOutputStream libOutputStream = new FileOutputStream(libFile);

// Read from the digest stream and write to the file steam.
int size;
byte[] buffer = new byte[4 * 1024];
while ((size = resourceInputStream.read(buffer)) != -1) {
libOutputStream.write(buffer, 0, size);
}

// Close all the streams.
resourceInputStream.close();
libOutputStream.close();
}
catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}

FixAltGrClient.LOGGER.info("Extracted library: " + libFile.getPath() + ".\n");
}
} else {
// Loose Classes
libFile = Paths.get(classFile.getAbsolutePath(), libResourcePath).toFile();
}

if (!libFile.exists()) {
throw new RuntimeException("Unable to locate JNI library at " + libFile.getPath() + "!\n");
}

FixAltGrClient.LOGGER.info("Loading library: " + libFile.getPath() + ".\n");
libraries.add(libFile);

return libraries.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ public class InputUtilMixin { //69696969696969 jubert to nooooobek
private static void isKeyPressed(long window, int code, CallbackInfoReturnable<Boolean> cir) {
if(code != 341) return;

if(!FixAltGrClient.listener.controlKeyPressed || FixAltGrClient.listener.altKeyPressed) {
if(!FixAltGrClient.listener.controlKeyPressed || FixAltGrClient.listener.altKeyPressed)
cir.setReturnValue(false);
FixAltGrClient.LOGGER.info("IT'S WORKING");
}
else {
try {
TimeUnit.MILLISECONDS.sleep(10);
Expand All @@ -26,10 +24,8 @@ private static void isKeyPressed(long window, int code, CallbackInfoReturnable<B
FixAltGrClient.LOGGER.error("The isKeyPressed sleep was interrupted!");
}

if (!FixAltGrClient.listener.controlKeyPressed || FixAltGrClient.listener.altKeyPressed) {
if (!FixAltGrClient.listener.controlKeyPressed || FixAltGrClient.listener.altKeyPressed)
cir.setReturnValue(false);
FixAltGrClient.LOGGER.info("IT'S WORKING");
}
else
cir.setReturnValue(true);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"schemaVersion": 1,
"id": "fixaltgr",
"id": "fix-alt-gr",
"version": "${version}",
"name": "Fix Alt Gr",
"description": "A simple Minecraft mod that (mostly) fixes the MC-127862 bug, which sometimes makes pressing the Alt Gr key lock your left control key in a pressed state.",
"authors": [
"Mikolaj_Kolek"
],
"contact": {
"homepage": "https://github.com/MikolajKolek/fix-alt-gr",
"homepage": "https://modrinth.com/mod/fix-alt-gr",
"issues": "https://github.com/MikolajKolek/fix-alt-gr/issues",
"sources": "https://github.com/MikolajKolek/fix-alt-gr"
},
Expand All @@ -21,7 +21,7 @@
]
},
"mixins": [
"fixaltgr.mixins.json",
"fixaltgr.mixins.json",
{
"config": "fixaltgr.client.mixins.json",
"environment": "client"
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/fixaltgr.mixins.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"required": true,
"package": "com.mikolajkolek.fixaltgr.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [],
"injectors": {
"defaultRequire": 1
}
}
{
"required": true,
"package": "com.mikolajkolek.fixaltgr.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit deeefee

Please sign in to comment.