Skip to content

Commit

Permalink
🎨 Packet
Browse files Browse the repository at this point in the history
  • Loading branch information
kitUIN committed Aug 8, 2024
1 parent 7c804ea commit ef982bb
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.kituin.chatimage.network;

// IF forge-1.16.5
//import net.minecraft.network.PacketBuffer;
// ELSE
//import net.minecraft.network.FriendlyByteBuf;
// END IF
public class BChannelPacket {
public String message;

public BChannelPacket(String message) {
this.message = message;
}

// IF forge-1.16.5
// public BChannelPacket(PacketBuffer buffer) {
// this.message = buffer.readUtf();
// }
// public void toBytes(PacketBuffer buffer) {
// buffer.writeUtf(this.message);
// }
// ELSE
// public BChannelPacket(FriendlyByteBuf buffer) {
// this.message = buffer.readUtf();
// }
// public void toBytes(FriendlyByteBuf buffer) {
// buffer.writeUtf(this.message);
// }
// END IF
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static void serverFileChannelReceived(ServerPlayer player, String res) {
// 通知之前请求但是没图片的客户端
List<String> names = USER_CACHE_MAP.get(title.url);
for (String uuid : names) {
FileBackChannel.sendToPlayer(new FileInfoChannelPacket("true->" + title.url), player.server.getPlayerList().getPlayer(UUID.fromString(uuid)));
FileBackChannel.sendToPlayer(new FileInfoChannelPacket("true->" + title.url), (ServerPlayer) player.server.getPlayerList().getPlayer(UUID.fromString(uuid)));
LOGGER.info("[echo to client(" + uuid + ")]" + title.url);
}
USER_CACHE_MAP.put(title.url, Lists.newArrayList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.github.kituin.chatimage.network;

// IF forge-1.16.5
//import net.minecraft.entity.player.ServerPlayerEntity;
//import net.minecraft.util.ResourceLocation;
//import net.minecraftforge.fml.network.NetworkDirection;
//import net.minecraftforge.fml.network.PacketDistributor;
//import net.minecraftforge.fml.network.simple.SimpleChannel;
// ELSE
//import net.minecraft.entity.player.ServerPlayer;
//import net.minecraft.resources.ResourceLocation;
//import net.minecraftforge.network.NetworkDirection;
//import net.minecraftforge.network.simple.SimpleChannel;
// END IF
// IF forge-1.16.5
//import net.minecraftforge.fml.network.NetworkRegistry.ChannelBuilder;
//
//import static io.github.kituin.chatimage.ChatImage.MOD_ID;
// ELSE IF <= forge-1.20
//import net.minecraftforge.network.NetworkRegistry.ChannelBuilder;
// ELSE
//import net.minecraftforge.network.ChannelBuilder;
//import net.minecraftforge.network.PacketDistributor;
// END IF
/**
* 发送文件分块到客户端通道
*/
public class DownloadFileChannel {

private static SimpleChannel INSTANCE;
public static final String VERSION = "1.0";
private static int ID = 0;

public static int nextID() {
return ID++;
}



public static void register() {
SimpleChannel net = ChannelBuilder
.named(new ResourceLocation(MOD_ID, "download_file_channel"))
// IF <= forge-1.20
// .networkProtocolVersion(() -> VERSION)
// .clientAcceptedVersions(s -> true)
// .serverAcceptedVersions(s -> true)
// ELSE
// .networkProtocolVersion(1)
// .acceptedVersions((s, v) -> v == 1)
// .clientAcceptedVersions((s, v) -> true)
// .serverAcceptedVersions((s, v) -> true)
// END IF
.simpleChannel();

INSTANCE = net;

net.messageBuilder(DownloadFileChannelPacket.class, nextID(), NetworkDirection.PLAY_TO_CLIENT)
.decoder(DownloadFileChannelPacket::new)
.encoder(DownloadFileChannelPacket::toBytes)
// IF <= forge-1.20
// .consumer(DownloadFileChannelPacket::clientHandle)
// ELSE
// .consumerNetworkThread(DownloadFileChannelPacket::clientHandle)
// END IF
.add();
}


// IF forge-1.16.5
// public static <MSG> void sendToPlayer(MSG message, ServerPlayerEntity player) {
// ELSE
// public static <MSG> void sendToPlayer(MSG message, ServerPlayer player) {
// END IF

// IF <= forge-1.20
// INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), message);
// ELSE
// INSTANCE.send(message,PacketDistributor.PLAYER.with(player));
// END IF
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.github.kituin.chatimage.network;

import net.minecraft.network.FriendlyByteBuf;
// IF forge-1.16.5
//import net.minecraft.network.PacketBuffer;
// ELSE
//import net.minecraft.network.FriendlyByteBuf;
// END IF
// IF forge-1.16.5
//import net.minecraftforge.fml.network.NetworkEvent;
//import java.util.function.Supplier;
// ELSE IF forge-1.18.2
// ELSE IF <= forge-1.20
//import java.util.function.Supplier;
//import net.minecraftforge.network.NetworkEvent;
// ELSE
Expand All @@ -14,22 +17,20 @@
import static io.github.kituin.chatimage.network.ChatImagePacket.clientDownloadFileChannelReceived;


public class DownloadFileChannelPacket {

public String message;

public DownloadFileChannelPacket(FriendlyByteBuf buffer) {
message = buffer.readUtf();
}

public class DownloadFileChannelPacket extends BChannelPacket {
public DownloadFileChannelPacket(String message) {
this.message = message;
}

public void toBytes(FriendlyByteBuf buf) {
buf.writeUtf(this.message);
super(message);
}
// IF <= forge-1.18.2
// IF forge-1.16.5
// public DownloadFileChannelPacket(PacketBuffer buffer) {
// super(buffer);
// }
// ELSE
// public DownloadFileChannelPacket(FriendlyByteBuf buffer) {
// super(buffer);
// }
// END IF
// IF <= forge-1.20
// public boolean clientHandle(Supplier<NetworkEvent.Context> supplier) {
// NetworkEvent.Context ctx = supplier.get();
// ctx.enqueueWork(() -> clientDownloadFileChannelReceived(this.message));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.github.kituin.chatimage.network;

import io.github.kituin.chatimage.ChatImage;
// IF forge-1.16.5
//import net.minecraft.entity.player.ServerPlayerEntity;
//import net.minecraft.util.ResourceLocation;
//import net.minecraftforge.fml.network.NetworkDirection;
//import net.minecraftforge.fml.network.PacketDistributor;
//import net.minecraftforge.fml.network.simple.SimpleChannel;
// ELSE
//import net.minecraft.entity.player.ServerPlayer;
//import net.minecraft.resources.ResourceLocation;
//import net.minecraftforge.network.NetworkDirection;
//import net.minecraftforge.network.simple.SimpleChannel;
// END IF
// IF forge-1.16.5
//import net.minecraftforge.fml.network.NetworkRegistry.ChannelBuilder;
// ELSE IF <= forge-1.20
//import net.minecraftforge.network.NetworkRegistry.ChannelBuilder;
// ELSE
//import net.minecraftforge.network.ChannelBuilder;
//import net.minecraftforge.network.PacketDistributor;
// END IF
public class FileBackChannel {
private static SimpleChannel INSTANCE;
public static final String VERSION = "1.0";
private static int ID = 0;

public static int nextID() {
return ID++;
}


public static void register() {
INSTANCE = ChannelBuilder
.named(new ResourceLocation(ChatImage.MOD_ID, "file_back"))
// IF <= forge-1.20
// .networkProtocolVersion(() -> VERSION)
// .clientAcceptedVersions(s -> true)
// .serverAcceptedVersions(s -> true)
// ELSE
// .networkProtocolVersion(1)
// .acceptedVersions((s, v) -> v == 1)
// .clientAcceptedVersions((s, v) -> true)
// .serverAcceptedVersions((s, v) -> true)
// END IF
.simpleChannel();

INSTANCE.messageBuilder(FileInfoChannelPacket.class, nextID(), NetworkDirection.PLAY_TO_CLIENT)
.encoder(FileInfoChannelPacket::toBytes)
.decoder(FileInfoChannelPacket::new)
// IF <= forge-1.20
// .consumer(FileInfoChannelPacket::clientHandle)
// ELSE
// .consumerNetworkThread(FileInfoChannelPacket::clientHandle)
// END IF
.add();
}
// IF forge-1.16.5
// public static <MSG> void sendToPlayer(MSG message, ServerPlayerEntity player) {
// ELSE
// public static <MSG> void sendToPlayer(MSG message, ServerPlayer player) {
// END IF

// IF <= forge-1.20
// INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), message);
// ELSE
// INSTANCE.send(message,PacketDistributor.PLAYER.with(player));
// END IF
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.github.kituin.chatimage.network;

import io.github.kituin.chatimage.ChatImage;
// IF forge-1.16.5
//import net.minecraft.util.ResourceLocation;
//import net.minecraftforge.fml.network.NetworkDirection;
//import net.minecraftforge.fml.network.simple.SimpleChannel;
// ELSE
//import net.minecraft.resources.ResourceLocation;
//import net.minecraftforge.network.NetworkDirection;
//import net.minecraftforge.network.simple.SimpleChannel;
// END IF
// IF forge-1.16.5
//import net.minecraftforge.fml.network.NetworkRegistry.ChannelBuilder;
// ELSE IF <= forge-1.20
//import net.minecraftforge.network.NetworkRegistry.ChannelBuilder;
// ELSE
//import net.minecraftforge.network.ChannelBuilder;
//import net.minecraftforge.network.PacketDistributor;
// END IF
/**
* 客户端发送文件分块到服务器通道
*/
public class FileChannel {
/**
* 客户端发送文件分块到服务器通道(Map)
*/
private static SimpleChannel INSTANCE;
public static final String VERSION = "1.0";
private static int ID = 0;

public static int nextID() {
return ID++;
}

public static void register() {
INSTANCE = ChannelBuilder
.named(new ResourceLocation(ChatImage.MOD_ID, "file_channel"))
// IF <= forge-1.20
// .networkProtocolVersion(() -> VERSION)
// .clientAcceptedVersions(s -> true)
// .serverAcceptedVersions(s -> true)
// ELSE
// .networkProtocolVersion(1)
// .acceptedVersions((s, v) -> v == 1)
// .clientAcceptedVersions((s, v) -> true)
// .serverAcceptedVersions((s, v) -> true)
// END IF
.simpleChannel();

INSTANCE.messageBuilder(FileChannelPacket.class, nextID(), NetworkDirection.PLAY_TO_SERVER)
.encoder(FileChannelPacket::toBytes)
.decoder(FileChannelPacket::new)
// IF <= forge-1.20
// .consumer(FileChannelPacket::serverHandle)
// ELSE
// .consumerNetworkThread(FileChannelPacket::serverHandle)
// END IF
.add();

}

public static <MSG> void sendToServer(MSG message) {
// IF <= forge-1.20
// INSTANCE.sendToServer(message);
// ELSE
// INSTANCE.send(message,PacketDistributor.SERVER.noArg());
// END IF
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
package io.github.kituin.chatimage.network;

// IF forge-1.16.5
//import net.minecraft.network.PacketBuffer;
// ELSE
//import net.minecraft.network.FriendlyByteBuf;
// END IF
// IF forge-1.16.5
//import net.minecraftforge.fml.network.NetworkEvent;
//import java.util.function.Supplier;
// ELSE IF forge-1.18.2
// ELSE IF <= forge-1.20
//import java.util.function.Supplier;
//import net.minecraftforge.network.NetworkEvent;
//import net.minecraft.network.FriendlyByteBuf;
// ELSE
//import net.minecraftforge.event.network.CustomPayloadEvent;
//import net.minecraft.network.FriendlyByteBuf;
// END IF
import net.minecraft.network.FriendlyByteBuf;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.server.level.ServerPlayer;

import static io.github.kituin.chatimage.network.ChatImagePacket.serverFileChannelReceived;


public class FileChannelPacket {


private final String message;

private static final Logger LOGGER = LogManager.getLogger();

public FileChannelPacket(FriendlyByteBuf buffer) {
message = buffer.readUtf();
}

public class FileChannelPacket extends BChannelPacket{
public FileChannelPacket(String message) {
this.message = message;
}

public void toBytes(FriendlyByteBuf buf) {
buf.writeUtf(this.message);
super(message);
}


// IF <= forge-1.18.2
// IF forge-1.16.5
// public FileChannelPacket(PacketBuffer buffer) {
// super(buffer);
// }
// ELSE
// public FileChannelPacket(FriendlyByteBuf buffer) {
// super(buffer);
// }
// END IF
// IF <= forge-1.20
// public boolean serverHandle(Supplier<NetworkEvent.Context> supplier) {
// NetworkEvent.Context ctx = supplier.get();
// ctx.enqueueWork(() -> serverFileChannelReceived(ctx.getSender(), this.message));
// ctx.enqueueWork(() -> serverFileChannelReceived((ServerPlayer)ctx.getSender(), this.message));
// ELSE
// public static boolean serverHandle(FileChannelPacket packet, CustomPayloadEvent.Context ctx) {
// ctx.enqueueWork(() -> serverFileChannelReceived(ctx.getSender(), packet.message));
Expand Down
Loading

0 comments on commit ef982bb

Please sign in to comment.