mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-21 03:10:54 -04:00
Test for and fix registries that need syncing (#3723)
* Test for and fix registries that need syncing * Fix duplicate
This commit is contained in:
parent
76551cf623
commit
f1240ba7db
5 changed files with 93 additions and 5 deletions
fabric-registry-sync-v0/src
main/java/net/fabricmc/fabric/impl/registry/sync
testmod
java/net/fabricmc/fabric/test/registry/sync
resources
|
@ -65,8 +65,9 @@ public class FabricRegistryInit implements ModInitializer {
|
|||
RegistryAttributeHolder.get(Registries.ITEM)
|
||||
.addAttribute(RegistryAttribute.SYNCED);
|
||||
|
||||
// Saved and synced using string ID.
|
||||
RegistryAttributeHolder.get(Registries.POTION);
|
||||
// Synced via PacketCodecs.registry
|
||||
RegistryAttributeHolder.get(Registries.POTION)
|
||||
.addAttribute(RegistryAttribute.SYNCED);
|
||||
|
||||
// Doesnt seem to be accessed apart from registering?
|
||||
RegistryAttributeHolder.get(Registries.CARVER);
|
||||
|
@ -104,8 +105,9 @@ public class FabricRegistryInit implements ModInitializer {
|
|||
RegistryAttributeHolder.get(Registries.PAINTING_VARIANT)
|
||||
.addAttribute(RegistryAttribute.SYNCED);
|
||||
|
||||
// Doesnt seem to be synced or saved, STAT_TYPE seems to handle the syncing.
|
||||
RegistryAttributeHolder.get(Registries.CUSTOM_STAT);
|
||||
// Synced via PacketCodecs.registry
|
||||
RegistryAttributeHolder.get(Registries.CUSTOM_STAT)
|
||||
.addAttribute(RegistryAttribute.SYNCED);
|
||||
|
||||
// Serialised by string, doesnt seem to be synced
|
||||
RegistryAttributeHolder.get(Registries.CHUNK_STATUS);
|
||||
|
@ -151,7 +153,8 @@ public class FabricRegistryInit implements ModInitializer {
|
|||
RegistryAttributeHolder.get(Registries.RECIPE_TYPE);
|
||||
|
||||
// Synced by id
|
||||
RegistryAttributeHolder.get(Registries.RECIPE_SERIALIZER);
|
||||
RegistryAttributeHolder.get(Registries.RECIPE_SERIALIZER)
|
||||
.addAttribute(RegistryAttribute.SYNCED);
|
||||
|
||||
// Synced by rawID in 24w03a+
|
||||
RegistryAttributeHolder.get(Registries.ATTRIBUTE)
|
||||
|
@ -212,5 +215,9 @@ public class FabricRegistryInit implements ModInitializer {
|
|||
// Synced by rawID.
|
||||
RegistryAttributeHolder.get(Registries.MAP_DECORATION_TYPE)
|
||||
.addAttribute(RegistryAttribute.SYNCED);
|
||||
|
||||
// Synced via PacketCodecs.registry
|
||||
RegistryAttributeHolder.get(Registries.ARMOR_MATERIAL)
|
||||
.addAttribute(RegistryAttribute.SYNCED);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package net.fabricmc.fabric.test.registry.sync;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
@ -58,6 +60,11 @@ public class RegistrySyncTest implements ModInitializer {
|
|||
public static final boolean REGISTER_BLOCKS = Boolean.parseBoolean(System.getProperty("fabric.registry.sync.test.register.blocks", "true"));
|
||||
public static final boolean REGISTER_ITEMS = Boolean.parseBoolean(System.getProperty("fabric.registry.sync.test.register.items", "true"));
|
||||
|
||||
// Store a list of Registries used with PacketCodecs.registry, and then check that they are marked as synced when the server starts.
|
||||
// We check them later as they may be used before the registry attributes are assigned.
|
||||
private static boolean hasCheckedEarlyRegistries = false;
|
||||
private static final List<RegistryKey<? extends Registry<?>>> sycnedRegistriesToCheck = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
if (REGISTER_BLOCKS) {
|
||||
|
@ -95,6 +102,9 @@ public class RegistrySyncTest implements ModInitializer {
|
|||
});
|
||||
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
|
||||
hasCheckedEarlyRegistries = true;
|
||||
sycnedRegistriesToCheck.forEach(RegistrySyncTest::checkSyncedRegistry);
|
||||
|
||||
if (!setupCalled.get()) {
|
||||
throw new IllegalStateException("DRM setup was not called before startup!");
|
||||
}
|
||||
|
@ -126,6 +136,22 @@ public class RegistrySyncTest implements ModInitializer {
|
|||
})));
|
||||
}
|
||||
|
||||
public static void checkSyncedRegistry(RegistryKey<? extends Registry<?>> registry) {
|
||||
if (!Registries.REGISTRIES.containsId(registry.getValue())) {
|
||||
// Skip dynamic registries, as there are always synced.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasCheckedEarlyRegistries) {
|
||||
sycnedRegistriesToCheck.add(registry);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!RegistryAttributeHolder.get(registry).hasAttribute(RegistryAttribute.SYNCED)) {
|
||||
throw new IllegalStateException("Registry " + registry.getValue() + " is not marked as SYNCED!");
|
||||
}
|
||||
}
|
||||
|
||||
private static void registerBlocks(String namespace, int amount, int startingId) {
|
||||
for (int i = 0; i < amount; i++) {
|
||||
Block block = new Block(AbstractBlock.Settings.create());
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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.mixin;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
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.network.RegistryByteBuf;
|
||||
import net.minecraft.network.codec.PacketCodec;
|
||||
import net.minecraft.network.codec.PacketCodecs;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.registry.RegistryKey;
|
||||
import net.minecraft.util.collection.IndexedIterable;
|
||||
|
||||
import net.fabricmc.fabric.test.registry.sync.RegistrySyncTest;
|
||||
|
||||
@Mixin(PacketCodecs.class)
|
||||
public interface PacketCodecsMixin {
|
||||
@Inject(method = "registry", at = @At("HEAD"))
|
||||
private static <T, R> void checkSynced(RegistryKey<? extends Registry<T>> registry, Function<Registry<T>, IndexedIterable<R>> registryTransformer, CallbackInfoReturnable<PacketCodec<RegistryByteBuf, R>> cir) {
|
||||
RegistrySyncTest.checkSyncedRegistry(registry);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"required": true,
|
||||
"package": "net.fabricmc.fabric.test.registry.sync.mixin",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"mixins": [
|
||||
"PacketCodecsMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
|
@ -8,6 +8,9 @@
|
|||
"depends": {
|
||||
"fabric-registry-sync-v0": "*"
|
||||
},
|
||||
"mixins": [
|
||||
"fabric-registry-sync-v0-testmod.mixins.json"
|
||||
],
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"net.fabricmc.fabric.test.registry.sync.CustomDynamicRegistryTest",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue