Skip to content

Commit

Permalink
More 24w05a
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Jan 31, 2024
1 parent 2a05555 commit d6e8ff4
Show file tree
Hide file tree
Showing 37 changed files with 216 additions and 298 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"replace": false,
"values": [
"fabric-biome-api-v1-testmod:custom_plains",
"fabric-biome-api-v1-testmod:test_end_highlands"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package net.fabricmc.fabric.impl.attachment;

import net.minecraft.registry.RegistryWrapper;

import org.jetbrains.annotations.Nullable;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.PersistentState;

Expand All @@ -38,8 +37,8 @@ public AttachmentPersistentState(ServerWorld world) {
this.wasSerialized = worldTarget.fabric_hasPersistentAttachments();
}

public static AttachmentPersistentState read(ServerWorld world, @Nullable NbtCompound nbt) {
((AttachmentTargetImpl) world).fabric_readAttachmentsFromNbt(nbt);
public static AttachmentPersistentState read(ServerWorld world, @Nullable NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
((AttachmentTargetImpl) world).fabric_readAttachmentsFromNbt(nbt, wrapperLookup);
return new AttachmentPersistentState(world);
}

Expand All @@ -49,10 +48,9 @@ public boolean isDirty() {
return wasSerialized || worldTarget.fabric_hasPersistentAttachments();
}

// TODO 1.20.5 Add WrapperLookup to API (?)
@Override
public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
worldTarget.fabric_writeAttachmentsToNbt(nbt);
worldTarget.fabric_writeAttachmentsToNbt(nbt, wrapperLookup);
return nbt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;
Expand All @@ -36,7 +38,7 @@ public class AttachmentSerializingImpl {
private static final Logger LOGGER = LoggerFactory.getLogger("fabric-data-attachment-api-v1");

@SuppressWarnings("unchecked")
public static void serializeAttachmentData(NbtCompound nbt, @Nullable IdentityHashMap<AttachmentType<?>, ?> attachments) {
public static void serializeAttachmentData(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup, @Nullable IdentityHashMap<AttachmentType<?>, ?> attachments) {
if (attachments == null) {
return;
}
Expand All @@ -48,7 +50,8 @@ public static void serializeAttachmentData(NbtCompound nbt, @Nullable IdentityHa
Codec<Object> codec = (Codec<Object>) type.persistenceCodec();

if (codec != null) {
codec.encodeStart(NbtOps.INSTANCE, entry.getValue())
RegistryOps<NbtElement> registryOps = RegistryOps.of(NbtOps.INSTANCE, wrapperLookup);
codec.encodeStart(registryOps, entry.getValue())
.get()
.ifRight(partial -> {
LOGGER.warn("Couldn't serialize attachment " + type.identifier() + ", skipping. Error:");
Expand All @@ -61,7 +64,7 @@ public static void serializeAttachmentData(NbtCompound nbt, @Nullable IdentityHa
nbt.put(AttachmentTarget.NBT_ATTACHMENT_KEY, compound);
}

public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentData(NbtCompound nbt) {
public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentData(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
var attachments = new IdentityHashMap<AttachmentType<?>, Object>();

if (nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY, NbtElement.COMPOUND_TYPE)) {
Expand All @@ -78,6 +81,7 @@ public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentDa
Codec<?> codec = type.persistenceCodec();

if (codec != null) {
RegistryOps<NbtElement> registryOps = RegistryOps.of(NbtOps.INSTANCE, wrapperLookup);
codec.parse(NbtOps.INSTANCE, compound.get(key))
.get()
.ifRight(partial -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jetbrains.annotations.Nullable;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.RegistryWrapper;

import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
Expand Down Expand Up @@ -53,11 +54,11 @@ static void copyOnRespawn(AttachmentTarget original, AttachmentTarget target, bo
throw new UnsupportedOperationException("Implemented via mixin");
}

default void fabric_writeAttachmentsToNbt(NbtCompound nbt) {
default void fabric_writeAttachmentsToNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
throw new UnsupportedOperationException("Implemented via mixin");
}

default void fabric_readAttachmentsFromNbt(NbtCompound nbt) {
default void fabric_readAttachmentsFromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
throw new UnsupportedOperationException("Implemented via mixin");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.WorldChunk;
Expand Down Expand Up @@ -85,13 +86,13 @@ public boolean hasAttached(AttachmentType<?> type) {
}

@Override
public void fabric_writeAttachmentsToNbt(NbtCompound nbt) {
AttachmentSerializingImpl.serializeAttachmentData(nbt, fabric_dataAttachments);
public void fabric_writeAttachmentsToNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
AttachmentSerializingImpl.serializeAttachmentData(nbt, wrapperLookup, fabric_dataAttachments);
}

@Override
public void fabric_readAttachmentsFromNbt(NbtCompound nbt) {
fabric_dataAttachments = AttachmentSerializingImpl.deserializeAttachmentData(nbt);
public void fabric_readAttachmentsFromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
fabric_dataAttachments = AttachmentSerializingImpl.deserializeAttachmentData(nbt, wrapperLookup);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package net.fabricmc.fabric.mixin.attachment;

import net.minecraft.registry.RegistryWrapper;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.RegistryWrapper;

import net.fabricmc.fabric.impl.attachment.AttachmentTargetImpl;

Expand All @@ -35,14 +34,14 @@ abstract class BlockEntityMixin implements AttachmentTargetImpl {
at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/BlockEntity;readNbt(Lnet/minecraft/nbt/NbtCompound;Lnet/minecraft/registry/RegistryWrapper$WrapperLookup;)V")
)
private static void readBlockEntityAttachments(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup, String string, BlockEntity blockEntity, CallbackInfoReturnable<BlockEntity> cir) {
((AttachmentTargetImpl) blockEntity).fabric_readAttachmentsFromNbt(nbt);
((AttachmentTargetImpl) blockEntity).fabric_readAttachmentsFromNbt(nbt, wrapperLookup);
}

@Inject(
method = "createNbt",
at = @At("RETURN")
)
private void writeBlockEntityAttachments(CallbackInfoReturnable<NbtCompound> cir) {
this.fabric_writeAttachmentsToNbt(cir.getReturnValue());
private void writeBlockEntityAttachments(RegistryWrapper.WrapperLookup wrapperLookup, CallbackInfoReturnable<NbtCompound> cir) {
this.fabric_writeAttachmentsToNbt(cir.getReturnValue(), wrapperLookup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package net.fabricmc.fabric.mixin.attachment;

import java.util.function.Function;
import java.util.function.BiFunction;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -25,6 +25,7 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.DynamicRegistryManager;

import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;

Expand All @@ -39,12 +40,12 @@ public class BlockEntityUpdateS2CPacketMixin {
method = "create(Lnet/minecraft/block/entity/BlockEntity;)Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;create(Lnet/minecraft/block/entity/BlockEntity;Ljava/util/function/Function;)Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;"
target = "Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;create(Lnet/minecraft/block/entity/BlockEntity;Ljava/util/function/BiFunction;)Lnet/minecraft/network/packet/s2c/play/BlockEntityUpdateS2CPacket;"
)
)
private static Function<BlockEntity, NbtCompound> stripPersistentAttachmentData(Function<BlockEntity, NbtCompound> getter) {
return be -> {
NbtCompound nbt = getter.apply(be);
private static BiFunction<BlockEntity, DynamicRegistryManager, NbtCompound> stripPersistentAttachmentData(BiFunction<BlockEntity, DynamicRegistryManager, NbtCompound> getter) {
return (be, drm) -> {
NbtCompound nbt = getter.apply(be, drm);
nbt.remove(AttachmentTarget.NBT_ATTACHMENT_KEY);
return nbt;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class ChunkSerializerMixin {
method = "deserialize"
)
private static WorldChunk readChunkAttachments(WorldChunk chunk, ServerWorld world, PointOfInterestStorage poiStorage, ChunkPos chunkPos, NbtCompound nbt) {
((AttachmentTargetImpl) chunk).fabric_readAttachmentsFromNbt(nbt);
((AttachmentTargetImpl) chunk).fabric_readAttachmentsFromNbt(nbt, world.getRegistryManager());
return chunk;
}

Expand All @@ -53,7 +53,7 @@ private static WorldChunk readChunkAttachments(WorldChunk chunk, ServerWorld wor
)
private static void writeChunkAttachments(ServerWorld world, Chunk chunk, CallbackInfoReturnable<NbtCompound> cir) {
if (chunk.getStatus().getChunkType() == ChunkStatus.ChunkType.LEVELCHUNK) {
((AttachmentTargetImpl) chunk).fabric_writeAttachmentsToNbt(cir.getReturnValue());
((AttachmentTargetImpl) chunk).fabric_writeAttachmentsToNbt(cir.getReturnValue(), world.getRegistryManager());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,36 @@
package net.fabricmc.fabric.mixin.attachment;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.world.World;

import net.fabricmc.fabric.impl.attachment.AttachmentTargetImpl;

@Mixin(Entity.class)
abstract class EntityMixin implements AttachmentTargetImpl {
@Shadow
public abstract World getWorld();

@Inject(
at = @At(value = "INVOKE", target = "net/minecraft/entity/Entity.readCustomDataFromNbt(Lnet/minecraft/nbt/NbtCompound;)V"),
method = "readNbt"
)
private void readEntityAttachments(NbtCompound nbt, CallbackInfo cir) {
this.fabric_readAttachmentsFromNbt(nbt);
this.fabric_readAttachmentsFromNbt(nbt, getWorld().getRegistryManager());
}

@Inject(
at = @At(value = "INVOKE", target = "net/minecraft/entity/Entity.writeCustomDataToNbt(Lnet/minecraft/nbt/NbtCompound;)V"),
method = "writeNbt"
)
private void writeEntityAttachments(NbtCompound nbt, CallbackInfoReturnable<NbtCompound> cir) {
this.fabric_writeAttachmentsToNbt(nbt);
this.fabric_writeAttachmentsToNbt(nbt, getWorld().getRegistryManager());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@

package net.fabricmc.fabric.mixin.attachment;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.PersistentState;

import net.fabricmc.fabric.impl.attachment.AttachmentPersistentState;

@Mixin(ServerWorld.class)
abstract class ServerWorldMixin {
@Shadow
@Final
private MinecraftServer server;

@Inject(at = @At("TAIL"), method = "<init>")
private void createAttachmentsPersistentState(CallbackInfo ci) {
// Force persistent state creation
ServerWorld world = (ServerWorld) (Object) this;
var type = new PersistentState.Type<>(
() -> new AttachmentPersistentState(world),
(nbt, wrapperLookup) -> AttachmentPersistentState.read(world, nbt),
(nbt, wrapperLookup) -> AttachmentPersistentState.read(world, nbt, server.getRegistryManager()),
null // Object builder API 12.1.0 and later makes this a no-op
);
world.getPersistentStateManager().getOrCreate(type, AttachmentPersistentState.ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ void testStaticReadWrite() {
map.put(dummy, 0.5d);
var fakeSave = new NbtCompound();

AttachmentSerializingImpl.serializeAttachmentData(fakeSave, map);
AttachmentSerializingImpl.serializeAttachmentData(fakeSave, null, map);
assertTrue(fakeSave.contains(AttachmentTarget.NBT_ATTACHMENT_KEY, NbtElement.COMPOUND_TYPE));
assertTrue(fakeSave.getCompound(AttachmentTarget.NBT_ATTACHMENT_KEY).contains(dummy.identifier().toString()));

map = AttachmentSerializingImpl.deserializeAttachmentData(fakeSave);
map = AttachmentSerializingImpl.deserializeAttachmentData(fakeSave, null);
assertEquals(1, map.size());
Map.Entry<AttachmentType<?>, Object> entry = map.entrySet().stream().findFirst().orElseThrow();
// in this case the key should be the exact same object
Expand Down Expand Up @@ -194,9 +194,9 @@ void testBlockEntityPersistence() {

int expected = 1;
blockEntity.setAttached(PERSISTENT, expected);
NbtCompound fakeSave = blockEntity.createNbtWithId();
NbtCompound fakeSave = blockEntity.createNbtWithId(null);

blockEntity = BlockEntity.createFromNbt(BlockPos.ORIGIN, mock(), fakeSave);
blockEntity = BlockEntity.createFromNbt(BlockPos.ORIGIN, mock(), fakeSave, null);
assertNotNull(blockEntity);
assertTrue(blockEntity.hasAttached(PERSISTENT));
assertEquals(expected, blockEntity.getAttached(PERSISTENT));
Expand All @@ -211,10 +211,10 @@ void testWorldPersistentState() {

int expected = 1;
world.setAttached(PERSISTENT, expected);
NbtCompound fakeSave = state.writeNbt(new NbtCompound());
NbtCompound fakeSave = state.writeNbt(new NbtCompound(), null);

world = mock(ServerWorld.class, CALLS_REAL_METHODS);
AttachmentPersistentState.read(world, fakeSave);
AttachmentPersistentState.read(world, fakeSave, null);
assertTrue(world.hasAttached(PERSISTENT));
assertEquals(expected, world.getAttached(PERSISTENT));
}
Expand Down
Loading

0 comments on commit d6e8ff4

Please sign in to comment.