mirror of
https://github.com/chylex/Better-Controls.git
synced 2025-05-29 16:34:05 +02:00
Simplify mixins
This commit is contained in:
parent
0d1dfdee87
commit
cbe3fa6a71
src/main
java/chylex/bettercontrols
gui
input
mixin
resources
@ -1,61 +0,0 @@
|
|||||||
package chylex.bettercontrols.gui;
|
|
||||||
import chylex.bettercontrols.mixin.AccessCycleButtonFields;
|
|
||||||
import chylex.bettercontrols.mixin.AccessOptionFields;
|
|
||||||
import chylex.bettercontrols.mixin.AccessScreenButtons;
|
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.client.Option;
|
|
||||||
import net.minecraft.client.gui.components.Button;
|
|
||||||
import net.minecraft.client.gui.components.CycleButton;
|
|
||||||
import net.minecraft.client.gui.components.events.ContainerEventHandler;
|
|
||||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
|
||||||
import net.minecraft.client.gui.screens.AccessibilityOptionsScreen;
|
|
||||||
import net.minecraft.client.gui.screens.controls.ControlsScreen;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
public final class ScreenPatcher {
|
|
||||||
private ScreenPatcher() {}
|
|
||||||
|
|
||||||
public static void onControlsScreenOpened(final ControlsScreen screen) {
|
|
||||||
final AccessScreenButtons accessor = (AccessScreenButtons)screen;
|
|
||||||
final List<? extends GuiEventListener> children = screen.children();
|
|
||||||
|
|
||||||
final CycleButton<?> autoJump = children.stream().flatMap(it -> getOptionButton(it, Option.AUTO_JUMP).stream()).findAny().orElse(null);
|
|
||||||
|
|
||||||
if (autoJump != null) {
|
|
||||||
final Button widget = new Button(autoJump.x, autoJump.y, autoJump.getWidth(), autoJump.getHeight(), BetterControlsScreen.TITLE.plainCopy().append("..."), btn -> {
|
|
||||||
Minecraft.getInstance().setScreen(new BetterControlsScreen(screen));
|
|
||||||
});
|
|
||||||
|
|
||||||
accessor.callRemoveWidget(autoJump);
|
|
||||||
accessor.callAddRenderableWidget(widget);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void onAccessibilityScreenOpened(final AccessibilityOptionsScreen screen) {
|
|
||||||
walkChildren(screen.children(), it -> {
|
|
||||||
getOptionButton(it, Option.TOGGLE_SPRINT).ifPresent(button -> button.active = false);
|
|
||||||
getOptionButton(it, Option.TOGGLE_CROUCH).ifPresent(button -> button.active = false);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void walkChildren(final List<? extends GuiEventListener> elements, final Consumer<GuiEventListener> callback) {
|
|
||||||
for (final GuiEventListener element : elements) {
|
|
||||||
callback.accept(element);
|
|
||||||
|
|
||||||
if (element instanceof final ContainerEventHandler container) {
|
|
||||||
walkChildren(container.children(), callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Optional<CycleButton<?>> getOptionButton(final GuiEventListener element, final Option option) {
|
|
||||||
if (element instanceof final CycleButton<?> button && ((AccessOptionFields)option).getCaption().equals(((AccessCycleButtonFields)button).getName())) {
|
|
||||||
return Optional.of(button);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +1,11 @@
|
|||||||
package chylex.bettercontrols.input;
|
package chylex.bettercontrols.input;
|
||||||
import com.mojang.blaze3d.platform.InputConstants.Type;
|
import com.mojang.blaze3d.platform.InputConstants.Type;
|
||||||
import net.minecraft.client.KeyMapping;
|
import net.minecraft.client.KeyMapping;
|
||||||
import net.minecraft.network.chat.Component;
|
|
||||||
import net.minecraft.network.chat.TranslatableComponent;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public class KeyBindingWithModifier extends KeyMapping {
|
public class KeyBindingWithModifier extends KeyMapping {
|
||||||
public static final String CATEGORY = "key.categories.bettercontrols";
|
public static final String CATEGORY = "key.categories.bettercontrols";
|
||||||
|
|
||||||
public static boolean checkCategoryMatches(final Component text) {
|
|
||||||
return text instanceof final TranslatableComponent t && CATEGORY.equals(t.getKey());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private ModifierKey modifier = null;
|
private ModifierKey modifier = null;
|
||||||
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
package chylex.bettercontrols.mixin;
|
|
||||||
import net.minecraft.client.gui.screens.controls.ControlList.CategoryEntry;
|
|
||||||
import net.minecraft.network.chat.Component;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
|
||||||
|
|
||||||
@Mixin(CategoryEntry.class)
|
|
||||||
public interface AccessControlsListCategory {
|
|
||||||
@Accessor
|
|
||||||
Component getName();
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package chylex.bettercontrols.mixin;
|
|
||||||
import net.minecraft.client.KeyMapping;
|
|
||||||
import net.minecraft.client.gui.screens.controls.ControlList.KeyEntry;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
|
||||||
|
|
||||||
@Mixin(KeyEntry.class)
|
|
||||||
public interface AccessControlsListKeyBinding {
|
|
||||||
@Accessor
|
|
||||||
KeyMapping getKey();
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package chylex.bettercontrols.mixin;
|
|
||||||
|
|
||||||
import net.minecraft.client.gui.components.CycleButton;
|
|
||||||
import net.minecraft.network.chat.Component;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
|
||||||
|
|
||||||
@Mixin(CycleButton.class)
|
|
||||||
public interface AccessCycleButtonFields {
|
|
||||||
@Accessor
|
|
||||||
Component getName();
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package chylex.bettercontrols.mixin;
|
|
||||||
|
|
||||||
import net.minecraft.client.Option;
|
|
||||||
import net.minecraft.network.chat.Component;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
|
||||||
|
|
||||||
@Mixin(Option.class)
|
|
||||||
public interface AccessOptionFields {
|
|
||||||
@Accessor
|
|
||||||
Component getCaption();
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package chylex.bettercontrols.mixin;
|
|
||||||
import net.minecraft.client.gui.components.Widget;
|
|
||||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
|
||||||
import net.minecraft.client.gui.narration.NarratableEntry;
|
|
||||||
import net.minecraft.client.gui.screens.Screen;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.gen.Invoker;
|
|
||||||
|
|
||||||
@SuppressWarnings("UnusedReturnValue")
|
|
||||||
@Mixin(Screen.class)
|
|
||||||
public interface AccessScreenButtons {
|
|
||||||
@Invoker
|
|
||||||
<T extends GuiEventListener & Widget & NarratableEntry> T callAddRenderableWidget(T widget);
|
|
||||||
|
|
||||||
@Invoker
|
|
||||||
void callRemoveWidget(GuiEventListener widget);
|
|
||||||
}
|
|
@ -1,18 +1,15 @@
|
|||||||
package chylex.bettercontrols.mixin;
|
package chylex.bettercontrols.mixin;
|
||||||
import chylex.bettercontrols.BetterControlsCommon;
|
import chylex.bettercontrols.BetterControlsCommon;
|
||||||
import chylex.bettercontrols.input.KeyBindingWithModifier;
|
import net.minecraft.client.KeyMapping;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.Options;
|
||||||
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
||||||
import net.minecraft.client.gui.screens.controls.ControlList;
|
import net.minecraft.client.gui.screens.controls.ControlList;
|
||||||
import net.minecraft.client.gui.screens.controls.ControlList.CategoryEntry;
|
|
||||||
import net.minecraft.client.gui.screens.controls.ControlList.Entry;
|
import net.minecraft.client.gui.screens.controls.ControlList.Entry;
|
||||||
import net.minecraft.client.gui.screens.controls.ControlList.KeyEntry;
|
|
||||||
import net.minecraft.client.gui.screens.controls.ControlsScreen;
|
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
||||||
|
|
||||||
@Mixin(ControlList.class)
|
@Mixin(ControlList.class)
|
||||||
public abstract class HookControlsListWidget extends ContainerObjectSelectionList<Entry> {
|
public abstract class HookControlsListWidget extends ContainerObjectSelectionList<Entry> {
|
||||||
@ -20,18 +17,8 @@ public abstract class HookControlsListWidget extends ContainerObjectSelectionLis
|
|||||||
super(client, width, height, top, bottom, itemHeight);
|
super(client, width, height, top, bottom, itemHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject(method = "<init>", at = @At("TAIL"))
|
@Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Options;keyMappings:[Lnet/minecraft/client/KeyMapping;"))
|
||||||
public void init(final ControlsScreen parent, final Minecraft client, final CallbackInfo ci) {
|
private KeyMapping[] excludeOwnKeyBindings(final Options options) {
|
||||||
children().removeIf(it -> {
|
return ArrayUtils.removeElements(options.keyMappings, BetterControlsCommon.getConfig().getAllKeyBindings());
|
||||||
if (it instanceof final CategoryEntry entry && KeyBindingWithModifier.checkCategoryMatches(((AccessControlsListCategory)entry).getName())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (it instanceof final KeyEntry entry && ArrayUtils.contains(BetterControlsCommon.getConfig().getAllKeyBindings(), ((AccessControlsListKeyBinding)entry).getKey())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package chylex.bettercontrols.mixin;
|
||||||
|
|
||||||
|
import chylex.bettercontrols.gui.BetterControlsScreen;
|
||||||
|
import net.minecraft.client.CycleOption;
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.Option;
|
||||||
|
import net.minecraft.client.Options;
|
||||||
|
import net.minecraft.client.gui.components.AbstractWidget;
|
||||||
|
import net.minecraft.client.gui.components.Button;
|
||||||
|
import net.minecraft.client.gui.screens.Screen;
|
||||||
|
import net.minecraft.client.gui.screens.controls.ControlsScreen;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||||
|
|
||||||
|
@Mixin(ControlsScreen.class)
|
||||||
|
public abstract class HookControlsScreen {
|
||||||
|
@Redirect(method = "init", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/CycleOption;createButton(Lnet/minecraft/client/Options;III)Lnet/minecraft/client/gui/components/AbstractWidget;"))
|
||||||
|
private AbstractWidget replaceAutoJumpButton(final CycleOption<?> option, final Options options, final int x, final int y, final int width) {
|
||||||
|
if (option == Option.AUTO_JUMP && !Screen.hasAltDown()) {
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
|
final ControlsScreen screen = (ControlsScreen)(Object)this;
|
||||||
|
return new Button(x, y, width, 20, BetterControlsScreen.TITLE.plainCopy().append("..."), btn -> Minecraft.getInstance().setScreen(new BetterControlsScreen(screen)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return option.createButton(options, x, y, width);
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package chylex.bettercontrols.mixin;
|
|
||||||
import chylex.bettercontrols.gui.ScreenPatcher;
|
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.client.gui.screens.AccessibilityOptionsScreen;
|
|
||||||
import net.minecraft.client.gui.screens.Screen;
|
|
||||||
import net.minecraft.client.gui.screens.controls.ControlsScreen;
|
|
||||||
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(value = Minecraft.class, priority = 100)
|
|
||||||
public abstract class HookOpenScreen {
|
|
||||||
@Inject(method = "setScreen", at = @At("TAIL"))
|
|
||||||
private void openScreen(final Screen ignore, final CallbackInfo ci) {
|
|
||||||
final Screen screen = Minecraft.getInstance().screen;
|
|
||||||
|
|
||||||
if (screen != null && !Screen.hasAltDown()) {
|
|
||||||
if (screen.getClass() == ControlsScreen.class) {
|
|
||||||
ScreenPatcher.onControlsScreenOpened((ControlsScreen)screen);
|
|
||||||
}
|
|
||||||
else if (screen.getClass() == AccessibilityOptionsScreen.class) {
|
|
||||||
ScreenPatcher.onAccessibilityScreenOpened((AccessibilityOptionsScreen)screen);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,23 @@
|
|||||||
|
package chylex.bettercontrols.mixin;
|
||||||
|
|
||||||
|
import net.minecraft.client.CycleOption;
|
||||||
|
import net.minecraft.client.Option;
|
||||||
|
import net.minecraft.client.Options;
|
||||||
|
import net.minecraft.client.gui.components.AbstractWidget;
|
||||||
|
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.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
@Mixin(CycleOption.class)
|
||||||
|
public abstract class HookToggleOptionButtons {
|
||||||
|
@Inject(method = "createButton", at = @At("RETURN"))
|
||||||
|
private void disableToggleOptions(final Options options, final int x, final int y, final int width, final CallbackInfoReturnable<AbstractWidget> ci) {
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
|
final CycleOption<?> me = (CycleOption<?>)(Object)this;
|
||||||
|
|
||||||
|
if (me == Option.TOGGLE_CROUCH || me == Option.TOGGLE_SPRINT) {
|
||||||
|
ci.getReturnValue().active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,22 +6,18 @@
|
|||||||
"client": [
|
"client": [
|
||||||
"AccessCameraFields",
|
"AccessCameraFields",
|
||||||
"AccessClientPlayerFields",
|
"AccessClientPlayerFields",
|
||||||
"AccessControlsListCategory",
|
|
||||||
"AccessControlsListKeyBinding",
|
|
||||||
"AccessCycleButtonFields",
|
|
||||||
"AccessKeyBindingFields",
|
"AccessKeyBindingFields",
|
||||||
"AccessOptionFields",
|
|
||||||
"AccessPlayerFields",
|
"AccessPlayerFields",
|
||||||
"AccessScreenButtons",
|
|
||||||
"AccessStickyKeyBindingStateGetter",
|
"AccessStickyKeyBindingStateGetter",
|
||||||
"HookClientPlayerFOV",
|
"HookClientPlayerFOV",
|
||||||
"HookClientPlayerInputTick",
|
"HookClientPlayerInputTick",
|
||||||
"HookClientPlayerTick",
|
"HookClientPlayerTick",
|
||||||
"HookControlsListWidget",
|
"HookControlsListWidget",
|
||||||
|
"HookControlsScreen",
|
||||||
"HookLoadGameOptions",
|
"HookLoadGameOptions",
|
||||||
"HookOpenScreen",
|
|
||||||
"HookPlayerFlightSpeed",
|
"HookPlayerFlightSpeed",
|
||||||
"HookStickyKeyBindingState"
|
"HookStickyKeyBindingState",
|
||||||
|
"HookToggleOptionButtons"
|
||||||
],
|
],
|
||||||
"injectors": {
|
"injectors": {
|
||||||
"defaultRequire": 1
|
"defaultRequire": 1
|
||||||
|
Loading…
Reference in New Issue
Block a user