Skip to content

Commit

Permalink
Make dynamic registry element path include namespace for none vanilla…
Browse files Browse the repository at this point in the history
… registries (#4180)

* Make dynamic registry element path be namespaced

Fixes #4179

* Remove old method of prepending the namespace

The old method of prepending only applied in certain locations, and thus gives different results depending on the circumstance

* Add simple RegistryKeysTest

---------

Co-authored-by: modmuss <[email protected]>

(cherry picked from commit 3f3c499)
  • Loading branch information
Gaming32 authored and modmuss50 committed Dec 16, 2024
1 parent e918aae commit 43e15ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
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 @@ -33,14 +33,10 @@
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryLoader;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.resource.ResourceFinder;
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 @@ -81,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/resource/ResourceFinder;json(Lnet/minecraft/registry/RegistryKey;)Lnet/minecraft/resource/ResourceFinder;"
)
)
private static ResourceFinder prependDirectoryWithNamespace(RegistryKey<? extends Registry<?>> registryKey, Operation<ResourceFinder> original) {
String originalDirectory = RegistryKeys.getPath(registryKey);
Identifier id = registryKey.getValue();
if (!id.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)
&& DynamicRegistriesImpl.FABRIC_DYNAMIC_REGISTRY_KEYS.contains(registryKey)) {
return ResourceFinder.json(id.getNamespace() + "/" + originalDirectory);
}

return original.call(registryKey);
}
}
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));
}
}

0 comments on commit 43e15ba

Please sign in to comment.