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-06-07 19:34:16 -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;
|
2020-06-19 10:58:45 -04:00
|
|
|
|
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;
|
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.MappedRegistry;
|
2022-03-02 13:38:02 -08:00
|
|
|
import net.minecraft.core.Registry;
|
|
|
|
|
|
2019-05-23 16:02:15 -07:00
|
|
|
import org.apache.commons.lang3.Validate;
|
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
|
|
|
|
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;
|
|
|
|
|
|
2021-07-17 00:08:49 -05:00
|
|
|
class NamespacedWrapper<T extends IForgeRegistryEntry<T>> extends MappedRegistry<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();
|
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
|
|
|
public NamespacedWrapper(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(owner.getRegistryKey(), Lifecycle.experimental(), holderLookup);
|
2017-06-19 15:02:18 -07:00
|
|
|
this.delegate = owner;
|
2022-03-02 13:38:02 -08:00
|
|
|
this.holders = new NamespacedHolderHelper<>(this, null, holderLookup);
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-03-01 14:59:27 -06:00
|
|
|
public Holder<T> registerMapping(int id, ResourceKey<T> key, T 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);
|
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 = get(value.getRegistryName());
|
|
|
|
|
|
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);
|
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(ResourceKey<T> key, T 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
|
2022-03-01 14:59:27 -06:00
|
|
|
public Holder<T> registerOrOverride(OptionalInt id, ResourceKey<T> key, T 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reading Functions
|
|
|
|
|
@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.getRaw(name); //get without default
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-07 19:34:16 -07:00
|
|
|
@Override
|
2020-10-29 01:11:57 +01:00
|
|
|
public Optional<T> getOptional(@Nullable ResourceLocation name)
|
2019-06-07 19:34:16 -07:00
|
|
|
{
|
|
|
|
|
return Optional.ofNullable( this.delegate.getRaw(name)); //get without default
|
|
|
|
|
}
|
|
|
|
|
|
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 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-03-06 21:01:12 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean isEmpty()
|
|
|
|
|
{
|
|
|
|
|
return this.delegate.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:02:18 -07:00
|
|
|
@Override
|
2022-03-02 13:38:02 -08:00
|
|
|
public int size()
|
|
|
|
|
{
|
|
|
|
|
return this.delegate.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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-02 13:38:02 -08:00
|
|
|
/** @Depreciated Forge: For internal use only. Use the Register events when registering values. */
|
|
|
|
|
@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 NamespacedWrapper<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, NamespacedWrapper.class).holders.onAdded(stage, id, value, oldValue);
|
|
|
|
|
}
|
2017-06-19 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
}
|