Skip to content

Commit

Permalink
Fix syncing block entity attachments on load. (#4390)
Browse files Browse the repository at this point in the history
* Fix syncing block entity attachments on load.

Closes #4389

* Return success

(cherry picked from commit 7e31339)
  • Loading branch information
modmuss50 committed Jan 21, 2025
1 parent 53ee34a commit 8212661
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion fabric-data-attachment-api-v1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ testDependencies(project, [
':fabric-biome-api-v1',
':fabric-command-api-v2',
':fabric-rendering-v1',
':fabric-client-gametest-api-v1'
':fabric-client-gametest-api-v1',
':fabric-events-interaction-v0',
])
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.World;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void fabric_readAttachmentsFromNbt(NbtCompound nbt, RegistryWrapper.Wrapp
if (this.fabric_shouldTryToSync() && this.fabric_dataAttachments != null) {
this.fabric_dataAttachments.forEach((type, value) -> {
if (type.isSynced()) {
acknowledgeSynced(type, value);
acknowledgeSynced(type, value, wrapperLookup);
}
});
}
Expand All @@ -117,8 +118,9 @@ public boolean fabric_hasPersistentAttachments() {
}

@Unique
private void acknowledgeSynced(AttachmentType<?> type, Object value) {
acknowledgeSyncedEntry(type, AttachmentChange.create(fabric_getSyncTargetInfo(), type, value, fabric_getDynamicRegistryManager()));
private void acknowledgeSynced(AttachmentType<?> type, Object value, RegistryWrapper.WrapperLookup wrapperLookup) {
DynamicRegistryManager dynamicRegistryManager = (wrapperLookup instanceof DynamicRegistryManager drm) ? drm : fabric_getDynamicRegistryManager();
acknowledgeSyncedEntry(type, AttachmentChange.create(fabric_getSyncTargetInfo(), type, value, dynamicRegistryManager));
}

@Unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
Expand All @@ -36,6 +40,7 @@
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;

public class AttachmentTestMod implements ModInitializer {
public static final String MOD_ID = "fabric-data-attachment-api-v1-testmod";
Expand Down Expand Up @@ -92,5 +97,19 @@ public void onInitialize() {
GenerationStep.Feature.VEGETAL_DECORATION,
RegistryKey.of(RegistryKeys.PLACED_FEATURE, Identifier.of(MOD_ID, "set_attachment"))
);

UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
if (player.getStackInHand(hand).getItem() == Items.CARROT) {
BlockEntity blockEntity = world.getBlockEntity(hitResult.getBlockPos());

if (blockEntity != null) {
blockEntity.setAttached(SYNCED_WITH_ALL, true);
player.sendMessage(Text.literal("Attached"), false);
return ActionResult.SUCCESS;
}
}

return ActionResult.PASS;
});
}
}

0 comments on commit 8212661

Please sign in to comment.