Skip to content

Commit da18db7

Browse files
committed
Default to ignoring decoration failures
1 parent 8bc0f34 commit da18db7

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

interfaces/src/main/kotlin/com/noxcrew/interfaces/exception/StandardInterfacesExceptionHandler.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ import net.kyori.adventure.text.Component
55
import org.slf4j.LoggerFactory
66

77
/** The standard implementation that handles an interfaces exception. */
8-
public object StandardInterfacesExceptionHandler : InterfacesExceptionHandler {
8+
public class StandardInterfacesExceptionHandler(
9+
/** Whether decorations are allowed to fail gracefully. */
10+
public val allowDecorationFailure: Boolean = true,
11+
) : InterfacesExceptionHandler {
912
private val logger = LoggerFactory.getLogger(StandardInterfacesExceptionHandler::class.java)
1013

11-
override suspend fun handleException(exception: Exception, context: InterfacesExceptionContext): InterfacesExceptionResolution =
12-
when (context.operation) {
14+
override suspend fun handleException(exception: Exception, context: InterfacesExceptionContext): InterfacesExceptionResolution {
15+
// Optionally gracefully let decorations fail without issue! This defaults to true because we also
16+
// allow items to be clicked without their decorations.
17+
if (allowDecorationFailure && context.operation == InterfacesOperation.DECORATING_ELEMENT) {
18+
logger.warn("Failed to decorate interface elements for ${context.player.name}")
19+
return InterfacesExceptionResolution.IGNORE
20+
}
21+
22+
return when (context.operation) {
1323
InterfacesOperation.BUILDING_PLAYER -> {
1424
if (context.retries < 3) {
1525
// When building a player menu we absolutely do not want to fail as
@@ -19,7 +29,7 @@ public object StandardInterfacesExceptionHandler : InterfacesExceptionHandler {
1929
} else {
2030
// Ignore the drawing but kick the player off the server
2131
logger.error(
22-
"Failed to build player inventory for ${context.player.name} 3 times, kicking player to prevent invalid state"
32+
"Failed to build player inventory for ${context.player.name} 3 times, kicking player to prevent invalid state",
2333
)
2434
InterfacesListeners.INSTANCE.runSync {
2535
context.player.kick(Component.text("Unknown exception occurred while rendering GUI menus"))
@@ -37,4 +47,5 @@ public object StandardInterfacesExceptionHandler : InterfacesExceptionHandler {
3747
InterfacesExceptionResolution.CLOSE
3848
}
3949
}
50+
}
4051
}

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public open class InterfaceProperties<P : Pane> {
7979
public var defaultTimeout: Duration = 2.5.seconds
8080

8181
/** The exception handler to use for this interface. */
82-
public var exceptionHandler: InterfacesExceptionHandler = StandardInterfacesExceptionHandler
82+
public var exceptionHandler: InterfacesExceptionHandler = StandardInterfacesExceptionHandler()
8383

8484
/** Adds a new close handler [closeHandler] that triggers whenever the inventory is closed for any of the given [reasons]. */
8585
public fun addCloseHandler(reasons: Collection<InventoryCloseEvent.Reason> = DEFAULT_REASONS, closeHandler: CloseHandler) {

interfaces/src/main/kotlin/com/noxcrew/interfaces/menu/PlayerInventoryMenu.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.bukkit.entity.Player
1313
/** The base for a menu that is applied on a player's whole inventory. */
1414
public abstract class PlayerInventoryMenu : BaseInventoryMenu {
1515
/** The exception handler to use for this menu. */
16-
public open val exceptionHandler: InterfacesExceptionHandler = StandardInterfacesExceptionHandler
16+
public open val exceptionHandler: InterfacesExceptionHandler = StandardInterfacesExceptionHandler()
1717

1818
/** Configures the GUI for the given [player]. */
1919
protected abstract suspend fun PlayerInterfaceBuilder.configure(player: Player)

interfaces/src/main/kotlin/com/noxcrew/interfaces/menu/SimpleInventoryMenu.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.bukkit.entity.Player
1313
/** The base for a simple built menu. */
1414
public abstract class SimpleInventoryMenu<B : InterfaceBuilder<out Pane, *>> : BaseInventoryMenu {
1515
/** The exception handler to use for this menu. */
16-
public open val exceptionHandler: InterfacesExceptionHandler = StandardInterfacesExceptionHandler
16+
public open val exceptionHandler: InterfacesExceptionHandler = StandardInterfacesExceptionHandler()
1717

1818
/** Builds the interface for this type using [configure]. */
1919
protected abstract suspend fun build(player: Player, configure: suspend B.() -> Unit): Interface<*, *>

0 commit comments

Comments
 (0)