Skip to content

Commit

Permalink
Handle exceptions when auto-initialising dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
lucyydotp committed Sep 24, 2023
1 parent 0adfd2c commit d6794f4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
22 changes: 17 additions & 5 deletions api/src/main/kotlin/com/noxcrew/sheeplib/DialogContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.noxcrew.sheeplib
import com.noxcrew.sheeplib.dialog.Dialog
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.update
import net.minecraft.ChatFormatting
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.Renderable
Expand All @@ -11,18 +12,21 @@ import net.minecraft.client.gui.components.events.GuiEventListener
import net.minecraft.client.gui.narration.NarratableEntry
import net.minecraft.client.gui.narration.NarrationElementOutput
import net.minecraft.client.gui.screens.ChatScreen
import net.minecraft.network.chat.Component
import org.slf4j.LoggerFactory
import kotlin.reflect.jvm.jvmName

/**
* The container for all open dialogs.
*/
public object DialogContainer : Renderable, ContainerEventHandler, NarratableEntry {

private val minecraft = Minecraft.getInstance()
private val logger = LoggerFactory.getLogger("SheepLib")

/** The container's children. */
private val children = atomic(listOf<GuiEventListener>())


/** The container's focused child, if any. */
private var focused: Dialog? = null

Expand Down Expand Up @@ -53,13 +57,21 @@ public object DialogContainer : Renderable, ContainerEventHandler, NarratableEnt

/** Adds a dialog to the container and focuses it. */
public operator fun <T> plusAssign(dialog: T) where T : GuiEventListener, T : Renderable {
if (dialog is Dialog) {
try {
dialog.initIfNeeded()
focused = dialog
} catch (ex: Throwable) {
Minecraft.getInstance().gui.chat.addMessage(
Component.translatable("sheeplib.error").withStyle { it.withColor(ChatFormatting.RED) }
)
logger.error("Exception while initialising ${dialog::class.jvmName}:\n" + ex.stackTraceToString())
return
}
}
children.update {
it + dialog
}
if (dialog is Dialog) {
dialog.initIfNeeded()
focused = dialog
}
}

/** Removes a dialog from the container. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SimpleDialog(
builder.check()
title = builder.title
builtLayout = builder.layout

}

/**
Expand Down
4 changes: 3 additions & 1 deletion api/src/main/resources/assets/sheeplib/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"sheeplib.progress.inProgress": "In progress",
"sheeplib.progress.inProgress.count": "In progress (%s/%s)",
"sheeplib.progress.complete": "Complete",
"sheeplib.progress.failed": "Failed"
"sheeplib.progress.failed": "Failed",

"sheeplib.error": "[SheepLib] An error occurred while opening a dialog.\nCheck your game log for more information."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.noxcrew.sheeplib.testmod

import com.noxcrew.sheeplib.dialog.Dialog
import com.noxcrew.sheeplib.theme.DefaultTheme
import com.noxcrew.sheeplib.theme.Themed
import net.minecraft.client.gui.layouts.Layout

/**
* A dialog that throws an exception when built.
* This is a class and not a simple dialog to build lazily.
*/
public class ExceptionDialog(x: Int, y: Int): Dialog(x, y), Themed by DefaultTheme {
override fun layout(): Layout {
error("Test exception from the exception dialog. This is not a bug.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public object SheepLibTestMod : ClientModInitializer {
"example" to ::MyFirstDialog,
"text" to ::textInputDialog,
"progress" to ::ProgressDialog,
"buttons" to ::ButtonCollectionsDialog
"buttons" to ::ButtonCollectionsDialog,
"exception" to ::ExceptionDialog
)


Expand Down

0 comments on commit d6794f4

Please sign in to comment.