Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify untracked should run on server thread #149

Merged
merged 2 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/ThreadUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.anatawa12.fixRtm

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import java.util.concurrent.ConcurrentLinkedQueue

object ThreadUtil {
fun runOnClientThread(block: () -> Unit) {
client.add(block)
}

fun runOnServerThread(block: () -> Unit) {
server.add(block)
}

@SubscribeEvent
fun onClientTick(e: TickEvent.ClientTickEvent) {
while (true) (client.poll() ?: return)()
}

@SubscribeEvent
fun onServerTick(e: TickEvent.ServerTickEvent) {
while (true) (server.poll() ?: return)()
}

private val client = ConcurrentLinkedQueue<() -> Unit>()
private val server = ConcurrentLinkedQueue<() -> Unit>()
}
19 changes: 11 additions & 8 deletions src/main/java/com/anatawa12/fixRtm/network/NotifyUntracked.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.anatawa12.fixRtm.network

import com.anatawa12.fixRtm.Loggers
import com.anatawa12.fixRtm.ThreadUtil
import io.netty.buffer.ByteBuf
import net.minecraft.world.WorldServer
import net.minecraftforge.fml.common.network.simpleimpl.IMessage
Expand Down Expand Up @@ -30,14 +31,16 @@ class NotifyUntracked : IMessage {

companion object : IMessageHandler<NotifyUntracked, Nothing?> {
override fun onMessage(message: NotifyUntracked, ctx: MessageContext): Nothing? {
val entry = ctx.serverHandler.player.world
.let { it as WorldServer }
.entityTracker
.trackedEntityHashTable
.lookup(message.entityId)
?: return null.also { logger.error("entry for #${message.entityId} not found") }

entry.trackingPlayers.remove(ctx.serverHandler.player)
val world = ctx.serverHandler.player.world as WorldServer
ThreadUtil.runOnServerThread {
val entry = world
.entityTracker
.trackedEntityHashTable
.lookup(message.entityId)
?: return@runOnServerThread logger.error("entry for #${message.entityId} not found")

entry.trackingPlayers.remove(ctx.serverHandler.player)
}

return null
}
Expand Down