From e9c7de9661886e711590642132ee0fe0d66fb2b7 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Wed, 19 Aug 2020 18:31:52 +0200 Subject: [PATCH 01/14] Added DynamicRegistryEntryAddedCallback --- build.gradle | 2 +- .../DynamicRegistryEntryAddedCallback.java | 37 +++++++++++ .../registry/sync/DynamicRegistryEvents.java | 48 ++++++++++++++ .../sync/DynamicRegistryManagerAccessor.java | 34 ++++++++++ .../sync/DynamicRegistryManagerMixin.java | 49 ++++++++++++++ .../mixin/registry/sync/MixinIdRegistry.java | 66 +++++++++---------- .../fabric-registry-sync-v0.mixins.json | 4 +- .../test/registry/sync/RegistrySyncTest.java | 9 ++- 8 files changed, 213 insertions(+), 36 deletions(-) create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java diff --git a/build.gradle b/build.gradle index 626da71a2..83b48389f 100644 --- a/build.gradle +++ b/build.gradle @@ -73,7 +73,7 @@ allprojects { dependencies { minecraft "com.mojang:minecraft:$Globals.mcVersion" mappings "net.fabricmc:yarn:${Globals.mcVersion}${Globals.yarnVersion}:v2" - modCompile "net.fabricmc:fabric-loader:0.8.9+build.203" + modCompile "net.fabricmc:fabric-loader:0.9.2+build.206" } configurations { diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java new file mode 100644 index 000000000..8363a4d2c --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java @@ -0,0 +1,37 @@ +/* + * 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 net.minecraft.util.registry.MutableRegistry; +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; + +@FunctionalInterface +public interface DynamicRegistryEntryAddedCallback { + void onEntryAdded(int rawId, RegistryKey key, Object object, MutableRegistry registry); + + static Event event(RegistryKey> registryKey) { + if (!DynamicRegistryEvents.ADD_ENTRY_EVENTS.containsKey(registryKey)) { + throw new IllegalArgumentException("Unsupported registry: " + registryKey); + } + + return DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); + } +} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java new file mode 100644 index 000000000..b23f0efa9 --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java @@ -0,0 +1,48 @@ +/* + * 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.impl.registry.sync; + +import java.util.Map; + +import com.google.common.collect.Maps; + +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; +import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; + +public abstract class DynamicRegistryEvents { + public static Map>, Event> ADD_ENTRY_EVENTS; + + static { + ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); + + for (RegistryKey> registryKey : DynamicRegistryManagerAccessor.getInfos().keySet()) { + ADD_ENTRY_EVENTS.put(registryKey, + EventFactory.createArrayBacked( + DynamicRegistryEntryAddedCallback.class, + callbacks -> (rawId, key, object, registry) -> { + for (DynamicRegistryEntryAddedCallback callback : callbacks) { + callback.onEntryAdded(rawId, key, object, registry); + } + })); + } + } +} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java new file mode 100644 index 000000000..d9e72dd6c --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java @@ -0,0 +1,34 @@ +/* + * 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 java.util.Map; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.util.registry.DynamicRegistryManager; +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; + +@Mixin(DynamicRegistryManager.class) +public interface DynamicRegistryManagerAccessor { + @Accessor("INFOS") + static Map>, ?> getInfos() { + throw new AbstractMethodError(); + } +} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java new file mode 100644 index 000000000..95128b71d --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java @@ -0,0 +1,49 @@ +/* + * 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 java.util.Map; + +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 org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import net.minecraft.util.registry.DynamicRegistryManager; +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; +import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback; +import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; + +@Mixin(DynamicRegistryManager.class) +public class DynamicRegistryManagerMixin { + @Inject(method = "create", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/dynamic/RegistryOps$class_5506$class_5507;()V"), locals = LocalCapture.CAPTURE_FAILHARD) + private static void onCreateImpl(CallbackInfoReturnable cir, DynamicRegistryManager.Impl registryManager) { + for (Map.Entry>, Event> event : DynamicRegistryEvents.ADD_ENTRY_EVENTS.entrySet()) { + //noinspection unchecked + RegistryKey> registryKey = (RegistryKey>) event.getKey(); + RegistryEntryAddedCallback.event(registryManager.get(registryKey)).register((rawId, id, object) -> { + RegistryKey key = RegistryKey.of(registryKey, id); + event.getValue().invoker().onEntryAdded(rawId, key, object, registryManager.get(registryKey)); + }); + } + } +} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java index 3e532299d..c88624030 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java @@ -45,8 +45,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.SimpleRegistry; import net.minecraft.util.registry.RegistryKey; +import net.minecraft.util.registry.SimpleRegistry; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; @@ -62,16 +62,16 @@ import net.fabricmc.fabric.impl.registry.sync.RemappableRegistry; public abstract class MixinIdRegistry implements RemappableRegistry, ListenableRegistry { @Shadow @Final - private ObjectList rawIdToEntry; + private ObjectList field_26682; @Shadow @Final - private Object2IntMap entryToRawId; + private Object2IntMap field_26683; @Shadow @Final - private BiMap idToEntry; + private BiMap entriesById; @Shadow @Final - private BiMap, T> keyToEntry; + private BiMap, T> entriesByKey; @Shadow private int nextId; @Unique @@ -135,23 +135,23 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab private boolean fabric_isObjectNew = false; @SuppressWarnings({"unchecked", "ConstantConditions"}) - @Inject(method = "set", at = @At("HEAD")) - public void setPre(int id, RegistryKey registryId, Object object, Lifecycle lifecycle, CallbackInfoReturnable info) { - int indexedEntriesId = entryToRawId.getInt((T) object); + @Inject(method = "method_31051", at = @At("HEAD")) + public void setPre(int id, RegistryKey registryId, Object object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { + int indexedEntriesId = field_26683.getInt((T) object); if (indexedEntriesId >= 0) { throw new RuntimeException("Attempted to register object " + object + " twice! (at raw IDs " + indexedEntriesId + " and " + id + " )"); } - if (!idToEntry.containsKey(registryId.getValue())) { + if (!entriesById.containsKey(registryId.getValue())) { fabric_isObjectNew = true; } else { - T oldObject = idToEntry.get(registryId.getValue()); + T oldObject = entriesById.get(registryId.getValue()); if (oldObject != null && oldObject != object) { - int oldId = entryToRawId.getInt(oldObject); + int oldId = field_26683.getInt(oldObject); - if (oldId != id) { + if (oldId != id && checkDuplicateKeys) { throw new RuntimeException("Attempted to register ID " + registryId + " at different raw IDs (" + oldId + ", " + id + ")! If you're trying to override an item, use .set(), not .register()!"); } @@ -164,8 +164,8 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } @SuppressWarnings("unchecked") - @Inject(method = "set", at = @At("RETURN")) - public void setPost(int id, RegistryKey registryId, Object object, Lifecycle lifecycle, CallbackInfoReturnable info) { + @Inject(method = "method_31051", at = @At("RETURN")) + public void setPost(int id, RegistryKey registryId, Object object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { if (fabric_isObjectNew) { fabric_addObjectEvent.invoker().onEntryAdded(id, registryId.getValue(), object); } @@ -184,7 +184,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab List strings = null; for (Identifier remoteId : remoteIndexedEntries.keySet()) { - if (!idToEntry.keySet().contains(remoteId)) { + if (!entriesById.keySet().contains(remoteId)) { if (strings == null) { strings = new ArrayList<>(); } @@ -206,11 +206,11 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab break; } case EXACT: { - if (!idToEntry.keySet().equals(remoteIndexedEntries.keySet())) { + if (!entriesById.keySet().equals(remoteIndexedEntries.keySet())) { List strings = new ArrayList<>(); for (Identifier remoteId : remoteIndexedEntries.keySet()) { - if (!idToEntry.keySet().contains(remoteId)) { + if (!entriesById.keySet().contains(remoteId)) { strings.add(" - " + remoteId + " (missing on local)"); } } @@ -242,7 +242,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab // compatibility. if (fabric_prevIndexedEntries == null) { fabric_prevIndexedEntries = new Object2IntOpenHashMap<>(); - fabric_prevEntries = HashBiMap.create(idToEntry); + fabric_prevEntries = HashBiMap.create(entriesById); for (Object o : registry) { fabric_prevIndexedEntries.put(registry.getId(o), registry.getRawId(o)); @@ -297,8 +297,8 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } // note: indexedEntries cannot be safely remove()d from - idToEntry.keySet().removeAll(droppedIds); - keyToEntry.keySet().removeIf(registryKey -> droppedIds.contains(registryKey.getValue())); + entriesById.keySet().removeAll(droppedIds); + entriesByKey.keySet().removeIf(registryKey -> droppedIds.contains(registryKey.getValue())); break; } @@ -306,7 +306,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab Int2IntMap idMap = new Int2IntOpenHashMap(); - for (Object o : rawIdToEntry) { + for (Object o : field_26682) { Identifier id = registry.getId(o); int rid = registry.getRawId(o); @@ -317,8 +317,8 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } // entries was handled above, if it was necessary. - rawIdToEntry.clear(); - entryToRawId.clear(); + field_26682.clear(); + field_26683.clear(); nextId = 0; List orderedRemoteEntries = new ArrayList<>(remoteIndexedEntries.keySet()); @@ -326,7 +326,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab for (Identifier identifier : orderedRemoteEntries) { int id = remoteIndexedEntries.getInt(identifier); - T object = idToEntry.get(identifier); + T object = entriesById.get(identifier); // Warn if an object is missing from the local registry. // This should only happen in AUTHORITATIVE mode, and as such we @@ -342,9 +342,9 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } // Add the new object, increment nextId to match. - rawIdToEntry.size(Math.max(this.rawIdToEntry.size(), id + 1)); - rawIdToEntry.set(id, object); - entryToRawId.put(object, id); + field_26682.size(Math.max(this.field_26682.size(), id + 1)); + field_26682.set(id, object); + field_26683.put(object, id); if (nextId <= id) { nextId = id + 1; @@ -362,26 +362,26 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab // Emit AddObject events for previously culled objects. for (Identifier id : fabric_prevEntries.keySet()) { - if (!idToEntry.containsKey(id)) { + if (!entriesById.containsKey(id)) { assert fabric_prevIndexedEntries.containsKey(id); addedIds.add(id); } } - idToEntry.clear(); - keyToEntry.clear(); + entriesById.clear(); + entriesByKey.clear(); - idToEntry.putAll(fabric_prevEntries); + entriesById.putAll(fabric_prevEntries); for (Map.Entry entry : fabric_prevEntries.entrySet()) { //noinspection unchecked - keyToEntry.put(RegistryKey.of(RegistryKey.ofRegistry(((Registry) Registry.REGISTRIES).getId(this)), entry.getKey()), entry.getValue()); + entriesByKey.put(RegistryKey.of(RegistryKey.ofRegistry(((Registry) Registry.REGISTRIES).getId(this)), entry.getKey()), entry.getValue()); } remap(name, fabric_prevIndexedEntries, RemapMode.AUTHORITATIVE); for (Identifier id : addedIds) { - fabric_getAddObjectEvent().invoker().onEntryAdded(entryToRawId.getInt(idToEntry.get(id)), id, idToEntry.get(id)); + fabric_getAddObjectEvent().invoker().onEntryAdded(field_26683.getInt(entriesById.get(id)), id, entriesById.get(id)); } fabric_prevIndexedEntries = null; diff --git a/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json b/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json index f1955d64f..98426dcba 100644 --- a/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json +++ b/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json @@ -10,7 +10,9 @@ "MixinPlayerManager", "MixinLevelStorageSession", "MixinRegistry", - "MixinSimpleRegistry" + "MixinSimpleRegistry", + "DynamicRegistryManagerAccessor", + "DynamicRegistryManagerMixin" ], "client": [ "client.MixinBlockColorMap", diff --git a/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java b/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java index 63c266d9e..4568012b6 100644 --- a/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java +++ b/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java @@ -28,9 +28,10 @@ import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.SimpleRegistry; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; +import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; import net.fabricmc.fabric.api.event.registry.RegistryAttribute; import net.fabricmc.fabric.api.event.registry.RegistryAttributeHolder; -import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; public class RegistrySyncTest implements ModInitializer { /** @@ -65,8 +66,14 @@ public class RegistrySyncTest implements ModInitializer { Registry.register(fabricRegistry, new Identifier("registry_sync", "test"), "test"); + Validate.isTrue(Registry.REGISTRIES.getIds().contains(new Identifier("registry_sync", "fabric_registry"))); + Validate.isTrue(RegistryAttributeHolder.get(fabricRegistry).hasAttribute(RegistryAttribute.MODDED)); Validate.isTrue(RegistryAttributeHolder.get(fabricRegistry).hasAttribute(RegistryAttribute.SYNCED)); Validate.isTrue(!RegistryAttributeHolder.get(fabricRegistry).hasAttribute(RegistryAttribute.PERSISTED)); + + DynamicRegistryEntryAddedCallback.event(Registry.BIOME_KEY).register((rawId, key, object, registry) -> { + System.out.println(key); + }); } } From e5def8e7eb51f73e94afe7abbeac9cf2f58872c4 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Wed, 19 Aug 2020 19:40:47 +0200 Subject: [PATCH 02/14] bumbed loader version, finalized DynamicRegistryEvents --- .../fabric/impl/registry/sync/DynamicRegistryEvents.java | 5 ++++- fabric-registry-sync-v0/src/main/resources/fabric.mod.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java index b23f0efa9..e8827b160 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java @@ -28,9 +28,12 @@ import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; -public abstract class DynamicRegistryEvents { +public final class DynamicRegistryEvents { public static Map>, Event> ADD_ENTRY_EVENTS; + private DynamicRegistryEvents() { + } + static { ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); diff --git a/fabric-registry-sync-v0/src/main/resources/fabric.mod.json b/fabric-registry-sync-v0/src/main/resources/fabric.mod.json index 6fa2597a5..cb03233b2 100644 --- a/fabric-registry-sync-v0/src/main/resources/fabric.mod.json +++ b/fabric-registry-sync-v0/src/main/resources/fabric.mod.json @@ -16,7 +16,7 @@ "FabricMC" ], "depends": { - "fabricloader": ">=0.4.0", + "fabricloader": ">=0.9.2", "fabric-api-base": "*", "fabric-networking-v0": "*" }, From 1e30c893704ec31fb37f6dd265dbeeb92a9618a9 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Wed, 19 Aug 2020 21:21:15 +0200 Subject: [PATCH 03/14] Fixed some generics --- .../DynamicRegistryEntryAddedCallback.java | 9 ++++---- .../registry/sync/DynamicRegistryEvents.java | 22 +++++++++---------- .../sync/DynamicRegistryManagerMixin.java | 6 ++--- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java index 8363a4d2c..ad4f7d129 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java @@ -24,14 +24,15 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; @FunctionalInterface -public interface DynamicRegistryEntryAddedCallback { - void onEntryAdded(int rawId, RegistryKey key, Object object, MutableRegistry registry); +public interface DynamicRegistryEntryAddedCallback { + void onEntryAdded(int rawId, RegistryKey key, T object, MutableRegistry registry); - static Event event(RegistryKey> registryKey) { + @SuppressWarnings("unchecked") + static Event> event(RegistryKey> registryKey) { if (!DynamicRegistryEvents.ADD_ENTRY_EVENTS.containsKey(registryKey)) { throw new IllegalArgumentException("Unsupported registry: " + registryKey); } - return DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); + return (Event>) DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); } } diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java index e8827b160..e0509c88c 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java @@ -28,24 +28,22 @@ import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; +@SuppressWarnings({"rawtypes", "unchecked"}) public final class DynamicRegistryEvents { - public static Map>, Event> ADD_ENTRY_EVENTS; - - private DynamicRegistryEvents() { - } + public static final Map>, Event> ADD_ENTRY_EVENTS; static { ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); for (RegistryKey> registryKey : DynamicRegistryManagerAccessor.getInfos().keySet()) { - ADD_ENTRY_EVENTS.put(registryKey, - EventFactory.createArrayBacked( - DynamicRegistryEntryAddedCallback.class, - callbacks -> (rawId, key, object, registry) -> { - for (DynamicRegistryEntryAddedCallback callback : callbacks) { - callback.onEntryAdded(rawId, key, object, registry); - } - })); + ADD_ENTRY_EVENTS.put(registryKey, EventFactory.createArrayBacked(DynamicRegistryEntryAddedCallback.class, + callbacks -> (rawId, key, object, registry) -> { + for (DynamicRegistryEntryAddedCallback callback : callbacks) { + callback.onEntryAdded(rawId, key, object, registry); + } + })); } } + + private DynamicRegistryEvents() { } } diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java index 95128b71d..28ed013bd 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java @@ -35,14 +35,14 @@ import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; @Mixin(DynamicRegistryManager.class) public class DynamicRegistryManagerMixin { + @SuppressWarnings({"unchecked", "rawtypes"}) @Inject(method = "create", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/dynamic/RegistryOps$class_5506$class_5507;()V"), locals = LocalCapture.CAPTURE_FAILHARD) private static void onCreateImpl(CallbackInfoReturnable cir, DynamicRegistryManager.Impl registryManager) { - for (Map.Entry>, Event> event : DynamicRegistryEvents.ADD_ENTRY_EVENTS.entrySet()) { - //noinspection unchecked + for (Map.Entry>, Event> event : DynamicRegistryEvents.ADD_ENTRY_EVENTS.entrySet()) { RegistryKey> registryKey = (RegistryKey>) event.getKey(); RegistryEntryAddedCallback.event(registryManager.get(registryKey)).register((rawId, id, object) -> { RegistryKey key = RegistryKey.of(registryKey, id); - event.getValue().invoker().onEntryAdded(rawId, key, object, registryManager.get(registryKey)); + ((DynamicRegistryEntryAddedCallback) event.getValue().invoker()).onEntryAdded(rawId, key, object, registryManager.get(registryKey)); }); } } From 5610e94af4bc10f1c18daa247027ce91b36eb889 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Fri, 21 Aug 2020 21:03:29 +0200 Subject: [PATCH 04/14] Changed the event to a more generic one --- .../DynamicRegistryEntryAddedCallback.java | 38 -------------- .../DynamicRegistrySetupCallback.java | 20 ++++++++ .../registry/sync/DynamicRegistryEvents.java | 49 ------------------- .../sync/DynamicRegistryManagerAccessor.java | 34 ------------- .../sync/DynamicRegistryManagerMixin.java | 18 +------ .../fabric-registry-sync-v0.mixins.json | 1 - .../test/registry/sync/RegistrySyncTest.java | 9 ++-- 7 files changed, 28 insertions(+), 141 deletions(-) delete mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java delete mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java delete mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java deleted file mode 100644 index ad4f7d129..000000000 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 net.minecraft.util.registry.MutableRegistry; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.RegistryKey; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; - -@FunctionalInterface -public interface DynamicRegistryEntryAddedCallback { - void onEntryAdded(int rawId, RegistryKey key, T object, MutableRegistry registry); - - @SuppressWarnings("unchecked") - static Event> event(RegistryKey> registryKey) { - if (!DynamicRegistryEvents.ADD_ENTRY_EVENTS.containsKey(registryKey)) { - throw new IllegalArgumentException("Unsupported registry: " + registryKey); - } - - return (Event>) DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); - } -} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java new file mode 100644 index 000000000..df3b536f5 --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java @@ -0,0 +1,20 @@ +package net.fabricmc.fabric.api.event.registry; + +import net.minecraft.util.registry.DynamicRegistryManager; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +@FunctionalInterface +public interface DynamicRegistrySetupCallback { + void onSetupRegistry(DynamicRegistryManager registryManager); + + Event EVENT = EventFactory.createArrayBacked( + DynamicRegistrySetupCallback.class, + callbacks -> registryManager -> { + for (DynamicRegistrySetupCallback callback : callbacks) { + callback.onSetupRegistry(registryManager); + } + } + ); +} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java deleted file mode 100644 index e0509c88c..000000000 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.impl.registry.sync; - -import java.util.Map; - -import com.google.common.collect.Maps; - -import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.RegistryKey; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; -import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; -import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public final class DynamicRegistryEvents { - public static final Map>, Event> ADD_ENTRY_EVENTS; - - static { - ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); - - for (RegistryKey> registryKey : DynamicRegistryManagerAccessor.getInfos().keySet()) { - ADD_ENTRY_EVENTS.put(registryKey, EventFactory.createArrayBacked(DynamicRegistryEntryAddedCallback.class, - callbacks -> (rawId, key, object, registry) -> { - for (DynamicRegistryEntryAddedCallback callback : callbacks) { - callback.onEntryAdded(rawId, key, object, registry); - } - })); - } - } - - private DynamicRegistryEvents() { } -} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java deleted file mode 100644 index d9e72dd6c..000000000 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 java.util.Map; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.util.registry.DynamicRegistryManager; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.RegistryKey; - -@Mixin(DynamicRegistryManager.class) -public interface DynamicRegistryManagerAccessor { - @Accessor("INFOS") - static Map>, ?> getInfos() { - throw new AbstractMethodError(); - } -} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java index 28ed013bd..47c7b0d63 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java @@ -16,8 +16,6 @@ package net.fabricmc.fabric.mixin.registry.sync; -import java.util.Map; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -25,25 +23,13 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import net.minecraft.util.registry.DynamicRegistryManager; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.RegistryKey; -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; -import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback; -import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; +import net.fabricmc.fabric.api.event.registry.DynamicRegistrySetupCallback; @Mixin(DynamicRegistryManager.class) public class DynamicRegistryManagerMixin { - @SuppressWarnings({"unchecked", "rawtypes"}) @Inject(method = "create", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/dynamic/RegistryOps$class_5506$class_5507;()V"), locals = LocalCapture.CAPTURE_FAILHARD) private static void onCreateImpl(CallbackInfoReturnable cir, DynamicRegistryManager.Impl registryManager) { - for (Map.Entry>, Event> event : DynamicRegistryEvents.ADD_ENTRY_EVENTS.entrySet()) { - RegistryKey> registryKey = (RegistryKey>) event.getKey(); - RegistryEntryAddedCallback.event(registryManager.get(registryKey)).register((rawId, id, object) -> { - RegistryKey key = RegistryKey.of(registryKey, id); - ((DynamicRegistryEntryAddedCallback) event.getValue().invoker()).onEntryAdded(rawId, key, object, registryManager.get(registryKey)); - }); - } + DynamicRegistrySetupCallback.EVENT.invoker().onSetupRegistry(registryManager); } } diff --git a/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json b/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json index 98426dcba..caa3e0205 100644 --- a/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json +++ b/fabric-registry-sync-v0/src/main/resources/fabric-registry-sync-v0.mixins.json @@ -11,7 +11,6 @@ "MixinLevelStorageSession", "MixinRegistry", "MixinSimpleRegistry", - "DynamicRegistryManagerAccessor", "DynamicRegistryManagerMixin" ], "client": [ diff --git a/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java b/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java index 4568012b6..ff346a4bc 100644 --- a/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java +++ b/fabric-registry-sync-v0/src/testmod/java/net/fabricmc/fabric/test/registry/sync/RegistrySyncTest.java @@ -28,10 +28,11 @@ import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.SimpleRegistry; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; +import net.fabricmc.fabric.api.event.registry.DynamicRegistrySetupCallback; import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; import net.fabricmc.fabric.api.event.registry.RegistryAttribute; import net.fabricmc.fabric.api.event.registry.RegistryAttributeHolder; +import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback; public class RegistrySyncTest implements ModInitializer { /** @@ -72,8 +73,10 @@ public class RegistrySyncTest implements ModInitializer { Validate.isTrue(RegistryAttributeHolder.get(fabricRegistry).hasAttribute(RegistryAttribute.SYNCED)); Validate.isTrue(!RegistryAttributeHolder.get(fabricRegistry).hasAttribute(RegistryAttribute.PERSISTED)); - DynamicRegistryEntryAddedCallback.event(Registry.BIOME_KEY).register((rawId, key, object, registry) -> { - System.out.println(key); + DynamicRegistrySetupCallback.EVENT.register(registryManager -> { + RegistryEntryAddedCallback.event(registryManager.get(Registry.BIOME_KEY)).register((rawId, id, object) -> { + System.out.println(id); + }); }); } } From a0c9b80ef4ebc50cb2992c95ce11e5e3cae81cab Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Thu, 27 Aug 2020 13:29:59 +0200 Subject: [PATCH 05/14] Added some javadoc and the license header --- .../DynamicRegistrySetupCallback.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java index df3b536f5..d71526081 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java @@ -1,3 +1,19 @@ +/* + * 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 net.minecraft.util.registry.DynamicRegistryManager; @@ -5,6 +21,22 @@ import net.minecraft.util.registry.DynamicRegistryManager; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; +/** + * This event gets triggered when a new {@link DynamicRegistryManager} gets created, but before it gets filled. + * Therefore, this is the ideal place to register callbacks to dynamic registries. + * For example, the following code is used to register a callback that gets triggered for any registered Biome, both JSON and code defined. + * + *
+ * {@code
+ * DynamicRegistrySetupCallback.EVENT.register(registryManager -> {
+ *     Registry biomes = registryManager.get(Registry.BIOME_KEY);
+ *     RegistryEntryAddedCallback.event(biomes).register((rawId, id, object) -> {
+ *         // Do something
+ *     });
+ * });
+ * }
+ * 
+ */ @FunctionalInterface public interface DynamicRegistrySetupCallback { void onSetupRegistry(DynamicRegistryManager registryManager); From d932d0ccc12a55dbfd9d3bc5841dbcea194b9598 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Fri, 4 Sep 2020 17:36:50 +0200 Subject: [PATCH 06/14] increased gradle memory in the hope it fixes the github action fail --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 3ad283407..6f2aa62e3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -org.gradle.jvmargs=-Xmx2560M +org.gradle.jvmargs=-Xmx4g -Xms4g From a46b0be60a9f04de1ab9f9cb866358ed6149d06d Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Wed, 19 Aug 2020 18:31:52 +0200 Subject: [PATCH 07/14] Added DynamicRegistryEntryAddedCallback --- .../DynamicRegistryEntryAddedCallback.java | 37 ++++++++++++++ .../registry/sync/DynamicRegistryEvents.java | 48 +++++++++++++++++++ .../sync/DynamicRegistryManagerAccessor.java | 34 +++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java create mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java new file mode 100644 index 000000000..8363a4d2c --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java @@ -0,0 +1,37 @@ +/* + * 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 net.minecraft.util.registry.MutableRegistry; +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; + +@FunctionalInterface +public interface DynamicRegistryEntryAddedCallback { + void onEntryAdded(int rawId, RegistryKey key, Object object, MutableRegistry registry); + + static Event event(RegistryKey> registryKey) { + if (!DynamicRegistryEvents.ADD_ENTRY_EVENTS.containsKey(registryKey)) { + throw new IllegalArgumentException("Unsupported registry: " + registryKey); + } + + return DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); + } +} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java new file mode 100644 index 000000000..b23f0efa9 --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java @@ -0,0 +1,48 @@ +/* + * 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.impl.registry.sync; + +import java.util.Map; + +import com.google.common.collect.Maps; + +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; +import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; + +public abstract class DynamicRegistryEvents { + public static Map>, Event> ADD_ENTRY_EVENTS; + + static { + ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); + + for (RegistryKey> registryKey : DynamicRegistryManagerAccessor.getInfos().keySet()) { + ADD_ENTRY_EVENTS.put(registryKey, + EventFactory.createArrayBacked( + DynamicRegistryEntryAddedCallback.class, + callbacks -> (rawId, key, object, registry) -> { + for (DynamicRegistryEntryAddedCallback callback : callbacks) { + callback.onEntryAdded(rawId, key, object, registry); + } + })); + } + } +} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java new file mode 100644 index 000000000..d9e72dd6c --- /dev/null +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java @@ -0,0 +1,34 @@ +/* + * 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 java.util.Map; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.util.registry.DynamicRegistryManager; +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; + +@Mixin(DynamicRegistryManager.class) +public interface DynamicRegistryManagerAccessor { + @Accessor("INFOS") + static Map>, ?> getInfos() { + throw new AbstractMethodError(); + } +} From 7953e09bd8de9547510ba73db6b3cdc47b1ec802 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Wed, 19 Aug 2020 19:40:47 +0200 Subject: [PATCH 08/14] bumbed loader version, finalized DynamicRegistryEvents --- .../fabric/impl/registry/sync/DynamicRegistryEvents.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java index b23f0efa9..e8827b160 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java @@ -28,9 +28,12 @@ import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; -public abstract class DynamicRegistryEvents { +public final class DynamicRegistryEvents { public static Map>, Event> ADD_ENTRY_EVENTS; + private DynamicRegistryEvents() { + } + static { ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); From 1d8aa64520908e8e8e97024684a4fe474539040b Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Wed, 19 Aug 2020 21:21:15 +0200 Subject: [PATCH 09/14] Fixed some generics --- .../DynamicRegistryEntryAddedCallback.java | 9 ++++---- .../registry/sync/DynamicRegistryEvents.java | 22 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java index 8363a4d2c..ad4f7d129 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java @@ -24,14 +24,15 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; @FunctionalInterface -public interface DynamicRegistryEntryAddedCallback { - void onEntryAdded(int rawId, RegistryKey key, Object object, MutableRegistry registry); +public interface DynamicRegistryEntryAddedCallback { + void onEntryAdded(int rawId, RegistryKey key, T object, MutableRegistry registry); - static Event event(RegistryKey> registryKey) { + @SuppressWarnings("unchecked") + static Event> event(RegistryKey> registryKey) { if (!DynamicRegistryEvents.ADD_ENTRY_EVENTS.containsKey(registryKey)) { throw new IllegalArgumentException("Unsupported registry: " + registryKey); } - return DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); + return (Event>) DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); } } diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java index e8827b160..e0509c88c 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java @@ -28,24 +28,22 @@ import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; +@SuppressWarnings({"rawtypes", "unchecked"}) public final class DynamicRegistryEvents { - public static Map>, Event> ADD_ENTRY_EVENTS; - - private DynamicRegistryEvents() { - } + public static final Map>, Event> ADD_ENTRY_EVENTS; static { ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); for (RegistryKey> registryKey : DynamicRegistryManagerAccessor.getInfos().keySet()) { - ADD_ENTRY_EVENTS.put(registryKey, - EventFactory.createArrayBacked( - DynamicRegistryEntryAddedCallback.class, - callbacks -> (rawId, key, object, registry) -> { - for (DynamicRegistryEntryAddedCallback callback : callbacks) { - callback.onEntryAdded(rawId, key, object, registry); - } - })); + ADD_ENTRY_EVENTS.put(registryKey, EventFactory.createArrayBacked(DynamicRegistryEntryAddedCallback.class, + callbacks -> (rawId, key, object, registry) -> { + for (DynamicRegistryEntryAddedCallback callback : callbacks) { + callback.onEntryAdded(rawId, key, object, registry); + } + })); } } + + private DynamicRegistryEvents() { } } From ea60ad8ae6c153c63609e59bb9914db9a25839f4 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Fri, 21 Aug 2020 21:03:29 +0200 Subject: [PATCH 10/14] Changed the event to a more generic one --- .../DynamicRegistryEntryAddedCallback.java | 38 -------------- .../registry/sync/DynamicRegistryEvents.java | 49 ------------------- .../sync/DynamicRegistryManagerAccessor.java | 34 ------------- 3 files changed, 121 deletions(-) delete mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java delete mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java delete mode 100644 fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java deleted file mode 100644 index ad4f7d129..000000000 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistryEntryAddedCallback.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 net.minecraft.util.registry.MutableRegistry; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.RegistryKey; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryEvents; - -@FunctionalInterface -public interface DynamicRegistryEntryAddedCallback { - void onEntryAdded(int rawId, RegistryKey key, T object, MutableRegistry registry); - - @SuppressWarnings("unchecked") - static Event> event(RegistryKey> registryKey) { - if (!DynamicRegistryEvents.ADD_ENTRY_EVENTS.containsKey(registryKey)) { - throw new IllegalArgumentException("Unsupported registry: " + registryKey); - } - - return (Event>) DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey); - } -} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java deleted file mode 100644 index e0509c88c..000000000 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/impl/registry/sync/DynamicRegistryEvents.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.impl.registry.sync; - -import java.util.Map; - -import com.google.common.collect.Maps; - -import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.RegistryKey; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; -import net.fabricmc.fabric.api.event.registry.DynamicRegistryEntryAddedCallback; -import net.fabricmc.fabric.mixin.registry.sync.DynamicRegistryManagerAccessor; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public final class DynamicRegistryEvents { - public static final Map>, Event> ADD_ENTRY_EVENTS; - - static { - ADD_ENTRY_EVENTS = Maps.newLinkedHashMap(); - - for (RegistryKey> registryKey : DynamicRegistryManagerAccessor.getInfos().keySet()) { - ADD_ENTRY_EVENTS.put(registryKey, EventFactory.createArrayBacked(DynamicRegistryEntryAddedCallback.class, - callbacks -> (rawId, key, object, registry) -> { - for (DynamicRegistryEntryAddedCallback callback : callbacks) { - callback.onEntryAdded(rawId, key, object, registry); - } - })); - } - } - - private DynamicRegistryEvents() { } -} diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java deleted file mode 100644 index d9e72dd6c..000000000 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerAccessor.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 java.util.Map; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.util.registry.DynamicRegistryManager; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.registry.RegistryKey; - -@Mixin(DynamicRegistryManager.class) -public interface DynamicRegistryManagerAccessor { - @Accessor("INFOS") - static Map>, ?> getInfos() { - throw new AbstractMethodError(); - } -} From 825410cfbc8642c926d4c6679d290a4364664d8d Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Sun, 13 Sep 2020 13:10:16 +0200 Subject: [PATCH 11/14] reverted memory change --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 6f2aa62e3..3ad283407 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -org.gradle.jvmargs=-Xmx4g -Xms4g +org.gradle.jvmargs=-Xmx2560M From 002bec9c0567fef2b47f11c1a43df88be43a0623 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Sun, 13 Sep 2020 13:17:51 +0200 Subject: [PATCH 12/14] updated mixin for new mappings --- .../mixin/registry/sync/MixinIdRegistry.java | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java index c88624030..870ccdbe0 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java @@ -62,16 +62,16 @@ import net.fabricmc.fabric.impl.registry.sync.RemappableRegistry; public abstract class MixinIdRegistry implements RemappableRegistry, ListenableRegistry { @Shadow @Final - private ObjectList field_26682; + private ObjectList rawIdToEntry; @Shadow @Final - private Object2IntMap field_26683; + private Object2IntMap entryToRawId; @Shadow @Final - private BiMap entriesById; + private BiMap idToEntry; @Shadow @Final - private BiMap, T> entriesByKey; + private BiMap, T> keyToEntry; @Shadow private int nextId; @Unique @@ -135,21 +135,21 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab private boolean fabric_isObjectNew = false; @SuppressWarnings({"unchecked", "ConstantConditions"}) - @Inject(method = "method_31051", at = @At("HEAD")) + @Inject(method = "set(ILnet/minecraft/util/registry/RegistryKey;Ljava/lang/Object;Lcom/mojang/serialization/Lifecycle;Z)Ljava/lang/Object;", at = @At("HEAD")) public void setPre(int id, RegistryKey registryId, Object object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { - int indexedEntriesId = field_26683.getInt((T) object); + int indexedEntriesId = entryToRawId.getInt((T) object); if (indexedEntriesId >= 0) { throw new RuntimeException("Attempted to register object " + object + " twice! (at raw IDs " + indexedEntriesId + " and " + id + " )"); } - if (!entriesById.containsKey(registryId.getValue())) { + if (!idToEntry.containsKey(registryId.getValue())) { fabric_isObjectNew = true; } else { - T oldObject = entriesById.get(registryId.getValue()); + T oldObject = idToEntry.get(registryId.getValue()); if (oldObject != null && oldObject != object) { - int oldId = field_26683.getInt(oldObject); + int oldId = entryToRawId.getInt(oldObject); if (oldId != id && checkDuplicateKeys) { throw new RuntimeException("Attempted to register ID " + registryId + " at different raw IDs (" + oldId + ", " + id + ")! If you're trying to override an item, use .set(), not .register()!"); @@ -164,7 +164,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } @SuppressWarnings("unchecked") - @Inject(method = "method_31051", at = @At("RETURN")) + @Inject(method = "set(ILnet/minecraft/util/registry/RegistryKey;Ljava/lang/Object;Lcom/mojang/serialization/Lifecycle;Z)Ljava/lang/Object;", at = @At("RETURN")) public void setPost(int id, RegistryKey registryId, Object object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { if (fabric_isObjectNew) { fabric_addObjectEvent.invoker().onEntryAdded(id, registryId.getValue(), object); @@ -184,7 +184,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab List strings = null; for (Identifier remoteId : remoteIndexedEntries.keySet()) { - if (!entriesById.keySet().contains(remoteId)) { + if (!idToEntry.keySet().contains(remoteId)) { if (strings == null) { strings = new ArrayList<>(); } @@ -206,11 +206,11 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab break; } case EXACT: { - if (!entriesById.keySet().equals(remoteIndexedEntries.keySet())) { + if (!idToEntry.keySet().equals(remoteIndexedEntries.keySet())) { List strings = new ArrayList<>(); for (Identifier remoteId : remoteIndexedEntries.keySet()) { - if (!entriesById.keySet().contains(remoteId)) { + if (!idToEntry.keySet().contains(remoteId)) { strings.add(" - " + remoteId + " (missing on local)"); } } @@ -242,7 +242,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab // compatibility. if (fabric_prevIndexedEntries == null) { fabric_prevIndexedEntries = new Object2IntOpenHashMap<>(); - fabric_prevEntries = HashBiMap.create(entriesById); + fabric_prevEntries = HashBiMap.create(idToEntry); for (Object o : registry) { fabric_prevIndexedEntries.put(registry.getId(o), registry.getRawId(o)); @@ -297,8 +297,8 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } // note: indexedEntries cannot be safely remove()d from - entriesById.keySet().removeAll(droppedIds); - entriesByKey.keySet().removeIf(registryKey -> droppedIds.contains(registryKey.getValue())); + idToEntry.keySet().removeAll(droppedIds); + keyToEntry.keySet().removeIf(registryKey -> droppedIds.contains(registryKey.getValue())); break; } @@ -306,7 +306,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab Int2IntMap idMap = new Int2IntOpenHashMap(); - for (Object o : field_26682) { + for (Object o : rawIdToEntry) { Identifier id = registry.getId(o); int rid = registry.getRawId(o); @@ -317,8 +317,8 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } // entries was handled above, if it was necessary. - field_26682.clear(); - field_26683.clear(); + rawIdToEntry.clear(); + entryToRawId.clear(); nextId = 0; List orderedRemoteEntries = new ArrayList<>(remoteIndexedEntries.keySet()); @@ -326,7 +326,7 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab for (Identifier identifier : orderedRemoteEntries) { int id = remoteIndexedEntries.getInt(identifier); - T object = entriesById.get(identifier); + T object = idToEntry.get(identifier); // Warn if an object is missing from the local registry. // This should only happen in AUTHORITATIVE mode, and as such we @@ -342,9 +342,9 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab } // Add the new object, increment nextId to match. - field_26682.size(Math.max(this.field_26682.size(), id + 1)); - field_26682.set(id, object); - field_26683.put(object, id); + rawIdToEntry.size(Math.max(this.rawIdToEntry.size(), id + 1)); + rawIdToEntry.set(id, object); + entryToRawId.put(object, id); if (nextId <= id) { nextId = id + 1; @@ -362,26 +362,26 @@ public abstract class MixinIdRegistry implements RemappableRegistry, Listenab // Emit AddObject events for previously culled objects. for (Identifier id : fabric_prevEntries.keySet()) { - if (!entriesById.containsKey(id)) { + if (!idToEntry.containsKey(id)) { assert fabric_prevIndexedEntries.containsKey(id); addedIds.add(id); } } - entriesById.clear(); - entriesByKey.clear(); + idToEntry.clear(); + keyToEntry.clear(); - entriesById.putAll(fabric_prevEntries); + idToEntry.putAll(fabric_prevEntries); for (Map.Entry entry : fabric_prevEntries.entrySet()) { //noinspection unchecked - entriesByKey.put(RegistryKey.of(RegistryKey.ofRegistry(((Registry) Registry.REGISTRIES).getId(this)), entry.getKey()), entry.getValue()); + keyToEntry.put(RegistryKey.of(RegistryKey.ofRegistry(((Registry) Registry.REGISTRIES).getId(this)), entry.getKey()), entry.getValue()); } remap(name, fabric_prevIndexedEntries, RemapMode.AUTHORITATIVE); for (Identifier id : addedIds) { - fabric_getAddObjectEvent().invoker().onEntryAdded(field_26683.getInt(entriesById.get(id)), id, entriesById.get(id)); + fabric_getAddObjectEvent().invoker().onEntryAdded(entryToRawId.getInt(idToEntry.get(id)), id, idToEntry.get(id)); } fabric_prevIndexedEntries = null; From 811b763fc3ea42364f080c83297901df02429219 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Thu, 1 Oct 2020 22:58:14 +0200 Subject: [PATCH 13/14] removed redundant supressions --- .../fabric/mixin/registry/sync/MixinIdRegistry.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java index ddcbfc2c9..a39365139 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/MixinIdRegistry.java @@ -132,9 +132,8 @@ public abstract class MixinIdRegistry extends Registry implements Remappab @Unique private boolean fabric_isObjectNew = false; - @SuppressWarnings({"unchecked", "ConstantConditions"}) @Inject(method = "set(ILnet/minecraft/util/registry/RegistryKey;Ljava/lang/Object;Lcom/mojang/serialization/Lifecycle;Z)Ljava/lang/Object;", at = @At("HEAD")) - public void setPre(int id, RegistryKey registryId, T object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { + public void setPre(int id, RegistryKey registryId, T object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { int indexedEntriesId = entryToRawId.getInt(object); if (indexedEntriesId >= 0) { @@ -161,9 +160,8 @@ public abstract class MixinIdRegistry extends Registry implements Remappab } } - @SuppressWarnings("unchecked") @Inject(method = "set(ILnet/minecraft/util/registry/RegistryKey;Ljava/lang/Object;Lcom/mojang/serialization/Lifecycle;Z)Ljava/lang/Object;", at = @At("RETURN")) - public void setPost(int id, RegistryKey registryId, T object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { + public void setPost(int id, RegistryKey registryId, T object, Lifecycle lifecycle, boolean checkDuplicateKeys, CallbackInfoReturnable info) { if (fabric_isObjectNew) { fabric_addObjectEvent.invoker().onEntryAdded(id, registryId.getValue(), object); } From 888073d094f731c6a4519ef6b5eb3479ae746e41 Mon Sep 17 00:00:00 2001 From: CheaterCodes Date: Thu, 1 Oct 2020 23:37:40 +0200 Subject: [PATCH 14/14] renamed event method --- .../api/event/registry/DynamicRegistrySetupCallback.java | 4 ++-- .../mixin/registry/sync/DynamicRegistryManagerMixin.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java index d71526081..d16aea52e 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/DynamicRegistrySetupCallback.java @@ -39,13 +39,13 @@ import net.fabricmc.fabric.api.event.EventFactory; */ @FunctionalInterface public interface DynamicRegistrySetupCallback { - void onSetupRegistry(DynamicRegistryManager registryManager); + void onRegistrySetup(DynamicRegistryManager registryManager); Event EVENT = EventFactory.createArrayBacked( DynamicRegistrySetupCallback.class, callbacks -> registryManager -> { for (DynamicRegistrySetupCallback callback : callbacks) { - callback.onSetupRegistry(registryManager); + callback.onRegistrySetup(registryManager); } } ); diff --git a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java index 47c7b0d63..58df768d5 100644 --- a/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java +++ b/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/mixin/registry/sync/DynamicRegistryManagerMixin.java @@ -30,6 +30,6 @@ import net.fabricmc.fabric.api.event.registry.DynamicRegistrySetupCallback; public class DynamicRegistryManagerMixin { @Inject(method = "create", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/dynamic/RegistryOps$class_5506$class_5507;()V"), locals = LocalCapture.CAPTURE_FAILHARD) private static void onCreateImpl(CallbackInfoReturnable cir, DynamicRegistryManager.Impl registryManager) { - DynamicRegistrySetupCallback.EVENT.invoker().onSetupRegistry(registryManager); + DynamicRegistrySetupCallback.EVENT.invoker().onRegistrySetup(registryManager); } }