From f4822321fa963dcf40618578d3242faa8c85acd5 Mon Sep 17 00:00:00 2001 From: Cat Core Date: Sat, 23 Mar 2024 19:47:33 +0100 Subject: [PATCH] More progress on registry api v2 - differentiate "synced" Index-Key-Value registries from simple Key-Value registries - start preparing registry remapping --- build.gradle | 7 +- .../fabric/api/util/Identifier.java | 4 + .../api/registry/v1/RegistryHelper.java | 6 +- .../mixin/registry/sync/class_2929Mixin.java | 47 +++++++ .../sync/versioned/SimpleRegistryMixin.java | 49 ++++++-- ...cy-fabric-registry-sync-api-v2.mixins.json | 1 + .../api/registry/v2/RegistryHelper.java | 39 +++++- .../api/registry/v2/RegistryHolder.java | 16 --- .../fabric/api/registry/v2/RegistryIds.java | 30 +++++ .../v2/event/RegistryBeforeAddCallback.java | 21 +++- .../v2/event/RegistryEntryAddedCallback.java | 21 +++- .../v2/event/RegistryInitializedEvent.java | 19 ++- .../v2/registry/holder/RegistryHolder.java | 34 +++++ .../registry/holder/SyncedRegistryHolder.java | 27 ++++ .../v2/registry/registrable/IdsHolder.java | 24 ++++ .../v2/registry/registrable/Registrable.java | 24 ++++ .../registrable/SyncedRegistrable.java | 26 ++++ .../RegistryHelperImplementation.java | 50 ++++++-- .../registry/accessor/RegistryIdSetter.java | 17 +++ .../mixin/registry/sync/IdListMixinV2.java | 45 +++++++ .../registry/sync/MutableRegistryMixin.java | 119 ++++++++++++++++++ .../registry/sync/SimpleRegistryMixinV2.java | 79 +++--------- .../common/src/main/resources/fabric.mod.json | 5 +- ...ic-registry-sync-api-v2-common.mixins.json | 2 + 24 files changed, 605 insertions(+), 107 deletions(-) create mode 100644 legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java delete mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHolder.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryIds.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/Registrable.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/SyncedRegistrable.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java diff --git a/build.gradle b/build.gradle index c2b40c659..7af16618d 100644 --- a/build.gradle +++ b/build.gradle @@ -284,6 +284,10 @@ allprojects { useLegacyMixinAp = true } + interfaceInjection { + enableDependencyInterfaceInjection = true + } + runs { "testModClient$mcVersion" { client() @@ -436,7 +440,8 @@ def addPomMetadataInformation(Project project, MavenPom pom) { def modJsonFile = project.file("src/main/resources/fabric.mod.json") if (!modJsonFile.exists()) { - modJsonFile = project.file("src/client/resources/fabric.mod.json") + System.out.println("Can't find fabric.mod.json at ${modJsonFile.toString()}!") + return; } def modJson = new JsonSlurper().parse(modJsonFile) diff --git a/legacy-fabric-api-base/common/src/main/java/net/legacyfabric/fabric/api/util/Identifier.java b/legacy-fabric-api-base/common/src/main/java/net/legacyfabric/fabric/api/util/Identifier.java index 32a65c7b3..51d3da699 100644 --- a/legacy-fabric-api-base/common/src/main/java/net/legacyfabric/fabric/api/util/Identifier.java +++ b/legacy-fabric-api-base/common/src/main/java/net/legacyfabric/fabric/api/util/Identifier.java @@ -71,6 +71,10 @@ public String toString() { return this.namespace + ':' + this.path; } + public String toTranslationKey() { + return this.namespace + "." + this.path; + } + public boolean equals(Object object) { if (this == object) { return true; diff --git a/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/api/registry/v1/RegistryHelper.java b/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/api/registry/v1/RegistryHelper.java index 3f18a60cc..9a6f3745e 100644 --- a/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/api/registry/v1/RegistryHelper.java +++ b/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/api/registry/v1/RegistryHelper.java @@ -88,11 +88,13 @@ public static Block getBlock(Identifier id) { * @return The item registered */ public static Item registerItem(Item item, Identifier id) { - return RegistryHelperImpl.registerItem(item, id); + net.legacyfabric.fabric.api.registry.v2.RegistryHelper.register(net.legacyfabric.fabric.api.registry.v2.RegistryIds.ITEMS, id, item); + + return item; } public static Item getItem(Identifier id) { - return RegistryHelperImpl.getValue(id, RegistryIds.ITEMS); + return net.legacyfabric.fabric.api.registry.v2.RegistryHelper.getValue(RegistryIds.ITEMS, id); } /** diff --git a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java new file mode 100644 index 000000000..1f8020359 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.mixin.registry.sync; + +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import net.minecraft.class_2929; + +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder; + +@Mixin(class_2929.class) +public abstract class class_2929Mixin implements IdsHolder { + @Shadow + @Nullable + public abstract T getById(int id); + + @Override + public IdsHolder fabric$new() { + return (IdsHolder) new class_2929<>(256); + } + + @Override + public int fabric$nextId() { + int id = 0; + + while (this.getById(id) != null) id++; + + return id; + } +} diff --git a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java index 4dd372dd9..4b6b945c4 100644 --- a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java +++ b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java @@ -1,23 +1,52 @@ -package net.legacyfabric.fabric.mixin.registry.sync.versioned; +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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. + */ -import net.legacyfabric.fabric.api.registry.v2.RegistryHolder; +package net.legacyfabric.fabric.mixin.registry.sync.versioned; -import net.legacyfabric.fabric.api.util.Identifier; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import net.minecraft.class_2929; import net.minecraft.util.registry.SimpleRegistry; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; +import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.SyncedRegistrable; +import net.legacyfabric.fabric.api.util.Identifier; @Mixin(SimpleRegistry.class) -public abstract class SimpleRegistryMixin implements RegistryHolder { +public abstract class SimpleRegistryMixin implements SyncedRegistryHolder, SyncedRegistrable { @Shadow public abstract void add(int id, K identifier, V object); + @Shadow + @Final + protected class_2929 field_13718; + + @Override + public void fabric$register(int rawId, Identifier identifier, V value) { + fabric$getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value); + add(rawId, fabric$toKeyType(identifier), value); + fabric$getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value); + } + @Override - public void register(int rawId, Identifier identifier, V value) { - getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value); - add(rawId, toKeyType(identifier), value); - getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value); + public IdsHolder fabric$getIdsHolder() { + return (IdsHolder) field_13718; } } diff --git a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/resources/legacy-fabric-registry-sync-api-v2.mixins.json b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/resources/legacy-fabric-registry-sync-api-v2.mixins.json index b4992aa42..008e6ed69 100644 --- a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/resources/legacy-fabric-registry-sync-api-v2.mixins.json +++ b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/resources/legacy-fabric-registry-sync-api-v2.mixins.json @@ -6,6 +6,7 @@ "defaultRequire": 1 }, "mixins": [ + "class_2929Mixin", "versioned.SimpleRegistryMixin" ], "client": [ diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHelper.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHelper.java index 15c6b4665..a7852b2c0 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHelper.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHelper.java @@ -1,16 +1,34 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2; +import java.util.function.Function; + +import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder; import net.legacyfabric.fabric.api.util.Identifier; import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation; -import java.util.function.Function; - public class RegistryHelper { public static void register(RegistryHolder registry, Identifier identifier, T value) { RegistryHelperImplementation.register(registry, identifier, value); } - public static void registry(Identifier registryId, Identifier identifier, T value) { + public static void register(Identifier registryId, Identifier identifier, T value) { register(RegistryHelperImplementation.getRegistry(registryId), identifier, value); } @@ -18,11 +36,24 @@ public static T register(RegistryHolder registry, Identifier identifier, return RegistryHelperImplementation.register(registry, identifier, valueConstructor); } - public static T registry(Identifier registryId, Identifier identifier, Function valueConstructor) { + public static T register(Identifier registryId, Identifier identifier, Function valueConstructor) { return register(RegistryHelperImplementation.getRegistry(registryId), identifier, valueConstructor); } public static void addRegistry(Identifier identifier, RegistryHolder registryHolder) { RegistryHelperImplementation.registerRegistry(identifier, registryHolder); } + + public static RegistryHolder getRegistry(Identifier identifier) { + return RegistryHelperImplementation.getRegistry(identifier); + } + + public static T getValue(Identifier registryId, Identifier identifier) { + return RegistryHelperImplementation.getRegistry(registryId) + .fabric$getValue(identifier); + } + + public static T getValue(RegistryHolder registry, Identifier identifier) { + return registry.fabric$getValue(identifier); + } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHolder.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHolder.java deleted file mode 100644 index b7e8aada1..000000000 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHolder.java +++ /dev/null @@ -1,16 +0,0 @@ -package net.legacyfabric.fabric.api.registry.v2; - -import net.legacyfabric.fabric.api.event.Event; -import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback; -import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback; -import net.legacyfabric.fabric.api.util.Identifier; - -public interface RegistryHolder { - Identifier getId(); - Event> getEntryAddedCallback(); - Event> getBeforeAddedCallback(); - - void register(int rawId, Identifier identifier, T value); - - K toKeyType(Object o); -} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryIds.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryIds.java new file mode 100644 index 000000000..83f11fba7 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryIds.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2; + +import net.legacyfabric.fabric.api.util.Identifier; + +public class RegistryIds { + public static final Identifier ITEMS = new Identifier("items"); + public static final Identifier BLOCKS = new Identifier("blocks"); + public static final Identifier BLOCK_ENTITY_TYPES = new Identifier("block_entity_types"); + public static final Identifier STATUS_EFFECTS = new Identifier("status_effects"); + public static final Identifier ENCHANTMENTS = new Identifier("enchantments"); + public static final Identifier BIOMES = new Identifier("biomes"); + public static final Identifier ENTITY_TYPES = new Identifier("entity_types"); +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryBeforeAddCallback.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryBeforeAddCallback.java index c4206bf40..362777c05 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryBeforeAddCallback.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryBeforeAddCallback.java @@ -1,7 +1,24 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.event; import net.legacyfabric.fabric.api.event.Event; -import net.legacyfabric.fabric.api.registry.v2.RegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder; import net.legacyfabric.fabric.api.util.Identifier; import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation; @@ -14,6 +31,6 @@ static Event> event(Identifier registryId) { } static Event> event(RegistryHolder registry) { - return registry.getBeforeAddedCallback(); + return registry.fabric$getBeforeAddedCallback(); } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryEntryAddedCallback.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryEntryAddedCallback.java index 72562274b..a165441bb 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryEntryAddedCallback.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryEntryAddedCallback.java @@ -1,7 +1,24 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.event; import net.legacyfabric.fabric.api.event.Event; -import net.legacyfabric.fabric.api.registry.v2.RegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder; import net.legacyfabric.fabric.api.util.Identifier; import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation; @@ -14,6 +31,6 @@ static Event> event(Identifier registryId) { } static Event> event(RegistryHolder registry) { - return registry.getEntryAddedCallback(); + return registry.fabric$getEntryAddedCallback(); } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryInitializedEvent.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryInitializedEvent.java index 9bf29f371..54f07c034 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryInitializedEvent.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryInitializedEvent.java @@ -1,7 +1,24 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.event; import net.legacyfabric.fabric.api.event.Event; -import net.legacyfabric.fabric.api.registry.v2.RegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder; import net.legacyfabric.fabric.api.util.Identifier; import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation; diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java new file mode 100644 index 000000000..d72879eb4 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.registry.holder; + +import net.legacyfabric.fabric.api.event.Event; +import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback; +import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback; +import net.legacyfabric.fabric.api.util.Identifier; + +public interface RegistryHolder { + Identifier fabric$getId(); + Event> fabric$getEntryAddedCallback(); + Event> fabric$getBeforeAddedCallback(); + + K fabric$toKeyType(Object o); + + T fabric$getValue(Identifier id); + Identifier fabric$getId(T value); +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java new file mode 100644 index 000000000..2d0ac6853 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.registry.holder; + +import net.legacyfabric.fabric.api.util.Identifier; + +public interface SyncedRegistryHolder extends RegistryHolder { + int fabric$getRawId(T value); + int fabric$getRawId(Identifier identifier); + T fabric$getValue(int rawId); + Identifier fabric$getId(int rawId); +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java new file mode 100644 index 000000000..26b37016e --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.registry.registrable; + +public interface IdsHolder extends Iterable { + IdsHolder fabric$new(); + + int fabric$nextId(); +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/Registrable.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/Registrable.java new file mode 100644 index 000000000..2bcac0a91 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/Registrable.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.registry.registrable; + +import net.legacyfabric.fabric.api.util.Identifier; + +public interface Registrable { + void fabric$register(int rawId, Identifier identifier, T value); +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/SyncedRegistrable.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/SyncedRegistrable.java new file mode 100644 index 000000000..bcbcfeed7 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/SyncedRegistrable.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.registry.registrable; + +public interface SyncedRegistrable extends Registrable { + IdsHolder fabric$getIdsHolder(); + + default int fabric$nextId() { + return fabric$getIdsHolder().fabric$nextId(); + } +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImplementation.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImplementation.java index a8057e00d..ef16ee3b0 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImplementation.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImplementation.java @@ -1,18 +1,36 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.impl.registry; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + import net.legacyfabric.fabric.api.event.Event; import net.legacyfabric.fabric.api.event.EventFactory; -import net.legacyfabric.fabric.api.registry.v2.RegistryHolder; import net.legacyfabric.fabric.api.registry.v2.event.RegistryInitializedEvent; +import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.Registrable; +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.SyncedRegistrable; import net.legacyfabric.fabric.api.util.Identifier; - import net.legacyfabric.fabric.api.util.VersionUtils; import net.legacyfabric.fabric.impl.registry.accessor.RegistryIdSetter; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; - public class RegistryHelperImplementation { public static final boolean hasFlatteningBegun = VersionUtils.matches(">=1.8 <=1.12.2"); public static final Map> INITIALIZATION_EVENTS = new HashMap<>(); @@ -52,14 +70,28 @@ public static void registerRegistry(Identifier identifier, RegistryHolder hol } public static void register(RegistryHolder registry, Identifier identifier, T value) { + if (!(registry instanceof Registrable)) throw new IllegalArgumentException("Can't register object to non registrable registry " + registry.fabric$getId()); + + Registrable registrable = (Registrable) registry; int computedId = -1; - registry.register(computedId, identifier, value); + + if (registry instanceof SyncedRegistrable) { + computedId = ((SyncedRegistrable) registrable).fabric$nextId(); + } + + registrable.fabric$register(computedId, identifier, value); } public static T register(RegistryHolder registry, Identifier identifier, Function valueConstructor) { - int computedId = -1; + if (!(registry instanceof SyncedRegistrable)) throw new IllegalArgumentException("Can't register object to non registrable registry " + registry.fabric$getId()); + + SyncedRegistrable registrable = (SyncedRegistrable) registry; + int computedId = registrable.fabric$nextId(); + T value = valueConstructor.apply(computedId); - registry.register(computedId, identifier, value); + + registrable.fabric$register(computedId, identifier, value); + return value; } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/accessor/RegistryIdSetter.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/accessor/RegistryIdSetter.java index f01623b83..f320032a0 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/accessor/RegistryIdSetter.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/accessor/RegistryIdSetter.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.impl.registry.accessor; import net.legacyfabric.fabric.api.util.Identifier; diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java new file mode 100644 index 000000000..3d4f9fded --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.mixin.registry.sync; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import net.minecraft.util.collection.IdList; + +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder; + +@Mixin(IdList.class) +public abstract class IdListMixinV2 implements IdsHolder { + @Shadow + public abstract Object fromId(int index); + + @Override + public IdsHolder fabric$new() { + return (IdsHolder) new IdList<>(); + } + + @Override + public int fabric$nextId() { + int id = 0; + + while (this.fromId(id) != null) id++; + + return id; + } +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java new file mode 100644 index 000000000..db0c9ce68 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.mixin.registry.sync; + +import java.util.Map; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; + +import net.minecraft.util.registry.MutableRegistry; + +import net.legacyfabric.fabric.api.event.Event; +import net.legacyfabric.fabric.api.event.EventFactory; +import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback; +import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback; +import net.legacyfabric.fabric.api.registry.v2.registry.holder.RegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.Registrable; +import net.legacyfabric.fabric.api.util.Identifier; +import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation; +import net.legacyfabric.fabric.impl.registry.accessor.RegistryIdSetter; + +@Mixin(MutableRegistry.class) +public abstract class MutableRegistryMixin implements RegistryHolder, RegistryIdSetter, Registrable { + @Shadow + public abstract void put(Object key, Object value); + + @Shadow + public abstract Object get(Object key); + + @Shadow + @Final + protected Map map; + @Unique + private final Event> fabric_addObjectEvent = EventFactory.createArrayBacked(RegistryEntryAddedCallback.class, + (callbacks) -> (rawId, id, object) -> { + for (RegistryEntryAddedCallback callback : callbacks) { + callback.onEntryAdded(rawId, id, object); + } + } + ); + + @Unique + private final Event> fabric_beforeAddObjectEvent = EventFactory.createArrayBacked(RegistryBeforeAddCallback.class, + (callbacks) -> (rawId, id, object) -> { + for (RegistryBeforeAddCallback callback : callbacks) { + callback.onEntryAdding(rawId, id, object); + } + } + ); + + @Unique + private Identifier fabric_id; + + @Override + public Event> fabric$getEntryAddedCallback() { + return this.fabric_addObjectEvent; + } + + @Override + public Event> fabric$getBeforeAddedCallback() { + return this.fabric_beforeAddObjectEvent; + } + + @Override + public Identifier fabric$getId() { + return this.fabric_id; + } + + @Override + public void fabric$setId(Identifier identifier) { + assert this.fabric_id == null; + this.fabric_id = identifier; + } + + @Override + public K fabric$toKeyType(Object o) { + return RegistryHelperImplementation.hasFlatteningBegun ? (K) new net.minecraft.util.Identifier(o.toString()) : (K) o.toString(); + } + + @Override + public void fabric$register(int rawId, Identifier identifier, V value) { + fabric$getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value); + put(fabric$toKeyType(identifier), value); + fabric$getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value); + } + + @Override + public V fabric$getValue(Identifier id) { + return (V) get(fabric$toKeyType(id)); + } + + @Override + public Identifier fabric$getId(V value) { + if (map.containsValue(value)) { + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() != null && entry.getValue().equals(value)) return new Identifier(entry.getKey()); + } + } + + return null; + } +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java index e33f91f4e..b9843a79b 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java @@ -1,67 +1,28 @@ -package net.legacyfabric.fabric.mixin.registry.sync; - -import net.legacyfabric.fabric.api.event.Event; -import net.legacyfabric.fabric.api.event.EventFactory; -import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback; -import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback; -import net.legacyfabric.fabric.api.registry.v2.RegistryHolder; +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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. + */ -import net.legacyfabric.fabric.api.util.Identifier; +package net.legacyfabric.fabric.mixin.registry.sync; -import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation; -import net.legacyfabric.fabric.impl.registry.accessor.RegistryIdSetter; +import org.spongepowered.asm.mixin.Mixin; import net.minecraft.util.registry.SimpleRegistry; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; +import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistryHolder; @Mixin(SimpleRegistry.class) -public abstract class SimpleRegistryMixinV2 implements RegistryHolder, RegistryIdSetter { - @Unique - private final Event> fabric_addObjectEvent = EventFactory.createArrayBacked(RegistryEntryAddedCallback.class, - (callbacks) -> (rawId, id, object) -> { - for (RegistryEntryAddedCallback callback : callbacks) { - callback.onEntryAdded(rawId, id, object); - } - } - ); - - @Unique - private final Event> fabric_beforeAddObjectEvent = EventFactory.createArrayBacked(RegistryBeforeAddCallback.class, - (callbacks) -> (rawId, id, object) -> { - for (RegistryBeforeAddCallback callback : callbacks) { - callback.onEntryAdding(rawId, id, object); - } - } - ); - - @Unique - private Identifier fabric_id; - - @Override - public Event> getEntryAddedCallback() { - return this.fabric_addObjectEvent; - } - - @Override - public Event> getBeforeAddedCallback() { - return this.fabric_beforeAddObjectEvent; - } - - @Override - public Identifier getId() { - return this.fabric_id; - } - - @Override - public void fabric$setId(Identifier identifier) { - assert this.fabric_id == null; - this.fabric_id = identifier; - } - - @Override - public K toKeyType(Object o) { - return RegistryHelperImplementation.hasFlatteningBegun ? (K) new net.minecraft.util.Identifier(o.toString()) : (K) o.toString(); - } +public abstract class SimpleRegistryMixinV2 implements SyncedRegistryHolder { } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/resources/fabric.mod.json b/legacy-fabric-registry-sync-api-v2/common/src/main/resources/fabric.mod.json index fbb16d7b2..f5406e502 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/resources/fabric.mod.json +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/resources/fabric.mod.json @@ -29,7 +29,10 @@ ], "custom": { "loom:injected_interfaces": { - "net/minecraft/class_1943": ["net/legacyfabric/fabric/api/registry/v2/RegistryHolder"] + "net/minecraft/class_1367": ["net/legacyfabric/fabric/api/registry/v2/RegistryHolder"], + "net/minecraft/class_1943": ["net/legacyfabric/fabric/api/registry/v2/SyncedRegistryHolder"], + "net/minecraft/class_1942": ["net/legacyfabric/fabric/api/registry/v2/registrable/IdsHolder"], + "net/minecraft/class_2929": ["net/legacyfabric/fabric/api/registry/v2/registrable/IdsHolder"] }, "modmenu": { "badges": [ "library" ], diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/resources/legacy-fabric-registry-sync-api-v2-common.mixins.json b/legacy-fabric-registry-sync-api-v2/common/src/main/resources/legacy-fabric-registry-sync-api-v2-common.mixins.json index 5c08a8478..7062c3539 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/resources/legacy-fabric-registry-sync-api-v2-common.mixins.json +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/resources/legacy-fabric-registry-sync-api-v2-common.mixins.json @@ -6,6 +6,8 @@ "defaultRequire": 1 }, "mixins": [ + "IdListMixinV2", + "MutableRegistryMixin", "SimpleRegistryMixinV2" ], "client": [