Skip to content

Commit

Permalink
Merge branch '1.19.3' into 1.19.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Siphalor committed May 13, 2023
2 parents 67c4c31 + 06bfe7c commit e63c277
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .giup
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"merge-paths": [
"1.15",
"1.15->1.14",
"1.15->1.16->1.17->1.18->1.19->unstable"
"1.15->1.16->1.17->1.18->1.19->1.19.3->1.19.4->unstable"
],
"commands": [
{
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ yarn_mappings = 1
loader_version = 0.14.18

#Mod properties
mod_version = 1.3.9
mod_version = 1.4.0
maven_group = de.siphalor
archives_base_name = amecsapi

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
10 changes: 10 additions & 0 deletions src/main/java/de/siphalor/amecs/api/PriorityKeyBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ public interface PriorityKeyBinding {
* @return Return true to cancel propagation of this event. Return false for normal evaluation.
*/
boolean onPressedPriority();

/**
* This method gets triggered when this key binding matches on an input release event. <br>
* Since there are no other checks before the invocation you need to check yourself for possible open screens.
*
* @return Return true to cancel propagation of this event. Return false for normal evaluation.
*/
default boolean onReleasedPriority() {
return false;
}
}
53 changes: 27 additions & 26 deletions src/main/java/de/siphalor/amecs/impl/KeyBindingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public class KeyBindingManager {
// split it in two maps because it is ways faster to only stream the map with the objects we need
// rather than streaming all and throwing out a bunch every time
public static Map<InputUtil.Key, List<KeyBinding>> keysById = new HashMap<>();
public static Map<InputUtil.Key, List<KeyBinding>> keysById_priority = new HashMap<>();
public static Map<InputUtil.Key, List<KeyBinding>> priorityKeysById = new HashMap<>();
/**
*
* @param keysById_map
* @param keyBinding
* Removes a key binding from one of the internal maps
* @param targetMap the key binding map to remove from
* @param keyBinding the key binding to remove
* @return whether the keyBinding was removed. It is not removed if it was not contained
*/
private static boolean removeKeyBindingFromListFromMap(Map<InputUtil.Key, List<KeyBinding>> keysById_map, KeyBinding keyBinding) {
private static boolean removeKeyBindingFromListFromMap(Map<InputUtil.Key, List<KeyBinding>> targetMap, KeyBinding keyBinding) {
// we need to get the backing list to remove elements thus we can not use any of the other methods that return streams
InputUtil.Key keyCode = ((IKeyBinding) keyBinding).amecs$getBoundKey();
List<KeyBinding> keyBindings = keysById_map.get(keyCode);
List<KeyBinding> keyBindings = targetMap.get(keyCode);
if (keyBindings == null) {
return false;
}
Expand All @@ -59,18 +59,14 @@ private static boolean removeKeyBindingFromListFromMap(Map<InputUtil.Key, List<K
}

/**
*
* @param keysById_map
* @param keyBinding
* Adds a key binding to one of the internal maps
* @param targetMap the key binding map to add to
* @param keyBinding the key binding to add
* @return whether the keyBinding was added. It is not added if it is already contained
*/
private static boolean addKeyBindingToListFromMap(Map<InputUtil.Key, List<KeyBinding>> keysById_map, KeyBinding keyBinding) {
private static boolean addKeyBindingToListFromMap(Map<InputUtil.Key, List<KeyBinding>> targetMap, KeyBinding keyBinding) {
InputUtil.Key keyCode = ((IKeyBinding) keyBinding).amecs$getBoundKey();
List<KeyBinding> keyBindings = keysById_map.get(keyCode);
if (keyBindings == null) {
keyBindings = new ArrayList<>();
keysById_map.put(keyCode, keyBindings);
}
List<KeyBinding> keyBindings = targetMap.computeIfAbsent(keyCode, k -> new ArrayList<>());
if (keyBindings.contains(keyBinding)) {
return false;
}
Expand All @@ -79,20 +75,20 @@ private static boolean addKeyBindingToListFromMap(Map<InputUtil.Key, List<KeyBin
}

/**
*
* @param keyBinding
* Registers a key binding to Amecs API
* @param keyBinding the key binding to register
* @return whether the keyBinding was added. It is not added if it is already contained
*/
public static boolean register(KeyBinding keyBinding) {
if (keyBinding instanceof PriorityKeyBinding) {
return addKeyBindingToListFromMap(keysById_priority, keyBinding);
return addKeyBindingToListFromMap(priorityKeysById, keyBinding);
} else {
return addKeyBindingToListFromMap(keysById, keyBinding);
}
}

public static Stream<KeyBinding> getMatchingKeyBindings(InputUtil.Key keyCode, boolean priority) {
List<KeyBinding> keyBindingList = (priority ? keysById_priority : keysById).get(keyCode);
List<KeyBinding> keyBindingList = (priority ? priorityKeysById : keysById).get(keyCode);
if (keyBindingList == null)
return Stream.empty();
// this looks not right: If you have a kb: alt + y and shift + alt + y and you press shift + alt + y both will be triggered
Expand All @@ -115,7 +111,7 @@ private static Stream<KeyBinding> getKeyBindingsFromMap(Map<InputUtil.Key, List<
}

private static void forEachKeyBinding(Consumer<KeyBinding> consumer) {
getKeyBindingsFromMap(keysById_priority).forEach(consumer);
getKeyBindingsFromMap(priorityKeysById).forEach(consumer);
getKeyBindingsFromMap(keysById).forEach(consumer);
}

Expand All @@ -134,8 +130,8 @@ public static void updatePressedStates() {
}

/**
*
* @param keyBinding
* Unregisters a key binding from Amecs API
* @param keyBinding the key binding to unregister
* @return whether the keyBinding was removed. It is not removed if it was not contained
*/
public static boolean unregister(KeyBinding keyBinding) {
Expand All @@ -147,13 +143,13 @@ public static boolean unregister(KeyBinding keyBinding) {
// instead
boolean removed = false;
removed |= removeKeyBindingFromListFromMap(keysById, keyBinding);
removed |= removeKeyBindingFromListFromMap(keysById_priority, keyBinding);
removed |= removeKeyBindingFromListFromMap(priorityKeysById, keyBinding);
return removed;
}

public static void updateKeysByCode() {
keysById.clear();
keysById_priority.clear();
priorityKeysById.clear();
KeyBindingUtils.getIdToKeyBindingMap().values().forEach(KeyBindingManager::register);
}

Expand All @@ -162,12 +158,17 @@ public static void unpressAll() {
}

public static boolean onKeyPressedPriority(InputUtil.Key keyCode) {
// because streams do evaluation lazy this code does only call onPressedPriority on so many keyBinding until one returns true
// Or if no one returns true all are called and an empty optional is returned
// because streams are lazily evaluated, this code only calls onPressedPriority so often until one returns true
Optional<KeyBinding> keyBindings = getMatchingKeyBindings(keyCode, true).filter(keyBinding -> ((PriorityKeyBinding) keyBinding).onPressedPriority()).findFirst();
return keyBindings.isPresent();
}

public static boolean onKeyReleasedPriority(InputUtil.Key keyCode) {
// because streams are lazily evaluated, this code only calls onPressedPriority so often until one returns true
Optional<KeyBinding> keyBindings = getMatchingKeyBindings(keyCode, true).filter(keyBinding -> ((PriorityKeyBinding) keyBinding).onReleasedPriority()).findFirst();
return keyBindings.isPresent();
}

public static void setKeyPressed(InputUtil.Key keyCode, boolean pressed) {
AmecsAPI.CURRENT_MODIFIERS.set(KeyModifier.fromKeyCode(keyCode.getCode()), pressed);

Expand Down
20 changes: 12 additions & 8 deletions src/main/java/de/siphalor/amecs/impl/mixin/MixinKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.Util;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -36,24 +35,29 @@
@Mixin(Keyboard.class)
public class MixinKeyboard {

@Inject(method = "onKey", at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;currentScreen:Lnet/minecraft/client/gui/screen/Screen;", ordinal = 1, shift = At.Shift.BEFORE), cancellable = true)
private void onKeyPriority(long window, int int_1, int int_2, int int_3, int int_4, CallbackInfo callbackInfo) {
if (int_3 == 1 || int_3 == 2) {
if (KeyBindingManager.onKeyPressedPriority(InputUtil.fromKeyCode(int_1, int_2)))
@Inject(method = "onKey", at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;currentScreen:Lnet/minecraft/client/gui/screen/Screen;", ordinal = 0, shift = At.Shift.BEFORE), cancellable = true)
private void onKeyPriority(long window, int key, int scanCode, int action, int modifiers, CallbackInfo callbackInfo) {
if (action == 1) {
if (KeyBindingManager.onKeyPressedPriority(InputUtil.fromKeyCode(key, scanCode))) {
callbackInfo.cancel();
}
} else if (action == 0) {
if (KeyBindingManager.onKeyReleasedPriority(InputUtil.fromKeyCode(key, scanCode))) {
callbackInfo.cancel();
}
}
}

@Inject(method = "onKey", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Keyboard;debugCrashStartTime:J", ordinal = 0))
private void onKey(long window, int int_1, int int_2, int int_3, int int_4, CallbackInfo callbackInfo) {
private void onKey(long window, int key, int scanCode, int action, int modifiers, CallbackInfo callbackInfo) {
// Key released
if (int_3 == 0 && MinecraftClient.getInstance().currentScreen instanceof KeybindsScreen) {
if (action == 0 && MinecraftClient.getInstance().currentScreen instanceof KeybindsScreen) {
KeybindsScreen screen = (KeybindsScreen) MinecraftClient.getInstance().currentScreen;

screen.selectedKeyBinding = null;
screen.lastKeyCodeUpdateTime = Util.getMeasuringTimeMs();
}

AmecsAPI.CURRENT_MODIFIERS.set(KeyModifier.fromKeyCode(InputUtil.fromKeyCode(int_1, int_2).getCode()), int_3 != 0);
AmecsAPI.CURRENT_MODIFIERS.set(KeyModifier.fromKeyCode(InputUtil.fromKeyCode(key, scanCode).getCode()), action != 0);
}
}
3 changes: 3 additions & 0 deletions src/testmod/java/de/siphalor/amecs/testmod/ClientInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public void onInitializeClient() {
KeyBindingHelper.registerKeyBinding(new TestPriorityKeybinding(new Identifier("amecsapi-testmod", "priority"), InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_Z, "key.categories.misc", new KeyModifiers(), () -> {
System.out.println("priority");
return true;
}, () -> {
System.out.println("priority release");
return true;
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@

public class TestPriorityKeybinding extends AmecsKeyBinding implements PriorityKeyBinding {
private final BooleanSupplier action;
private final BooleanSupplier releaseAction;

public TestPriorityKeybinding(Identifier id, InputUtil.Type type, int code, String category, KeyModifiers defaultModifiers, BooleanSupplier action) {
public TestPriorityKeybinding(Identifier id, InputUtil.Type type, int code, String category, KeyModifiers defaultModifiers, BooleanSupplier action, BooleanSupplier releaseAction) {
super(id, type, code, category, defaultModifiers);
this.action = action;
this.releaseAction = releaseAction;
}

@Override
public boolean onPressedPriority() {
return action.getAsBoolean();
}

@Override
public boolean onReleasedPriority() {
return releaseAction.getAsBoolean();
}
}

0 comments on commit e63c277

Please sign in to comment.