From aa63df6e1f8d8907462670f196de62f26259640a Mon Sep 17 00:00:00 2001 From: asie Date: Tue, 18 Dec 2018 01:11:05 +0100 Subject: [PATCH] additional hardening for registry desync issues --- .../impl/FabricAPIClientInitiailizer.java | 33 +++++++++++++++++++ .../fabric/mixin/registry/MixinBootstrap.java | 3 -- .../fabric/registry/RegistrySyncManager.java | 32 +++++++++--------- src/main/resources/fabric.mod.json | 3 +- 4 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 src/main/java/net/fabricmc/fabric/impl/FabricAPIClientInitiailizer.java diff --git a/src/main/java/net/fabricmc/fabric/impl/FabricAPIClientInitiailizer.java b/src/main/java/net/fabricmc/fabric/impl/FabricAPIClientInitiailizer.java new file mode 100644 index 000000000..da5d242ba --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/impl/FabricAPIClientInitiailizer.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.networking.CustomPayloadPacketRegistry; +import net.fabricmc.fabric.registry.RegistrySyncManager; +import net.minecraft.client.MinecraftClient; +import net.minecraft.server.integrated.IntegratedServer; + +public class FabricAPIClientInitiailizer implements ClientModInitializer { + @Override + public void onInitializeClient() { + CustomPayloadPacketRegistry.CLIENT.register(RegistrySyncManager.ID, (ctx, buf) -> { + // if not hosting server, apply packet + RegistrySyncManager.receivePacket(ctx, buf, !MinecraftClient.getInstance().method_1496()); + }); + } +} diff --git a/src/main/java/net/fabricmc/fabric/mixin/registry/MixinBootstrap.java b/src/main/java/net/fabricmc/fabric/mixin/registry/MixinBootstrap.java index 6e9486f29..618e03821 100644 --- a/src/main/java/net/fabricmc/fabric/mixin/registry/MixinBootstrap.java +++ b/src/main/java/net/fabricmc/fabric/mixin/registry/MixinBootstrap.java @@ -52,8 +52,5 @@ public class MixinBootstrap { ((ListenableRegistry) Registry.BLOCK).registerListener(new BootstrapBlockRegistryListener()); ((ListenableRegistry) Registry.FLUID).registerListener(new BootstrapFluidRegistryListener()); ((ListenableRegistry) Registry.ITEM).registerListener(new BootstrapItemRegistryListener()); - - // The packet code is not side-specific, so this is fine! - CustomPayloadPacketRegistry.CLIENT.register(RegistrySyncManager.ID, RegistrySyncManager::receivePacket); } } \ No newline at end of file diff --git a/src/main/java/net/fabricmc/fabric/registry/RegistrySyncManager.java b/src/main/java/net/fabricmc/fabric/registry/RegistrySyncManager.java index b15e3057c..f6889b979 100644 --- a/src/main/java/net/fabricmc/fabric/registry/RegistrySyncManager.java +++ b/src/main/java/net/fabricmc/fabric/registry/RegistrySyncManager.java @@ -51,23 +51,25 @@ public final class RegistrySyncManager { return packet; } - public static void receivePacket(PacketContext context, PacketByteBuf buf) { + public static void receivePacket(PacketContext context, PacketByteBuf buf, boolean accept) { CompoundTag compound = buf.readCompoundTag(); - try { - context.getTaskQueue().executeFuture(() -> { - try { - apply(compound, false); - } catch (RemapException e) { - // TODO: log error properly - e.printStackTrace(); - } - }).get(30, TimeUnit.SECONDS); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (InterruptedException | TimeoutException e) { - // TODO: better error handling - new Exception("Failed to apply received packets in time!", e).printStackTrace(); + if (accept) { + try { + context.getTaskQueue().executeFuture(() -> { + try { + apply(compound, false); + } catch (RemapException e) { + // TODO: log error properly + e.printStackTrace(); + } + }).get(30, TimeUnit.SECONDS); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (InterruptedException | TimeoutException e) { + // TODO: better error handling + new Exception("Failed to apply received packets in time!", e).printStackTrace(); + } } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index eae9dac45..66d93df84 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -6,7 +6,8 @@ "description": "Core API module providing key hooks and intercompatibility features.", "license": "Apache-2.0", "initializers": [ - "net.fabricmc.fabric.impl.FabricAPIInitializer" + "net.fabricmc.fabric.impl.FabricAPIInitializer", + "net.fabricmc.fabric.impl.FabricAPIClientInitializer" ], "mixins": { "client": "net.fabricmc.fabric.mixins.client.json",