mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-14 19:25:23 -05:00
Add test for nested dynamic objects
This commit is contained in:
parent
741bd52c1e
commit
486e3e1ce0
4 changed files with 53 additions and 0 deletions
|
@ -56,6 +56,8 @@ public class RegistrySyncTest implements ModInitializer {
|
|||
|
||||
public static final RegistryKey<Registry<TestDynamicObject>> TEST_DYNAMIC_REGISTRY_KEY =
|
||||
RegistryKey.ofRegistry(new Identifier("fabric", "test_dynamic"));
|
||||
public static final RegistryKey<Registry<TestNestedDynamicObject>> TEST_NESTED_DYNAMIC_REGISTRY_KEY =
|
||||
RegistryKey.ofRegistry(new Identifier("fabric", "test_dynamic_nested"));
|
||||
public static final RegistryKey<Registry<TestDynamicObject>> TEST_SYNCED_1_DYNAMIC_REGISTRY_KEY =
|
||||
RegistryKey.ofRegistry(new Identifier("fabric", "test_dynamic_synced_1"));
|
||||
public static final RegistryKey<Registry<TestDynamicObject>> TEST_SYNCED_2_DYNAMIC_REGISTRY_KEY =
|
||||
|
@ -104,6 +106,10 @@ public class RegistrySyncTest implements ModInitializer {
|
|||
.synced();
|
||||
DynamicRegistries.register(TEST_SYNCED_2_DYNAMIC_REGISTRY_KEY, TestDynamicObject.CODEC)
|
||||
.synced(TestDynamicObject.NETWORK_CODEC);
|
||||
// A registry that is loaded before its dependency that is used in a RegistryEntry.
|
||||
DynamicRegistries.register(TEST_NESTED_DYNAMIC_REGISTRY_KEY, TestNestedDynamicObject.CODEC)
|
||||
.synced()
|
||||
.sortBefore(TEST_SYNCED_1_DYNAMIC_REGISTRY_KEY);
|
||||
|
||||
DynamicRegistrySetupCallback.EVENT.register(registryManager -> {
|
||||
setupCalled.set(true);
|
||||
|
@ -113,6 +119,7 @@ public class RegistrySyncTest implements ModInitializer {
|
|||
addListenerForDynamic(registryManager, TEST_DYNAMIC_REGISTRY_KEY);
|
||||
addListenerForDynamic(registryManager, TEST_SYNCED_1_DYNAMIC_REGISTRY_KEY);
|
||||
addListenerForDynamic(registryManager, TEST_SYNCED_2_DYNAMIC_REGISTRY_KEY);
|
||||
addListenerForDynamic(registryManager, TEST_NESTED_DYNAMIC_REGISTRY_KEY);
|
||||
});
|
||||
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.fabricmc.fabric.test.registry.sync;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
import net.minecraft.registry.entry.RegistryElementCodec;
|
||||
import net.minecraft.registry.entry.RegistryEntry;
|
||||
|
||||
public record TestNestedDynamicObject(RegistryEntry<TestDynamicObject> nested) {
|
||||
public static final Codec<TestNestedDynamicObject> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
RegistryElementCodec.of(RegistrySyncTest.TEST_SYNCED_1_DYNAMIC_REGISTRY_KEY, TestDynamicObject.CODEC)
|
||||
.fieldOf("nested")
|
||||
.forGetter(TestNestedDynamicObject::nested)
|
||||
).apply(instance, TestNestedDynamicObject::new));
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"nested": "fabric-registry-sync-v0-testmod:synced"
|
||||
}
|
|
@ -25,6 +25,7 @@ import net.fabricmc.api.ClientModInitializer;
|
|||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||
import net.fabricmc.fabric.test.registry.sync.RegistrySyncTest;
|
||||
import net.fabricmc.fabric.test.registry.sync.TestDynamicObject;
|
||||
import net.fabricmc.fabric.test.registry.sync.TestNestedDynamicObject;
|
||||
|
||||
public final class RegistrySyncClientTest implements ClientModInitializer {
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
|
@ -41,6 +42,9 @@ public final class RegistrySyncClientTest implements ClientModInitializer {
|
|||
TestDynamicObject synced2 = handler.getRegistryManager()
|
||||
.get(RegistrySyncTest.TEST_SYNCED_2_DYNAMIC_REGISTRY_KEY)
|
||||
.get(SYNCED_ID);
|
||||
TestNestedDynamicObject synced3 = handler.getRegistryManager()
|
||||
.get(RegistrySyncTest.TEST_NESTED_DYNAMIC_REGISTRY_KEY)
|
||||
.get(SYNCED_ID);
|
||||
|
||||
if (synced1 == null) {
|
||||
throw new AssertionError("Did not receive " + RegistrySyncTest.TEST_SYNCED_1_DYNAMIC_REGISTRY_KEY + "/" + SYNCED_ID);
|
||||
|
@ -60,6 +64,14 @@ public final class RegistrySyncClientTest implements ClientModInitializer {
|
|||
throw new AssertionError("Entries in " + RegistrySyncTest.TEST_SYNCED_2_DYNAMIC_REGISTRY_KEY + " should use network codec");
|
||||
}
|
||||
|
||||
if (synced3 == null) {
|
||||
throw new AssertionError("Did not receive " + RegistrySyncTest.TEST_NESTED_DYNAMIC_REGISTRY_KEY + "/" + SYNCED_ID);
|
||||
}
|
||||
|
||||
if (synced3.nested().value() != synced1) {
|
||||
throw new AssertionError("Did not match up synced nested entry to the other synced value");
|
||||
}
|
||||
|
||||
LOGGER.info("Dynamic registry sync tests passed!");
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue