2016-09-18 01:08:42 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.minecraftforge.event;
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.Validate;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableList;
|
2016-09-18 01:08:42 +00:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2017-06-19 22:02:18 +00:00
|
|
|
import net.minecraftforge.fml.common.ModContainer;
|
2016-09-18 01:08:42 +00:00
|
|
|
import net.minecraftforge.fml.common.eventhandler.Event;
|
|
|
|
import net.minecraftforge.fml.common.eventhandler.GenericEvent;
|
2016-10-08 23:30:53 +00:00
|
|
|
import net.minecraftforge.fml.common.eventhandler.IContextSetter;
|
2017-06-19 22:02:18 +00:00
|
|
|
import net.minecraftforge.registries.IForgeRegistry;
|
|
|
|
import net.minecraftforge.registries.IForgeRegistryEntry;
|
2016-09-18 01:08:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RegistryEvent supertype.
|
|
|
|
*/
|
2016-10-08 23:30:53 +00:00
|
|
|
public class RegistryEvent<T extends IForgeRegistryEntry<T>> extends GenericEvent<T> implements IContextSetter
|
2016-09-18 01:08:42 +00:00
|
|
|
{
|
|
|
|
RegistryEvent(Class<T> clazz) {
|
|
|
|
super(clazz);
|
|
|
|
}
|
|
|
|
/**
|
2017-06-19 22:02:18 +00:00
|
|
|
* Register new registries when you receive this event, through the {@link RecipeBuilder}
|
2016-09-18 01:08:42 +00:00
|
|
|
*/
|
2017-06-19 22:02:18 +00:00
|
|
|
public static class NewRegistry extends Event
|
|
|
|
{
|
2016-09-18 01:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register your objects for the appropriate registry type when you receive this event.
|
|
|
|
*
|
|
|
|
* <code>event.getRegistry().register(...)</code>
|
|
|
|
*
|
|
|
|
* The registries will be visited in alphabetic order of their name, except blocks and items,
|
|
|
|
* which will be visited FIRST and SECOND respectively.
|
|
|
|
*
|
|
|
|
* ObjectHolders will reload between Blocks and Items, and after all registries have been visited.
|
|
|
|
* @param <T> The registry top level type
|
|
|
|
*/
|
2017-06-19 22:02:18 +00:00
|
|
|
public static class Register<T extends IForgeRegistryEntry<T>> extends RegistryEvent<T>
|
|
|
|
{
|
2016-09-18 01:08:42 +00:00
|
|
|
private final IForgeRegistry<T> registry;
|
2017-06-19 22:02:18 +00:00
|
|
|
private final ResourceLocation name;
|
2016-09-18 01:08:42 +00:00
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
public Register(ResourceLocation name, IForgeRegistry<T> registry)
|
2016-09-18 01:08:42 +00:00
|
|
|
{
|
|
|
|
super(registry.getRegistrySuperType());
|
2017-06-19 22:02:18 +00:00
|
|
|
this.name = name;
|
2016-09-18 01:08:42 +00:00
|
|
|
this.registry = registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IForgeRegistry<T> getRegistry()
|
|
|
|
{
|
|
|
|
return registry;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
public ResourceLocation getName()
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class MissingMappings<T extends IForgeRegistryEntry<T>> extends RegistryEvent<T>
|
|
|
|
{
|
|
|
|
private final IForgeRegistry<T> registry;
|
|
|
|
private final ResourceLocation name;
|
|
|
|
private final ImmutableList<Mapping<T>> mappings;
|
|
|
|
private ModContainer activeMod;
|
|
|
|
|
|
|
|
public MissingMappings(ResourceLocation name, IForgeRegistry<T> registry, Collection<Mapping<T>> missed)
|
|
|
|
{
|
|
|
|
super(registry.getRegistrySuperType());
|
|
|
|
this.registry = registry;
|
|
|
|
this.name = name;
|
|
|
|
this.mappings = ImmutableList.copyOf(missed);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ResourceLocation getName()
|
|
|
|
{
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IForgeRegistry<T> getRegistry()
|
|
|
|
{
|
|
|
|
return this.registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImmutableList<Mapping<T>> getMappings()
|
|
|
|
{
|
|
|
|
return ImmutableList.copyOf(this.mappings.stream().filter(e -> e.key.getResourceDomain().equals(this.activeMod.getModId())).collect(Collectors.toList()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImmutableList<Mapping<T>> getAllMappings()
|
|
|
|
{
|
|
|
|
return this.mappings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actions you can take with this missing mapping.
|
|
|
|
* <ul>
|
|
|
|
* <li>{@link #IGNORE} means this missing mapping will be ignored.
|
|
|
|
* <li>{@link #WARN} means this missing mapping will generate a warning.
|
|
|
|
* <li>{@link #FAIL} means this missing mapping will prevent the world from loading.
|
|
|
|
* </ul>
|
|
|
|
*/
|
|
|
|
public enum Action
|
2016-09-18 01:08:42 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2016-09-18 01:08:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
public static class Mapping<T extends IForgeRegistryEntry<T>>
|
|
|
|
{
|
|
|
|
public final IForgeRegistry<T> registry;
|
|
|
|
public final ResourceLocation key;
|
|
|
|
public final int id;
|
|
|
|
private Action action = Action.DEFAULT;
|
|
|
|
private T target;
|
|
|
|
|
|
|
|
public Mapping(IForgeRegistry<T> registry, ResourceLocation key, int id)
|
|
|
|
{
|
|
|
|
this.registry = registry;
|
|
|
|
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(registry.getKey(target) != null, String.format("The specified entry %s hasn't been registered in registry yet.", target));
|
|
|
|
action = Action.REMAP;
|
|
|
|
this.target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
public Action getAction()
|
|
|
|
{
|
|
|
|
return this.action;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T getTarget()
|
|
|
|
{
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 01:08:42 +00:00
|
|
|
}
|
|
|
|
}
|