From 0a315a4666e4ffcbd640f7ee7df85f592d50ef48 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 10 Apr 2024 02:37:15 +0900 Subject: [PATCH] Remove our networking sync code as it is no longer required Signed-off-by: shedaniel --- .../main/resources/architectury.accessWidener | 1 + .../fabric/BufCustomPacketPayload.java | 50 ------------ .../forge/BufCustomPacketPayload.java | 44 ---------- .../forge/ClientNetworkingManager.java | 71 ---------------- .../networking/forge/NetworkManagerImpl.java | 81 ++++++------------- 5 files changed, 27 insertions(+), 220 deletions(-) delete mode 100644 fabric/src/main/java/dev/architectury/networking/fabric/BufCustomPacketPayload.java delete mode 100644 neoforge/src/main/java/dev/architectury/networking/forge/BufCustomPacketPayload.java delete mode 100644 neoforge/src/main/java/dev/architectury/networking/forge/ClientNetworkingManager.java diff --git a/common/src/main/resources/architectury.accessWidener b/common/src/main/resources/architectury.accessWidener index ba3ba25a..a9325b60 100644 --- a/common/src/main/resources/architectury.accessWidener +++ b/common/src/main/resources/architectury.accessWidener @@ -133,6 +133,7 @@ accessible class net/minecraft/client/particle/ParticleEngine$MutableSpriteSet accessible field net/minecraft/client/particle/ParticleEngine$MutableSpriteSet sprites Ljava/util/List; transitive-accessible class net/minecraft/world/item/CreativeModeTab$Output transitive-accessible class net/minecraft/world/item/CreativeModeTab$TabVisibility +accessible field net/minecraft/client/multiplayer/MultiPlayerGameMode connection Lnet/minecraft/client/multiplayer/ClientPacketListener; ############################## # This section is generated automatically with Gradle task generateAccessWidener!!! diff --git a/fabric/src/main/java/dev/architectury/networking/fabric/BufCustomPacketPayload.java b/fabric/src/main/java/dev/architectury/networking/fabric/BufCustomPacketPayload.java deleted file mode 100644 index 22349ec3..00000000 --- a/fabric/src/main/java/dev/architectury/networking/fabric/BufCustomPacketPayload.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of architectury. - * Copyright (C) 2020, 2021, 2022 architectury - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package dev.architectury.networking.fabric; - -import io.netty.buffer.ByteBuf; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.ByteBufCodecs; -import net.minecraft.network.codec.StreamCodec; -import net.minecraft.network.protocol.common.custom.CustomPacketPayload; - -/** - * Wraps a {@link FriendlyByteBuf}. - */ -public record BufCustomPacketPayload(Type _type, - byte[] payload) implements CustomPacketPayload { - public BufCustomPacketPayload(Type _type, RegistryFriendlyByteBuf payload) { - this(_type, payload.readByteArray()); - } - - @Override - public Type type() { - return this._type(); - } - - public void write(RegistryFriendlyByteBuf buf) { - buf.writeByteArray(payload); - } - - public static StreamCodec streamCodec(Type type) { - return ByteBufCodecs.BYTE_ARRAY.map(bytes -> new BufCustomPacketPayload(type, bytes), BufCustomPacketPayload::payload); - } -} diff --git a/neoforge/src/main/java/dev/architectury/networking/forge/BufCustomPacketPayload.java b/neoforge/src/main/java/dev/architectury/networking/forge/BufCustomPacketPayload.java deleted file mode 100644 index f2d43cac..00000000 --- a/neoforge/src/main/java/dev/architectury/networking/forge/BufCustomPacketPayload.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This file is part of architectury. - * Copyright (C) 2020, 2021, 2022 architectury - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package dev.architectury.networking.forge; - -import io.netty.buffer.ByteBuf; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.ByteBufCodecs; -import net.minecraft.network.codec.StreamCodec; -import net.minecraft.network.protocol.common.custom.CustomPacketPayload; - -/** - * Wraps a {@link FriendlyByteBuf} because NeoForge doesn't easily let us use the buf directly. - */ -public record BufCustomPacketPayload(Type type, byte[] payload) implements CustomPacketPayload { - public BufCustomPacketPayload(Type type, RegistryFriendlyByteBuf buf) { - this(type, buf.readByteArray()); - } - - public void write(RegistryFriendlyByteBuf buf) { - buf.writeByteArray(payload); - } - - public static StreamCodec streamCodec(Type type) { - return ByteBufCodecs.BYTE_ARRAY.map(bytes -> new BufCustomPacketPayload(type, bytes), BufCustomPacketPayload::payload); - } -} diff --git a/neoforge/src/main/java/dev/architectury/networking/forge/ClientNetworkingManager.java b/neoforge/src/main/java/dev/architectury/networking/forge/ClientNetworkingManager.java deleted file mode 100644 index 33974213..00000000 --- a/neoforge/src/main/java/dev/architectury/networking/forge/ClientNetworkingManager.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of architectury. - * Copyright (C) 2020, 2021, 2022 architectury - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package dev.architectury.networking.forge; - -import dev.architectury.impl.NetworkAggregator; -import dev.architectury.networking.NetworkManager; -import net.minecraft.client.Minecraft; -import net.minecraft.core.RegistryAccess; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.entity.player.Player; -import net.neoforged.api.distmarker.Dist; -import net.neoforged.api.distmarker.OnlyIn; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; -import net.neoforged.neoforge.common.NeoForge; - -import java.util.Collections; -import java.util.Set; - -@OnlyIn(Dist.CLIENT) -public class ClientNetworkingManager { - public static void initClient() { - NeoForge.EVENT_BUS.register(ClientNetworkingManager.class); - - NetworkManager.registerReceiver(NetworkManager.Side.S2C, NetworkManagerImpl.SYNC_IDS_S2C, Collections.emptyList(), (buffer, context) -> { - Set receivables = NetworkManagerImpl.serverReceivables; - int size = buffer.readInt(); - receivables.clear(); - for (int i = 0; i < size; i++) { - receivables.add(buffer.readResourceLocation()); - } - context.queue(() -> { - NetworkManager.sendToServer(NetworkManagerImpl.SYNC_IDS_C2S, NetworkManagerImpl.sendSyncPacket(NetworkAggregator.C2S_RECEIVER, context.registryAccess())); - }); - }); - } - - public static Player getClientPlayer() { - return Minecraft.getInstance().player; - } - - public static RegistryAccess getClientRegistryAccess() { - if (Minecraft.getInstance().level != null) { - return Minecraft.getInstance().level.registryAccess(); - } - - return Minecraft.getInstance().getConnection().registryAccess(); - } - - @SubscribeEvent - public static void loggedOut(ClientPlayerNetworkEvent.LoggingOut event) { - NetworkManagerImpl.serverReceivables.clear(); - } -} diff --git a/neoforge/src/main/java/dev/architectury/networking/forge/NetworkManagerImpl.java b/neoforge/src/main/java/dev/architectury/networking/forge/NetworkManagerImpl.java index 90016350..6889df51 100644 --- a/neoforge/src/main/java/dev/architectury/networking/forge/NetworkManagerImpl.java +++ b/neoforge/src/main/java/dev/architectury/networking/forge/NetworkManagerImpl.java @@ -20,7 +20,6 @@ package dev.architectury.networking.forge; -import com.google.common.collect.*; import com.mojang.logging.LogUtils; import dev.architectury.impl.NetworkAggregator; import dev.architectury.networking.NetworkManager; @@ -29,13 +28,12 @@ import dev.architectury.platform.hooks.EventBusesHooks; import dev.architectury.utils.ArchitecturyConstants; import dev.architectury.utils.Env; -import dev.architectury.utils.EnvExecutor; -import io.netty.buffer.Unpooled; +import net.minecraft.client.Minecraft; import net.minecraft.core.RegistryAccess; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.Packet; -import net.minecraft.network.protocol.PacketFlow; import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; @@ -45,43 +43,15 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.neoforged.api.distmarker.Dist; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.DistExecutor; +import net.neoforged.api.distmarker.OnlyIn; import net.neoforged.fml.common.Mod; -import net.neoforged.neoforge.event.entity.player.PlayerEvent; import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent; import net.neoforged.neoforge.network.handling.ISynchronizedWorkHandler; import org.slf4j.Logger; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; - -import static dev.architectury.networking.forge.ClientNetworkingManager.getClientPlayer; -import static dev.architectury.networking.forge.ClientNetworkingManager.getClientRegistryAccess; - @Mod.EventBusSubscriber(modid = ArchitecturyConstants.MOD_ID) public class NetworkManagerImpl { private static final Logger LOGGER = LogUtils.getLogger(); - static final ResourceLocation SYNC_IDS_S2C = new ResourceLocation("architectury:sync_ids_s2c"); - static final ResourceLocation SYNC_IDS_C2S = new ResourceLocation("architectury:sync_ids_c2s"); - static final Set serverReceivables = Sets.newHashSet(); - private static final Multimap clientReceivables = Multimaps.newMultimap(Maps.newHashMap(), Sets::newHashSet); - - static { - DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> ClientNetworkingManager::initClient); - NetworkManager.registerReceiver(NetworkManager.Side.C2S, SYNC_IDS_C2S, Collections.emptyList(), (buffer, context) -> { - Set receivables = (Set) clientReceivables.get(context.getPlayer()); - int size = buffer.readInt(); - receivables.clear(); - for (int i = 0; i < size; i++) { - receivables.add(buffer.readResourceLocation()); - } - }); - EnvExecutor.runInEnv(Env.SERVER, () -> () -> NetworkManager.registerS2CPayloadType(SYNC_IDS_S2C)); - } public static NetworkAggregator.Adaptor getAdaptor() { return new NetworkAggregator.Adaptor() { @@ -149,39 +119,40 @@ public RegistryAccess registryAccess() { }; } + @OnlyIn(Dist.CLIENT) public static boolean canServerReceive(ResourceLocation id) { - return serverReceivables.contains(id); + if (Minecraft.getInstance().getConnection() != null) { + return Minecraft.getInstance().getConnection().isConnected(id); + } else { + return false; + } } public static boolean canPlayerReceive(ServerPlayer player, ResourceLocation id) { - return clientReceivables.get(player).contains(id); + return player.connection.isConnected(id); } public static Packet createAddEntityPacket(Entity entity) { return SpawnEntityPacket.create(entity); } - static RegistryFriendlyByteBuf sendSyncPacket(Map map, RegistryAccess access) { - List availableIds = Lists.newArrayList(map.keySet()); - RegistryFriendlyByteBuf packetBuffer = new RegistryFriendlyByteBuf(Unpooled.buffer(), access); - packetBuffer.writeInt(availableIds.size()); - for (ResourceLocation availableId : availableIds) { - packetBuffer.writeResourceLocation(availableId); - } - return packetBuffer; - } - - @SubscribeEvent - public static void loggedIn(PlayerEvent.PlayerLoggedInEvent event) { - NetworkManager.sendToPlayer((ServerPlayer) event.getEntity(), SYNC_IDS_S2C, sendSyncPacket(NetworkAggregator.C2S_RECEIVER, event.getEntity().registryAccess())); + @OnlyIn(Dist.CLIENT) + public static Player getClientPlayer() { + return Minecraft.getInstance().player; } - @SubscribeEvent - public static void loggedOut(PlayerEvent.PlayerLoggedOutEvent event) { - clientReceivables.removeAll(event.getEntity()); - } - - static NetworkManager.Side side(PacketFlow flow) { - return flow.isClientbound() ? NetworkManager.Side.S2C : flow.isServerbound() ? NetworkManager.Side.C2S : null; + @OnlyIn(Dist.CLIENT) + public static RegistryAccess getClientRegistryAccess() { + if (Minecraft.getInstance().level != null) { + return Minecraft.getInstance().level.registryAccess(); + } else if (Minecraft.getInstance().getConnection() != null) { + return Minecraft.getInstance().getConnection().registryAccess(); + } else if (Minecraft.getInstance().gameMode != null) { + // Sometimes the packet is sent way too fast and is between the connection and the level, better safe than sorry + return Minecraft.getInstance().gameMode.connection.registryAccess(); + } + + // Fail-safe + return RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY); } }