-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial * style * tests + more input checking * revert formatting * requests + more tests * testmod * check not frozen * Fixes * throw on default impl * optimize implementation --------- Co-authored-by: modmuss50 <[email protected]> (cherry picked from commit e2e49f7)
- Loading branch information
Showing
14 changed files
with
397 additions
and
4 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/FabricRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.api.event.registry; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import net.minecraft.registry.Registry; | ||
import net.minecraft.util.Identifier; | ||
|
||
/** | ||
* General-purpose Fabric-provided extensions for {@link Registry} objects. | ||
* | ||
* <p>Note: This interface is automatically implemented on all registries via Mixin and interface injection.</p> | ||
*/ | ||
@ApiStatus.NonExtendable | ||
public interface FabricRegistry { | ||
/** | ||
* Adds an alias for an entry in this registry. Once added, all queries to this registry that refer to the {@code old} | ||
* {@link Identifier} will be redirected towards {@code newId}. This is useful if a mod wants to change an ID without | ||
* breaking compatibility with existing worlds. | ||
* @param old the {@link Identifier} that will become an alias for {@code newId} | ||
* @param newId the {@link Identifier} for which {@code old} will become an alias | ||
*/ | ||
default void addAlias(Identifier old, Identifier newId) { | ||
throw new UnsupportedOperationException("implemented via mixin"); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/RegistryMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.mixin.registry.sync; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
|
||
import net.minecraft.registry.Registry; | ||
|
||
import net.fabricmc.fabric.api.event.registry.FabricRegistry; | ||
|
||
@Mixin(Registry.class) | ||
public interface RegistryMixin extends FabricRegistry { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...istry-sync-v0/src/test/java/net/fabricmc/fabric/test/registry/sync/RegistryAliasTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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 static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import net.minecraft.Bootstrap; | ||
import net.minecraft.SharedConstants; | ||
import net.minecraft.registry.Registry; | ||
import net.minecraft.registry.RegistryKey; | ||
import net.minecraft.util.Identifier; | ||
|
||
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; | ||
|
||
public class RegistryAliasTest { | ||
private static final Identifier OBSOLETE_ID = id("obsolete"); | ||
private static final Identifier NEW_ID = id("new"); | ||
private static final Identifier OTHER = id("other"); | ||
private RegistryKey<Registry<String>> testRegistryKey; | ||
private Registry<String> testRegistry; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
SharedConstants.createGameVersion(); | ||
Bootstrap.initialize(); | ||
} | ||
|
||
private static Identifier id(String s) { | ||
return Identifier.of("registry_sync_test_alias_test", s); | ||
} | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
testRegistryKey = RegistryKey.ofRegistry(id(UUID.randomUUID().toString())); | ||
testRegistry = Mockito.spy(FabricRegistryBuilder.createSimple(testRegistryKey).buildAndRegister()); | ||
|
||
Registry.register(testRegistry, NEW_ID, "entry"); | ||
Registry.register(testRegistry, OTHER, "other"); | ||
testRegistry.addAlias(OBSOLETE_ID, NEW_ID); | ||
} | ||
|
||
@Test | ||
void testAlias() { | ||
RegistryKey<String> obsoleteKey = RegistryKey.of(testRegistryKey, OBSOLETE_ID); | ||
|
||
assertTrue(testRegistry.containsId(OBSOLETE_ID)); | ||
assertFalse(testRegistry.getIds().contains(OBSOLETE_ID)); | ||
assertEquals("entry", testRegistry.get(OBSOLETE_ID)); | ||
assertEquals("entry", testRegistry.get(obsoleteKey)); | ||
|
||
Identifier moreObsolete = id("more_obsolete"); | ||
assertFalse(testRegistry.containsId(moreObsolete)); | ||
|
||
testRegistry.addAlias(moreObsolete, OBSOLETE_ID); | ||
|
||
assertTrue(testRegistry.containsId(moreObsolete)); | ||
assertEquals("entry", testRegistry.get(moreObsolete)); | ||
} | ||
|
||
@Test | ||
void forbidAmbiguousAlias() { | ||
assertThrows(IllegalArgumentException.class, () -> testRegistry.addAlias(OBSOLETE_ID, OTHER)); | ||
} | ||
|
||
@Test | ||
void forbidCircularAliases() { | ||
assertThrows(IllegalArgumentException.class, () -> testRegistry.addAlias(NEW_ID, OBSOLETE_ID)); | ||
} | ||
|
||
@Test | ||
void forbidExistingIdAsAlias() { | ||
assertThrows(IllegalArgumentException.class, () -> testRegistry.addAlias(NEW_ID, OTHER)); | ||
} | ||
|
||
@Test | ||
void forbidOverridingAliasWithEntry() { | ||
assertThrows(IllegalArgumentException.class, () -> Registry.register(testRegistry, OBSOLETE_ID, "obsolete")); | ||
} | ||
} |
Oops, something went wrong.