Added HelloServer handler
This commit is contained in:
parent
2260d547dc
commit
b74f680191
4 changed files with 85 additions and 43 deletions
|
@ -7,7 +7,7 @@ import net.minecraft.resources.ResourceLocation;
|
|||
import ru.bclib.api.TagAPI;
|
||||
import ru.bclib.api.WorldDataAPI;
|
||||
import ru.bclib.api.dataexchange.DataExchangeAPI;
|
||||
import ru.bclib.api.dataexchange.TestHandler;
|
||||
import ru.bclib.api.dataexchange.handler.HelloServer;
|
||||
import ru.bclib.config.Configs;
|
||||
import ru.bclib.recipes.CraftingRecipes;
|
||||
import ru.bclib.registry.BaseBlockEntities;
|
||||
|
@ -28,7 +28,7 @@ public class BCLib implements ModInitializer {
|
|||
CraftingRecipes.init();
|
||||
WorldDataAPI.registerModCache(MOD_ID);
|
||||
Configs.save();
|
||||
DataExchangeAPI.registerDescriptor(TestHandler.DESCRIPTOR);
|
||||
DataExchangeAPI.registerDescriptor(HelloServer.DESCRIPTOR);
|
||||
}
|
||||
|
||||
public static boolean isDevEnvironment() {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
package ru.bclib.api.dataexchange;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import ru.bclib.BCLib;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class TestHandler extends DataHandler{
|
||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "__test"), TestHandler::new, true);
|
||||
|
||||
public TestHandler() {
|
||||
super(DESCRIPTOR.IDENTIFIER, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deserializeFromIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean fromClient) {
|
||||
int length = buf.readInt();
|
||||
CharSequence text = buf.readCharSequence(length, StandardCharsets.UTF_8);
|
||||
BCLib.LOGGER.info("PROCESSING INCOMING TEST-DATA fromClient="+fromClient+": "+text);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Environment(EnvType.CLIENT)
|
||||
protected void runOnClient(Minecraft client) {
|
||||
BCLib.LOGGER.info("RUNNING INCOMING TEST-DATA ON CLIENT");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void serializeData(FriendlyByteBuf buf) {
|
||||
CharSequence text = "Welcome from BCLib";
|
||||
buf.writeInt(text.length());
|
||||
buf.writeCharSequence(text, StandardCharsets.UTF_8);
|
||||
BCLib.LOGGER.info("BUILDING OUTGOING TEST-DATA ON SERVER");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package ru.bclib.api.dataexchange.handler;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.fabricmc.loader.api.ModContainer;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import ru.bclib.BCLib;
|
||||
import ru.bclib.api.dataexchange.DataHandler;
|
||||
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
|
||||
import ru.bclib.api.datafixer.DataFixerAPI;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class HelloServer extends DataHandler {
|
||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "hello_server"), HelloServer::new, true);
|
||||
|
||||
public HelloServer() {
|
||||
super(DESCRIPTOR.IDENTIFIER, false);
|
||||
}
|
||||
|
||||
public static String getModVersion(String modID){
|
||||
Optional<ModContainer> optional = FabricLoader.getInstance().getModContainer(modID);
|
||||
if (optional.isPresent()) {
|
||||
ModContainer modContainer = optional.get();
|
||||
return modContainer.getMetadata().getVersion().toString();
|
||||
}
|
||||
return "0.0.0";
|
||||
}
|
||||
|
||||
protected static String getBCLibVersion(){
|
||||
return getModVersion(BCLib.MOD_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deserializeFromIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean fromClient) {
|
||||
String bclibVersion = DataFixerAPI.getModVersion(buf.readInt());
|
||||
String localBclibVersion = getBCLibVersion();
|
||||
|
||||
BCLib.LOGGER.info("Hello Server received from BCLib. (server="+localBclibVersion+", client="+bclibVersion+")");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Environment(EnvType.CLIENT)
|
||||
protected void runOnClient(Minecraft client) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void serializeData(FriendlyByteBuf buf) {
|
||||
buf.writeInt(DataFixerAPI.getModVersion(getBCLibVersion()));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue