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;
|
|
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
2022-03-02 13:38:02 -08:00
|
|
|
import java.util.List;
|
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;
|
2022-03-02 13:38:02 -08:00
|
|
|
import java.util.Random;
|
2017-06-19 15:02:18 -07:00
|
|
|
import java.util.Set;
|
2022-03-01 14:59:27 -06:00
|
|
|
import java.util.function.Function;
|
2022-03-02 13:38:02 -08:00
|
|
|
import java.util.stream.Stream;
|
2017-06-19 15:02:18 -07:00
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
2022-03-01 14:59:27 -06:00
|
|
|
import net.minecraft.core.Holder;
|
2022-03-02 13:38:02 -08:00
|
|
|
import net.minecraft.core.HolderSet;
|
|
|
|
|
import net.minecraft.core.Registry;
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
import org.apache.commons.lang3.Validate;
|
|
|
|
|
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.resources.ResourceKey;
|
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2022-03-02 13:38:02 -08:00
|
|
|
import net.minecraft.tags.TagKey;
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.core.DefaultedRegistry;
|
2022-03-02 13:38:02 -08:00
|
|
|
import com.mojang.datafixers.util.Pair;
|
2020-06-19 10:58:45 -04:00
|
|
|
import com.mojang.serialization.Lifecycle;
|
|
|
|
|
|
2022-03-16 15:36:00 -05:00
|
|
|
class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends DefaultedRegistry<T> implements ILockableRegistry, IHolderHelperHolder<T>
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2022-03-02 13:38:02 -08:00
|
|
|
private final ForgeRegistry<T> delegate;
|
|
|
|
|
private final NamespacedHolderHelper<T> holders;
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
private boolean locked = false;
|
2022-03-02 13:38:02 -08:00
|
|
|
private Lifecycle elementsLifecycle = Lifecycle.experimental();
|
2017-06-19 15:02:18 -07:00
|
|
|
|
2022-03-01 14:59:27 -06:00
|
|
|
private NamespacedDefaultedWrapper(ForgeRegistry<T> owner, Function<T, Holder.Reference<T>> holderLookup)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2022-03-01 14:59:27 -06:00
|
|
|
super("empty", owner.getRegistryKey(), Lifecycle.experimental(), holderLookup);
|
2017-06-19 15:02:18 -07:00
|
|
|
this.delegate = owner;
|
2022-03-16 15:36:00 -05:00
|
|
|
this.holders = new NamespacedHolderHelper<>(owner, this, this.delegate.getDefaultKey(), holderLookup);
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-03-01 14:59:27 -06:00
|
|
|
public Holder<T> registerMapping(final int id, final ResourceKey<T> key, final T value, final 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.");
|
2022-03-02 13:38:02 -08:00
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
Validate.notNull(value);
|
2022-03-02 13:38:02 -08:00
|
|
|
this.elementsLifecycle = this.elementsLifecycle.add(lifecycle);
|
2017-06-19 15:02:18 -07:00
|
|
|
|
|
|
|
|
if (value.getRegistryName() == null)
|
2021-03-09 13:37:23 -08:00
|
|
|
value.setRegistryName(key.location());
|
2017-06-19 15:02:18 -07:00
|
|
|
|
2022-03-02 13:38:02 -08:00
|
|
|
T oldValue = this.delegate.getRaw(value.getRegistryName());
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
int realId = this.delegate.add(id, value);
|
2019-05-23 16:02:15 -07:00
|
|
|
|
2022-03-02 13:38:02 -08:00
|
|
|
return this.holders.onAdded(RegistryManager.ACTIVE, realId, value, oldValue);
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-03-01 14:59:27 -06:00
|
|
|
public Holder<T> register(final ResourceKey<T> p_205891_, final T p_205892_, final Lifecycle p_205893_)
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
2022-03-01 14:59:27 -06:00
|
|
|
return registerMapping(-1, p_205891_, p_205892_, p_205893_);
|
2020-08-13 00:13:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-03-01 14:59:27 -06:00
|
|
|
public Holder<T> registerOrOverride(final OptionalInt id, final ResourceKey<T> p_205885_, final T p_205886_, final Lifecycle p_205887_)
|
|
|
|
|
{
|
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();
|
2022-03-01 14:59:27 -06:00
|
|
|
return registerMapping(wanted, p_205885_, p_205886_, p_205887_);
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:38:02 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean containsKey(ResourceKey<T> key)
|
|
|
|
|
{
|
|
|
|
|
return this.delegate.getRegistryName().equals(key.registry()) && containsKey(key.location());
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@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
|
2022-03-02 13:38:02 -08:00
|
|
|
public ResourceLocation getDefaultKey()
|
|
|
|
|
{
|
|
|
|
|
return this.delegate.getDefaultKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Lifecycle lifecycle(T value)
|
|
|
|
|
{
|
2021-11-26 18:57:41 +00:00
|
|
|
return Lifecycle.stable();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:38:02 -08:00
|
|
|
@Override
|
|
|
|
|
public Lifecycle elementsLifecycle()
|
|
|
|
|
{
|
|
|
|
|
return this.elementsLifecycle;
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 15:08:53 -08:00
|
|
|
@Override
|
2022-03-02 13:38:02 -08:00
|
|
|
public boolean isEmpty()
|
2019-02-14 15:08:53 -08:00
|
|
|
{
|
2022-03-02 13:38:02 -08:00
|
|
|
return this.delegate.isEmpty();
|
2019-02-14 15:08:53 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@Override
|
2022-03-02 13:38:02 -08:00
|
|
|
public int size()
|
|
|
|
|
{
|
|
|
|
|
return this.delegate.size();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 15:36:00 -05:00
|
|
|
@Override
|
|
|
|
|
public NamespacedHolderHelper<T> getHolderHelper()
|
|
|
|
|
{
|
|
|
|
|
return this.holders;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 13:38:02 -08:00
|
|
|
@Override public Optional<Holder<T>> getHolder(int id) { return this.holders.getHolder(id); }
|
|
|
|
|
@Override public Optional<Holder<T>> getHolder(ResourceKey<T> key) { return this.holders.getHolder(key); }
|
|
|
|
|
@Override public Holder<T> getOrCreateHolder(ResourceKey<T> key) { return this.holders.getOrCreateHolder(key); }
|
|
|
|
|
@Override public Optional<Holder<T>> getRandom(Random rand) { return this.holders.getRandom(rand); }
|
|
|
|
|
@Override public Stream<Holder.Reference<T>> holders() { return this.holders.holders(); }
|
|
|
|
|
@Override public boolean isKnownTagName(TagKey<T> name) { return this.holders.isKnownTagName(name); }
|
|
|
|
|
@Override public Stream<Pair<TagKey<T>, HolderSet.Named<T>>> getTags() { return this.holders.getTags(); }
|
|
|
|
|
@Override public HolderSet.Named<T> getOrCreateTag(TagKey<T> name) { return this.holders.getOrCreateTag(name); }
|
|
|
|
|
@Override public Stream<TagKey<T>> getTagNames() { return this.holders.getTagNames(); }
|
|
|
|
|
@Override public Registry<T> freeze() { return this.holders.freeze(); }
|
|
|
|
|
@Override public Holder.Reference<T> createIntrusiveHolder(T value) { return this.holders.createIntrusiveHolder(value); }
|
|
|
|
|
@Override public Optional<HolderSet.Named<T>> getTag(TagKey<T> name) { return this.holders.getTag(name); }
|
|
|
|
|
@Override public void bindTags(Map<TagKey<T>, List<Holder<T>>> newTags) { this.holders.bindTags(newTags); }
|
|
|
|
|
@Override public void resetTags() { this.holders.resetTags(); }
|
|
|
|
|
@Deprecated @Override public void unfreeze() { this.holders.unfreeze(); }
|
2017-06-19 15:02:18 -07:00
|
|
|
|
2022-03-16 15:36:00 -05:00
|
|
|
/** @deprecated Forge: For internal use only. Use the Register events when registering values. */
|
2022-03-02 13:38:02 -08:00
|
|
|
@Deprecated @Override public void lock(){ this.locked = true; }
|
2022-03-01 14:59:27 -06:00
|
|
|
|
2022-03-02 13:38:02 -08:00
|
|
|
|
|
|
|
|
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>, IForgeRegistry.AddCallback<V>
|
2017-06-19 15:02:18 -07:00
|
|
|
{
|
|
|
|
|
public static final ResourceLocation ID = new ResourceLocation("forge", "registry_defaulted_wrapper");
|
2022-03-02 13:38:02 -08:00
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@Override
|
|
|
|
|
public void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage)
|
|
|
|
|
{
|
2022-03-01 14:59:27 -06:00
|
|
|
ForgeRegistry<V> fowner = (ForgeRegistry<V>)owner;
|
|
|
|
|
owner.setSlaveMap(ID, new NamespacedDefaultedWrapper<V>(fowner, fowner.getBuilder().getVanillaHolder()));
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
2022-03-02 13:38:02 -08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public void onAdd(IForgeRegistryInternal<V> owner, RegistryManager stage, int id, V value, V oldValue)
|
|
|
|
|
{
|
|
|
|
|
owner.getSlaveMap(ID, NamespacedDefaultedWrapper.class).holders.onAdded(stage, id, value, oldValue);
|
|
|
|
|
}
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
}
|