Skip to content

Commit

Permalink
Merge branch '1.20.5' into tracked-data-handler-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Feb 19, 2024
2 parents 03d9015 + 4fcbeea commit cad7e71
Show file tree
Hide file tree
Showing 40 changed files with 747 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,33 @@

package net.fabricmc.fabric.test.biome;

import net.minecraft.registry.Registerable;
import net.minecraft.registry.RegistryBuilder;
import net.minecraft.registry.RegistryEntryLookup;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.ConfiguredFeatures;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.PlacedFeature;
import net.minecraft.world.gen.feature.PlacedFeatures;
import net.minecraft.world.gen.placementmodifier.BiomePlacementModifier;
import net.minecraft.world.gen.placementmodifier.SquarePlacementModifier;

import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;

public class DataGeneratorEntrypoint implements net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint {
public static final RegistryKey<ConfiguredFeature<?, ?>> COMMON_DESERT_WELL = RegistryKey.of(
RegistryKeys.CONFIGURED_FEATURE,
new Identifier(FabricBiomeTest.MOD_ID, "fab_desert_well")
);
public static final RegistryKey<PlacedFeature> PLACED_COMMON_DESERT_WELL = RegistryKey.of(
RegistryKeys.PLACED_FEATURE,
new Identifier(FabricBiomeTest.MOD_ID, "fab_desert_well")
);

@Override
public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {
FabricDataGenerator.Pack pack = dataGenerator.createPack();
Expand All @@ -31,6 +52,24 @@ public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {

@Override
public void buildRegistry(RegistryBuilder registryBuilder) {
registryBuilder.addRegistry(RegistryKeys.CONFIGURED_FEATURE, this::bootstrapConfiguredFeatures);
registryBuilder.addRegistry(RegistryKeys.PLACED_FEATURE, this::bootstrapPlacedFeatures);
registryBuilder.addRegistry(RegistryKeys.BIOME, TestBiomes::bootstrap);
}

private void bootstrapConfiguredFeatures(Registerable<ConfiguredFeature<?, ?>> registerable) {
ConfiguredFeatures.register(registerable, COMMON_DESERT_WELL, Feature.DESERT_WELL);
}

private void bootstrapPlacedFeatures(Registerable<PlacedFeature> registerable) {
RegistryEntryLookup<ConfiguredFeature<?, ?>> configuredFeatures = registerable.getRegistryLookup(RegistryKeys.CONFIGURED_FEATURE);
RegistryEntry<ConfiguredFeature<?, ?>> commonDesertWell = configuredFeatures.getOrThrow(COMMON_DESERT_WELL);

// The placement config is taken from the vanilla desert well, but no randomness
PlacedFeatures.register(registerable, PLACED_COMMON_DESERT_WELL, commonDesertWell,
SquarePlacementModifier.of(),
PlacedFeatures.MOTION_BLOCKING_HEIGHTMAP,
BiomePlacementModifier.of()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.test.biome;

import static net.fabricmc.fabric.test.biome.DataGeneratorEntrypoint.PLACED_COMMON_DESERT_WELL;

import com.google.common.base.Preconditions;

import net.minecraft.registry.RegistryKey;
Expand All @@ -25,8 +27,6 @@
import net.minecraft.world.biome.BiomeKeys;
import net.minecraft.world.biome.source.util.MultiNoiseUtil;
import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.PlacedFeature;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
Expand All @@ -48,15 +48,6 @@
public class FabricBiomeTest implements ModInitializer {
public static final String MOD_ID = "fabric-biome-api-v1-testmod";

public static final RegistryKey<ConfiguredFeature<?, ?>> COMMON_DESERT_WELL = RegistryKey.of(
RegistryKeys.CONFIGURED_FEATURE,
new Identifier(FabricBiomeTest.MOD_ID, "fab_desert_well")
);
public static final RegistryKey<PlacedFeature> PLACED_COMMON_DESERT_WELL = RegistryKey.of(
RegistryKeys.PLACED_FEATURE,
new Identifier(FabricBiomeTest.MOD_ID, "fab_desert_well")
);

@Override
public void onInitialize() {
Preconditions.checkArgument(NetherBiomes.canGenerateInNether(BiomeKeys.NETHER_WASTES));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@

package net.fabricmc.fabric.test.biome;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.PlacedFeature;
import net.minecraft.world.gen.feature.PlacedFeatures;
import net.minecraft.world.gen.placementmodifier.BiomePlacementModifier;
import net.minecraft.world.gen.placementmodifier.SquarePlacementModifier;

import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricDynamicRegistryProvider;
Expand All @@ -41,17 +31,9 @@ public WorldgenProvider(FabricDataOutput output, CompletableFuture<RegistryWrapp

@Override
protected void configure(RegistryWrapper.WrapperLookup registries, Entries entries) {
final RegistryWrapper.Impl<Biome> biomeRegistry = registries.getWrapperOrThrow(RegistryKeys.BIOME);

entries.addAll(biomeRegistry);

ConfiguredFeature<?, ?> COMMON_DESERT_WELL = new ConfiguredFeature<>(Feature.DESERT_WELL, DefaultFeatureConfig.INSTANCE);

RegistryEntry<ConfiguredFeature<?, ?>> featureRef = entries.add(FabricBiomeTest.COMMON_DESERT_WELL, COMMON_DESERT_WELL);

// The placement config is taken from the vanilla desert well, but no randomness
PlacedFeature PLACED_COMMON_DESERT_WELL = new PlacedFeature(featureRef, List.of(SquarePlacementModifier.of(), PlacedFeatures.MOTION_BLOCKING_HEIGHTMAP, BiomePlacementModifier.of()));
entries.add(FabricBiomeTest.PLACED_COMMON_DESERT_WELL, PLACED_COMMON_DESERT_WELL);
entries.addAll(registries.getWrapperOrThrow(RegistryKeys.BIOME));
entries.add(registries.getWrapperOrThrow(RegistryKeys.CONFIGURED_FEATURE), DataGeneratorEntrypoint.COMMON_DESERT_WELL);
entries.add(registries.getWrapperOrThrow(RegistryKeys.PLACED_FEATURE), DataGeneratorEntrypoint.PLACED_COMMON_DESERT_WELL);
}

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

package net.fabricmc.fabric.mixin.content.registry;

import com.llamalad7.mixinextras.sugar.Local;
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 org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import net.minecraft.block.BlockState;
import net.minecraft.entity.ai.pathing.LandPathNodeMaker;
Expand All @@ -36,8 +36,8 @@ public class LandPathNodeMakerMixin {
/**
* Overrides the node type for the specified position, if the position is a direct target in a path.
*/
@Inject(method = "getCommonNodeType", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/BlockView;getBlockState(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/BlockState;"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
private static void getCommonNodeType(BlockView world, BlockPos pos, CallbackInfoReturnable<PathNodeType> cir, BlockState state) {
@Inject(method = "getCommonNodeType", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;getBlock()Lnet/minecraft/block/Block;"), cancellable = true)
private static void getCommonNodeType(BlockView world, BlockPos pos, CallbackInfoReturnable<PathNodeType> cir, @Local BlockState state) {
PathNodeType nodeType = LandPathNodeTypesRegistry.getPathNodeType(state, world, pos, false);

if (nodeType != null) {
Expand All @@ -48,9 +48,9 @@ private static void getCommonNodeType(BlockView world, BlockPos pos, CallbackInf
/**
* Overrides the node type for the specified position, if the position is found as neighbor block in a path.
*/
@Inject(method = "getNodeTypeFromNeighbors", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/BlockView;getBlockState(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/BlockState;"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
private static void getNodeTypeFromNeighbors(BlockView world, BlockPos.Mutable pos, PathNodeType nodeType, CallbackInfoReturnable<PathNodeType> cir, int i, int j, int k, int l, int m, int n, BlockState state) {
PathNodeType neighborNodeType = LandPathNodeTypesRegistry.getPathNodeType(state, world, pos, true);
@Inject(method = "getNodeTypeFromNeighbors", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/entity/ai/pathing/LandPathNodeMaker;getCommonNodeType(Lnet/minecraft/world/BlockView;Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/entity/ai/pathing/PathNodeType;"), cancellable = true)
private static void getNodeTypeFromNeighbors(BlockView world, BlockPos.Mutable pos, PathNodeType nodeType, CallbackInfoReturnable<PathNodeType> cir) {
PathNodeType neighborNodeType = LandPathNodeTypesRegistry.getPathNodeType(world.getBlockState(pos), world, pos, true);

if (neighborNodeType != null) {
cir.setReturnValue(neighborNodeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AttachmentSerializingImpl {

@SuppressWarnings("unchecked")
public static void serializeAttachmentData(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup, @Nullable IdentityHashMap<AttachmentType<?>, ?> attachments) {
if (attachments == null) {
if (attachments == null || attachments.isEmpty()) {
return;
}

Expand All @@ -50,7 +50,7 @@ public static void serializeAttachmentData(NbtCompound nbt, RegistryWrapper.Wrap
Codec<Object> codec = (Codec<Object>) type.persistenceCodec();

if (codec != null) {
RegistryOps<NbtElement> registryOps = RegistryOps.of(NbtOps.INSTANCE, wrapperLookup);
RegistryOps<NbtElement> registryOps = wrapperLookup.method_57093(NbtOps.INSTANCE);
codec.encodeStart(registryOps, entry.getValue())
.get()
.ifRight(partial -> {
Expand All @@ -64,10 +64,10 @@ public static void serializeAttachmentData(NbtCompound nbt, RegistryWrapper.Wrap
nbt.put(AttachmentTarget.NBT_ATTACHMENT_KEY, compound);
}

@Nullable
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)) {
var attachments = new IdentityHashMap<AttachmentType<?>, Object>();
NbtCompound compound = nbt.getCompound(AttachmentTarget.NBT_ATTACHMENT_KEY);

for (String key : compound.getKeys()) {
Expand All @@ -81,7 +81,7 @@ public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentDa
Codec<?> codec = type.persistenceCodec();

if (codec != null) {
RegistryOps<NbtElement> registryOps = RegistryOps.of(NbtOps.INSTANCE, wrapperLookup);
RegistryOps<NbtElement> registryOps = wrapperLookup.method_57093(NbtOps.INSTANCE);
codec.parse(registryOps, compound.get(key))
.get()
.ifRight(partial -> {
Expand All @@ -93,9 +93,15 @@ public static IdentityHashMap<AttachmentType<?>, Object> deserializeAttachmentDa
);
}
}

if (attachments.isEmpty()) {
return null;
}

return attachments;
}

return attachments;
return null;
}

public static boolean hasPersistentAttachments(@Nullable IdentityHashMap<AttachmentType<?>, ?> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.IdentityHashMap;
import java.util.Map;
Expand All @@ -45,9 +45,13 @@
import net.minecraft.entity.MarkerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.RegistryOps;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.ProtoChunk;
import net.minecraft.world.chunk.WorldChunk;

Expand Down Expand Up @@ -134,11 +138,11 @@ void testStaticReadWrite() {
map.put(dummy, 0.5d);
var fakeSave = new NbtCompound();

AttachmentSerializingImpl.serializeAttachmentData(fakeSave, null, map);
AttachmentSerializingImpl.serializeAttachmentData(fakeSave, mockDRM(), 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, null);
map = AttachmentSerializingImpl.deserializeAttachmentData(fakeSave, mockDRM());
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 All @@ -147,6 +151,25 @@ void testStaticReadWrite() {
assertEquals(0.5d, entry.getValue());
}

@Test
void deserializeNull() {
var nbt = new NbtCompound();
assertNull(AttachmentSerializingImpl.deserializeAttachmentData(nbt, mockDRM()));

nbt.put(new Identifier("test").toString(), new NbtCompound());
assertNull(AttachmentSerializingImpl.deserializeAttachmentData(nbt, mockDRM()));
}

@Test
void serializeNullOrEmpty() {
var nbt = new NbtCompound();
AttachmentSerializingImpl.serializeAttachmentData(nbt, mockDRM(), null);
assertFalse(nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY));

AttachmentSerializingImpl.serializeAttachmentData(nbt, mockDRM(), new IdentityHashMap<>());
assertFalse(nbt.contains(AttachmentTarget.NBT_ATTACHMENT_KEY));
}

@Test
void testEntityCopy() {
AttachmentType<Boolean> notCopiedOnRespawn = AttachmentRegistry.create(
Expand All @@ -173,17 +196,19 @@ void testEntityCopy() {

@Test
void testEntityPersistence() {
Entity entity = new MarkerEntity(EntityType.MARKER, mock());
DynamicRegistryManager drm = mockDRM();
World mockWorld = mock(World.class);
when(mockWorld.getRegistryManager()).thenReturn(drm);
Entity entity = new MarkerEntity(EntityType.MARKER, mockWorld);
assertFalse(entity.hasAttached(PERSISTENT));

int expected = 1;
entity.setAttached(PERSISTENT, expected);
NbtCompound fakeSave = new NbtCompound();
entity.writeNbt(fakeSave);

entity = spy(new MarkerEntity(EntityType.MARKER, mock())); // fresh object, like on restart
entity = new MarkerEntity(EntityType.MARKER, mockWorld); // fresh object, like on restart
entity.setChangeListener(mock());
doNothing().when(entity).calculateDimensions();
entity.readNbt(fakeSave);
assertTrue(entity.hasAttached(PERSISTENT));
assertEquals(expected, entity.getAttached(PERSISTENT));
Expand All @@ -196,9 +221,9 @@ void testBlockEntityPersistence() {

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

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

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

world = mock(ServerWorld.class, CALLS_REAL_METHODS);
AttachmentPersistentState.read(world, fakeSave, null);
AttachmentPersistentState.read(world, fakeSave, mockDRM());
assertTrue(world.hasAttached(PERSISTENT));
assertEquals(expected, world.getAttached(PERSISTENT));
}
Expand All @@ -225,4 +250,10 @@ void testWorldPersistentState() {
* Chunk serializing is coupled with world saving in ChunkSerializer which is too much of a pain to mock,
* so testing is handled by the testmod instead.
*/

private static DynamicRegistryManager mockDRM() {
DynamicRegistryManager drm = mock(DynamicRegistryManager.class);
when(drm.method_57093(any())).thenReturn((RegistryOps<Object>) (Object) RegistryOps.of(NbtOps.INSTANCE, drm));
return drm;
}
}
Loading

0 comments on commit cad7e71

Please sign in to comment.