diff --git a/README.md b/README.md index 24dbae098..221ca699b 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Set apiModules = [ "fabric-api-base", "fabric-command-api-v1", "fabric-lifecycle-events-v1", - "fabric-networking-v0" + "fabric-networking-api-v1" ] // Add each module as a dependency @@ -66,7 +66,7 @@ setOf( "fabric-api-base", "fabric-command-api-v1", "fabric-lifecycle-events-v1", - "fabric-networking-v0" + "fabric-networking-api-v1" ).forEach { // Add each module as a dependency modImplementation(fabricApi.module(it, FABRIC_API_VERSION)) diff --git a/fabric-containers-v0/build.gradle b/fabric-containers-v0/build.gradle index ecb43014f..5b7d539e0 100644 --- a/fabric-containers-v0/build.gradle +++ b/fabric-containers-v0/build.gradle @@ -3,5 +3,5 @@ version = getSubprojectVersion(project, "0.1.10") moduleDependencies(project, [ 'fabric-api-base', - 'fabric-networking-v0' + 'fabric-networking-api-v1' ]) diff --git a/fabric-containers-v0/src/main/java/net/fabricmc/fabric/impl/client/container/ScreenProviderRegistryImpl.java b/fabric-containers-v0/src/main/java/net/fabricmc/fabric/impl/client/container/ScreenProviderRegistryImpl.java index 876365b02..4d2495be7 100644 --- a/fabric-containers-v0/src/main/java/net/fabricmc/fabric/impl/client/container/ScreenProviderRegistryImpl.java +++ b/fabric-containers-v0/src/main/java/net/fabricmc/fabric/impl/client/container/ScreenProviderRegistryImpl.java @@ -22,15 +22,15 @@ import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.screen.ScreenHandler; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.screen.ContainerScreenFactory; import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry; import net.fabricmc.fabric.api.container.ContainerFactory; -import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; import net.fabricmc.fabric.impl.container.ContainerProviderImpl; public class ScreenProviderRegistryImpl implements ScreenProviderRegistry { @@ -67,12 +67,14 @@ public class ScreenProviderRegistryImpl implements ScreenProviderRegistry { } public static void init() { - ClientSidePacketRegistry.INSTANCE.register(ContainerProviderImpl.OPEN_CONTAINER, (packetContext, packetByteBuf) -> { - Identifier identifier = packetByteBuf.readIdentifier(); - int syncId = packetByteBuf.readUnsignedByte(); - packetByteBuf.retain(); + ClientPlayNetworking.registerGlobalReceiver(ContainerProviderImpl.OPEN_CONTAINER, (client, handler, buf, responseSender) -> { + Identifier identifier = buf.readIdentifier(); + int syncId = buf.readUnsignedByte(); - MinecraftClient.getInstance().execute(() -> { + // Retain the buf since we must open the screen handler with it's extra modded data on the client thread + buf.retain(); + + client.execute(() -> { try { ContainerFactory factory = FACTORIES.get(identifier); @@ -81,11 +83,13 @@ public class ScreenProviderRegistryImpl implements ScreenProviderRegistry { return; } - HandledScreen gui = factory.create(syncId, identifier, packetContext.getPlayer(), packetByteBuf); - packetContext.getPlayer().currentScreenHandler = gui.getScreenHandler(); - MinecraftClient.getInstance().openScreen(gui); + ClientPlayerEntity player = client.player; + HandledScreen gui = factory.create(syncId, identifier, player, buf); + + player.currentScreenHandler = gui.getScreenHandler(); + client.openScreen(gui); } finally { - packetByteBuf.release(); + buf.release(); } }); }); diff --git a/fabric-containers-v0/src/main/resources/fabric.mod.json b/fabric-containers-v0/src/main/resources/fabric.mod.json index 76281ce45..5866b8ac7 100644 --- a/fabric-containers-v0/src/main/resources/fabric.mod.json +++ b/fabric-containers-v0/src/main/resources/fabric.mod.json @@ -18,7 +18,7 @@ "depends": { "fabricloader": ">=0.4.0", "fabric-api-base": "*", - "fabric-networking-v0": "*" + "fabric-networking-api-v1": "*" }, "description": "Adds hooks for containers.", "mixins": [ diff --git a/fabric-dimensions-v1/src/main/resources/fabric.mod.json b/fabric-dimensions-v1/src/main/resources/fabric.mod.json index 6a6606b1f..02d45a00b 100644 --- a/fabric-dimensions-v1/src/main/resources/fabric.mod.json +++ b/fabric-dimensions-v1/src/main/resources/fabric.mod.json @@ -6,9 +6,7 @@ "depends": { "fabricloader": ">=0.4.0", "minecraft": ">=1.16-rc.3", - "fabric-api-base": "*", - "fabric-registry-sync-v0": "*", - "fabric-networking-v0": "*" + "fabric-api-base": "*" }, "mixins": [ "fabric-dimensions-v1.mixins.json" diff --git a/fabric-screen-handler-api-v1/build.gradle b/fabric-screen-handler-api-v1/build.gradle index 5dd1a91f5..a97209052 100644 --- a/fabric-screen-handler-api-v1/build.gradle +++ b/fabric-screen-handler-api-v1/build.gradle @@ -12,5 +12,5 @@ dependencies { moduleDependencies(project, [ 'fabric-api-base', - 'fabric-networking-v0' + 'fabric-networking-api-v1' ]) diff --git a/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/Networking.java b/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/Networking.java index 07ab4eb6b..ca125291c 100644 --- a/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/Networking.java +++ b/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/Networking.java @@ -28,7 +28,7 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; -import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; public final class Networking { @@ -67,6 +67,6 @@ public final class Networking { buf.writeText(factory.getDisplayName()); factory.writeScreenOpeningData(player, buf); - ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, OPEN_ID, buf); + ServerPlayNetworking.send(player, OPEN_ID, buf); } } diff --git a/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/client/ClientNetworking.java b/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/client/ClientNetworking.java index 8c580e21e..3ee14abcb 100644 --- a/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/client/ClientNetworking.java +++ b/fabric-screen-handler-api-v1/src/main/java/net/fabricmc/fabric/impl/screenhandler/client/ClientNetworking.java @@ -33,7 +33,7 @@ import net.minecraft.util.registry.Registry; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.impl.screenhandler.ExtendedScreenHandlerType; import net.fabricmc.fabric.impl.screenhandler.Networking; @@ -43,45 +43,52 @@ public final class ClientNetworking implements ClientModInitializer { @Override public void onInitializeClient() { - ClientSidePacketRegistry.INSTANCE.register(Networking.OPEN_ID, (ctx, buf) -> { + ClientPlayNetworking.registerGlobalReceiver(Networking.OPEN_ID, (client, handler, buf, responseSender) -> { Identifier typeId = buf.readIdentifier(); int syncId = buf.readVarInt(); Text title = buf.readText(); + // Retain the buf since we must open the screen handler with it's extra modded data on the client thread + // The buf will be released after the screen is opened buf.retain(); - ctx.getTaskQueue().execute(() -> openScreen(typeId, syncId, title, buf)); + + client.execute(() -> this.openScreen(typeId, syncId, title, buf)); }); } @SuppressWarnings({"rawtypes", "unchecked"}) private void openScreen(Identifier typeId, int syncId, Text title, PacketByteBuf buf) { - ScreenHandlerType type = Registry.SCREEN_HANDLER.get(typeId); + try { + ScreenHandlerType type = Registry.SCREEN_HANDLER.get(typeId); - if (type == null) { - LOGGER.warn("Unknown screen handler ID: {}", typeId); - return; - } + if (type == null) { + LOGGER.warn("Unknown screen handler ID: {}", typeId); + return; + } - if (!(type instanceof ExtendedScreenHandlerType)) { - LOGGER.warn("Received extended opening packet for non-extended screen handler {}", typeId); - return; - } + if (!(type instanceof ExtendedScreenHandlerType)) { + LOGGER.warn("Received extended opening packet for non-extended screen handler {}", typeId); + return; + } - HandledScreens.Provider screenFactory = HandledScreens.getProvider(type); + HandledScreens.Provider screenFactory = HandledScreens.getProvider(type); - if (screenFactory != null) { - MinecraftClient client = MinecraftClient.getInstance(); - PlayerEntity player = client.player; + if (screenFactory != null) { + MinecraftClient client = MinecraftClient.getInstance(); + PlayerEntity player = client.player; - Screen screen = screenFactory.create( - ((ExtendedScreenHandlerType) type).create(syncId, player.getInventory(), buf), - player.getInventory(), - title - ); + Screen screen = screenFactory.create( + ((ExtendedScreenHandlerType) type).create(syncId, player.inventory, buf), + player.getInventory(), + title + ); - player.currentScreenHandler = ((ScreenHandlerProvider) screen).getScreenHandler(); - client.openScreen(screen); - } else { - LOGGER.warn("Screen not registered for screen handler {}!", typeId); + player.currentScreenHandler = ((ScreenHandlerProvider) screen).getScreenHandler(); + client.openScreen(screen); + } else { + LOGGER.warn("Screen not registered for screen handler {}!", typeId); + } + } finally { + buf.release(); // Release the buf } } } diff --git a/fabric-screen-handler-api-v1/src/main/resources/fabric.mod.json b/fabric-screen-handler-api-v1/src/main/resources/fabric.mod.json index 434523493..04cac1002 100644 --- a/fabric-screen-handler-api-v1/src/main/resources/fabric.mod.json +++ b/fabric-screen-handler-api-v1/src/main/resources/fabric.mod.json @@ -18,7 +18,7 @@ "depends": { "fabricloader": ">=0.8.0", "fabric-api-base": "*", - "fabric-networking-v0": "*" + "fabric-networking-api-v1": "*" }, "entrypoints": { "client": ["net.fabricmc.fabric.impl.screenhandler.client.ClientNetworking"]