Files
MinecraftForge/src/main/java/net/minecraftforge/registries/NamespacedWrapper.java
T

177 lines
5.0 KiB
Java
Raw Normal View History

/*
* 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
*/
2017-06-19 15:02:18 -07:00
package net.minecraftforge.registries;
import java.util.Collection;
2017-06-19 15:02:18 -07:00
import java.util.Iterator;
import java.util.Map;
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;
2020-06-19 10:58:45 -04:00
2021-07-17 00:08:49 -05:00
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.MappedRegistry;
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
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();
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
public NamespacedWrapper(ForgeRegistry<T> owner)
2017-06-19 15:02:18 -07:00
{
super(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)
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 <R extends T> R register(ResourceKey<T> key, R value, Lifecycle lifecycle)
2017-06-19 15:02:18 -07: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;
if (id.isPresent() && byId(id.getAsInt()) != null)
2020-08-13 00:13:48 -07:00
wanted = id.getAsInt();
return registerMapping(wanted, key, value, lifecycle);
2017-06-19 15:02:18 -07:00
}
// Reading Functions
@Override
@Nullable
public T get(@Nullable ResourceLocation name)
2017-06-19 15:02:18 -07:00
{
return this.delegate.getRaw(name); //get without default
2017-06-19 15:02:18 -07:00
}
@Override
public Optional<T> getOptional(@Nullable ResourceLocation name)
{
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
{
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);
}
@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
public T byId(int id)
2017-06-19 15:02:18 -07:00
{
return this.delegate.getValue(id);
}
@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();
}
@Override
2021-07-17 00:08:49 -05:00
public Set<Map.Entry<ResourceKey<T>, T>> entrySet()
{
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();
return values.stream().skip(random.nextInt(values.size())).findFirst().orElse(null);
2017-06-19 15:02:18 -07:00
}
2020-06-19 10:58:45 -04:00
/*
@Override
public boolean isEmpty()
{
return this.delegate.isEmpty();
}
2020-06-19 10:58:45 -04:00
*/
2017-06-19 15:02:18 -07:00
//internal
@Override
public void lock(){ this.locked = true; }
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 NamespacedWrapper<V>((ForgeRegistry<V>)owner));
}
}
}