Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Syncable Data Attachments #1823

Draft
wants to merge 15 commits into
base: 1.21.x
Choose a base branch
from
Draft
Prev Previous commit
Format!
  • Loading branch information
Technici4n committed Jan 6, 2025
commit 8842c6223cb6226065c08dbd29acc7dbca016c8f
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
--- a/net/minecraft/commands/arguments/selector/EntitySelector.java
+++ b/net/minecraft/commands/arguments/selector/EntitySelector.java
@@ -10,15 +_,18 @@
import javax.annotation.Nullable;
import net.minecraft.Util;
import net.minecraft.advancements.critereon.MinMaxBounds;
+import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
+import net.minecraft.util.AbortableIterationConsumer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.flag.FeatureFlagSet;
+import net.minecraft.world.level.Level;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
@@ -105,7 +_,7 @@
}

Expand All @@ -44,7 +25,7 @@
}

- private void addEntities(List<Entity> p_121155_, ServerLevel p_121156_, @Nullable AABB p_352947_, Predicate<Entity> p_121158_) {
+ private void addEntities(List<Entity> p_121155_, Level p_121156_, @Nullable AABB p_352947_, Predicate<Entity> p_121158_) {
+ private void addEntities(List<Entity> p_121155_, net.minecraft.world.level.Level p_121156_, @Nullable AABB p_352947_, Predicate<Entity> p_121158_) {
int i = this.getResultLimit();
if (p_121155_.size() < i) {
if (p_352947_ != null) {
Expand All @@ -54,7 +35,7 @@
+ if (p_121156_ instanceof ServerLevel serverLevel) {
+ serverLevel.getEntities(this.type, p_121158_, p_121155_, i);
+ } else {
+ ((ClientLevel) p_121156_).entitiesForRendering().forEach(entity -> {
+ ((net.minecraft.client.multiplayer.ClientLevel) p_121156_).entitiesForRendering().forEach(entity -> {
+ if (p_121158_.test(entity) && p_121155_.size() < i) {
+ p_121155_.add(entity);
+ }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
--- a/net/minecraft/world/level/block/entity/BlockEntity.java
+++ b/net/minecraft/world/level/block/entity/BlockEntity.java
@@ -21,13 +_,15 @@
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.resources.ResourceLocation;
+import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
@@ -26,8 +_,9 @@
import net.minecraft.world.level.block.state.BlockState;
import org.slf4j.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,16 @@

package net.neoforged.neoforge.attachment;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

import io.netty.buffer.Unpooled;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.LevelChunk;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.common.extensions.IEntityExtension;
import net.neoforged.neoforge.common.util.FriendlyByteBufUtil;
import net.neoforged.neoforge.event.entity.living.LivingConversionEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.event.level.ChunkWatchEvent;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.connection.ConnectionType;
import net.neoforged.neoforge.network.payload.SyncAttachmentsPayload;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import net.neoforged.neoforge.registries.RegistryBuilder;
import net.neoforged.neoforge.registries.callback.AddCallback;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
@EventBusSubscriber(modid = NeoForgeVersion.MOD_ID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.attachment;

import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand Down Expand Up @@ -30,10 +38,6 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@ApiStatus.Internal
@EventBusSubscriber(modid = NeoForgeVersion.MOD_ID)
public final class AttachmentSync {
Expand All @@ -44,8 +48,8 @@ public final class AttachmentSync {
public static final Registry<AttachmentType<?>> SYNCED_ATTACHMENT_TYPES = new RegistryBuilder<>(
ResourceKey.<AttachmentType<?>>createRegistryKey(
ResourceLocation.fromNamespaceAndPath(NeoForgeVersion.MOD_ID, "synced_attachment_types")))
.sync(true)
.create();
.sync(true)
.create();

public static final AddCallback<AttachmentType<?>> ATTACHMENT_TYPE_ADD_CALLBACK = (registry, id, key, value) -> {
if (value.syncHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.attachment;

import net.minecraft.network.RegistryFriendlyByteBuf;
Expand All @@ -9,10 +14,10 @@
*
* <p>Sync is handled automatically in the following cases:
* <ul>
* <li>A client is receiving initial data for this attachment holder.</li>
* <li>An attachment is default-created through {@link IAttachmentHolder#getData(AttachmentType)}.</li>
* <li>An attachment is updated through {@link IAttachmentHolder#setData(AttachmentType, Object)}.</li>
* <li>An attachment is removed through {@link IAttachmentHolder#removeData(AttachmentType)}.</li>
* <li>A client is receiving initial data for this attachment holder.</li>
* <li>An attachment is default-created through {@link IAttachmentHolder#getData(AttachmentType)}.</li>
* <li>An attachment is updated through {@link IAttachmentHolder#setData(AttachmentType, Object)}.</li>
* <li>An attachment is removed through {@link IAttachmentHolder#removeData(AttachmentType)}.</li>
* </ul>
*
* <p>For other cases such as modifications to mutable synced attachments,
Expand All @@ -28,7 +33,7 @@ public interface IAttachmentSyncHandler<T> {
* <p>The returned value should be consistent for a given holder and player.
*
* @param holder the holder for the attachment, can be cast if the subtype is known
* @param to the player that might receive the data
* @param to the player that might receive the data
* @return {@code true} to send data to the player, {@code false} otherwise
*/
default boolean sendToPlayer(IAttachmentHolder holder, ServerPlayer to) {
Expand All @@ -51,7 +56,7 @@ default boolean sendToPlayer(IAttachmentHolder holder, ServerPlayer to) {
/**
* Reads attachment data on the client side.
*
* @param holder the attachment holder, can be cast if the subtype is known
* @param holder the attachment holder, can be cast if the subtype is known
* @param previousValue the previous value of the attachment, or {@code null} if there was no previous value
* @return the new value of the attachment, or {@code null} if the attachment should be removed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.status.ChunkStatus;
import net.neoforged.neoforge.attachment.AttachmentInternals;
import net.neoforged.neoforge.attachment.AttachmentSync;
import net.neoforged.neoforge.common.world.AuxiliaryLightManager;
import net.neoforged.neoforge.common.world.LevelChunkAuxiliaryLightManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.network.payload;

import java.util.List;
import net.minecraft.core.BlockPos;
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;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.LevelChunk;
import net.neoforged.neoforge.attachment.AttachmentHolder;
import net.neoforged.neoforge.attachment.AttachmentInternals;
import net.neoforged.neoforge.attachment.AttachmentSync;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@ApiStatus.Internal
public record SyncAttachmentsPayload(
Target target,
List<AttachmentType<?>> types,
byte[] syncPayload)
implements CustomPacketPayload {

public static final Type<SyncAttachmentsPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(NeoForgeVersion.MOD_ID, "sync_attachments"));;
public static final StreamCodec<RegistryFriendlyByteBuf, SyncAttachmentsPayload> STREAM_CODEC = StreamCodec.composite(
Target.STREAM_CODEC,
Expand All @@ -42,7 +40,6 @@ public record SyncAttachmentsPayload(
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}

public sealed interface Target {
StreamCodec<RegistryFriendlyByteBuf, Target> STREAM_CODEC = StreamCodec.of(
(buf, target) -> {
Expand All @@ -60,7 +57,7 @@ public sealed interface Target {
buf.writeVarInt(entityTarget.entity());
}
case LevelTarget ignored -> {
buf.writeByte(3);
buf.writeByte(3);
}
}
},
Expand All @@ -85,8 +82,11 @@ public sealed interface Target {
}

public record BlockEntityTarget(BlockPos pos) implements Target {}

public record ChunkTarget(ChunkPos pos) implements Target {}

public record EntityTarget(int entity) implements Target {}

// TODO: Should there be a way to sync overworld data while the player is in another level? (For "global" data).
public record LevelTarget() implements Target {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.attachment.AttachmentInternals;
import net.neoforged.neoforge.attachment.AttachmentSync;
import org.jetbrains.annotations.ApiStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.oldtest;

import com.mojang.brigadier.Command;
Expand All @@ -8,6 +13,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.serialization.Codec;
import java.util.function.Supplier;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
Expand All @@ -29,8 +35,6 @@
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

@Mod(AttachmentSyncTest.MOD_ID)
public class AttachmentSyncTest {
public static final String MOD_ID = "attachment_sync_test";
Expand Down Expand Up @@ -97,8 +101,7 @@ private static void registerCommands(CommandDispatcher<CommandSourceStack> dispa
.then(
addGetSet(
Commands.argument("entity", EntityArgument.entity()),
context -> EntityArgument.getEntity(context, "entity")
)))
context -> EntityArgument.getEntity(context, "entity"))))
.then(
addGetSet(
Commands.literal("level"),
Expand Down
Loading