Skip to content

Synced data attachments on players aren't re-sent when they change dimension #2510

Description

@legobmw99

Minecraft Version: 1.21.8

NeoForge Version: 21.8.11

Steps to Reproduce:

Here's a simple examplemod:

import com.mojang.serialization.Codec;
import net.minecraft.client.Minecraft;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.attachment.AttachmentSyncHandler;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.attachment.IAttachmentHolder;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

@Mod(value = "examplemod")
public class TattleTaleAttachment {

    private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES =
            DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, "examplemod");

    private static final Supplier<AttachmentType<Integer>> TT_ATTACHMENT =

            ATTACHMENT_TYPES.register("tt_attachment", () -> AttachmentType
                    .builder(() -> 0)
                    .serialize(Codec.INT.fieldOf("value"))
                    .sync(new AttachmentSyncHandler<>() {
                        @Override
                        public void write(RegistryFriendlyByteBuf buf, Integer attachment, boolean initialSync) {
                            System.out.println("writing: " + attachment);
                            buf.writeInt(attachment);
                        }

                        @Override
                        public @Nullable Integer read(IAttachmentHolder holder,
                                                      RegistryFriendlyByteBuf buf,
                                                      @Nullable Integer previousValue) {
                            int value = buf.readInt();
                            System.out.println("reading! " + value);
                            return value;
                        }

                    })
                    .copyOnDeath()
                    .build());

    public TattleTaleAttachment(IEventBus bus) {
        ATTACHMENT_TYPES.register(bus);
    }

    @EventBusSubscriber(modid = "examplemod")
    public final class Events {

        @SubscribeEvent
        public static void onJoinWorld(final PlayerEvent.PlayerLoggedInEvent event) {
            event.getEntity().setData(TT_ATTACHMENT, 1);
        }

        @SubscribeEvent
        public static void onClientTick(final ClientTickEvent.Post event) {
            if (Minecraft.getInstance().player != null) {
                System.out.println("Current value: " + Minecraft.getInstance().player.getData(TT_ATTACHMENT));
            }
        }
    }

}

When you log in, this will spam Current value: 1 in the console. Go to the nether, and you'll start seeing Current value: 0. Relog, and it turns back to 1.

Description of issue:

When changing dimension, my attachment is not synced. Before syncing was built in I was subscribing to PlayerEvent.PlayerChangedDimensionEvent and syncing there as well, and I think Neo should as well.

Metadata

Metadata

Assignees

Labels

triageNeeds triaging and confirmation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions