2018-07-01 21:17:28 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2019-02-10 22:57:03 +00:00
|
|
|
* Copyright (c) 2016-2019.
|
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;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.Validate;
|
|
|
|
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.util.registry.RegistryNamespaced;
|
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
|
|
|
|
2018-09-06 03:06:18 +00:00
|
|
|
class NamespacedWrapper<V extends IForgeRegistryEntry<V>> extends RegistryNamespaced<ResourceLocation, V> 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;
|
|
|
|
private ForgeRegistry<V> delegate;
|
|
|
|
|
|
|
|
public NamespacedWrapper(ForgeRegistry<V> owner)
|
|
|
|
{
|
|
|
|
this.delegate = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void register(int id, ResourceLocation key, V value)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
2017-06-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 10:40:22 +00:00
|
|
|
public void put(ResourceLocation key, V value)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
register(-1, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reading Functions
|
|
|
|
@Override
|
|
|
|
@Nullable
|
2018-09-22 10:40:22 +00:00
|
|
|
public V get(@Nullable ResourceLocation name)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getValue(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
2018-09-22 10:40:22 +00:00
|
|
|
public ResourceLocation getKey(V value)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getKey(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean containsKey(ResourceLocation key)
|
|
|
|
{
|
|
|
|
return this.delegate.containsKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 10:40:22 +00:00
|
|
|
public int getId(@Nullable V value)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getID(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
2018-09-22 10:40:22 +00:00
|
|
|
public V get(int id)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
return this.delegate.getValue(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<V> iterator()
|
|
|
|
{
|
|
|
|
return this.delegate.iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<ResourceLocation> getKeys()
|
|
|
|
{
|
|
|
|
return this.delegate.getKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
2018-09-22 10:40:22 +00:00
|
|
|
public V getRandom(Random random)
|
2017-06-19 22:02:18 +00:00
|
|
|
{
|
2018-09-06 11:39:01 +00:00
|
|
|
Collection<V> values = this.delegate.getValues();
|
2018-01-18 20:46:05 +00:00
|
|
|
return values.stream().skip(random.nextInt(values.size())).findFirst().orElse(null);
|
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 NamespacedWrapper<V>((ForgeRegistry<V>)owner));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|