Skip to content

Fix address edit box crash #8370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.simibubi.create.content.logistics;

import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Consumer;

import org.lwjgl.glfw.GLFW;

import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.Create;
import com.simibubi.create.content.trains.schedule.DestinationSuggestions;
import com.simibubi.create.foundation.gui.widget.ScrollInput;
import com.simibubi.create.foundation.utility.CreateLang;
Expand All @@ -25,13 +27,22 @@ public class AddressEditBox extends EditBox {
private DestinationSuggestions destinationSuggestions;
private Consumer<String> mainResponder;
private String prevValue = "=)";
private Field shiftPressedField;

public AddressEditBox(Screen screen, Font pFont, int pX, int pY, int pWidth, int pHeight, boolean anchorToBottom) {
this(screen, pFont, pX, pY, pWidth, pHeight, anchorToBottom, null);
}

public AddressEditBox(Screen screen, Font pFont, int pX, int pY, int pWidth, int pHeight, boolean anchorToBottom, String localAddress) {
super(pFont, pX, pY, pWidth, pHeight, Component.empty());

try {
shiftPressedField = EditBox.class.getDeclaredField("shiftPressed");
shiftPressedField.setAccessible(true);
} catch (Exception e) {
Create.LOGGER.error("Failed to get shiftPressed field from EditBox", e);
}

destinationSuggestions = AddressEditBoxHelper.createSuggestions(screen, this, anchorToBottom, localAddress);
destinationSuggestions.setAllowSuggestions(true);
destinationSuggestions.updateCommandInfo();
Expand Down Expand Up @@ -89,9 +100,33 @@ public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) {
return false;
}

public boolean getShiftPressed() {
try {
return (boolean) shiftPressedField.get(this);
} catch (Exception e) {
Create.LOGGER.error("Failed to get shiftPressed field on EditBox", e);
return false;
}
}
public void setShiftPressed(boolean pressed) {
try {
shiftPressedField.setBoolean(this, pressed);
} catch (Exception e) {
Create.LOGGER.error("Failed to set shiftPressed field on EditBox", e);
}
}

@Override
public void setValue(String text) {
/*
* Ensuring that shiftPressed is false before setting the value is a necessary workaround
* because the vanilla EditBox implementation doesn't account for setValue being called
* via non-key events (e.g. mouse click).
*/
boolean wasShiftPressed = getShiftPressed();
setShiftPressed(false);
super.setValue(text);
setShiftPressed(wasShiftPressed);
}

@Override
Expand Down