mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-08 21:14:41 -04:00
Added DynamicRegistryEntryAddedCallback
This commit is contained in:
parent
d932d0ccc1
commit
a46b0be60a
3 changed files with 119 additions and 0 deletions
fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric
api/event/registry
impl/registry/sync
mixin/registry/sync
|
@ -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<DynamicRegistryEntryAddedCallback> event(RegistryKey<? extends Registry<?>> registryKey) {
|
||||
if (!DynamicRegistryEvents.ADD_ENTRY_EVENTS.containsKey(registryKey)) {
|
||||
throw new IllegalArgumentException("Unsupported registry: " + registryKey);
|
||||
}
|
||||
|
||||
return DynamicRegistryEvents.ADD_ENTRY_EVENTS.get(registryKey);
|
||||
}
|
||||
}
|
|
@ -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<RegistryKey<? extends Registry<?>>, Event<DynamicRegistryEntryAddedCallback>> ADD_ENTRY_EVENTS;
|
||||
|
||||
static {
|
||||
ADD_ENTRY_EVENTS = Maps.newLinkedHashMap();
|
||||
|
||||
for (RegistryKey<? extends Registry<?>> 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);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<RegistryKey<? extends Registry<?>>, ?> getInfos() {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue