2018-07-01 21:17:28 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2020-07-02 17:49:11 +00:00
|
|
|
* Copyright (c) 2016-2020.
|
2018-07-01 21:17:28 +00:00
|
|
|
*
|
|
|
|
* 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 22:02:18 +00:00
|
|
|
package net.minecraftforge.registries;
|
|
|
|
|
2018-01-18 20:46:05 +00:00
|
|
|
import java.util.Collection;
|
2017-06-19 22:02:18 +00:00
|
|
|
import java.util.Iterator;
|
2020-08-26 16:49:34 +00:00
|
|
|
import java.util.Map;
|
2019-05-23 23:02:15 +00:00
|
|
|
import java.util.Optional;
|
2020-08-13 07:13:48 +00:00
|
|
|
import java.util.OptionalInt;
|
2017-06-19 22:02:18 +00:00
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.Validate;
|
|
|
|
|
2020-06-19 14:58:45 +00:00
|
|
|
import net.minecraft.util.RegistryKey;
|
2017-06-19 22:02:18 +00:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2019-05-23 23:02:15 +00:00
|
|
|
import net.minecraft.util.registry.DefaultedRegistry;
|
2018-08-30 01:30:15 +00:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2017-06-19 22:02:18 +00:00
|
|
|
|
2020-06-19 14:58:45 +00:00
|
|
|
import com.mojang.serialization.Lifecycle;
|
|
|
|
|
2019-05-23 23:02:15 +00:00
|
|
|
class NamespacedDefaultedWrapper<T extends IForgeRegistryEntry<T>> extends DefaultedRegistry<T> implements ILockableRegistry
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
2018-08-30 01:30:15 +00:00
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
2017-06-19 22:02:18 +00:00
|
|
|
private boolean locked = false;
|
2019-05-23 23:02:15 +00:00
|
|
|
private ForgeRegistry<T> delegate;
|
2017-06-19 22:02:18 +00:00
|
|
|
|
2019-05-23 23:02:15 +00:00
|
|
|
private NamespacedDefaultedWrapper(ForgeRegistry<T> owner)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
2020-08-26 16:49:34 +00:00
|
|
|
super("empty", owner.getRegistryKey(), Lifecycle.experimental());
|
2017-06-19 22:02:18 +00:00
|
|
|
this.delegate = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-08-13 07:13:48 +00:00
|
|
|
public <V extends T> V register(int id, RegistryKey<T> key, V value, Lifecycle lifecycle)
|
2017-06-19 22:02:18 +00: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)
|
2020-06-19 14:58:45 +00:00
|
|
|
value.setRegistryName(key.func_240901_a_());
|
2017-06-19 22:02:18 +00:00
|
|
|
|
|
|
|
int realId = this.delegate.add(id, value);
|
|
|
|
if (realId != id && id != -1)
|
2018-08-30 01:30:15 +00: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 23:02:15 +00:00
|
|
|
|
|
|
|
return value;
|
2017-06-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-08-13 07:13:48 +00:00
|
|
|
public <V extends T> V register(RegistryKey<T> key, V value, Lifecycle lifecycle)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
2020-08-13 07:13:48 +00:00
|
|
|
return register(-1, key, value, lifecycle);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <V extends T> V func_241874_a(OptionalInt id, RegistryKey<T> key, V value, Lifecycle lifecycle) {
|
|
|
|
int wanted = -1;
|
|
|
|
if (id.isPresent() && getByValue(id.getAsInt()) != null)
|
|
|
|
wanted = id.getAsInt();
|
|
|
|
return register(wanted, key, value, lifecycle);
|
2017-06-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 23:08:53 +00:00
|
|
|
// Reading Functions
|
2017-06-24 08:46:05 +00:00
|
|
|
@Override
|
2020-08-13 07:13:48 +00:00
|
|
|
public Optional<T> func_241873_b(@Nullable ResourceLocation name)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
2019-05-23 23:02:15 +00:00
|
|
|
return Optional.ofNullable( this.delegate.getRaw(name)); //get without default
|
2017-06-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
2019-05-23 23:02:15 +00:00
|
|
|
public T getOrDefault(@Nullable ResourceLocation name)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
2019-02-14 23:08:53 +00:00
|
|
|
return this.delegate.getValue(name); //getOrDefault
|
2017-06-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 06:35:34 +00:00
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public T func_230516_a_(@Nullable RegistryKey<T> name)
|
|
|
|
{
|
|
|
|
return name == null ? null : this.delegate.getRaw(name.func_240901_a_()); //get without default
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
@Override
|
|
|
|
@Nullable
|
2019-05-23 23:02:15 +00:00
|
|
|
public ResourceLocation getKey(T value)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getKey(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 23:02:15 +00:00
|
|
|
public boolean containsKey(ResourceLocation key)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.containsKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 23:02:15 +00:00
|
|
|
public int getId(@Nullable T value)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getID(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
2019-05-23 23:02:15 +00:00
|
|
|
public T getByValue(int id)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getValue(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 23:02:15 +00:00
|
|
|
public Iterator<T> iterator()
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 23:02:15 +00:00
|
|
|
public Set<ResourceLocation> keySet()
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getKeys();
|
|
|
|
}
|
|
|
|
|
2020-08-26 16:49:34 +00:00
|
|
|
@Override
|
|
|
|
public Set<Map.Entry<RegistryKey<T>, T>> func_239659_c_()
|
|
|
|
{
|
|
|
|
return this.delegate.getEntries();
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
@Override
|
|
|
|
@Nullable
|
2019-05-23 23:02:15 +00:00
|
|
|
public T getRandom(Random random)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
2019-05-23 23:02:15 +00:00
|
|
|
Collection<T> values = this.delegate.getValues();
|
2018-11-15 21:52:45 +00:00
|
|
|
return values.stream().skip(random.nextInt(values.size())).findFirst().orElse(this.delegate.getDefault());
|
2017-06-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 23:08:53 +00:00
|
|
|
@Override
|
2019-05-23 23:02:15 +00:00
|
|
|
public ResourceLocation getDefaultKey()
|
2019-02-14 23:08:53 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getDefaultKey();
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
//internal
|
|
|
|
@Override
|
|
|
|
public void lock(){ this.locked = true; }
|
|
|
|
|
2018-09-06 03:06:18 +00:00
|
|
|
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V>
|
2017-06-19 22:02:18 +00: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 NamespacedDefaultedWrapper<V>((ForgeRegistry<V>)owner));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|