mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
Add support to Forge registry wrappers for new Holder system. Closes #8491
Fix TagBasedToolTypesTest not generating needed data correctly.
This commit is contained in:
1 parent
14bca0d757
commit
735382be03
19 files changed
+630
-54
No files matched your search
@@ -88,6 +88,7 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
|
||||
|
||||
private final Codec<V> codec = new RegistryCodec();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ForgeRegistry(RegistryManager stage, ResourceLocation name, RegistryBuilder<V> builder)
|
||||
{
|
||||
this.name = name;
|
||||
@@ -209,6 +210,11 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
|
||||
return this.names.isEmpty();
|
||||
}
|
||||
|
||||
int size()
|
||||
{
|
||||
return this.names.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue(ResourceLocation key)
|
||||
{
|
||||
@@ -392,12 +398,6 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
|
||||
}
|
||||
}
|
||||
|
||||
if (this.builder.getVanillaHolder() != null)
|
||||
{
|
||||
var ref = this.builder.getVanillaHolder().apply(value);
|
||||
ref.bind(rkey, value);
|
||||
}
|
||||
|
||||
if (this.add != null)
|
||||
this.add.onAdd(this, this.stage, idToUse, value, oldEntry);
|
||||
|
||||
|
||||
@@ -6,35 +6,43 @@
|
||||
package net.minecraftforge.registries;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.core.Registry;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.core.DefaultedRegistry;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.serialization.Lifecycle;
|
||||
|
||||
class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends DefaultedRegistry<T> implements ILockableRegistry
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final ForgeRegistry<T> delegate;
|
||||
private final NamespacedHolderHelper<T> holders;
|
||||
|
||||
private boolean locked = false;
|
||||
private ForgeRegistry<T> delegate;
|
||||
private Lifecycle elementsLifecycle = Lifecycle.experimental();
|
||||
|
||||
private NamespacedDefaultedWrapper(ForgeRegistry<T> owner, Function<T, Holder.Reference<T>> holderLookup)
|
||||
{
|
||||
super("empty", owner.getRegistryKey(), Lifecycle.experimental(), holderLookup);
|
||||
this.delegate = owner;
|
||||
this.holders = new NamespacedHolderHelper<>(this, this.delegate.getDefaultKey(), holderLookup);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,16 +50,18 @@ class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends Defau
|
||||
{
|
||||
if (locked)
|
||||
throw new IllegalStateException("Can not register to a locked registry. Modder should use Forge Register methods.");
|
||||
|
||||
Validate.notNull(value);
|
||||
this.elementsLifecycle = this.elementsLifecycle.add(lifecycle);
|
||||
|
||||
if (value.getRegistryName() == null)
|
||||
value.setRegistryName(key.location());
|
||||
|
||||
int realId = this.delegate.add(id, value);
|
||||
if (realId != id && id != -1)
|
||||
LOGGER.warn("Registered object did not get ID it asked for. Name: {} Type: {} Expected: {} Got: {}", key, value.getRegistryType().getName(), id, realId);
|
||||
T oldValue = this.delegate.getRaw(value.getRegistryName());
|
||||
|
||||
return super.registerMapping(realId, key, value, lifecycle);
|
||||
int realId = this.delegate.add(id, value);
|
||||
|
||||
return this.holders.onAdded(RegistryManager.ACTIVE, realId, value, oldValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,6 +119,12 @@ class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends Defau
|
||||
return this.delegate.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(ResourceKey<T> key)
|
||||
{
|
||||
return this.delegate.getRegistryName().equals(key.registry()) && containsKey(key.location());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId(@Nullable T value)
|
||||
{
|
||||
@@ -123,10 +139,23 @@ class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends Defau
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lifecycle lifecycle(T p_122764_) {
|
||||
public ResourceLocation getDefaultKey()
|
||||
{
|
||||
return this.delegate.getDefaultKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lifecycle lifecycle(T value)
|
||||
{
|
||||
return Lifecycle.stable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lifecycle elementsLifecycle()
|
||||
{
|
||||
return this.elementsLifecycle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator()
|
||||
{
|
||||
@@ -145,28 +174,54 @@ class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends Defau
|
||||
return this.delegate.getEntries();
|
||||
}
|
||||
|
||||
//TODO-PATCHING: Figure out if getRandom needs to be overriden.
|
||||
|
||||
@Override
|
||||
public ResourceLocation getDefaultKey()
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return this.delegate.getDefaultKey();
|
||||
return this.delegate.isEmpty();
|
||||
}
|
||||
|
||||
//internal
|
||||
@Override
|
||||
public void lock(){ this.locked = true; }
|
||||
public int size()
|
||||
{
|
||||
return this.delegate.size();
|
||||
}
|
||||
|
||||
//@Override public void unfreeze() {}
|
||||
@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(); }
|
||||
|
||||
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>
|
||||
/** @Depreciated Forge: For internal use only. Use the Register events when registering values. */
|
||||
@Deprecated @Override public void lock(){ this.locked = true; }
|
||||
|
||||
|
||||
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>, IForgeRegistry.AddCallback<V>
|
||||
{
|
||||
public static final ResourceLocation ID = new ResourceLocation("forge", "registry_defaulted_wrapper");
|
||||
|
||||
@Override
|
||||
public void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage)
|
||||
{
|
||||
ForgeRegistry<V> fowner = (ForgeRegistry<V>)owner;
|
||||
owner.setSlaveMap(ID, new NamespacedDefaultedWrapper<V>(fowner, fowner.getBuilder().getVanillaHolder()));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
/*
|
||||
* Minecraft Forge - Forge Development LLC
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.registries;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectList;
|
||||
import net.minecraft.Util;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.TagKey;
|
||||
|
||||
class NamespacedHolderHelper<T extends IForgeRegistryEntry<T>>
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final Registry<T> self;
|
||||
@Nullable
|
||||
private final ResourceLocation defaultKey;
|
||||
@Nullable
|
||||
private final Function<T, Holder.Reference<T>> holderLookup;
|
||||
|
||||
private boolean frozen = false; // Frozen is vanilla's variant of locked, but it can be unfrozen
|
||||
private List<Holder.Reference<T>> holdersSorted;
|
||||
private ObjectList<Holder.Reference<T>> holdersById = new ObjectArrayList<>(256);
|
||||
private Map<ResourceLocation, Holder.Reference<T>> holdersByName = new HashMap<>();
|
||||
private Map<T, Holder.Reference<T>> holders = new IdentityHashMap<>();
|
||||
private volatile Map<TagKey<T>, HolderSet.Named<T>> tags = new IdentityHashMap<>();
|
||||
private Holder.Reference<T> defaultHolder;
|
||||
|
||||
NamespacedHolderHelper(Registry<T> self, @Nullable ResourceLocation defaultKey, @Nullable Function<T, Holder.Reference<T>> holderLookup)
|
||||
{
|
||||
this.self = self;
|
||||
this.defaultKey = defaultKey;
|
||||
this.holderLookup = holderLookup;
|
||||
}
|
||||
|
||||
|
||||
Optional<Holder<T>> getHolder(int id)
|
||||
{
|
||||
return id >= 0 && id < this.holdersById.size() ? Optional.ofNullable(this.holdersById.get(id)) : Optional.empty();
|
||||
}
|
||||
|
||||
Optional<Holder<T>> getHolder(ResourceKey<T> key)
|
||||
{
|
||||
return Optional.ofNullable(this.holdersByName.get(key.location()));
|
||||
}
|
||||
|
||||
Holder<T> getOrCreateHolder(ResourceKey<T> key)
|
||||
{
|
||||
return this.holdersByName.computeIfAbsent(key.location(), k ->
|
||||
{
|
||||
if (this.holderLookup != null)
|
||||
throw new IllegalStateException("This registry can't create new holders without value");
|
||||
if (this.frozen)
|
||||
throw new IllegalStateException("Registry is already frozen (trying to add key " + k + ")");
|
||||
return Holder.Reference.createStandAlone(this.self, key);
|
||||
});
|
||||
}
|
||||
|
||||
Optional<Holder<T>> getRandom(Random rand)
|
||||
{
|
||||
Optional<Holder<T>> ret = Util.getRandomSafe(this.getSortedHolders(), rand).map(Holder::hackyErase);
|
||||
if (this.defaultKey != null)
|
||||
ret = ret.or(() -> Optional.of(this.defaultHolder));
|
||||
return ret;
|
||||
}
|
||||
|
||||
Stream<Holder.Reference<T>> holders()
|
||||
{
|
||||
return this.getSortedHolders().stream();
|
||||
}
|
||||
|
||||
boolean isKnownTagName(TagKey<T> name)
|
||||
{
|
||||
return this.tags.containsKey(name);
|
||||
}
|
||||
|
||||
Stream<Pair<TagKey<T>, HolderSet.Named<T>>> getTags()
|
||||
{
|
||||
return this.tags.entrySet().stream().map(e -> Pair.of(e.getKey(), e.getValue()));
|
||||
}
|
||||
|
||||
HolderSet.Named<T> getOrCreateTag(TagKey<T> name)
|
||||
{
|
||||
HolderSet.Named<T> named = this.tags.get(name);
|
||||
if (named == null)
|
||||
{
|
||||
named = createTag(name);
|
||||
// They use volatile and set it this way to not have the performance penalties of synced read access. But this makes a lot of new maps.. We need to look into performance alternatives.
|
||||
Map<TagKey<T>, HolderSet.Named<T>> map = new IdentityHashMap<>(this.tags);
|
||||
map.put(name, named);
|
||||
this.tags = map;
|
||||
}
|
||||
|
||||
return named;
|
||||
}
|
||||
|
||||
Stream<TagKey<T>> getTagNames()
|
||||
{
|
||||
return this.tags.keySet().stream();
|
||||
}
|
||||
|
||||
//TODO: Move this to ValidateCallback?
|
||||
Registry<T> freeze()
|
||||
{
|
||||
this.frozen = true;
|
||||
List<ResourceLocation> unregisterd = this.holdersByName.entrySet().stream()
|
||||
.filter(e -> !e.getValue().isBound())
|
||||
.map(e -> e.getKey()).sorted().toList();
|
||||
|
||||
if (!unregisterd.isEmpty())
|
||||
throw new IllegalStateException("Unbound values in registry " + this.self.key() + ": " + unregisterd.stream().map(ResourceLocation::toString).collect(Collectors.joining(", \n\t")));
|
||||
|
||||
if (this.holderLookup != null)
|
||||
{
|
||||
List<Holder.Reference<T>> intrusive = this.holders.values().stream().filter(h -> h.getType() == Holder.Reference.Type.INTRUSIVE && !h.isBound()).toList();
|
||||
if (!intrusive.isEmpty())
|
||||
throw new IllegalStateException("Some intrusive holders were not added to registry: " + intrusive);
|
||||
}
|
||||
return this.self;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
Holder.Reference<T> createIntrusiveHolder(T value)
|
||||
{
|
||||
if (this.holderLookup == null)
|
||||
throw new IllegalStateException("This registry can't create intrusive holders");
|
||||
if (this.frozen)
|
||||
throw new IllegalStateException("Registry is already frozen");
|
||||
|
||||
return this.holders.computeIfAbsent(value, k -> Holder.Reference.createIntrusive(this.self, value));
|
||||
}
|
||||
|
||||
Optional<HolderSet.Named<T>> getTag(TagKey<T> name)
|
||||
{
|
||||
return Optional.ofNullable(this.tags.get(name));
|
||||
}
|
||||
|
||||
void bindTags(Map<TagKey<T>, List<Holder<T>>> newTags)
|
||||
{
|
||||
Map<Holder.Reference<T>, List<TagKey<T>>> holderToTag = new IdentityHashMap<>();
|
||||
this.holdersByName.values().forEach(v -> holderToTag.put(v, new ArrayList<>()));
|
||||
newTags.forEach((k, v) ->
|
||||
{
|
||||
for(Holder<T> holder : v)
|
||||
{
|
||||
if (!holder.isValidInRegistry(this.self))
|
||||
throw new IllegalStateException("Can't create named set " + k + " containing value " + holder + " from outside registry " + this);
|
||||
|
||||
if (!(holder instanceof Holder.Reference<T>))
|
||||
throw new IllegalStateException("Found direct holder " + holder + " value in tag " + k);
|
||||
holderToTag.get((Holder.Reference<T>)holder).add(k);
|
||||
}
|
||||
});
|
||||
|
||||
Set<TagKey<T>> set = Sets.difference(this.tags.keySet(), newTags.keySet());
|
||||
if (!set.isEmpty())
|
||||
LOGGER.warn("Not all defined tags for registry {} are present in data pack: {}", this.self.key(), set.stream().map(k -> k.location().toString()).sorted().collect(Collectors.joining(", \n\t")));
|
||||
|
||||
Map<TagKey<T>, HolderSet.Named<T>> tmpTags = new IdentityHashMap<>(this.tags);
|
||||
newTags.forEach((k, v) -> tmpTags.computeIfAbsent(k, this::createTag).bind(v));
|
||||
holderToTag.forEach(Holder.Reference::bindTags);
|
||||
this.tags = tmpTags;
|
||||
}
|
||||
|
||||
void resetTags()
|
||||
{
|
||||
this.tags.values().forEach(t -> t.bind(List.of()));
|
||||
this.holders.values().forEach(v -> v.bindTags(Set.of()));
|
||||
}
|
||||
|
||||
void unfreeze()
|
||||
{
|
||||
this.frozen = false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
Holder<T> onAdded(RegistryManager stage, int id, T newValue, T oldValue)
|
||||
{
|
||||
if (stage != RegistryManager.ACTIVE)
|
||||
return null; // Only do holder shit on the active registries, not pending or snapshots.
|
||||
|
||||
ResourceKey<T> key = ResourceKey.create(this.self.key(), newValue.getRegistryName());
|
||||
|
||||
//Holder.Reference<T> oldHolder = oldValue == null ? null : getHolder(key, oldValue);
|
||||
// Do we need to do anything with the old holder? We cant update its pointer unless its non-intrusive...
|
||||
// And if thats the case, then we *should* get its reference in newHolder
|
||||
|
||||
Holder.Reference<T> newHolder = getHolder(key, newValue);
|
||||
|
||||
this.holdersById.size(Math.max(this.holdersById.size(), id + 1));
|
||||
this.holdersById.set(id, newHolder);
|
||||
this.holdersByName.put(newValue.getRegistryName(), newHolder);
|
||||
this.holders.put(newValue, newHolder);
|
||||
newHolder.bind(key, newValue);
|
||||
this.holdersSorted = null;
|
||||
|
||||
|
||||
if (this.defaultKey != null && this.defaultKey.equals(key.location()))
|
||||
this.defaultHolder = newHolder;
|
||||
|
||||
return newHolder;
|
||||
}
|
||||
|
||||
private HolderSet.Named<T> createTag(TagKey<T> name)
|
||||
{
|
||||
return new HolderSet.Named<>(this.self, name);
|
||||
}
|
||||
|
||||
|
||||
private Holder.Reference<T> getHolder(ResourceKey<T> key, T value)
|
||||
{
|
||||
if (this.holderLookup != null)
|
||||
return this.holderLookup.apply(value);
|
||||
|
||||
return this.holdersByName.computeIfAbsent(value.getRegistryName(), k -> Holder.Reference.createStandAlone(this.self, key));
|
||||
}
|
||||
|
||||
private List<Holder.Reference<T>> getSortedHolders()
|
||||
{
|
||||
if (this.holdersSorted == null)
|
||||
this.holdersSorted = this.holdersById.stream().filter(Objects::nonNull).toList();
|
||||
|
||||
return this.holdersSorted;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,33 +6,46 @@
|
||||
package net.minecraftforge.registries;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.core.MappedRegistry;
|
||||
import net.minecraft.core.Registry;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.serialization.Lifecycle;
|
||||
|
||||
class NamespacedWrapper<T extends IForgeRegistryEntry<T>> extends MappedRegistry<T> implements ILockableRegistry
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final ForgeRegistry<T> delegate;
|
||||
private final NamespacedHolderHelper<T> holders;
|
||||
|
||||
private boolean locked = false;
|
||||
private ForgeRegistry<T> delegate;
|
||||
private Lifecycle elementsLifecycle = Lifecycle.experimental();
|
||||
|
||||
public NamespacedWrapper(ForgeRegistry<T> owner, Function<T, Holder.Reference<T>> holderLookup)
|
||||
{
|
||||
super(owner.getRegistryKey(), Lifecycle.experimental(), holderLookup);
|
||||
this.delegate = owner;
|
||||
this.holders = new NamespacedHolderHelper<>(this, null, holderLookup);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,14 +55,18 @@ class NamespacedWrapper<T extends IForgeRegistryEntry<T>> extends MappedRegistry
|
||||
throw new IllegalStateException("Can not register to a locked registry. Modder should use Forge Register methods.");
|
||||
|
||||
Validate.notNull(value);
|
||||
this.elementsLifecycle = this.elementsLifecycle.add(lifecycle);
|
||||
|
||||
if (value.getRegistryName() == null)
|
||||
value.setRegistryName(key.location());
|
||||
|
||||
T oldValue = get(value.getRegistryName());
|
||||
|
||||
int realId = this.delegate.add(id, value);
|
||||
if (realId != id && id != -1)
|
||||
LOGGER.warn("Registered object did not get ID it asked for. Name: {} Type: {} Expected: {} Got: {}", key, value.getRegistryType().getName(), id, realId);
|
||||
return super.registerMapping(realId, key, value, lifecycle);
|
||||
|
||||
return this.holders.onAdded(RegistryManager.ACTIVE, realId, value, oldValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -106,6 +123,12 @@ class NamespacedWrapper<T extends IForgeRegistryEntry<T>> extends MappedRegistry
|
||||
return this.delegate.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(ResourceKey<T> key)
|
||||
{
|
||||
return this.delegate.getRegistryName().equals(key.registry()) && containsKey(key.location());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId(@Nullable T value)
|
||||
{
|
||||
@@ -120,10 +143,17 @@ class NamespacedWrapper<T extends IForgeRegistryEntry<T>> extends MappedRegistry
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lifecycle lifecycle(T p_122764_) {
|
||||
public Lifecycle lifecycle(T value)
|
||||
{
|
||||
return Lifecycle.stable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lifecycle elementsLifecycle()
|
||||
{
|
||||
return this.elementsLifecycle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator()
|
||||
{
|
||||
@@ -142,30 +172,54 @@ class NamespacedWrapper<T extends IForgeRegistryEntry<T>> extends MappedRegistry
|
||||
return this.delegate.getEntries();
|
||||
}
|
||||
|
||||
// TODO-PATCHING: Figure out if getRandom needs to be overriden.
|
||||
|
||||
/*
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return this.delegate.isEmpty();
|
||||
}
|
||||
*/
|
||||
|
||||
//internal
|
||||
@Override
|
||||
public void lock(){ this.locked = true; }
|
||||
public int size()
|
||||
{
|
||||
return this.delegate.size();
|
||||
}
|
||||
|
||||
//@Override public void unfreeze() {}
|
||||
@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(); }
|
||||
|
||||
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>
|
||||
/** @Depreciated Forge: For internal use only. Use the Register events when registering values. */
|
||||
@Deprecated @Override public void lock(){ this.locked = true; }
|
||||
|
||||
|
||||
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>, IForgeRegistry.AddCallback<V>
|
||||
{
|
||||
public static final ResourceLocation ID = new ResourceLocation("forge", "registry_defaulted_wrapper");
|
||||
|
||||
@Override
|
||||
public void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage)
|
||||
{
|
||||
ForgeRegistry<V> fowner = (ForgeRegistry<V>)owner;
|
||||
owner.setSlaveMap(ID, new NamespacedWrapper<V>(fowner, fowner.getBuilder().getVanillaHolder()));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -382,4 +382,9 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
|
||||
{
|
||||
return this.vanillaHolder;
|
||||
}
|
||||
|
||||
boolean getHasWrapper()
|
||||
{
|
||||
return this.hasWrapper;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user