Skip to content

Commit

Permalink
Fix UI close crash (CleanroomMC#43)
Browse files Browse the repository at this point in the history
(cherry picked from commit 35104c3)
  • Loading branch information
serenibyss authored and miozune committed Jan 31, 2024
1 parent 278a22f commit 733cedd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ public void onOpenScreen(GuiOpenEvent event) {
// queue the screen to open it on next tick
if (event.gui != null) {
ClientGUI.open(event.gui);
event.setCanceled(true);
} else {
ClientGUI.close();
// clear any queued-for-opening screen if needed otherwise, just
// in case someone tries to close the screen before we actually
// open it. If that clears the queue, then we cancel the event.
if (GuiManager.resetState()) {
event.setCanceled(true);
}
}
event.setCanceled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void open(@NotNull GuiScreen screen) {
}

/**
* Closes any GUI that is open in the next client tick.
* Closes any GUI that is open in this tick.
*/
public static void close() {
GuiManager.closeScreen();
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/com/cleanroommc/modularui/factory/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.PacketBuffer;

import org.jetbrains.annotations.ApiStatus;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
Expand All @@ -31,7 +33,6 @@ public class GuiManager {
private static ModularScreen queuedClientScreen;
private static NEISettingsImpl queuedNEISettings;
private static GuiScreen queuedGuiScreen;
private static boolean closeScreen;
private static boolean openingQueue = false;

public static void registerFactory(UIFactory<?> factory) {
Expand Down Expand Up @@ -96,13 +97,8 @@ public static void checkQueuedScreen() {
Minecraft.getMinecraft().displayGuiScreen(screenWrapper);
} else if (queuedGuiScreen != null) {
Minecraft.getMinecraft().displayGuiScreen(queuedGuiScreen);
} else if (closeScreen) {
Minecraft.getMinecraft().displayGuiScreen(null);
}
queuedClientScreen = null;
queuedNEISettings = null;
queuedGuiScreen = null;
closeScreen = false;
resetState();
openingQueue = false;
}

Expand All @@ -111,23 +107,31 @@ static void openScreen(ModularScreen screen, NEISettingsImpl neiSettings) {
queuedClientScreen = screen;
queuedNEISettings = neiSettings;
queuedGuiScreen = null;
closeScreen = false;
}

@SideOnly(Side.CLIENT)
static void openScreen(GuiScreen screen) {
queuedClientScreen = null;
queuedNEISettings = null;
queuedGuiScreen = screen;
closeScreen = false;
}

@SideOnly(Side.CLIENT)
static void closeScreen() {
queuedClientScreen = null;
queuedNEISettings = null;
queuedGuiScreen = null;
closeScreen = true;
Minecraft.getMinecraft().displayGuiScreen(null);
resetState();
}

@ApiStatus.Internal
@SideOnly(Side.CLIENT)
public static boolean resetState() {
if (queuedClientScreen != null || queuedGuiScreen != null) {
queuedClientScreen = null;
queuedNEISettings = null;
queuedGuiScreen = null;
return true;
}
return false;
}

public static boolean isOpeningQueue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.cleanroommc.modularui.api.UIFactory;
import com.cleanroommc.modularui.network.IPacket;
import com.cleanroommc.modularui.network.NetworkUtils;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.client.Minecraft;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.network.PacketBuffer;
Expand Down Expand Up @@ -41,6 +45,7 @@ public void read(PacketBuffer buf) {
this.data = NetworkUtils.readPacketBuffer(buf);
}

@SideOnly(Side.CLIENT)
@Override
public @Nullable IPacket executeClient(NetHandlerPlayClient handler) {
GuiManager.open(this.windowId, this.factory, this.data, Minecraft.getMinecraft().thePlayer);
Expand Down

0 comments on commit 733cedd

Please sign in to comment.