Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Fix bug that makes setting the window title crash on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gbl committed Feb 21, 2018
1 parent 7c856d2 commit 0ce5eed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class DurabilityViewer
@Mod.Instance("durabilityviewer")
public static DurabilityViewer instance;
private static ConfigurationHandler confHandler;
private static String changedWindowTitle;

@Mod.EventHandler
public void preInit(final FMLPreInitializationEvent event) {
Expand All @@ -41,6 +42,7 @@ public void preInit(final FMLPreInitializationEvent event) {

@Mod.EventHandler
public void init(final FMLInitializationEvent event) {
changedWindowTitle=null;
KeyHandler.init();
System.out.println("on Init, confHandler is "+confHandler);
MinecraftForge.EVENT_BUS.register(this);
Expand All @@ -59,7 +61,7 @@ public void onConnectedToServerEvent(FMLNetworkEvent.ClientConnectedToServerEven
String serverName = (event.isLocal() ? "local game" : mc.getCurrentServerData().serverName);
if (serverName==null)
serverName="unknown server";
Display.setTitle(mc.getSession().getUsername() + " on "+serverName);
changedWindowTitle=mc.getSession().getUsername() + " on "+serverName;
}

@SideOnly(Side.CLIENT)
Expand All @@ -68,7 +70,18 @@ public void onDisconnectFromServerEvent(FMLNetworkEvent.ClientDisconnectionFromS
if (!ConfigurationHandler.showPlayerServerName())
return;
Minecraft mc=Minecraft.getMinecraft();
Display.setTitle(mc.getSession().getUsername() + " not connected");
changedWindowTitle=mc.getSession().getUsername() + " not connected";
}

// On windows, Display.setTitle crashes if we call it from
// (Dis)connectFromServerEvent. This is because these run on the netty
// thread, set a global lock, send a WM_SETTEXT, and need the main thread
// to process that WM_SETTEXT - but the main thread needs the global lock
// as well. As a workaround, we just set a global variable here, and retrieve
// it from within onRender from GuiItemDurability.
public static String getAndResetChangedWindowTitle() {
String result=changedWindowTitle;
changedWindowTitle=null;
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.guntram.mcmod.durabilityviewer.client.gui;

import com.google.common.collect.Ordering;
import de.guntram.mcmod.durabilityviewer.DurabilityViewer;
import de.guntram.mcmod.durabilityviewer.handler.ConfigurationHandler;
import de.guntram.mcmod.durabilityviewer.itemindicator.InventorySlotsIndicator;
import de.guntram.mcmod.durabilityviewer.itemindicator.ItemIndicator;
Expand All @@ -26,6 +27,7 @@
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import org.lwjgl.opengl.Display;

public class GuiItemDurability extends Gui
{
Expand Down Expand Up @@ -100,6 +102,13 @@ private class RenderSize {

@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRender(final RenderGameOverlayEvent.Post event) {

// This needs to be done before everything else to make sure
// the title change that occurs when logging off gets through.
String newTitle=DurabilityViewer.getAndResetChangedWindowTitle();
if (newTitle!=null)
Display.setTitle(newTitle);


if (!visible
|| event.isCanceled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static String getConfigFileName() {

@SubscribeEvent
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {
// System.out.println("OnConfigChanged for "+event.getModID());
if (event.getModID().equalsIgnoreCase("durabilityviewer")) {
loadConfig();
}
Expand All @@ -55,7 +54,7 @@ private void loadConfig() {
effectDuration=config.getBoolean("Effect Duration", Configuration.CATEGORY_CLIENT, true, "Show effect durations");
minPercent = config.getInt("Minimum Percent", Configuration.CATEGORY_CLIENT, minPercent, 1, 100, "Play sound when durability below X percent");
minDurability = config.getInt("Minimum Durability", Configuration.CATEGORY_CLIENT, minDurability, 1, 1500, "Play sound when durability below X");
showPlayerServerName = config.getBoolean("Set window title", Configuration.CATEGORY_CLIENT, false, "Set window title to player and server name (warning - this seems to lock up MC on some windows machines)");
showPlayerServerName = config.getBoolean("Set window title", Configuration.CATEGORY_CLIENT, true, "Set window title to player and server name");
// useCustomSound = config.getBoolean("Use custom sound", Configuration.CATEGORY_CLIENT, false, "Use your own warning sound. You need to create your own custom.ogg in the mod folder");

tooltipColor=TextFormatting.fromColorIndex(color);
Expand All @@ -75,7 +74,6 @@ public static Configuration getConfig() {
return getInstance().config;
}


public static boolean showEffectDuration() {
return getInstance().effectDuration;
}
Expand Down

0 comments on commit 0ce5eed

Please sign in to comment.