From feee3514a928e20263774722ce623a4d2ab4efd7 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 6 Aug 2021 08:23:23 +0200 Subject: [PATCH] Unpolute public API --- .../ru/bclib/api/dataexchange/Connector.java | 8 +- .../api/dataexchange/ConnectorClientside.java | 20 +-- .../api/dataexchange/ConnectorServerside.java | 20 +-- .../api/dataexchange/DataExchangeAPI.java | 106 ++------------ .../dataexchange/handler/DataExchange.java | 133 ++++++++++++++++++ 5 files changed, 172 insertions(+), 115 deletions(-) create mode 100644 src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java diff --git a/src/main/java/ru/bclib/api/dataexchange/Connector.java b/src/main/java/ru/bclib/api/dataexchange/Connector.java index 55a046a0..6f933489 100644 --- a/src/main/java/ru/bclib/api/dataexchange/Connector.java +++ b/src/main/java/ru/bclib/api/dataexchange/Connector.java @@ -1,16 +1,18 @@ package ru.bclib.api.dataexchange; +import ru.bclib.api.dataexchange.handler.DataExchange; + import java.util.Set; abstract class Connector { - protected final DataExchangeAPI api; + protected final DataExchange api; - Connector(DataExchangeAPI api) { + Connector(DataExchange api) { this.api = api; } public abstract boolean onClient(); protected Set getDescriptors(){ - return api.descriptors; + return api.getDescriptors(); } } diff --git a/src/main/java/ru/bclib/api/dataexchange/ConnectorClientside.java b/src/main/java/ru/bclib/api/dataexchange/ConnectorClientside.java index 2e34765f..3ed6fa1f 100644 --- a/src/main/java/ru/bclib/api/dataexchange/ConnectorClientside.java +++ b/src/main/java/ru/bclib/api/dataexchange/ConnectorClientside.java @@ -8,11 +8,15 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.network.FriendlyByteBuf; import ru.bclib.BCLib; +import ru.bclib.api.dataexchange.handler.DataExchange; +/** + * This is an internal class that handles a Clienetside players Connection to a Server + */ @Environment(EnvType.CLIENT) -class ConnectorClientside extends Connector { +public class ConnectorClientside extends Connector { private Minecraft client; - ConnectorClientside(DataExchangeAPI api) { + ConnectorClientside(DataExchange api) { super(api); this.client = null; } @@ -23,7 +27,7 @@ class ConnectorClientside extends Connector { return true; } - protected void onPlayInit(ClientPacketListener handler, Minecraft client){ + public void onPlayInit(ClientPacketListener handler, Minecraft client){ if (this.client!=null && this.client != client){ BCLib.LOGGER.warning("Client changed!"); } @@ -34,8 +38,8 @@ class ConnectorClientside extends Connector { }); } } - - void onPlayReady(ClientPacketListener handler, PacketSender sender, Minecraft client){ + + public void onPlayReady(ClientPacketListener handler, PacketSender sender, Minecraft client){ for(DataHandlerDescriptor desc : getDescriptors()){ if (desc.sendOnJoin){ DataHandler h = desc.JOIN_INSTANCE.get(); @@ -45,8 +49,8 @@ class ConnectorClientside extends Connector { } } } - - void onPlayDisconnect(ClientPacketListener handler, Minecraft client){ + + public void onPlayDisconnect(ClientPacketListener handler, Minecraft client){ for(DataHandlerDescriptor desc : getDescriptors()) { ClientPlayNetworking.unregisterReceiver(desc.IDENTIFIER); } @@ -57,7 +61,7 @@ class ConnectorClientside extends Connector { h.receiveFromServer(client, handler, buf, responseSender); } - void sendToServer(DataHandler h){ + public void sendToServer(DataHandler h){ if (client==null){ throw new RuntimeException("[internal error] Client not initialized yet!"); } diff --git a/src/main/java/ru/bclib/api/dataexchange/ConnectorServerside.java b/src/main/java/ru/bclib/api/dataexchange/ConnectorServerside.java index b5d7e920..0a42a147 100644 --- a/src/main/java/ru/bclib/api/dataexchange/ConnectorServerside.java +++ b/src/main/java/ru/bclib/api/dataexchange/ConnectorServerside.java @@ -7,10 +7,14 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; import ru.bclib.BCLib; +import ru.bclib.api.dataexchange.handler.DataExchange; -class ConnectorServerside extends Connector { +/** + * This is an internal class that handles a Serverside Connection to a Client-Player + */ +public class ConnectorServerside extends Connector { private MinecraftServer server; - ConnectorServerside(DataExchangeAPI api) { + ConnectorServerside(DataExchange api) { super(api); server = null; } @@ -20,7 +24,7 @@ class ConnectorServerside extends Connector { return false; } - protected void onPlayInit(ServerGamePacketListenerImpl handler, MinecraftServer server){ + public void onPlayInit(ServerGamePacketListenerImpl handler, MinecraftServer server){ if (this.server!=null && this.server != server){ BCLib.LOGGER.warning("Server changed!"); } @@ -31,8 +35,8 @@ class ConnectorServerside extends Connector { }); } } - - void onPlayReady(ServerGamePacketListenerImpl handler, PacketSender sender, MinecraftServer server){ + + public void onPlayReady(ServerGamePacketListenerImpl handler, PacketSender sender, MinecraftServer server){ for(DataHandlerDescriptor desc : getDescriptors()){ if (desc.sendOnJoin){ DataHandler h = desc.JOIN_INSTANCE.get(); @@ -42,8 +46,8 @@ class ConnectorServerside extends Connector { } } } - - void onPlayDisconnect(ServerGamePacketListenerImpl handler, MinecraftServer server){ + + public void onPlayDisconnect(ServerGamePacketListenerImpl handler, MinecraftServer server){ for(DataHandlerDescriptor desc : getDescriptors()){ ServerPlayNetworking.unregisterReceiver(handler, desc.IDENTIFIER); } @@ -54,7 +58,7 @@ class ConnectorServerside extends Connector { h.receiveFromClient(server, player, handler, buf, responseSender); } - void sendToClient(DataHandler h){ + public void sendToClient(DataHandler h){ if (server==null){ throw new RuntimeException("[internal error] Server not initialized yet!"); } diff --git a/src/main/java/ru/bclib/api/dataexchange/DataExchangeAPI.java b/src/main/java/ru/bclib/api/dataexchange/DataExchangeAPI.java index 309dc291..83d8ca53 100644 --- a/src/main/java/ru/bclib/api/dataexchange/DataExchangeAPI.java +++ b/src/main/java/ru/bclib/api/dataexchange/DataExchangeAPI.java @@ -7,68 +7,24 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.minecraft.network.FriendlyByteBuf; +import ru.bclib.api.dataexchange.handler.DataExchange; import java.io.File; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.Function; import java.util.function.Predicate; -public class DataExchangeAPI { +public class DataExchangeAPI extends DataExchange { private final static List MODS = Lists.newArrayList(); - private static DataExchangeAPI instance; - private ConnectorServerside server; - private ConnectorClientside client; - protected final Set descriptors; - private static class AutoFileSyncEntry { - public final Predicate needTransfer; - public final File fileName; + protected DataExchangeAPI() { + super((api) -> new ConnectorClientside(api), (api) -> new ConnectorServerside(api)); + } + - private AutoFileSyncEntry(Predicate needTransfer, File fileName) { - this.needTransfer = needTransfer; - this.fileName = fileName; - } - } - protected final List autoSyncFiles = new ArrayList<>(4); - - - private DataExchangeAPI(){ - descriptors = new HashSet<>(); - } - - static DataExchangeAPI getInstance(){ - if (instance==null){ - instance = new DataExchangeAPI(); - } - return instance; - } - - @Environment(EnvType.CLIENT) - private void initClientside(){ - if (client!=null) return; - client = new ConnectorClientside(this); - ClientLoginConnectionEvents.INIT.register((a, b) ->{ - System.out.println("INIT"); - }); - ClientLoginConnectionEvents.QUERY_START.register((a, b) ->{ - System.out.println("INIT"); - }); - ClientPlayConnectionEvents.INIT.register(client::onPlayInit); - ClientPlayConnectionEvents.JOIN.register(client::onPlayReady); - ClientPlayConnectionEvents.DISCONNECT.register(client::onPlayDisconnect); - } - - private void initServerSide(){ - if (server!=null) return; - server = new ConnectorServerside(this); - - ServerPlayConnectionEvents.INIT.register(server::onPlayInit); - ServerPlayConnectionEvents.JOIN.register(server::onPlayReady); - ServerPlayConnectionEvents.DISCONNECT.register(server::onPlayDisconnect); - } - /** * Register a mod to participate in the DataExchange. * @@ -91,34 +47,10 @@ public class DataExchangeAPI { * @param desc The Descriptor you want to add. */ public static void registerDescriptor(DataHandlerDescriptor desc){ - DataExchangeAPI api = DataExchangeAPI.getInstance(); + DataExchangeAPI api = DataExchange.getInstance(); api.descriptors.add(desc); } - - - /** - * Initializes all datastructures that need to exist in the client component. - *

- * 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(); - api.initClientside(); - - } - - /** - * Initializes all datastructures that need to exist in the server component. - *

- * 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. *

@@ -136,24 +68,6 @@ public class DataExchangeAPI { DataExchangeAPI.getInstance().client.sendToServer(h); } } - - /** - * Automatically called before the player enters the world. - *

- * This is automatically called by BCLib. It will send all {@link DataHandler}-Objects that have {@link DataHandlerDescriptor#sendBeforeEnter} set to* - * {@Code true}, - */ - @Environment(EnvType.CLIENT) - public static void sendOnEnter(){ - getInstance().descriptors.forEach((desc)-> { - if (desc.sendBeforeEnter){ - DataHandler h = desc.JOIN_INSTANCE.get(); - if (!h.getOriginatesOnServer()) { - getInstance().client.sendToServer(h); - } - } - }); - } /** * Registers a File for automatic client syncing. @@ -162,6 +76,6 @@ public class DataExchangeAPI { * @param fileName The name of the File */ public static void addAutoSyncFile(Predicate needTransfer, File fileName){ - DataExchangeAPI.getInstance().autoSyncFiles.add(new AutoFileSyncEntry(needTransfer, fileName)); + DataExchangeAPI.getInstance().addAutoSyncFileData(needTransfer, fileName); } } diff --git a/src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java b/src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java new file mode 100644 index 00000000..138aedc9 --- /dev/null +++ b/src/main/java/ru/bclib/api/dataexchange/handler/DataExchange.java @@ -0,0 +1,133 @@ +package ru.bclib.api.dataexchange.handler; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import ru.bclib.api.dataexchange.ConnectorClientside; +import ru.bclib.api.dataexchange.ConnectorServerside; +import ru.bclib.api.dataexchange.DataExchangeAPI; +import ru.bclib.api.dataexchange.DataHandler; +import ru.bclib.api.dataexchange.DataHandlerDescriptor; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + +abstract public class DataExchange { + private static class AutoFileSyncEntry { + public final Predicate needTransfer; + public final File fileName; + + private AutoFileSyncEntry(Predicate needTransfer, File fileName) { + this.needTransfer = needTransfer; + this.fileName = fileName; + } + } + + private static DataExchangeAPI instance; + protected static DataExchangeAPI getInstance(){ + if (instance==null){ + instance = new DataExchangeAPI(); + } + return instance; + } + + protected ConnectorServerside server; + protected ConnectorClientside client; + protected final Set descriptors; + protected final List autoSyncFiles = new ArrayList<>(4); + + private final Function clientSupplier; + private final Function serverSupplier; + + protected DataExchange(Function client, Function server){ + descriptors = new HashSet<>(); + this.clientSupplier = client; + this.serverSupplier = server; + } + + public Set getDescriptors() { return descriptors; } + + @Environment(EnvType.CLIENT) + protected void initClientside(){ + if (client!=null) return; + client = clientSupplier.apply(this); + ClientLoginConnectionEvents.INIT.register((a, b) ->{ + System.out.println("INIT"); + }); + ClientLoginConnectionEvents.QUERY_START.register((a, b) ->{ + System.out.println("INIT"); + }); + ClientPlayConnectionEvents.INIT.register(client::onPlayInit); + ClientPlayConnectionEvents.JOIN.register(client::onPlayReady); + ClientPlayConnectionEvents.DISCONNECT.register(client::onPlayDisconnect); + } + + protected void initServerSide(){ + if (server!=null) return; + server = serverSupplier.apply(this); + + ServerPlayConnectionEvents.INIT.register(server::onPlayInit); + ServerPlayConnectionEvents.JOIN.register(server::onPlayReady); + ServerPlayConnectionEvents.DISCONNECT.register(server::onPlayDisconnect); + } + + /** + * Initializes all datastructures that need to exist in the client component. + *

+ * This is automatically called by BCLib. You can register {@link DataHandler}-Objects before this Method is called + */ + @Environment(EnvType.CLIENT) + public void prepareClientside(){ + DataExchangeAPI api = DataExchangeAPI.getInstance(); + api.initClientside(); + + } + + /** + * Initializes all datastructures that need to exist in the server component. + *

+ * This is automatically called by BCLib. You can register {@link DataHandler}-Objects before this Method is called + */ + public static void prepareServerside(){ + DataExchange api = DataExchangeAPI.getInstance(); + api.initServerSide(); + } + + + + /** + * Automatically called before the player enters the world. + *

+ * This is automatically called by BCLib. It will send all {@link DataHandler}-Objects that have {@link DataHandlerDescriptor#sendBeforeEnter} set to* + * {@Code true}, + */ + @Environment(EnvType.CLIENT) + public static void sendOnEnter(){ + getInstance().descriptors.forEach((desc)-> { + if (desc.sendBeforeEnter){ + DataHandler h = desc.JOIN_INSTANCE.get(); + if (!h.getOriginatesOnServer()) { + getInstance().client.sendToServer(h); + } + } + }); + } + + /** + * Registers a File for automatic client syncing. + * + * @param needTransfer If the predicate returns true, the file needs to get copied to the server. + * @param fileName The name of the File + */ + protected void addAutoSyncFileData(Predicate needTransfer, File fileName){ + autoSyncFiles.add(new AutoFileSyncEntry(needTransfer, fileName)); + } +}