Skip to content

Commit

Permalink
Partially port key binds api to 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
EnnuiL committed Jun 24, 2023
1 parent 0b2e598 commit 1286571
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 373 deletions.
5 changes: 2 additions & 3 deletions library/gui/key_binds/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ qslModule {
moduleName = "key_binds"
id = "quilt_key_binds"
description = "Key binds registration and utilities."
library = "gui"
moduleDependencies {
core {
api("resource_loader")
impl("qsl_base")
api("qsl_base")
testmodOnly("lifecycle_events")
}
}
clientOnly()
injectedInterface("net/minecraft/class_304") {
values = [
"org/quiltmc/qsl/key/binds/api/ChordedKeyBind",
"org/quiltmc/qsl/key/binds/api/QuiltKeyBind",
"org/quiltmc/qsl/key/binds/api/ToggleableKeyBind"
]
}
clientOnly()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

package org.quiltmc.qsl.key.binds.api;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import com.mojang.blaze3d.platform.InputUtil;

import net.minecraft.client.option.KeyBind;

import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.qsl.base.api.util.InjectedInterface;
import org.quiltmc.qsl.key.binds.impl.chords.KeyChord;

// TODO - Explain what are key chords.
/**
* An interface that adds key chord support to key binds.
*/
@Environment(EnvType.CLIENT)
@ClientOnly
@InjectedInterface(KeyBind.class)
public interface ChordedKeyBind {
/**
Expand All @@ -39,24 +37,24 @@ public interface ChordedKeyBind {
* @return the key bind's bound key chord.
*/
default KeyChord getBoundChord() {
throw new UnsupportedOperationException();
throw new IllegalStateException("Mixin injection failed.");
}

/**
* Sets the bound key chord of the key bind.
*/
default void setBoundChord(KeyChord chord) { }
default void setBoundChord(KeyChord chord) {}

// TODO - This is a temporary measure until Chasm arrives. Replace it with a proper constructor or builder
/**
* Specifies the default key chord for the key bind.
*
* <p>This method is to be used only on creating a key bind instance.
* <p>
* This method is to be used only on creating a key bind instance.
*
* @param keys the keys of the default key chord
* @return the original key bind instance
*/
default KeyBind withChord(InputUtil.Key... keys) {
throw new UnsupportedOperationException();
throw new IllegalStateException("Mixin injection failed.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import net.minecraft.client.option.KeyBind;

import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl;

// TODO - This isn't a registry anymore; What the heck should it be named?
Expand All @@ -32,7 +33,7 @@
*
* <p>This class also allows for getting key binds registered by other mods.
*/
@Environment(EnvType.CLIENT)
@ClientOnly
public class KeyBindRegistry {
// TODO - Shouldn't we use Vanilla's Map for getAllKeyBinds instead? Two birds, one stone, zero thoughts, head empty
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@

import com.mojang.blaze3d.platform.InputUtil;

import net.minecraft.client.option.KeyBind;

import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.qsl.base.api.util.InjectedInterface;

/**
* An injected interface for KeyBind that adds few Quilt-relevant utility methods.
*/
@ClientOnly
@InjectedInterface(KeyBind.class)
public interface QuiltKeyBind {
/**
* Gets whenever the key bind is from Vanilla or not.
Expand All @@ -29,7 +36,7 @@ public interface QuiltKeyBind {
* @return {@code true} if the key bind is from Vanilla, or {@code false} otherwise
*/
default boolean isVanilla() {
return false;
throw new IllegalStateException("Mixin injection failed.");
}

/**
Expand All @@ -41,6 +48,6 @@ default boolean isVanilla() {
* @return the key bind's bound key
*/
default InputUtil.Key getBoundKey() {
throw new UnsupportedOperationException();
throw new IllegalStateException("Mixin injection failed.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

import net.minecraft.client.option.KeyBind;

import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.qsl.base.api.util.InjectedInterface;

// TODO - Add Javadocs; You can nab the ones from KeyBindRegistry
/**
* An interface for adding toggling capabilities to key binds.
*/
@Environment(EnvType.CLIENT)
@ClientOnly
@InjectedInterface(KeyBind.class)
public interface ToggleableKeyBind {
/**
Expand All @@ -36,7 +37,7 @@ public interface ToggleableKeyBind {
* @return {@code true} if the key bind is enabled, {@code false} otherwise
*/
default boolean isEnabled() {
return true;
throw new IllegalStateException("Mixin injection failed.");
}

/**
Expand All @@ -45,7 +46,7 @@ default boolean isEnabled() {
* @return {@code true} if the key bind is disabled, {@code false} otherwise
*/
default boolean isDisabled() {
return false;
throw new IllegalStateException("Mixin injection failed.");
}

/**
Expand All @@ -54,27 +55,31 @@ default boolean isDisabled() {
* @return {@code true} if the key bind can be disabled, {@code false} otherwise
*/
default boolean canDisable() {
return false;
throw new IllegalStateException("Mixin injection failed.");
}

/**
* Enables the key bind.
*
* <p>If the key bind has been disabled more than once, this method will only
* <p>
* If the key bind has been disabled more than once, this method will only
* decrement its internal counter instead of enabling the key bind.
*/
default void enable() { }
default void enable() {
throw new IllegalStateException("Mixin injection failed.");
}

/**
* Disables the key bind.
*
* <p>When a key bind is disabled, it is effectively hidden from the game,
* <p>
* When a key bind is disabled, it is effectively hidden from the game,
* being non-existent to it. config/quilt/key_binds.json, however, will
* still remember the key bind's bound keys, similar to non-existent key binds.
*
* <p>If the key bind is disabled while already disabled, it will be increment
* <p>
* If the key bind is disabled while already disabled, it will be increment
* an internal counter, making the next enable only decrement it instead of
* enabling the key bind.
*/
default void disable() { }
default void disable() {
throw new IllegalStateException("Mixin injection failed.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
package org.quiltmc.qsl.key.binds.impl;

import org.jetbrains.annotations.ApiStatus;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import net.minecraft.client.option.GameOptions;
import net.minecraft.client.option.KeyBind;

import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.qsl.key.binds.mixin.client.GameOptionsAccessor;

@Environment(EnvType.CLIENT)
@ClientOnly
@ApiStatus.Internal
public class KeyBindManager {
private final GameOptions options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import org.jetbrains.annotations.ApiStatus;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import net.minecraft.client.option.KeyBind;

@Environment(EnvType.CLIENT)
import org.quiltmc.loader.api.minecraft.ClientOnly;

@ClientOnly
@ApiStatus.Internal
public class KeyBindRegistryImpl {
private static final List<KeyBind> ALL_KEY_BINDS = new ReferenceArrayList<>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

import com.mojang.blaze3d.platform.InputUtil;

import org.quiltmc.loader.api.minecraft.ClientOnly;

@ClientOnly
public class KeyChord {
// TODO - Private this, add methods for getting/modifying it
public SortedMap<InputUtil.Key, Boolean> keys = new Object2BooleanAVLTreeMap<>();
Expand All @@ -30,13 +33,13 @@ public KeyChord(SortedMap<InputUtil.Key, Boolean> keys) {
this.keys = keys;
}

public KeyChord() { }
public KeyChord() {}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && o instanceof KeyChord keyChord) {
} else if (o instanceof KeyChord keyChord) {
return this.keys.keySet().equals(keyChord.keys.keySet());
} else {
return false;
Expand Down

This file was deleted.

Loading

0 comments on commit 1286571

Please sign in to comment.