Skip to content

Commit

Permalink
Fix q button placement and make it a bit more flexible. Fixes #4483 h…
Browse files Browse the repository at this point in the history
…opefully
  • Loading branch information
quat1024 committed Dec 20, 2023
1 parent dd08948 commit fda36a5
Showing 1 changed file with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableSet;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.events.GuiEventListener;
Expand All @@ -10,35 +11,62 @@
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.client.resources.language.I18n;

import org.jetbrains.annotations.Nullable;
import org.violetmoon.quark.base.handler.GeneralConfig;
import org.violetmoon.zeta.client.event.play.ZScreen;
import org.violetmoon.zeta.event.bus.PlayEvent;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class QButtonHandler {

@PlayEvent
public static void onGuiInit(ZScreen.Init.Post event) {
Screen gui = event.getScreen();

if(GeneralConfig.enableQButton && (gui instanceof TitleScreen || gui instanceof PauseScreen)) {
//todo: Player Reporting / Open to Lan occupy the same location depending on if its single or multiplayer
// Temporarily, Im setting it to be next to ReportBugs, but we will need to revisit it.
ImmutableSet<String> targets = GeneralConfig.qButtonOnRight
? ImmutableSet.of(I18n.get("fml.menu.modoptions"), I18n.get("menu.online"))
: ImmutableSet.of(I18n.get("menu.options"), I18n.get("menu.reportBugs"));

List<GuiEventListener> listeners = event.getListenersList();
for(GuiEventListener b : listeners)
if(b instanceof AbstractWidget abs) {
if(targets.contains(abs.getMessage().getString())) {
Button qButton = new QButton(abs.getX() + (GeneralConfig.qButtonOnRight ? 103 : -24), abs.getY());
event.addListener(qButton);
return;
}
if(!GeneralConfig.enableQButton)
return;

Set<String> targetButtonTranslationKeys = getTargetButtons(event.getScreen());
if(targetButtonTranslationKeys == null || targetButtonTranslationKeys.isEmpty())
return;

Set<String> targetButtonNames = targetButtonTranslationKeys.stream()
.map(I18n::get)
.collect(Collectors.toSet());

List<GuiEventListener> listeners = event.getListenersList();
for(GuiEventListener listener : listeners)
if(listener instanceof AbstractWidget widget) {
if(targetButtonNames.contains(widget.getMessage().getString())) {
int x = widget.getX();

if(GeneralConfig.qButtonOnRight)
x += widget.getWidth() + 4; //4px of padding
else
x -= 24; //4px of padding, accounting for the 20px width of the Q button

Button qButton = new QButton(x, widget.getY());
event.addListener(qButton);
return;
}
}
}
}

private static @Nullable Set<String> getTargetButtons(Screen gui) {
if(gui instanceof TitleScreen)
if(GeneralConfig.qButtonOnRight)
return Set.of("menu.online"); // Minecraft Realms
else
return Set.of("fml.menu.mods.title", "fml.menu.mods"); // Mods (idk which one is used)

if(gui instanceof PauseScreen)
if(GeneralConfig.qButtonOnRight)
return Set.of("menu.shareToLan", "menu.playerReporting"); // Open to LAN, Player Reporting
else
return Set.of("menu.options"); // Options...

return null;
}

}

0 comments on commit fda36a5

Please sign in to comment.