Jump to content

[1.16.5] Adding a Button to KeyBindings


Skyriis

Recommended Posts

Event Handler

    private static KeyBindingListReplacement replacement;

    @SubscribeEvent
    public static void onOpenGui(final GuiScreenEvent.InitGuiEvent.Post event) {
        if (!(event.getGui() instanceof ControlsScreen)) return;
        ControlsScreen controlsScreen = (ControlsScreen) event.getGui();
        replacement = new KeyBindingListReplacement(controlsScreen, event.getGui().getMinecraft());
        ObfuscationReflectionHelper.setPrivateValue(ControlsScreen.class, controlsScreen, replacement, "field_146494_r");
    }

    @SubscribeEvent
    public static void onScroll(final GuiScreenEvent.MouseScrollEvent.Pre event) {
        if (!(event.getGui() instanceof ControlsScreen)) return;
        replacement.mouseScrolled(event.getMouseX(), event.getMouseY(), event.getScrollDelta());
    }

 

Replacement:

@OnlyIn(Dist.CLIENT)
public class KeyBindingListReplacement extends KeyBindingList {
    private final ControlsScreen controlsScreen;
    private int maxListLabelWidth;

    public KeyBindingListReplacement(ControlsScreen controls, Minecraft mcIn) {
        super(controls, mcIn);
        this.controlsScreen = controls;
        this.clearEntries();

        KeyBinding[] akeybinding = ArrayUtils.clone(mcIn.gameSettings.keyBindings);
        Arrays.sort(akeybinding);
        String lastCategory = null;

        for (KeyBinding keybinding : akeybinding) {
            String keyCategory = keybinding.getKeyCategory();
            if (!keyCategory.equals(lastCategory)) {
                lastCategory = keyCategory;
                this.addEntry(new KeyBindingList.CategoryEntry(new TranslationTextComponent(keyCategory)));
            }

            ITextComponent itextcomponent = new TranslationTextComponent(keybinding.getKeyDescription());
            int i = mcIn.fontRenderer.getStringPropertyWidth(itextcomponent);
            if (i > this.maxListLabelWidth) {
                this.maxListLabelWidth = i;
            }

            this.addEntry(new KeyEntry(keybinding, itextcomponent));
        }
    }

 

Link to comment
Share on other sites

That worked.

 

Here is my solution

@SubscribeEvent
    public static void onOpenGui(final GuiScreenEvent.InitGuiEvent.Post event) {
        if (!(event.getGui() instanceof ControlsScreen)) return;

        final ControlsScreen controlsScreen = (ControlsScreen) event.getGui();
        final KeyBindingList replacement = new KeyBindingListReplacement(controlsScreen, event.getGui().getMinecraft());
        final KeyBindingList old = ObfuscationReflectionHelper.getPrivateValue(ControlsScreen.class, controlsScreen, "field_146494_r");

        controlsScreen.getEventListeners().remove(old);

        ObfuscationReflectionHelper.setPrivateValue(ControlsScreen.class, controlsScreen, replacement, "field_146494_r");

        try {
            Method addChildMethod = ObfuscationReflectionHelper.findMethod(Screen.class, "func_230481_d_", IGuiEventListener.class);
            addChildMethod.setAccessible(true);
            addChildMethod.invoke(controlsScreen, replacement);
            addChildMethod.setAccessible(false);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

 

Link to comment
Share on other sites

2 hours ago, diesieben07 said:

That is not how you should handle exceptions

I'll work on that :)

2 hours ago, diesieben07 said:

And there is no reason to call setAccessible(true) when using ObfuscationReflectionHelper.

And callign setAccessible(false) when you never use the Method again afterwards also doesn't do anything

Thanks. I'll remove those lines.

Link to comment
Share on other sites

Now the added Button (btnSyncToCloud) isn't clickable but hoverable.

All other buttons are clickable and work how the should.

 

Changed KeyBindingListReplacement.KeyEntry.class

@OnlyIn(Dist.CLIENT)
    public class KeyEntryReplacement extends KeyBindingList.Entry {
        /**
         * The keybinding specified for this KeyEntry
         */
        private final KeyBinding keybinding;
        /**
         * The localized key description for this KeyEntry
         */
        private final ITextComponent keyDesc;
        private final Button btnChangeKeyBinding;
        private final Button btnReset;
        private final IconButton btnSyncToCloud;

        private KeyEntryReplacement(final KeyBinding keyBinding, final ITextComponent description) {
            this.keybinding = keyBinding;
            this.keyDesc = description;

            this.btnChangeKeyBinding = new Button(0, 0, 75 + 20, 20, description, (p_214386_2_) -> KeyBindingListReplacement.this.controlsScreen.buttonId = keyBinding) {
                protected IFormattableTextComponent getNarrationMessage() {
                    return keyBinding.isInvalid() ? new TranslationTextComponent("narrator.controls.unbound", description) : new TranslationTextComponent("narrator.controls.bound", description, super.getNarrationMessage());
                }
            };

            this.btnReset = new Button(0, 0, 50, 20, new TranslationTextComponent("controls.reset"), (p_214387_2_) -> {
                keybinding.setToDefault();
                KeyBindingListReplacement.this.minecraft.gameSettings.setKeyBindingCode(keyBinding, keyBinding.getDefault());
                KeyBinding.resetKeyBindingArrayAndHash();
            }) {
                protected IFormattableTextComponent getNarrationMessage() {
                    return new TranslationTextComponent("narrator.controls.reset", description);
                }
            };
            
            this.btnSyncToCloud = new IconButton("cloud", (button) -> SyncHandler.addToSyncList(this.keybinding)) {
                @Override
                protected IFormattableTextComponent getNarrationMessage() {
                    return new TranslationTextComponent("belovedkeybindings.buttons.upload_to_cloud", description);
                }
            };
        }

        @Override
        public void render(MatrixStack matrixStack, int p_230432_2_, int renderPosY, int renderPosX, int p_230432_5_, int p_230432_6_, int mouseX, int mouseY, boolean p_230432_9_, float particalTicks) {...}

        public List<? extends IGuiEventListener> getEventListeners() {
            return ImmutableList.of(this.btnChangeKeyBinding, this.btnReset, this.btnSyncToCloud);
        }

        @Override
        public boolean mouseClicked(double mouseX, double mouseY, int button) {
            return this.btnChangeKeyBinding.mouseClicked(mouseX, mouseY, button) || this.btnReset.mouseClicked(mouseX, mouseY, button) || this.btnSyncToCloud.mouseClicked(mouseX, mouseY, button);
        }

        @Override
        public boolean mouseReleased(double mouseX, double mouseY, int button) {
            return this.btnChangeKeyBinding.mouseReleased(mouseX, mouseY, button) || this.btnReset.mouseReleased(mouseX, mouseY, button) || this.btnSyncToCloud.mouseReleased(mouseX, mouseY, button);
        }
    }

 

IconButton.class

@OnlyIn(Dist.CLIENT)
public class IconButton extends ImageButton {
    private final ResourceLocation resourceLocation;
    private final int yOffset = 128;

    public IconButton(String iconName, IPressable onPressIn) {
        super(0, 0, 20, 20, 0, 0, 128, new ResourceLocation(BelovedKeybindings.MODID, "textures/icons/" + iconName + ".png"), onPressIn);
        this.resourceLocation = new ResourceLocation(BelovedKeybindings.MODID, "textures/icons/" + iconName + ".png");
    }

    @Override
    public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        Minecraft minecraft = Minecraft.getInstance();
        minecraft.getTextureManager().bindTexture(this.resourceLocation);
        int i = 0;
        if (this.isMouseOver(mouseX, mouseY)) {
            i += yOffset;
        }

        RenderSystem.disableDepthTest();
        blit(matrixStack, this.x, this.y, 16, 16, 0, i, 128, 128, 256, 256);
        RenderSystem.enableDepthTest();

        if (this.isMouseOver(mouseX, mouseY)) {
            this.renderToolTip(matrixStack, mouseX, mouseY);
        }
    }

    @Override
    public void renderToolTip(MatrixStack matrixStack, int mouseX, int mouseY) {
        super.renderToolTip(matrixStack, mouseX, mouseY);
        Minecraft.getInstance().fontRenderer.drawStringWithShadow(matrixStack, getNarrationMessage().getString(), mouseX + 8, mouseY, Color.WHITE.getRGB());
    }
}

 

Any ideas how to fix that?

Edited by Skyriis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.