2023-09-26 01:45:28 +01:00
|
|
|
package com.minelittlepony.api.events;
|
2021-02-03 18:42:35 +02:00
|
|
|
|
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
2021-08-03 18:30:34 +02:00
|
|
|
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
|
2021-02-07 11:43:25 +02:00
|
|
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
2021-08-03 18:30:34 +02:00
|
|
|
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
|
|
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
2021-02-07 11:43:25 +02:00
|
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
2021-02-03 18:42:35 +02:00
|
|
|
import net.fabricmc.loader.api.FabricLoader;
|
2021-08-03 18:30:34 +02:00
|
|
|
import net.minecraft.client.MinecraftClient;
|
2021-02-03 18:42:35 +02:00
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
|
2023-09-26 01:45:28 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
import com.minelittlepony.api.pony.Pony;
|
|
|
|
import com.minelittlepony.api.pony.PonyData;
|
2021-02-03 18:42:35 +02:00
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
2021-08-03 18:30:34 +02:00
|
|
|
public class Channel {
|
|
|
|
private static final Identifier CLIENT_PONY_DATA = new Identifier("minelittlepony", "pony_data");
|
|
|
|
private static final Identifier REQUEST_PONY_DATA = new Identifier("minelittlepony", "request_pony_data");
|
|
|
|
|
2023-09-26 01:45:28 +01:00
|
|
|
private static final Logger LOGGER = LogManager.getLogger("MineLittlePony");
|
|
|
|
|
2021-08-03 18:30:34 +02:00
|
|
|
private static boolean registered;
|
|
|
|
|
|
|
|
public static void bootstrap() {
|
|
|
|
ClientLoginConnectionEvents.INIT.register((handler, client) -> {
|
|
|
|
registered = false;
|
2023-09-26 01:45:28 +01:00
|
|
|
LOGGER.info("Resetting registered flag");
|
2021-08-03 18:30:34 +02:00
|
|
|
});
|
|
|
|
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
2023-09-26 01:45:28 +01:00
|
|
|
LOGGER.info("Sending consent packet to " + handler.getPlayer().getName().getString());
|
2021-08-03 18:30:34 +02:00
|
|
|
|
|
|
|
sender.sendPacket(REQUEST_PONY_DATA, PacketByteBufs.empty());
|
|
|
|
});
|
|
|
|
|
|
|
|
ClientPlayNetworking.registerGlobalReceiver(REQUEST_PONY_DATA, (client, handler, ignored, sender) -> {
|
|
|
|
if (client.player != null) {
|
2023-09-25 21:07:09 +01:00
|
|
|
Pony pony = Pony.getManager().getPony(client.player);
|
2021-08-03 18:30:34 +02:00
|
|
|
registered = true;
|
2023-09-26 01:45:28 +01:00
|
|
|
LOGGER.info("Server has just consented");
|
2021-08-03 18:30:34 +02:00
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
sender.sendPacket(CLIENT_PONY_DATA, MsgPonyData.write(pony.metadata(), PacketByteBufs.create()));
|
2021-08-03 18:30:34 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
ServerPlayNetworking.registerGlobalReceiver(CLIENT_PONY_DATA, (server, player, ignore, buffer, ignore2) -> {
|
2023-09-25 21:07:09 +01:00
|
|
|
PonyData packet = MsgPonyData.read(buffer);
|
2021-02-07 11:43:25 +02:00
|
|
|
server.execute(() -> {
|
2023-09-25 21:07:09 +01:00
|
|
|
PonyDataCallback.EVENT.invoker().onPonyDataAvailable(player, packet, EnvType.SERVER);
|
2021-02-03 18:42:35 +02:00
|
|
|
});
|
|
|
|
});
|
2021-08-03 18:30:34 +02:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
public static void broadcastPonyData(PonyData packet) {
|
2021-08-03 18:30:34 +02:00
|
|
|
if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) {
|
|
|
|
throw new RuntimeException("Client packet send called by the server");
|
|
|
|
}
|
2021-02-03 18:42:35 +02:00
|
|
|
|
2022-06-09 22:27:24 +02:00
|
|
|
if (!registered) {
|
2021-08-03 18:30:34 +02:00
|
|
|
if (MinecraftClient.getInstance().isInSingleplayer() || MinecraftClient.getInstance().isIntegratedServerRunning()) {
|
2023-09-26 01:45:28 +01:00
|
|
|
LOGGER.info("Sending pony skin data over as we are either in single-player or lan");
|
2021-08-03 18:30:34 +02:00
|
|
|
} else {
|
2023-09-26 01:45:28 +01:00
|
|
|
LOGGER.info("Skipping network packet as the server has not consented");
|
2021-08-03 18:30:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 18:42:35 +02:00
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
ClientPlayNetworking.send(CLIENT_PONY_DATA, MsgPonyData.write(packet, PacketByteBufs.create()));
|
2021-02-03 18:42:35 +02:00
|
|
|
}
|
|
|
|
}
|