Added HelloServer handler

This commit is contained in:
Frank Bauer 2021-07-29 10:57:58 +02:00
parent 2260d547dc
commit b74f680191
4 changed files with 85 additions and 43 deletions

View file

@ -4,6 +4,7 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.FriendlyByteBuf;
import java.util.HashSet;
import java.util.Set;
@ -19,7 +20,7 @@ public class DataExchangeAPI {
descriptors = new HashSet<>();
}
public static DataExchangeAPI getInstance(){
static DataExchangeAPI getInstance(){
if (instance==null){
instance = new DataExchangeAPI();
}
@ -45,12 +46,21 @@ public class DataExchangeAPI {
ServerPlayConnectionEvents.DISCONNECT.register(server::onPlayDisconnect);
}
/**
* Add a new Descriptor for a DataHandler.
* @param desc The Descriptor you want to add.
*/
public static void registerDescriptor(DataHandlerDescriptor desc){
DataExchangeAPI api = DataExchangeAPI.getInstance();
api.descriptors.add(desc);
}
/**
* Initializes all datastructures that need to exist in the client component.
* <p>
* This is automatically called by BCLib. You can register {@link DataHandler}-Objects before this Method is called
*/
@Environment(EnvType.CLIENT)
public static void prepareClientside(){
DataExchangeAPI api = DataExchangeAPI.getInstance();
@ -58,11 +68,27 @@ public class DataExchangeAPI {
}
/**
* Initializes all datastructures that need to exist in the server component.
* <p>
* This is automatically called by BCLib. You can register {@link DataHandler}-Objects before this Method is called
*/
public static void prepareServerside(){
DataExchangeAPI api = DataExchangeAPI.getInstance();
api.initServerSide();
}
/**
* Sends the Handler.
* <p>
* Depending on what the result of {@link DataHandler#getOriginatesOnServer()}, the Data is sent from the server
* to the client (if {@code true}) or the other way around.
* <p>
* The method {@link DataHandler#serializeData(FriendlyByteBuf)} is called just before the data is sent. You should
* use this method to add the Data you need to the communication.
* @param h The Data that you want to send
*/
public static void send(DataHandler h){
if (h.getOriginatesOnServer()){
DataExchangeAPI.getInstance().server.sendToClient(h);