Skyriis Posted February 24, 2021 Posted February 24, 2021 (edited) Hi Guys, i'm trying to add a Button on the left of each keybinding in the Controls Screen. Is there a way to do that without replacing the whole Screen? Edited February 28, 2021 by Skyriis Quote
Skyriis Posted February 25, 2021 Author Posted February 25, 2021 I tried the InitGuiEvent but the KeyBindingList is no Widget and no Screen so i can't modify it there. Can I? Is there a way to modify the KeyBindingList? Quote
Skyriis Posted February 25, 2021 Author Posted February 25, 2021 (edited) I was able to replace the keybindinglist with my own list but now the list isn't scollable anymore. I fixed that by using the GuiScreenEvent.MouseScrollEvent. Is there a better way of doing that? Edited February 25, 2021 by Skyriis Quote
Skyriis Posted February 25, 2021 Author Posted February 25, 2021 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)); } } Quote
Skyriis Posted February 25, 2021 Author Posted February 25, 2021 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(); } } Quote
Skyriis Posted February 26, 2021 Author Posted February 26, 2021 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. Quote
Skyriis Posted February 28, 2021 Author Posted February 28, 2021 (edited) 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 February 28, 2021 by Skyriis Quote
Skyriis Posted February 28, 2021 Author Posted February 28, 2021 I more or less found the problem. There is a event listender on the left side of the screen which was blocking the button. I just moved the button to another place because i could not find the listender. Now everything works like it should. Quote
Recommended Posts
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.