Skip to content

Commit

Permalink
chore: add NetworkHelper#enumStreamCodec method
Browse files Browse the repository at this point in the history
Like NeoForgeExtraCodecs method, but we need it cross-platform...
  • Loading branch information
desht committed May 10, 2024
1 parent e2af3ee commit cb74b03
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.architectury.networking.NetworkManager;
import dev.architectury.platform.Platform;
import dev.architectury.utils.Env;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
Expand All @@ -19,4 +20,18 @@ public static <T extends CustomPacketPayload> void registerS2C(CustomPacketPaylo
NetworkManager.registerS2CPayloadType(type, codec);
}
}

public static <B extends FriendlyByteBuf, V extends Enum<V>> StreamCodec<B, V> enumStreamCodec(Class<V> enumClass) {
return new StreamCodec<>() {
@Override
public V decode(B buf) {
return buf.readEnum(enumClass);
}

@Override
public void encode(B buf, V value) {
buf.writeEnum(value);
}
};
}
}

0 comments on commit cb74b03

Please sign in to comment.