Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dynamic registry element path include namespace #4180

Open
wants to merge 4 commits into
base: 1.21.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

// Vanilla doesn't mark namespaces in the directories of tags and dynamic registry elements at all,
// so we prepend the directories with the namespace if it's a modded registry id.
@Mixin(RegistryKeys.class)
public class RegistryKeysMixin {
@ModifyReturnValue(method = "getTagPath", at = @At("RETURN"))
@ModifyReturnValue(method = "getPath", at = @At("RETURN"))
private static String prependDirectoryWithNamespace(String original, @Local(argsOnly = true) RegistryKey<? extends Registry<?>> registryRef) {
Identifier id = registryRef.getValue();

// Vanilla doesn't mark namespaces in the directories of tags at all,
// so we prepend the directories with the namespace if it's a modded registry id.
// No need to check DIRECTORIES, since this is only used by vanilla registries.
if (!id.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)) {
return id.getNamespace() + "/" + id.getPath();
}

return original;
}

@ModifyReturnValue(method = "getTagPath", at = @At("RETURN"))
private static String prependTagDirectoryWithNamespace(String original, @Local(argsOnly = true) RegistryKey<? extends Registry<?>> registryRef) {
Identifier id = registryRef.getValue();

if (!id.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)) {
return "tags/" + id.getNamespace() + "/" + id.getPath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryLoader;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.event.registry.DynamicRegistrySetupCallback;
import net.fabricmc.fabric.impl.registry.sync.DynamicRegistriesImpl;
import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryViewImpl;

@Mixin(RegistryLoader.class)
Expand Down Expand Up @@ -79,27 +77,4 @@ private static void beforeLoad(@Coerce Object registryLoadable, List<RegistryWra

DynamicRegistrySetupCallback.EVENT.invoker().onRegistrySetup(new DynamicRegistryViewImpl(registries));
}

// Vanilla doesn't mark namespaces in the directories of dynamic registries at all,
// so we prepend the directories with the namespace if it's a modded registry registered using the Fabric API.
@WrapOperation(
method = {
"loadFromNetwork(Ljava/util/Map;Lnet/minecraft/resource/ResourceFactory;Lnet/minecraft/registry/RegistryOps$RegistryInfoGetter;Lnet/minecraft/registry/MutableRegistry;Lcom/mojang/serialization/Decoder;Ljava/util/Map;)V",
"loadFromResource(Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/registry/RegistryOps$RegistryInfoGetter;Lnet/minecraft/registry/MutableRegistry;Lcom/mojang/serialization/Decoder;Ljava/util/Map;)V"
},
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/registry/RegistryKeys;getPath(Lnet/minecraft/registry/RegistryKey;)Ljava/lang/String;"
)
)
private static String prependDirectoryWithNamespace(RegistryKey<? extends Registry<?>> registryKey, Operation<String> original) {
String originalDirectory = original.call(registryKey);
Identifier id = registryKey.getValue();
if (!id.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)
&& DynamicRegistriesImpl.FABRIC_DYNAMIC_REGISTRY_KEYS.contains(registryKey)) {
return id.getNamespace() + "/" + originalDirectory;
}

return originalDirectory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import net.minecraft.Bootstrap;
import net.minecraft.SharedConstants;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

public class RegistryKeysTest {
private static final RegistryKey<Registry<Object>> TEST_KEY = RegistryKey.ofRegistry(Identifier.of("registry-keys", "test"));

@BeforeAll
static void beforeAll() {
SharedConstants.createGameVersion();
Bootstrap.initialize();
}

@Test
void getPath() {
assertEquals("item", RegistryKeys.getPath(RegistryKeys.ITEM));
assertEquals("registry-keys/test", RegistryKeys.getPath(TEST_KEY));
}

@Test
void getTagPath() {
assertEquals("tags/item", RegistryKeys.getTagPath(RegistryKeys.ITEM));
assertEquals("tags/registry-keys/test", RegistryKeys.getTagPath(TEST_KEY));
}
}