Created
March 2, 2019 23:00
-
-
Save JTK222/ad748efcec85336fa4e6696bdc2da7a0 to your computer and use it in GitHub Desktop.
"Simple Networking Implementation/Helper"
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
package net.dark_roleplay.library.networking; | |
import net.dark_roleplay.library.DRPLibrary; | |
import net.minecraftforge.client.model.ModelLoaderRegistry; | |
import net.minecraftforge.fml.network.simple.SimpleChannel; | |
public class NetworkHelper { | |
private static SimpleChannel CURRENT_CHANNEL = null; | |
private static int CURRENT_PACKET_ID = 0; | |
/** | |
* Used to set the SimpleChannel, to which {@link SimplePacket} are registered | |
* when using {@link NetworkHelper#registerPacket(Class, SimplePacket)} | |
* | |
* @param channel | |
*/ | |
public static void initChannel(SimpleChannel channel) { | |
CURRENT_CHANNEL = channel; | |
CURRENT_PACKET_ID = 0; | |
} | |
/** | |
* Clears your SimpleChannel so other mods don't accidently register their Packets to your SimpleChannel | |
*/ | |
public static void clearChannel() { | |
CURRENT_CHANNEL = null; | |
} | |
/** | |
* Warning: | |
* Changing the Order in which your Packets are registered, will also change their IDs. | |
* this may result in crashes on both, Client and Dedicated Server. | |
* Therefore keep in mind that you need to update the protocol version, | |
* of your channel when this is changed. | |
* | |
* @param packetClass the Class of your Packet | |
* @param packet a basic instance, this instance doesn't have | |
*/ | |
public static <T extends SimplePacket<T>> void registerPacket(Class<T> packetClass) { | |
if(CURRENT_CHANNEL == null) { | |
DRPLibrary.LOGGER.error("Tried to register a Packet without initializing a SimpleChannel"); | |
}else { | |
CURRENT_CHANNEL.<T>registerMessage( | |
CURRENT_PACKET_ID++, | |
packetClass, | |
(msg, packetBuffer) -> msg.encode(msg, packetBuffer), | |
(packetBuffer) -> { | |
try { | |
return packetClass.newInstance().decode(packetBuffer); | |
} catch (InstantiationException | IllegalAccessException e) { | |
DRPLibrary.LOGGER.error("Unable to create an instance of the packet class. No Default constructor found", e); | |
return null; | |
} | |
}, | |
(msg, contextSupplier) -> msg.onMessage(msg, contextSupplier) | |
); | |
} | |
} | |
} |
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
package net.dark_roleplay.library.networking; | |
import java.util.function.Supplier; | |
import net.minecraft.network.PacketBuffer; | |
import net.minecraftforge.fml.network.NetworkEvent; | |
/** | |
* It is recommended to register Packets of this Instance using {@link NetworkHelper} | |
* | |
* @param <T> this should be your implementation of this Class | |
*/ | |
public abstract class SimplePacket<T>{ | |
//TODO finish and cleanup JavaDoc | |
/** | |
* Use this to encode your message into a the passed in PacketBuffer | |
* @param message an instance of your packet implementation | |
* @param packet | |
*/ | |
public abstract void encode(T message, PacketBuffer packet); | |
public abstract T decode(PacketBuffer packet); | |
public abstract void onMessage(T message, Supplier<NetworkEvent.Context> context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment