Skip to content

Commit

Permalink
Networking system rewritten
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterJulsen committed Oct 17, 2023
1 parent 9674e97 commit cc64c7a
Show file tree
Hide file tree
Showing 23 changed files with 312 additions and 192 deletions.
59 changes: 32 additions & 27 deletions src/main/java/de/mrjulsen/mineify/network/NetworkManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package de.mrjulsen.mineify.network;

import java.util.Optional;

import javax.annotation.Nullable;

import de.mrjulsen.mineify.ModMain;
import de.mrjulsen.mineify.network.packets.DefaultServerResponsePacket;
import de.mrjulsen.mineify.network.packets.DownloadSoundPacket;
import de.mrjulsen.mineify.network.packets.ErrorMessagePacket;
import de.mrjulsen.mineify.network.packets.IPacketBase;
import de.mrjulsen.mineify.network.packets.NextSoundDataRequestPacket;
import de.mrjulsen.mineify.network.packets.NextSoundDataResponsePacket;
import de.mrjulsen.mineify.network.packets.PlaySoundPacket;
Expand All @@ -27,47 +32,47 @@
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
import net.minecraftforge.network.simple.SimpleChannel.MessageBuilder;

public class NetworkManager {
public static final String PROTOCOL_VERSION = String.valueOf(1);
private static int currentId = 0;

public static final SimpleChannel MOD_CHANNEL = NetworkRegistry.ChannelBuilder.named(new ResourceLocation(ModMain.MOD_ID, "mineify_channel")).networkProtocolVersion(() -> PROTOCOL_VERSION).clientAcceptedVersions(PROTOCOL_VERSION::equals).serverAcceptedVersions(PROTOCOL_VERSION::equals).simpleChannel();
public static final SimpleChannel MOD_CHANNEL = NetworkRegistry.ChannelBuilder.named(new ResourceLocation(ModMain.MOD_ID, "network")).networkProtocolVersion(() -> PROTOCOL_VERSION).clientAcceptedVersions(PROTOCOL_VERSION::equals).serverAcceptedVersions(PROTOCOL_VERSION::equals).simpleChannel();

public static void registerNetworkPackets() {
register(DownloadSoundPacket.class).encoder(DownloadSoundPacket::encode).decoder(DownloadSoundPacket::decode).consumerNetworkThread(DownloadSoundPacket::handle).add();
register(UploadSoundPacket.class).encoder(UploadSoundPacket::encode).decoder(UploadSoundPacket::decode).consumerNetworkThread(UploadSoundPacket::handle).add();
register(UploadSoundCompletionPacket.class).encoder(UploadSoundCompletionPacket::encode).decoder(UploadSoundCompletionPacket::decode).consumerNetworkThread(UploadSoundCompletionPacket::handle).add();
register(SoundDeleteRequestPacket.class).encoder(SoundDeleteRequestPacket::encode).decoder(SoundDeleteRequestPacket::decode).consumerNetworkThread(SoundDeleteRequestPacket::handle).add();
register(SoundListRequestPacket.class).encoder(SoundListRequestPacket::encode).decoder(SoundListRequestPacket::decode).consumerNetworkThread(SoundListRequestPacket::handle).add();
register(SoundListResponsePacket.class).encoder(SoundListResponsePacket::encode).decoder(SoundListResponsePacket::decode).consumerNetworkThread(SoundListResponsePacket::handle).add();
register(SoundPlayerBlockEntityPacket.class).encoder(SoundPlayerBlockEntityPacket::encode).decoder(SoundPlayerBlockEntityPacket::decode).consumerNetworkThread(SoundPlayerBlockEntityPacket::handle).add();
register(StopSoundPacket.class).encoder(StopSoundPacket::encode).decoder(StopSoundPacket::decode).consumerNetworkThread(StopSoundPacket::handle).add();
register(ErrorMessagePacket.class).encoder(ErrorMessagePacket::encode).decoder(ErrorMessagePacket::decode).consumerNetworkThread(ErrorMessagePacket::handle).add();
register(NextSoundDataRequestPacket.class).encoder(NextSoundDataRequestPacket::encode).decoder(NextSoundDataRequestPacket::decode).consumerNetworkThread(NextSoundDataRequestPacket::handle).add();
register(NextSoundDataResponsePacket.class).encoder(NextSoundDataResponsePacket::encode).decoder(NextSoundDataResponsePacket::decode).consumerNetworkThread(NextSoundDataResponsePacket::handle).add();
register(PlaySoundPacket.class).encoder(PlaySoundPacket::encode).decoder(PlaySoundPacket::decode).consumerNetworkThread(PlaySoundPacket::handle).add();
register(DefaultServerResponsePacket.class).encoder(DefaultServerResponsePacket::encode).decoder(DefaultServerResponsePacket::decode).consumerNetworkThread(DefaultServerResponsePacket::handle).add();
register(SoundFilesCountRequestPacket.class).encoder(SoundFilesCountRequestPacket::encode).decoder(SoundFilesCountRequestPacket::decode).consumerNetworkThread(SoundFilesCountRequestPacket::handle).add();
register(SoundFilesCountResponsePacket.class).encoder(SoundFilesCountResponsePacket::encode).decoder(SoundFilesCountResponsePacket::decode).consumerNetworkThread(SoundFilesCountResponsePacket::handle).add();
register(SoundFilesSizeRequestPacket.class).encoder(SoundFilesSizeRequestPacket::encode).decoder(SoundFilesSizeRequestPacket::decode).consumerNetworkThread(SoundFilesSizeRequestPacket::handle).add();
register(StopSoundWithPathPacket.class).encoder(StopSoundWithPathPacket::encode).decoder(StopSoundWithPathPacket::decode).consumerNetworkThread(StopSoundWithPathPacket::handle).add();
register(SoundModificationPacket.class).encoder(SoundModificationPacket::encode).decoder(SoundModificationPacket::decode).consumerNetworkThread(SoundModificationPacket::handle).add();
register(SoundModificationWithPathPacket.class).encoder(SoundModificationWithPathPacket::encode).decoder(SoundModificationWithPathPacket::decode).consumerNetworkThread(SoundModificationWithPathPacket::handle).add();
register(PlaySoundRequestPacket.class).encoder(PlaySoundRequestPacket::encode).decoder(PlaySoundRequestPacket::decode).consumerNetworkThread(PlaySoundRequestPacket::handle).add();
register(SetCooldownPacket.class).encoder(SetCooldownPacket::encode).decoder(SetCooldownPacket::decode).consumerNetworkThread(SetCooldownPacket::handle).add();
registerPacket(UploadSoundPacket.class, new UploadSoundPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(UploadSoundCompletionPacket.class, new UploadSoundCompletionPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(SoundDeleteRequestPacket.class, new SoundDeleteRequestPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(SoundListRequestPacket.class, new SoundListRequestPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(SoundPlayerBlockEntityPacket.class, new SoundPlayerBlockEntityPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(NextSoundDataRequestPacket.class, new NextSoundDataRequestPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(SoundFilesCountRequestPacket.class, new SoundFilesCountRequestPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(SoundFilesSizeRequestPacket.class, new SoundFilesSizeRequestPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(PlaySoundRequestPacket.class, new PlaySoundRequestPacket(), NetworkDirection.PLAY_TO_SERVER);
registerPacket(SetCooldownPacket.class, new SetCooldownPacket(), NetworkDirection.PLAY_TO_SERVER);


registerPacket(DownloadSoundPacket.class, new DownloadSoundPacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(SoundListResponsePacket.class, new SoundListResponsePacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(StopSoundPacket.class, new StopSoundPacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(ErrorMessagePacket.class, new ErrorMessagePacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(NextSoundDataResponsePacket.class, new NextSoundDataResponsePacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(PlaySoundPacket.class, new PlaySoundPacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(DefaultServerResponsePacket.class, new DefaultServerResponsePacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(SoundFilesCountResponsePacket.class, new SoundFilesCountResponsePacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(StopSoundWithPathPacket.class, new StopSoundWithPathPacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(SoundModificationPacket.class, new SoundModificationPacket(), NetworkDirection.PLAY_TO_CLIENT);
registerPacket(SoundModificationWithPathPacket.class, new SoundModificationWithPathPacket(), NetworkDirection.PLAY_TO_CLIENT);


}

public static SimpleChannel getPlayChannel() {
return MOD_CHANNEL;
}

private static <T> MessageBuilder<T> register(Class<T> clazz) {
MessageBuilder<T> mb = MOD_CHANNEL.messageBuilder(clazz, currentId);
currentId++;
return mb;
private static <T> void registerPacket(Class<T> clazz, IPacketBase<T> packet, @Nullable NetworkDirection direction) {
MOD_CHANNEL.registerMessage(currentId++, clazz, packet::encode, packet::decode, packet::handle, Optional.of(direction));
}

public static void sendToClient(Object o, ServerPlayer player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

public class DefaultServerResponsePacket {
public class DefaultServerResponsePacket implements IPacketBase<DefaultServerResponsePacket> {
public long requestId;

public DefaultServerResponsePacket() { }

public DefaultServerResponsePacket(long requestId) {
this.requestId = requestId;
}

public static void encode(DefaultServerResponsePacket packet, FriendlyByteBuf buffer) {
@Override
public void encode(DefaultServerResponsePacket packet, FriendlyByteBuf buffer) {
buffer.writeLong(packet.requestId);
}

public static DefaultServerResponsePacket decode(FriendlyByteBuf buffer) {
@Override
public DefaultServerResponsePacket decode(FriendlyByteBuf buffer) {
long requestId = buffer.readLong();

DefaultServerResponsePacket instance = new DefaultServerResponsePacket(requestId);
return instance;
}

public static void handle(DefaultServerResponsePacket packet, Supplier<NetworkEvent.Context> context) {
@Override
public void handle(DefaultServerResponsePacket packet, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() ->
{
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

public class DownloadSoundPacket {
public final long requestId;
public final int dataOffset;
public final int maxLength;
public final EStreamingMode streamingMode;
public final byte[] data;
public class DownloadSoundPacket implements IPacketBase<DownloadSoundPacket> {
public long requestId;
public int dataOffset;
public int maxLength;
public EStreamingMode streamingMode;
public byte[] data;

public DownloadSoundPacket() { }

public DownloadSoundPacket(long requestId, int dataOffset, byte[] data, int maxLength, EStreamingMode streamingMode) {
this.data = data;
Expand All @@ -25,15 +26,17 @@ public DownloadSoundPacket(long requestId, int dataOffset, byte[] data, int maxL
this.maxLength = maxLength;
}

public static void encode(DownloadSoundPacket packet, FriendlyByteBuf buffer) {
@Override
public void encode(DownloadSoundPacket packet, FriendlyByteBuf buffer) {
buffer.writeLong(packet.requestId);
buffer.writeInt(packet.dataOffset);
buffer.writeInt(packet.maxLength);
buffer.writeEnum(packet.streamingMode);
buffer.writeByteArray(packet.data);
}

public static DownloadSoundPacket decode(FriendlyByteBuf buffer) {
@Override
public DownloadSoundPacket decode(FriendlyByteBuf buffer) {
long requestId = buffer.readLong();
int dataOffset = buffer.readInt();
int maxLength = buffer.readInt();
Expand All @@ -44,7 +47,8 @@ public static DownloadSoundPacket decode(FriendlyByteBuf buffer) {
return instance;
}

public static void handle(DownloadSoundPacket packet, Supplier<NetworkEvent.Context> context) {
@Override
public void handle(DownloadSoundPacket packet, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() ->
{
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,29 @@
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

public class ErrorMessagePacket {
public final ToastMessage message;
public class ErrorMessagePacket implements IPacketBase<ErrorMessagePacket> {
public ToastMessage message;

public ErrorMessagePacket() { }

public ErrorMessagePacket(ToastMessage message) {
this.message = message;
}

public static void encode(ErrorMessagePacket packet, FriendlyByteBuf buffer) {
@Override
public void encode(ErrorMessagePacket packet, FriendlyByteBuf buffer) {
packet.message.serialize(buffer);
}

public static ErrorMessagePacket decode(FriendlyByteBuf buffer) {
@Override
public ErrorMessagePacket decode(FriendlyByteBuf buffer) {
ToastMessage message = ToastMessage.deserialize(buffer);
ErrorMessagePacket instance = new ErrorMessagePacket(message);
return instance;
}

public static void handle(ErrorMessagePacket packet, Supplier<NetworkEvent.Context> context) {
@Override
public void handle(ErrorMessagePacket packet, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() ->
{
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientWrapper.handleErrorMessagePacket(packet, context));
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/de/mrjulsen/mineify/network/packets/IPacketBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.mrjulsen.mineify.network.packets;

import java.util.function.Supplier;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

public interface IPacketBase<T> {
void encode(T message, FriendlyByteBuf buffer);
T decode(FriendlyByteBuf buffer);
void handle(T message, Supplier<NetworkEvent.Context> supplier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,34 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

public class NextSoundDataRequestPacket {
private final long requestId;
private final int index;
public class NextSoundDataRequestPacket implements IPacketBase<NextSoundDataRequestPacket> {
private long requestId;
private int index;

public NextSoundDataRequestPacket() { }

public NextSoundDataRequestPacket(long requestId, int index) {
this.requestId = requestId;
this.index = index;
}

public static void encode(NextSoundDataRequestPacket packet, FriendlyByteBuf buffer) {
@Override
public void encode(NextSoundDataRequestPacket packet, FriendlyByteBuf buffer) {
buffer.writeLong(packet.requestId);
buffer.writeInt(packet.index);
}

public static NextSoundDataRequestPacket decode(FriendlyByteBuf buffer) {
@Override
public NextSoundDataRequestPacket decode(FriendlyByteBuf buffer) {
long requestId = buffer.readLong();
int index = buffer.readInt();

NextSoundDataRequestPacket instance = new NextSoundDataRequestPacket(requestId, index);
return instance;
}

public static void handle(NextSoundDataRequestPacket packet, Supplier<NetworkEvent.Context> context) {
@Override
public void handle(NextSoundDataRequestPacket packet, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {

if (InstanceManager.Server.fileCache.containsKey(packet.requestId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

public class NextSoundDataResponsePacket {
public final long requestId;
public final int index;
public final byte[] data;
public final boolean hasNext;
public class NextSoundDataResponsePacket implements IPacketBase<NextSoundDataResponsePacket> {
public long requestId;
public int index;
public byte[] data;
public boolean hasNext;

public NextSoundDataResponsePacket() { }

public NextSoundDataResponsePacket(long requestId, byte[] data, boolean hasNext, int index) {
this.requestId = requestId;
Expand All @@ -21,14 +23,16 @@ public NextSoundDataResponsePacket(long requestId, byte[] data, boolean hasNext,
this.index = index;
}

public static void encode(NextSoundDataResponsePacket packet, FriendlyByteBuf buffer) {
@Override
public void encode(NextSoundDataResponsePacket packet, FriendlyByteBuf buffer) {
buffer.writeLong(packet.requestId);
buffer.writeInt(packet.index);
buffer.writeBoolean(packet.hasNext);
buffer.writeByteArray(packet.data);
}

public static NextSoundDataResponsePacket decode(FriendlyByteBuf buffer) {
@Override
public NextSoundDataResponsePacket decode(FriendlyByteBuf buffer) {
long requestId = buffer.readLong();
int index = buffer.readInt();
boolean hasNext = buffer.readBoolean();
Expand All @@ -38,10 +42,12 @@ public static NextSoundDataResponsePacket decode(FriendlyByteBuf buffer) {
return instance;
}

public static void handle(NextSoundDataResponsePacket packet, Supplier<NetworkEvent.Context> context) {
@Override
public void handle(NextSoundDataResponsePacket packet, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() ->
{
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientWrapper.handleNextSoundDataResponsePacket(packet, context));
});
context.get().setPacketHandled(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

public class PlaySoundPacket {
public final BlockPos pos;
public final long requestId;
public final int attenuationDistance;
public final float pitch;
public final float volume;
public final String path;

public class PlaySoundPacket implements IPacketBase<PlaySoundPacket> {
public BlockPos pos;
public long requestId;
public int attenuationDistance;
public float pitch;
public float volume;
public String path;

public PlaySoundPacket() { }

public PlaySoundPacket(long requestId, BlockPos pos, int attenuationDistance, float volume, float pitch, String path) {
this.pos = pos;
Expand All @@ -28,7 +29,8 @@ public PlaySoundPacket(long requestId, BlockPos pos, int attenuationDistance, fl
this.path = path;
}

public static void encode(PlaySoundPacket packet, FriendlyByteBuf buffer) {
@Override
public void encode(PlaySoundPacket packet, FriendlyByteBuf buffer) {
buffer.writeLong(packet.requestId);
buffer.writeBlockPos(packet.pos);
buffer.writeInt(packet.attenuationDistance);
Expand All @@ -39,7 +41,8 @@ public static void encode(PlaySoundPacket packet, FriendlyByteBuf buffer) {
buffer.writeUtf(packet.path);
}

public static PlaySoundPacket decode(FriendlyByteBuf buffer) {
@Override
public PlaySoundPacket decode(FriendlyByteBuf buffer) {
long requestId = buffer.readLong();
BlockPos pos = buffer.readBlockPos();
int attenuationDistance = buffer.readInt();
Expand All @@ -52,7 +55,8 @@ public static PlaySoundPacket decode(FriendlyByteBuf buffer) {
return instance;
}

public static void handle(PlaySoundPacket packet, Supplier<NetworkEvent.Context> context) {
@Override
public void handle(PlaySoundPacket packet, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() ->
{
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
Expand Down
Loading

0 comments on commit cc64c7a

Please sign in to comment.