-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/io/redstudioragnarok/redcore/utils/NetworkUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.redstudioragnarok.redcore.utils; | ||
|
||
import net.minecraft.util.IThreadListener; | ||
import net.minecraftforge.fml.common.FMLCommonHandler; | ||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; | ||
|
||
/** | ||
* A utility class for handling network related tasks. | ||
* <p> | ||
* This class contains methods for dealing with network messages in a multithreaded environment. Its primary method, | ||
* {@link #processMessage(MessageContext, Runnable)}, allows processing of network messages in a thread-safe manner, | ||
* ensuring the operations are performed on the main thread. | ||
* <p> | ||
* This class is designed to be thread-safe and its methods can be invoked from any thread without external synchronization. | ||
* | ||
* @author Desoroxxx | ||
* @since 0.4 | ||
*/ | ||
public final class NetworkUtil { | ||
|
||
/** | ||
* Processes a message from a given context using a provided runnable. | ||
* <p> | ||
* This method is thread-safe; it checks if the calling thread is the main thread. | ||
* If so, it immediately runs the runnable. | ||
* If not, it schedules the runnable to be run on the main thread. | ||
* | ||
* @param messageContext The context of the message to be processed. | ||
* @param runnable The task to be run on the Minecraft main thread. | ||
*/ | ||
public static void processMessage(final MessageContext messageContext, final Runnable runnable) { | ||
final IThreadListener thread = FMLCommonHandler.instance().getWorldThread(messageContext.netHandler); | ||
|
||
if (thread.isCallingFromMinecraftThread()) | ||
runnable.run(); | ||
else | ||
thread.addScheduledTask(runnable); | ||
} | ||
} |