A small update this week about yet another bug fix. This time there was an issue with the 1.20.1 version not synchronising its butterfly data properly. These kinds of problems will crop up from time to time when porting the mod over different versions of the game.
You may notice that Minecraft mods often take time to update to new versions. Maintaining a mod for multiple versions of the games can be time consuming, and its because the code often changes a lot between versions. Even between minor versions you can have some major differences.
When I develop the mod I mainly work with Forge 1.20.2. After I have implemented a version I will then port it to all the versions I’ve chosen to support. This usually works out okay, but it can lead to errors when the code has changed.
Minecraft’s networking code is constantly changing. In the four versions I’ve chosen to support the code to synchronise data is very different. It is constantly improving, but that means that ensuring it works in four different versions requires a fair amount of maintenance.
In 1.20.2 I have a NetworkEventListener
that listens for events from the server. I use this to synchronise the butterfly data from the server to the client. Any local data the client has will be overridden by the server. This is why you can’t just create a local data pack and expect it to work when you connect to any server.
To set this up in 1.20.2 is fairly simple. I just need to register the class on the event bus, and register the handler in the same way as any other Forge event.
/** * Listens for network-based events. */ public class NetworkEventListener { /** * Construction * @param forgeEventBus The event bus to register with. */ public NetworkEventListener(IEventBus forgeEventBus) { forgeEventBus.register(this); // Responds to a datapack sync request, which will then send any data to the client. forgeEventBus.addListener(this::onDatapackSync); // Called when the client receives butterfly data from the server. forgeEventBus.addListener(this::onButterflyCollectionPayload); } }
Unfortunately in 1.20.1 it isn’t so simple. You can register the event and the game will compile and run as if nothing is wrong, but as soon as you start a server-based game it will break. The problem is that in 1.20.1 the network channels hadn’t been hooked into the Forge event system yet, so you had to create a custom channel to handle it.
/** * Listens for network-based events. */ public class NetworkEventListener { // A network channel used to listen for butterfly data messages. public static final EventNetworkChannel BUTTERFLY_NETWORK_CHANNEL = NetworkRegistry.ChannelBuilder. named(ClientBoundButterflyDataPacket.ID). clientAcceptedVersions(a -> true). serverAcceptedVersions(a -> true). networkProtocolVersion(() -> NetworkConstants.NETVERSION). eventNetworkChannel(); /** * Construction * @param forgeEventBus The event bus to register with. */ public NetworkEventListener(IEventBus forgeEventBus) { forgeEventBus.register(this); forgeEventBus.addListener(this::onDatapackSync); // This is needed for 1.20.1 BUTTERFLY_NETWORK_CHANNEL.addListener(this::onButterflyCollectionPayload); } }
Obviously this was being registered in previous versions, but a bad merge must have removed the call to addListener()
. Adding this back in means that the 1.20.1 version works again, and I’ve made a new release that fixes the mod in that version. Thank you to SociallyAwkrd for reporting this bug and giving me the chance to fix it!