Files
MinecraftForge/src/main/java/net/minecraftforge/registries/NamespacedDefaultedWrapper.java
T
be5446b91c Update to 1.17
Update to Official classnames
Patchwork
Make the game compile and run
Merge in the new HUD overlay API
Merge in the new render properties API
PEEL
Use Gradle 7.2 snapshot to fix eclipse integration bug in 7.1, Would use 7.0 but FG needs 7.1 for some reason.
New FML installer
Update to 1.17.1

Co-authored-by: LexManos <lexmanos@gmail.com>
Co-authored-by: SizableShrimp <sizableshrimp@sizableshrimp.me>
Co-authored-by: cpw <cpw+github@weeksfamily.ca>
Co-authored-by: Curle <curle@gemwire.uk>
Co-authored-by: Unnoen <Unnoen@users.noreply.github.com>
Co-authored-by: covers1624 <laughlan.cov@internode.on.net>
Co-authored-by: DaemonUmbra <daemon.umbra@gmail.com>
Co-authored-by: gigaherz <gigaherz@gmail.com>
Signed-off-by: SizableShrimp <sizableshrimp@sizableshrimp.me>
2021-07-22 19:57:02 +01:00

178 lines
5.1 KiB
Java

/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* 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
*/
package net.minecraftforge.registries;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Random;
import java.util.Set;
import javax.annotation.Nullable;
import org.apache.commons.lang3.Validate;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.DefaultedRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.mojang.serialization.Lifecycle;
class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends DefaultedRegistry<T> implements ILockableRegistry
{
private static final Logger LOGGER = LogManager.getLogger();
private boolean locked = false;
private ForgeRegistry<T> delegate;
private NamespacedDefaultedWrapper(ForgeRegistry<T> owner)
{
super("empty", owner.getRegistryKey(), Lifecycle.experimental());
this.delegate = owner;
}
@Override
public <V extends T> V registerMapping(int id, ResourceKey<T> key, V value, Lifecycle lifecycle)
{
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)
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);
return value;
}
@Override
public <V extends T> V register(ResourceKey<T> key, V value, Lifecycle lifecycle)
{
return registerMapping(-1, key, value, lifecycle);
}
@Override
public <V extends T> V registerOrOverride(OptionalInt id, ResourceKey<T> key, V value, Lifecycle lifecycle) {
int wanted = -1;
if (id.isPresent() && byId(id.getAsInt()) != null)
wanted = id.getAsInt();
return registerMapping(wanted, key, value, lifecycle);
}
// Reading Functions
@Override
public Optional<T> getOptional(@Nullable ResourceLocation name)
{
return Optional.ofNullable( this.delegate.getRaw(name)); //get without default
}
@Override
@Nullable
public T get(@Nullable ResourceLocation name)
{
return this.delegate.getValue(name); //getOrDefault
}
@Override
@Nullable
public T get(@Nullable ResourceKey<T> name)
{
return name == null ? null : this.delegate.getRaw(name.location()); //get without default
}
@Override
@Nullable
public ResourceLocation getKey(T value)
{
return this.delegate.getKey(value);
}
@Override
public boolean containsKey(ResourceLocation key)
{
return this.delegate.containsKey(key);
}
@Override
public int getId(@Nullable T value)
{
return this.delegate.getID(value);
}
@Override
@Nullable
public T byId(int id)
{
return this.delegate.getValue(id);
}
@Override
public Iterator<T> iterator()
{
return this.delegate.iterator();
}
@Override
public Set<ResourceLocation> keySet()
{
return this.delegate.getKeys();
}
@Override
public Set<Map.Entry<ResourceKey<T>, T>> entrySet()
{
return this.delegate.getEntries();
}
@Override
@Nullable
public T getRandom(Random random)
{
Collection<T> values = this.delegate.getValues();
return values.stream().skip(random.nextInt(values.size())).findFirst().orElse(this.delegate.getDefault());
}
@Override
public ResourceLocation getDefaultKey()
{
return this.delegate.getDefaultKey();
}
//internal
@Override
public void lock(){ this.locked = true; }
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>
{
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));
}
}
}