/* * Copyright (c) Forge Development LLC and contributors * SPDX-License-Identifier: LGPL-2.1-only */ package net.minecraftforge.registries; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.eventbus.api.Event; import org.apache.commons.lang3.Validate; import java.util.Collection; import java.util.List; import java.util.Locale; /** * Fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS forge bus}. */ public class MissingMappingsEvent extends Event { private final ResourceKey> key; private final IForgeRegistry registry; private final List> mappings; public MissingMappingsEvent(ResourceKey> key, IForgeRegistry registry, Collection> missed) { this.key = key; this.registry = registry; this.mappings = List.copyOf(missed); } public ResourceKey> getKey() { return this.key; } public IForgeRegistry getRegistry() { return this.registry; } /** * @return An immutable list of missing mappings for the given namespace. * Empty if the registry key doesn't match {@link #getKey()}. */ @SuppressWarnings("unchecked") public List> getMappings(ResourceKey> registryKey, String namespace) { return registryKey == this.key ? (List>) (List) this.mappings.stream().filter(e -> e.key.getNamespace().equals(namespace)).toList() : List.of(); } /** * @return An immutable list of all missing mappings. * Empty if the registry key doesn't match {@link #getKey()}. */ @SuppressWarnings("unchecked") public List> getAllMappings(ResourceKey> registryKey) { return registryKey == this.key ? (List>) (List) this.mappings : List.of(); } /** * Actions you can take with this missing mapping. *
    *
  • {@link #IGNORE} means this missing mapping will be ignored. *
  • {@link #WARN} means this missing mapping will generate a warning. *
  • {@link #FAIL} means this missing mapping will prevent the world from loading. *
*/ public enum Action { /** * Take the default action */ DEFAULT, /** * Ignore this missing mapping. This means the mapping will be abandoned */ IGNORE, /** * Generate a warning but allow loading to continue */ WARN, /** * Fail to load */ FAIL, /** * Remap this name to a new name (add a migration mapping) */ REMAP } public static class Mapping implements Comparable> { private final IForgeRegistry registry; private final IForgeRegistry pool; final ResourceLocation key; final int id; Action action = Action.DEFAULT; T target; public Mapping(IForgeRegistry registry, IForgeRegistry pool, ResourceLocation key, int id) { this.registry = registry; this.pool = pool; this.key = key; this.id = id; } /** * Ignore the missing item. */ public void ignore() { action = Action.IGNORE; } /** * Warn the user about the missing item. */ public void warn() { action = Action.WARN; } /** * Prevent the world from loading due to the missing item. */ public void fail() { action = Action.FAIL; } /** * Remap the missing entry to the specified object. *

* Use this if you have renamed an entry. * Existing references using the old name will point to the new one. * * @param target Entry to remap to. */ public void remap(T target) { Validate.notNull(target, "Remap target can not be null"); Validate.isTrue(pool.getKey(target) != null, String.format(Locale.ENGLISH, "The specified entry %s hasn't been registered in registry yet.", target)); action = Action.REMAP; this.target = target; } public IForgeRegistry getRegistry() { return this.registry; } public ResourceLocation getKey() { return key; } public int getId() { return this.id; } @Override public int compareTo(Mapping o) { int ret = this.registry.getRegistryName().compareNamespaced(o.registry.getRegistryName()); if (ret == 0) ret = this.key.compareNamespaced(o.key); return ret; } } }