Skip to content

Commit

Permalink
Create NetworkUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Desoroxxx committed Jul 31, 2023
1 parent 313a64b commit b5a8f8d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ You now have `read` and `write` methods for all Vectors from Red Core, it simply

### Added

- **NetworkUtil:** Designed to streamline network coding practices, this utility makes writing cleaner, safer, and more efficient networking code effortless.
- Added `read` and `write` methods to all vectors that allows for easy networking

### Changed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Red-Core is the foundational framework for Red Studio projects and associated pr
- **Stopwatch:** Tailored for precise profiling, this stopwatch sets itself apart in the burgeoning roster of Java-based stopwatch solutions, by its simplicity and easy of use.
- **RedClientTicker:** A useful ticker that allows you to have ticks every 2, 5 or 10 normal ticks, for things that shouldn't run 20 times a second.
- **OptiNotFine:** A must-have when working with OptiFine compatibility, it allows you to know if OptiFine is installed, whether shaders are loaded, and to force fast render off.
- **NetworkUtil:** Designed to streamline network coding practices, this utility makes writing cleaner, safer, and more efficient networking code effortless.

## Why Red-Core?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.net.URI;

/**
* This class defines constants for Red Core.
* <p>
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/io/redstudioragnarok/redcore/utils/NetworkUtil.java
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);
}
}

0 comments on commit b5a8f8d

Please sign in to comment.