Skip to content

Commit

Permalink
fix: don't listen to ClientPlayerNetworkEvent on server
Browse files Browse the repository at this point in the history
  • Loading branch information
SettingDust committed Nov 23, 2024
1 parent 5237433 commit 7fbda9b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/settingdust/item_converter/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.api.distmarker.OnlyIn
import net.minecraftforge.fml.loading.FMLPaths
import kotlin.io.path.createFile
import kotlin.io.path.div
Expand All @@ -17,6 +19,7 @@ internal val json = Json {
prettyPrint = true
}

@OnlyIn(Dist.CLIENT)
@Serializable
data class ClientConfig(val pressTicks: Int = 20) {
companion object {
Expand Down
19 changes: 3 additions & 16 deletions src/main/kotlin/settingdust/item_converter/Entrypoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceKey
import net.minecraft.resources.ResourceLocation
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.client.event.ClientPlayerNetworkEvent
import net.minecraftforge.event.RegisterCommandsEvent
import net.minecraftforge.event.server.ServerStartingEvent
import net.minecraftforge.event.server.ServerStoppingEvent
Expand All @@ -23,7 +22,7 @@ import net.minecraftforge.fml.loading.FMLPaths
import org.apache.commons.lang3.math.Fraction
import org.apache.logging.log4j.LogManager
import org.jgrapht.graph.SimpleDirectedWeightedGraph
import settingdust.item_converter.client.SlotInteractManager
import settingdust.item_converter.client.ItemConverterClient
import settingdust.item_converter.networking.Networking
import thedarkcolour.kotlinforforge.forge.FORGE_BUS
import thedarkcolour.kotlinforforge.forge.MOD_BUS
Expand All @@ -46,12 +45,7 @@ object ItemConverter {
MOD_BUS.register(ModEventHandler)
FORGE_BUS.register(this)
Networking
if (FMLEnvironment.dist == Dist.CLIENT) clientInit()
}

fun clientInit() {
SlotInteractManager
ClientConfig.reload()
if (FMLEnvironment.dist == Dist.CLIENT) ItemConverterClient
}

fun id(path: String) = ResourceLocation(ID, path)
Expand Down Expand Up @@ -107,14 +101,7 @@ object ItemConverter {
serverCoroutineScope = null
}


@SubscribeEvent
fun onClientPlayerNetwork(event: ClientPlayerNetworkEvent.LoggingIn) {
refreshGraph(event.player.level.registryAccess())
}


private fun refreshGraph(registryAccess: RegistryAccess) {
fun refreshGraph(registryAccess: RegistryAccess) {
ConvertRules.graph = SimpleDirectedWeightedGraph<SimpleItemPredicate, FractionUnweightedEdge>(null) {
FractionUnweightedEdge(Fraction.ZERO)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package settingdust.item_converter.client

import net.minecraftforge.client.event.ClientPlayerNetworkEvent
import net.minecraftforge.eventbus.api.SubscribeEvent
import settingdust.item_converter.ClientConfig
import settingdust.item_converter.ItemConverter.refreshGraph
import thedarkcolour.kotlinforforge.forge.FORGE_BUS

object ItemConverterClient {
init {
SlotInteractManager
ClientConfig.Companion.reload()
FORGE_BUS.register(this)
}

@SubscribeEvent
fun onClientPlayerNetwork(event: ClientPlayerNetworkEvent.LoggingIn) {
refreshGraph(event.player.level.registryAccess())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import net.minecraft.sounds.SoundSource
import net.minecraft.world.inventory.Slot
import net.minecraft.world.item.BlockItem
import net.minecraft.world.item.ItemStack
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.api.distmarker.OnlyIn
import net.minecraftforge.client.ForgeHooksClient
import net.minecraftforge.client.event.ScreenEvent
import net.minecraftforge.common.MinecraftForge
Expand All @@ -27,6 +29,7 @@ import settingdust.item_converter.networking.C2SConvertItemPacket
import settingdust.item_converter.networking.C2SConvertItemPacket.Mode
import settingdust.item_converter.networking.Networking

@OnlyIn(Dist.CLIENT)
data class ItemConvertScreen(
val parent: Screen?, val slot: Slot
) : Screen(Component.translatable("gui.${ItemConverter.ID}.item_convert")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import net.minecraft.client.gui.GuiComponent
import net.minecraft.client.gui.components.Widget
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen
import net.minecraft.resources.ResourceLocation
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.api.distmarker.OnlyIn
import net.minecraftforge.client.event.InputEvent
import net.minecraftforge.client.event.RegisterGuiOverlaysEvent
import net.minecraftforge.client.event.RegisterKeyMappingsEvent
Expand All @@ -23,6 +25,7 @@ import settingdust.item_converter.networking.Networking
import thedarkcolour.kotlinforforge.forge.FORGE_BUS
import thedarkcolour.kotlinforforge.forge.MOD_BUS

@OnlyIn(Dist.CLIENT)
object SlotInteractManager {
var pressedTicks = 0
var converting = false
Expand Down

0 comments on commit 7fbda9b

Please sign in to comment.