2018-07-01 14:17:28 -07:00
|
|
|
/*
|
2022-02-28 13:41:23 -08:00
|
|
|
* Minecraft Forge - Forge Development LLC
|
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
2018-07-01 14:17:28 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
package net.minecraftforge.registries;
|
|
|
|
|
|
2018-01-18 12:46:05 -08:00
|
|
|
import java.util.Collection;
|
2017-06-19 15:02:18 -07:00
|
|
|
import java.util.Iterator;
|
2020-08-26 09:49:34 -07:00
|
|
|
import java.util.Map;
|
2019-05-23 16:02:15 -07:00
|
|
|
import java.util.Optional;
|
2020-08-13 00:13:48 -07:00
|
|
|
import java.util.OptionalInt;
|
2017-06-19 15:02:18 -07:00
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.Validate;
|
|
|
|
|
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.resources.ResourceKey;
|
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
|
import net.minecraft.core.DefaultedRegistry;
|
2018-08-29 21:30:15 -04:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2017-06-19 15:02:18 -07:00
|
|
|
|
2020-06-19 10:58:45 -04:00
|
|
|
import com.mojang.serialization.Lifecycle;
|
|
|
|
|
|
2019-05-23 16:02:15 -07:00
|
|
|
class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends DefaultedRegistry<T> implements ILockableRegistry
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2018-08-29 21:30:15 -04:00
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
2017-06-19 15:02:18 -07:00
|
|
|
private boolean locked = false;
|
2019-05-23 16:02:15 -07:00
|
|
|
private ForgeRegistry<T> delegate;
|
2017-06-19 15:02:18 -07:00
|
|
|
|
2019-05-23 16:02:15 -07:00
|
|
|
private NamespacedDefaultedWrapper(ForgeRegistry<T> owner)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2020-08-26 09:49:34 -07:00
|
|
|
super("empty", owner.getRegistryKey(), Lifecycle.experimental());
|
2017-06-19 15:02:18 -07:00
|
|
|
this.delegate = owner;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2021-07-17 00:08:49 -05:00
|
|
|
public <V extends T> V registerMapping(int id, ResourceKey<T> key, V value, Lifecycle lifecycle)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
if (locked)
|
|
|
|
|
throw new IllegalStateException("Can not register to a locked registry. Modder should use Forge Register methods.");
|
|
|
|
|
Validate.notNull(value);
|
|
|
|
|
|
|
|
|
|
if (value.getRegistryName() == null)
|
2021-03-09 13:37:23 -08:00
|
|
|
value.setRegistryName(key.location());
|
2017-06-19 15:02:18 -07:00
|
|
|
|
|
|
|
|
int realId = this.delegate.add(id, value);
|
|
|
|
|
if (realId != id && id != -1)
|
2018-08-29 21:30:15 -04:00
|
|
|
LOGGER.warn("Registered object did not get ID it asked for. Name: {} Type: {} Expected: {} Got: {}", key, value.getRegistryType().getName(), id, realId);
|
2019-05-23 16:02:15 -07:00
|
|
|
|
|
|
|
|
return value;
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2021-07-17 00:08:49 -05:00
|
|
|
public <V extends T> V register(ResourceKey<T> key, V value, Lifecycle lifecycle)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2021-03-09 13:37:23 -08:00
|
|
|
return registerMapping(-1, key, value, lifecycle);
|
2020-08-13 00:13:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2021-07-17 00:08:49 -05:00
|
|
|
public <V extends T> V registerOrOverride(OptionalInt id, ResourceKey<T> key, V value, Lifecycle lifecycle) {
|
2020-08-13 00:13:48 -07:00
|
|
|
int wanted = -1;
|
2021-03-09 13:37:23 -08:00
|
|
|
if (id.isPresent() && byId(id.getAsInt()) != null)
|
2020-08-13 00:13:48 -07:00
|
|
|
wanted = id.getAsInt();
|
2021-03-09 13:37:23 -08:00
|
|
|
return registerMapping(wanted, key, value, lifecycle);
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-14 15:08:53 -08:00
|
|
|
// Reading Functions
|
2017-06-24 01:46:05 -07:00
|
|
|
@Override
|
2020-10-29 01:11:57 +01:00
|
|
|
public Optional<T> getOptional(@Nullable ResourceLocation name)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2019-05-23 16:02:15 -07:00
|
|
|
return Optional.ofNullable( this.delegate.getRaw(name)); //get without default
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2021-03-09 13:37:23 -08:00
|
|
|
public T get(@Nullable ResourceLocation name)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2019-02-14 15:08:53 -08:00
|
|
|
return this.delegate.getValue(name); //getOrDefault
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-23 23:35:34 -07:00
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2021-07-17 00:08:49 -05:00
|
|
|
public T get(@Nullable ResourceKey<T> name)
|
2020-09-23 23:35:34 -07:00
|
|
|
{
|
2021-03-09 13:37:23 -08:00
|
|
|
return name == null ? null : this.delegate.getRaw(name.location()); //get without default
|
2020-09-23 23:35:34 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2019-05-23 16:02:15 -07:00
|
|
|
public ResourceLocation getKey(T value)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
return this.delegate.getKey(value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 18:57:41 +00:00
|
|
|
@Override
|
|
|
|
|
public Optional<ResourceKey<T>> getResourceKey(T p_122755_)
|
|
|
|
|
{
|
|
|
|
|
return this.delegate.getResourceKey(p_122755_);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public boolean containsKey(ResourceLocation key)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
return this.delegate.containsKey(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public int getId(@Nullable T value)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
return this.delegate.getID(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2021-03-09 13:37:23 -08:00
|
|
|
public T byId(int id)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
return this.delegate.getValue(id);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 18:57:41 +00:00
|
|
|
@Override
|
|
|
|
|
public Lifecycle lifecycle(T p_122764_) {
|
|
|
|
|
return Lifecycle.stable();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public Iterator<T> iterator()
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
return this.delegate.iterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public Set<ResourceLocation> keySet()
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
return this.delegate.getKeys();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 09:49:34 -07:00
|
|
|
@Override
|
2021-07-17 00:08:49 -05:00
|
|
|
public Set<Map.Entry<ResourceKey<T>, T>> entrySet()
|
2020-08-26 09:49:34 -07:00
|
|
|
{
|
|
|
|
|
return this.delegate.getEntries();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@Override
|
|
|
|
|
@Nullable
|
2019-05-23 16:02:15 -07:00
|
|
|
public T getRandom(Random random)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2019-05-23 16:02:15 -07:00
|
|
|
Collection<T> values = this.delegate.getValues();
|
2018-11-15 21:52:45 +00:00
|
|
|
return values.stream().skip(random.nextInt(values.size())).findFirst().orElse(this.delegate.getDefault());
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-14 15:08:53 -08:00
|
|
|
@Override
|
2019-05-23 16:02:15 -07:00
|
|
|
public ResourceLocation getDefaultKey()
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
|
|
|
|
return this.delegate.getDefaultKey();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
//internal
|
|
|
|
|
@Override
|
|
|
|
|
public void lock(){ this.locked = true; }
|
|
|
|
|
|
2018-09-05 20:06:18 -07:00
|
|
|
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
public static final ResourceLocation ID = new ResourceLocation("forge", "registry_defaulted_wrapper");
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage)
|
|
|
|
|
{
|
|
|
|
|
owner.setSlaveMap(ID, new NamespacedDefaultedWrapper<V>((ForgeRegistry<V>)owner));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|