Skip to content

Commit

Permalink
Test for and fix registries that need syncing (#3723)
Browse files Browse the repository at this point in the history
* Test for and fix registries that need syncing

* Fix duplicate
  • Loading branch information
modmuss50 committed Apr 20, 2024
1 parent 76551cf commit f1240ba
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public void onInitialize() {
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);
Expand Down Expand Up @@ -104,8 +105,9 @@ public void onInitialize() {
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);
Expand Down Expand Up @@ -151,7 +153,8 @@ public void onInitialize() {
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)
Expand Down Expand Up @@ -212,5 +215,9 @@ public void onInitialize() {
// Synced by rawID.
RegistryAttributeHolder.get(Registries.MAP_DECORATION_TYPE)
.addAttribute(RegistryAttribute.SYNCED);

// Synced via PacketCodecs.registry
RegistryAttributeHolder.get(Registries.ARMOR_MATERIAL)
.addAttribute(RegistryAttribute.SYNCED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -95,6 +102,9 @@ public void onInitialize() {
});

ServerLifecycleEvents.SERVER_STARTING.register(server -> {
hasCheckedEarlyRegistries = true;
sycnedRegistriesToCheck.forEach(RegistrySyncTest::checkSyncedRegistry);

if (!setupCalled.get()) {
throw new IllegalStateException("DRM setup was not called before startup!");
}
Expand Down Expand Up @@ -126,6 +136,22 @@ public void onInitialize() {
})));
}

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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"required": true,
"package": "net.fabricmc.fabric.test.registry.sync.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"PacketCodecsMixin"
],
"injectors": {
"defaultRequire": 1
}
}
3 changes: 3 additions & 0 deletions fabric-registry-sync-v0/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f1240ba

Please sign in to comment.