Ability to send data when player first enters the level
This commit is contained in:
parent
4fc9c06204
commit
7bfe0b01cd
6 changed files with 80 additions and 12 deletions
|
@ -3,8 +3,10 @@ package ru.bclib.api.dataexchange;
|
|||
import com.google.common.collect.Lists;
|
||||
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 net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
@ -34,7 +36,12 @@ public class DataExchangeAPI {
|
|||
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);
|
||||
|
@ -117,5 +124,21 @@ public class DataExchangeAPI {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically called before the player enters the world.
|
||||
* <p>
|
||||
* This 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package ru.bclib.api.dataexchange;
|
||||
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
|
@ -44,7 +42,9 @@ public abstract class DataHandler {
|
|||
client.execute(() -> runOnGameThread(client, null, true));
|
||||
}
|
||||
|
||||
private ServerPlayer lastMessageSender;
|
||||
void receiveFromClient(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender){
|
||||
lastMessageSender = player;
|
||||
deserializeFromIncomingData(buf, responseSender, false);
|
||||
server.execute(() -> runOnGameThread(null, server, false));
|
||||
}
|
||||
|
@ -53,11 +53,15 @@ public abstract class DataHandler {
|
|||
}
|
||||
|
||||
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient){
|
||||
|
||||
}
|
||||
|
||||
protected void serializeData(FriendlyByteBuf buf) {
|
||||
}
|
||||
|
||||
final protected boolean reply(DataHandler message, MinecraftServer server){
|
||||
if (lastMessageSender==null) return false;
|
||||
message.sendToClient(server, lastMessageSender);
|
||||
return true;
|
||||
}
|
||||
|
||||
void sendToClient(MinecraftServer server){
|
||||
|
|
|
@ -6,21 +6,23 @@ import java.util.function.Supplier;
|
|||
|
||||
public class DataHandlerDescriptor {
|
||||
public DataHandlerDescriptor(ResourceLocation identifier, Supplier<DataHandler> instancer){
|
||||
this(identifier, instancer, instancer, false);
|
||||
this(identifier, instancer, instancer, false, false);
|
||||
}
|
||||
|
||||
public DataHandlerDescriptor(ResourceLocation identifier, Supplier<DataHandler> instancer, boolean sendOnJoin){
|
||||
this(identifier, instancer, instancer, sendOnJoin);
|
||||
public DataHandlerDescriptor(ResourceLocation identifier, Supplier<DataHandler> instancer, boolean sendOnJoin, boolean sendBeforeEnter){
|
||||
this(identifier, instancer, instancer, sendOnJoin, sendBeforeEnter);
|
||||
}
|
||||
public DataHandlerDescriptor(ResourceLocation identifier, Supplier<DataHandler> receiv_instancer, Supplier<DataHandler> join_instancer, boolean sendOnJoin){
|
||||
public DataHandlerDescriptor(ResourceLocation identifier, Supplier<DataHandler> receiv_instancer, Supplier<DataHandler> join_instancer, boolean sendOnJoin, boolean sendBeforeEnter){
|
||||
this.INSTANCE = receiv_instancer;
|
||||
this.JOIN_INSTANCE = join_instancer;
|
||||
this.IDENTIFIER = identifier;
|
||||
|
||||
this.sendOnJoin = sendOnJoin;
|
||||
this.sendBeforeEnter = sendBeforeEnter;
|
||||
}
|
||||
|
||||
public final boolean sendOnJoin;
|
||||
public final boolean sendBeforeEnter;
|
||||
public final ResourceLocation IDENTIFIER;
|
||||
public final Supplier<DataHandler> INSTANCE;
|
||||
public final Supplier<DataHandler> JOIN_INSTANCE;
|
||||
|
|
|
@ -27,7 +27,7 @@ import java.util.Optional;
|
|||
import java.util.function.Consumer;
|
||||
|
||||
public class HelloClient extends DataHandler {
|
||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "hello_client"), HelloClient::new, true);
|
||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "hello_client"), HelloClient::new, false, false);
|
||||
|
||||
public HelloClient() {
|
||||
super(DESCRIPTOR.IDENTIFIER, true);
|
||||
|
@ -42,7 +42,7 @@ public class HelloClient extends DataHandler {
|
|||
return "0.0.0";
|
||||
}
|
||||
|
||||
protected static String getBCLibVersion(){
|
||||
static String getBCLibVersion(){
|
||||
return getModVersion(BCLib.MOD_ID);
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ public class HelloClient extends DataHandler {
|
|||
@Override
|
||||
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
|
||||
String localBclibVersion = getBCLibVersion();
|
||||
BCLib.LOGGER.info("Hello Client received from BCLib. (client="+localBclibVersion+", server="+bclibVersion+")");
|
||||
BCLib.LOGGER.info("Received Hello from Server. (client="+localBclibVersion+", server="+bclibVersion+")");
|
||||
|
||||
if (DataFixerAPI.getModVersion(localBclibVersion) == DataFixerAPI.getModVersion(bclibVersion)){
|
||||
showBCLibError(client);
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package ru.bclib.api.dataexchange.handler;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import ru.bclib.BCLib;
|
||||
import ru.bclib.api.dataexchange.DataHandler;
|
||||
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
|
||||
import ru.bclib.api.datafixer.DataFixerAPI;
|
||||
|
||||
public class HelloServer extends DataHandler {
|
||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "hello_server"), HelloServer::new, false, true);
|
||||
|
||||
protected String bclibVersion ="0.0.0";
|
||||
public HelloServer() {
|
||||
super(DESCRIPTOR.IDENTIFIER, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deserializeFromIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean fromClient) {
|
||||
bclibVersion = DataFixerAPI.getModVersion(buf.readInt());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
|
||||
String localBclibVersion = HelloClient.getBCLibVersion();
|
||||
BCLib.LOGGER.info("Received Hello from Server. (server="+localBclibVersion+", client="+bclibVersion+")");
|
||||
reply(new HelloClient(), server);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void serializeData(FriendlyByteBuf buf) {
|
||||
buf.writeInt(DataFixerAPI.getModVersion(HelloClient.getBCLibVersion()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
package ru.bclib.mixin.client;public class ClientLevelMixin {
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue