2019-02-14 15:08:53 -08:00
|
|
|
/*
|
|
|
|
|
* Minecraft Forge
|
|
|
|
|
* Copyright (c) 2016-2019.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2019-02-14 18:49:40 -05:00
|
|
|
|
2019-02-14 15:08:53 -08:00
|
|
|
package net.minecraftforge.registries;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
2019-07-31 22:43:46 -07:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
2019-02-14 15:08:53 -08:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
2019-07-31 22:43:46 -07:00
|
|
|
import java.util.Map;
|
2019-05-23 16:02:15 -07:00
|
|
|
import java.util.Optional;
|
2019-02-14 15:08:53 -08:00
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.Validate;
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
import org.apache.logging.log4j.Marker;
|
|
|
|
|
import org.apache.logging.log4j.MarkerManager;
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
|
import com.google.common.collect.HashBiMap;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.util.IntIdentityHashBiMap;
|
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
2019-05-23 16:02:15 -07:00
|
|
|
import net.minecraft.util.registry.MutableRegistry;
|
2019-02-14 15:08:53 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A IRegistry implementation that allows for removing of objects. This is a internal helper
|
|
|
|
|
* class used by Forge for managing things that reset during Server initialization.
|
|
|
|
|
*/
|
2019-05-23 16:02:15 -07:00
|
|
|
public class ClearableRegistry<T> extends MutableRegistry<T>
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
|
private static final Marker REGISTRY = MarkerManager.getMarker("REGISTRY");
|
|
|
|
|
|
2019-05-23 16:02:15 -07:00
|
|
|
private final IntIdentityHashBiMap<T> ids = new IntIdentityHashBiMap<>(256);
|
|
|
|
|
private final BiMap<ResourceLocation, T> map = HashBiMap.create();
|
2019-02-14 15:08:53 -08:00
|
|
|
private final Set<ResourceLocation> keys = Collections.unmodifiableSet(map.keySet());
|
2019-05-23 16:02:15 -07:00
|
|
|
private List<T> values = new ArrayList<>();
|
2019-07-31 22:43:46 -07:00
|
|
|
private Map<ResourceLocation, Set<T>> known = new HashMap<>();
|
2019-02-14 15:08:53 -08:00
|
|
|
private final ResourceLocation name;
|
2019-07-31 22:43:46 -07:00
|
|
|
private final boolean isDelegated;
|
2019-02-14 15:08:53 -08:00
|
|
|
private int nextId = 0;
|
|
|
|
|
|
2019-07-31 22:43:46 -07:00
|
|
|
public ClearableRegistry(ResourceLocation name) { this(name, null); }
|
|
|
|
|
public ClearableRegistry(ResourceLocation name, Class<T> superType)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
this.name = name;
|
2019-07-31 22:43:46 -07:00
|
|
|
this.isDelegated = ForgeRegistryEntry.class.isAssignableFrom(superType); //TODO: Make this IDelegatedRegistryEntry?
|
2019-02-14 15:08:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2019-05-23 16:02:15 -07:00
|
|
|
public ResourceLocation getKey(T value)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return map.inverse().get(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2019-05-23 16:02:15 -07:00
|
|
|
public int getId(T value)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return ids.getId(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2019-05-23 16:02:15 -07:00
|
|
|
public T getByValue(int id)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
2019-05-23 16:02:15 -07:00
|
|
|
return ids.getByValue(id);
|
2019-02-14 15:08:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public Iterator<T> iterator()
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return ids.iterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2019-05-23 16:02:15 -07:00
|
|
|
public T getOrDefault(ResourceLocation key)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return map.get(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-08 10:45:28 -04:00
|
|
|
public <V extends T> V register(int id, ResourceLocation key, V value)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
Validate.isTrue(id >= 0, "Invalid ID, can not be < 0");
|
|
|
|
|
Validate.notNull(key);
|
|
|
|
|
Validate.notNull(value);
|
|
|
|
|
|
2019-05-23 16:02:15 -07:00
|
|
|
T old = map.get(key);
|
2019-02-14 15:08:53 -08:00
|
|
|
if (old != null)
|
|
|
|
|
{
|
2019-07-31 22:43:46 -07:00
|
|
|
LOGGER.debug(REGISTRY, "{}: Adding duplicate key '{}' to registry. Old: {} New: {}", name, key, old, value);
|
2019-02-14 15:08:53 -08:00
|
|
|
values.remove(old);
|
2019-07-31 22:43:46 -07:00
|
|
|
if (isDelegated)
|
|
|
|
|
{
|
|
|
|
|
Set<T> others = known.computeIfAbsent(key, k -> new HashSet<>());
|
|
|
|
|
others.add(old);
|
|
|
|
|
others.forEach(e -> getDelegate(e).changeReference(value));
|
|
|
|
|
}
|
2019-02-14 15:08:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
map.put(key, value);
|
|
|
|
|
ids.put(value, id);
|
|
|
|
|
values.add(value);
|
|
|
|
|
if (nextId <= id)
|
|
|
|
|
nextId = id + 1;
|
2019-05-23 16:02:15 -07:00
|
|
|
|
2019-07-31 22:43:46 -07:00
|
|
|
if (isDelegated)
|
|
|
|
|
getDelegate(value).setName(key);
|
|
|
|
|
|
2019-05-23 16:02:15 -07:00
|
|
|
return value;
|
2019-02-14 15:08:53 -08:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 22:43:46 -07:00
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
|
|
|
private RegistryDelegate<T> getDelegate(T thing)
|
|
|
|
|
{
|
|
|
|
|
if (isDelegated)
|
|
|
|
|
return (RegistryDelegate<T>)((ForgeRegistryEntry)thing).delegate;
|
|
|
|
|
else
|
|
|
|
|
throw new IllegalStateException("Tried to get existing delegate from registry that is not delegated.");
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 15:08:53 -08:00
|
|
|
@Override
|
2019-06-08 10:45:28 -04:00
|
|
|
public <V extends T> V register(ResourceLocation key, V value)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
2019-06-08 10:45:28 -04:00
|
|
|
return register(nextId, key, value);
|
2019-02-14 15:08:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public Set<ResourceLocation> keySet()
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isEmpty()
|
|
|
|
|
{
|
|
|
|
|
return map.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2019-05-23 16:02:15 -07:00
|
|
|
public T getRandom(Random random)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return values.isEmpty() ? null : values.get(random.nextInt(values.size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public boolean containsKey(ResourceLocation key)
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return map.containsKey(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void clear()
|
|
|
|
|
{
|
|
|
|
|
LOGGER.debug(REGISTRY, "{}: Clearing registry", name);
|
2019-07-31 22:43:46 -07:00
|
|
|
if (isDelegated)
|
|
|
|
|
{
|
|
|
|
|
known.values().forEach(s -> {
|
|
|
|
|
s.forEach(e -> getDelegate(e).changeReference(e));
|
|
|
|
|
s.clear();
|
|
|
|
|
});
|
|
|
|
|
known.clear();
|
|
|
|
|
}
|
2019-02-14 15:08:53 -08:00
|
|
|
map.clear();
|
|
|
|
|
values.clear();
|
|
|
|
|
ids.clear();
|
|
|
|
|
nextId = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getNextId()
|
|
|
|
|
{
|
|
|
|
|
return nextId;
|
|
|
|
|
}
|
2019-05-23 16:02:15 -07:00
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 22:43:46 -07:00
|
|
|
public Optional<T> getValue(ResourceLocation key)
|
|
|
|
|
{
|
2019-06-21 22:42:16 -04:00
|
|
|
return Optional.ofNullable(map.get(key));
|
2019-05-23 16:02:15 -07:00
|
|
|
}
|
2019-02-14 15:08:53 -08:00
|
|
|
}
|