From 13a40c6632002ac4d2edc72df214ee7d8fed40e1 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Mon, 30 Dec 2024 08:01:04 -0500 Subject: [PATCH] Don't track fake player network addons (#3977) (#4331) * Don't track fake player network addons * Review feedback Co-authored-by: modmuss --- fabric-events-interaction-v0/build.gradle | 2 +- .../interaction/FakePlayerNetworkHandler.java | 4 +++- .../src/main/resources/fabric.mod.json | 1 + .../networking/UntrackedNetworkHandler.java | 21 +++++++++++++++++++ .../ServerPlayNetworkHandlerMixin.java | 8 +++++-- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/UntrackedNetworkHandler.java diff --git a/fabric-events-interaction-v0/build.gradle b/fabric-events-interaction-v0/build.gradle index 9ced6ef8b4..60da64c69a 100644 --- a/fabric-events-interaction-v0/build.gradle +++ b/fabric-events-interaction-v0/build.gradle @@ -1,3 +1,3 @@ version = getSubprojectVersion(project) -moduleDependencies(project, ['fabric-api-base']) +moduleDependencies(project, ['fabric-api-base', 'fabric-networking-api-v1']) diff --git a/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/impl/event/interaction/FakePlayerNetworkHandler.java b/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/impl/event/interaction/FakePlayerNetworkHandler.java index ba3fe0fe55..edcb79d373 100644 --- a/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/impl/event/interaction/FakePlayerNetworkHandler.java +++ b/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/impl/event/interaction/FakePlayerNetworkHandler.java @@ -25,7 +25,9 @@ import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; -public class FakePlayerNetworkHandler extends ServerPlayNetworkHandler { +import net.fabricmc.fabric.impl.networking.UntrackedNetworkHandler; + +public final class FakePlayerNetworkHandler extends ServerPlayNetworkHandler implements UntrackedNetworkHandler { private static final ClientConnection FAKE_CONNECTION = new ClientConnection(NetworkSide.CLIENTBOUND); public FakePlayerNetworkHandler(ServerPlayerEntity player) { diff --git a/fabric-events-interaction-v0/src/main/resources/fabric.mod.json b/fabric-events-interaction-v0/src/main/resources/fabric.mod.json index cc6234340a..e86ca4915c 100644 --- a/fabric-events-interaction-v0/src/main/resources/fabric.mod.json +++ b/fabric-events-interaction-v0/src/main/resources/fabric.mod.json @@ -18,6 +18,7 @@ "depends": { "fabricloader": ">=0.4.0", "fabric-api-base": "*", + "fabric-networking-api-v1": "*", "minecraft": ">=1.15-alpha.19.37.a" }, "entrypoints": { diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/UntrackedNetworkHandler.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/UntrackedNetworkHandler.java new file mode 100644 index 0000000000..19f7be7f5b --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/UntrackedNetworkHandler.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 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.networking; + +// An internal marker interface used by the fake player API to prevent the network addon from being tracked. +public interface UntrackedNetworkHandler { +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java index 675ede32b7..95848211f3 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java @@ -34,6 +34,7 @@ import net.fabricmc.fabric.impl.networking.DisconnectPacketSource; import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; +import net.fabricmc.fabric.impl.networking.UntrackedNetworkHandler; import net.fabricmc.fabric.impl.networking.server.ServerPlayNetworkAddon; // We want to apply a bit earlier than other mods which may not use us in order to prevent refCount issues @@ -52,8 +53,11 @@ abstract class ServerPlayNetworkHandlerMixin implements NetworkHandlerExtensions @Inject(method = "", at = @At("RETURN")) private void initAddon(CallbackInfo ci) { this.addon = new ServerPlayNetworkAddon((ServerPlayNetworkHandler) (Object) this, this.server); - // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field - this.addon.lateInit(); + + if (!(this instanceof UntrackedNetworkHandler)) { + // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field + this.addon.lateInit(); + } } @Inject(method = "onCustomPayload", at = @At("HEAD"), cancellable = true)