From 9a72ceeeb4cfca42a7af9ce154e3fd6f49881bb1 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 14 May 2022 16:51:10 +0700 Subject: [PATCH] Fail fast on attempt to load drives in illegal state --- .../ru/dbotthepony/mc/otm/block/entity/Cables.kt | 9 +++++++-- .../dbotthepony/mc/otm/capability/drive/DrivePool.kt | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt index 4eca753bb..1610f95e3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt @@ -9,6 +9,7 @@ import net.minecraft.world.level.block.entity.BlockEntity import net.minecraft.world.level.block.state.BlockState import net.minecraftforge.common.capabilities.Capability import net.minecraftforge.common.util.LazyOptional +import ru.dbotthepony.mc.otm.SERVER_IS_DYING import ru.dbotthepony.mc.otm.block.CableBlock import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.graph.Graph6Node @@ -54,12 +55,16 @@ class MatterCableBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : override fun onNeighbour(node: Graph6Node<*>, direction: Direction) { val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[direction.ordinal], true) - if (newState !== blockState) level!!.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + + if (newState !== blockState && !SERVER_IS_DYING) + level!!.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) } override fun onUnNeighbour(node: Graph6Node<*>, direction: Direction) { val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[direction.ordinal], false) - if (newState !== blockState) level!!.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + + if (newState !== blockState && !SERVER_IS_DYING) + level!!.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) } override fun setRemoved() { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/DrivePool.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/DrivePool.kt index c0ec28b12..1c6066356 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/DrivePool.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/DrivePool.kt @@ -17,6 +17,7 @@ import net.minecraftforge.event.server.ServerAboutToStartEvent import net.minecraftforge.event.server.ServerStoppingEvent import net.minecraftforge.event.world.WorldEvent import org.apache.logging.log4j.LogManager +import ru.dbotthepony.mc.otm.SERVER_IS_DYING import java.io.File import java.lang.ref.WeakReference import java.util.ArrayList @@ -53,6 +54,9 @@ object DrivePool { if (!isLegalAccess()) throw IllegalAccessException("Can not access drive pool from outside of server thread.") + if (SERVER_IS_DYING) + throw IllegalStateException("Fail-fast: Server is shutting down") + val get = pool[id] if (get != null) { @@ -88,6 +92,9 @@ object DrivePool { if (!isLegalAccess()) throw IllegalAccessException("Can not access drive pool from outside of server thread.") + if (SERVER_IS_DYING) + throw IllegalStateException("Fail-fast: Server is shutting down") + pool[id] = WeakDriveReference(drive) } @@ -96,6 +103,9 @@ object DrivePool { if (!isLegalAccess()) throw IllegalAccessException("Can not access drive pool from outside of server thread.") + if (SERVER_IS_DYING) + throw IllegalStateException("Fail-fast: Server is shutting down") + pool[id]?.access() }