2016-08-02 22:55:18 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2019-02-10 22:57:03 +00:00
|
|
|
* Copyright (c) 2016-2019.
|
2016-08-02 22:55:18 +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;
|
2016-08-02 22:55:18 +00:00
|
|
|
|
2019-06-09 07:23:00 +00:00
|
|
|
import java.util.HashSet;
|
2016-08-02 22:55:18 +00:00
|
|
|
import java.util.List;
|
2019-06-09 07:23:00 +00:00
|
|
|
import java.util.Set;
|
2016-08-02 22:55:18 +00:00
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
2017-06-19 22:02:18 +00:00
|
|
|
import net.minecraftforge.registries.IForgeRegistry.*;
|
2016-08-02 22:55:18 +00:00
|
|
|
|
2017-01-11 23:17:56 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2018-09-06 03:06:18 +00:00
|
|
|
public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2018-09-16 20:59:08 +00:00
|
|
|
private static final int MAX_ID = Integer.MAX_VALUE - 1;
|
|
|
|
|
2016-08-02 22:55:18 +00:00
|
|
|
private ResourceLocation registryName;
|
|
|
|
private Class<T> registryType;
|
|
|
|
private ResourceLocation optionalDefaultKey;
|
2017-06-19 22:02:18 +00:00
|
|
|
private int minId = 0;
|
2018-09-16 20:59:08 +00:00
|
|
|
private int maxId = MAX_ID;
|
2016-08-02 22:55:18 +00:00
|
|
|
private List<AddCallback<T>> addCallback = Lists.newArrayList();
|
|
|
|
private List<ClearCallback<T>> clearCallback = Lists.newArrayList();
|
|
|
|
private List<CreateCallback<T>> createCallback = Lists.newArrayList();
|
2018-06-27 05:00:53 +00:00
|
|
|
private List<ValidateCallback<T>> validateCallback = Lists.newArrayList();
|
2019-01-11 22:52:30 +00:00
|
|
|
private List<BakeCallback<T>> bakeCallback = Lists.newArrayList();
|
2017-06-15 20:00:46 +00:00
|
|
|
private boolean saveToDisc = true;
|
2019-06-09 04:55:42 +00:00
|
|
|
private boolean sync = true;
|
2017-06-19 22:02:18 +00:00
|
|
|
private boolean allowOverrides = true;
|
|
|
|
private boolean allowModifications = false;
|
|
|
|
private DummyFactory<T> dummyFactory;
|
2017-08-15 12:10:07 +00:00
|
|
|
private MissingFactory<T> missingFactory;
|
2019-06-09 07:23:00 +00:00
|
|
|
private Set<ResourceLocation> legacyNames = new HashSet<>();
|
2016-08-02 22:55:18 +00:00
|
|
|
|
|
|
|
public RegistryBuilder<T> setName(ResourceLocation name)
|
|
|
|
{
|
|
|
|
this.registryName = name;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RegistryBuilder<T> setType(Class<T> type)
|
|
|
|
{
|
|
|
|
this.registryType = type;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RegistryBuilder<T> setIDRange(int min, int max)
|
|
|
|
{
|
2018-09-16 20:59:08 +00:00
|
|
|
this.minId = Math.max(min, 0);
|
|
|
|
this.maxId = Math.min(max, MAX_ID);
|
2016-08-02 22:55:18 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
public RegistryBuilder<T> setMaxID(int max)
|
|
|
|
{
|
|
|
|
return this.setIDRange(0, max);
|
|
|
|
}
|
|
|
|
|
2017-03-23 18:23:54 +00:00
|
|
|
public RegistryBuilder<T> setDefaultKey(ResourceLocation key)
|
|
|
|
{
|
|
|
|
this.optionalDefaultKey = key;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-08-02 22:55:18 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public RegistryBuilder<T> addCallback(Object inst)
|
|
|
|
{
|
|
|
|
if (inst instanceof AddCallback)
|
|
|
|
this.add((AddCallback<T>)inst);
|
|
|
|
if (inst instanceof ClearCallback)
|
|
|
|
this.add((ClearCallback<T>)inst);
|
|
|
|
if (inst instanceof CreateCallback)
|
|
|
|
this.add((CreateCallback<T>)inst);
|
2018-06-27 05:00:53 +00:00
|
|
|
if (inst instanceof ValidateCallback)
|
|
|
|
this.add((ValidateCallback<T>)inst);
|
2019-01-11 22:52:30 +00:00
|
|
|
if (inst instanceof BakeCallback)
|
|
|
|
this.add((BakeCallback<T>)inst);
|
2017-06-19 22:02:18 +00:00
|
|
|
if (inst instanceof DummyFactory)
|
|
|
|
this.set((DummyFactory<T>)inst);
|
2017-08-15 12:10:07 +00:00
|
|
|
if (inst instanceof MissingFactory)
|
|
|
|
this.set((MissingFactory<T>)inst);
|
2016-08-02 22:55:18 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RegistryBuilder<T> add(AddCallback<T> add)
|
|
|
|
{
|
|
|
|
this.addCallback.add(add);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RegistryBuilder<T> add(ClearCallback<T> clear)
|
|
|
|
{
|
|
|
|
this.clearCallback.add(clear);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RegistryBuilder<T> add(CreateCallback<T> create)
|
|
|
|
{
|
|
|
|
this.createCallback.add(create);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-06-27 05:00:53 +00:00
|
|
|
public RegistryBuilder<T> add(ValidateCallback<T> validate)
|
|
|
|
{
|
|
|
|
this.validateCallback.add(validate);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-01-11 22:52:30 +00:00
|
|
|
public RegistryBuilder<T> add(BakeCallback<T> bake)
|
|
|
|
{
|
|
|
|
this.bakeCallback.add(bake);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
public RegistryBuilder<T> set(DummyFactory<T> factory)
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
this.dummyFactory = factory;
|
2016-08-02 22:55:18 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-08-15 12:10:07 +00:00
|
|
|
public RegistryBuilder<T> set(MissingFactory<T> missing)
|
|
|
|
{
|
|
|
|
this.missingFactory = missing;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
public RegistryBuilder<T> disableSaving()
|
2017-06-14 17:14:56 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
this.saveToDisc = false;
|
2017-06-14 17:14:56 +00:00
|
|
|
return this;
|
|
|
|
}
|
2019-06-09 04:55:42 +00:00
|
|
|
|
|
|
|
public RegistryBuilder<T> disableSync()
|
|
|
|
{
|
|
|
|
this.sync = false;
|
|
|
|
return this;
|
|
|
|
}
|
2017-06-14 17:14:56 +00:00
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
public RegistryBuilder<T> disableOverrides()
|
2017-06-14 17:14:56 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
this.allowOverrides = false;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RegistryBuilder<T> allowModification()
|
|
|
|
{
|
|
|
|
this.allowModifications = true;
|
2017-06-14 17:14:56 +00:00
|
|
|
return this;
|
|
|
|
}
|
2019-06-09 07:23:00 +00:00
|
|
|
|
|
|
|
public RegistryBuilder<T> legacyName(String name)
|
|
|
|
{
|
|
|
|
return legacyName(new ResourceLocation(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
public RegistryBuilder<T> legacyName(ResourceLocation name)
|
|
|
|
{
|
|
|
|
this.legacyNames.add(name);
|
|
|
|
return this;
|
|
|
|
}
|
2017-06-14 17:14:56 +00:00
|
|
|
|
2016-08-02 22:55:18 +00:00
|
|
|
public IForgeRegistry<T> create()
|
|
|
|
{
|
2019-01-11 22:52:30 +00:00
|
|
|
return RegistryManager.ACTIVE.createRegistry(registryName, this);
|
2016-08-02 22:55:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:17:56 +00:00
|
|
|
@Nullable
|
2019-01-11 22:52:30 +00:00
|
|
|
public AddCallback<T> getAdd()
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
if (addCallback.isEmpty())
|
2016-08-02 22:55:18 +00:00
|
|
|
return null;
|
2017-06-19 22:02:18 +00:00
|
|
|
if (addCallback.size() == 1)
|
|
|
|
return addCallback.get(0);
|
2016-08-02 22:55:18 +00:00
|
|
|
|
2017-06-26 03:45:48 +00:00
|
|
|
return (owner, stage, id, obj, old) ->
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
for (AddCallback<T> cb : this.addCallback)
|
2017-06-26 03:45:48 +00:00
|
|
|
cb.onAdd(owner, stage, id, obj, old);
|
2016-08-02 22:55:18 +00:00
|
|
|
};
|
|
|
|
}
|
2017-01-11 23:17:56 +00:00
|
|
|
|
|
|
|
@Nullable
|
2019-01-11 22:52:30 +00:00
|
|
|
public ClearCallback<T> getClear()
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
if (clearCallback.isEmpty())
|
2016-08-02 22:55:18 +00:00
|
|
|
return null;
|
2017-06-19 22:02:18 +00:00
|
|
|
if (clearCallback.size() == 1)
|
|
|
|
return clearCallback.get(0);
|
2016-08-02 22:55:18 +00:00
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
return (owner, stage) ->
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
for (ClearCallback<T> cb : this.clearCallback)
|
|
|
|
cb.onClear(owner, stage);
|
2016-08-02 22:55:18 +00:00
|
|
|
};
|
|
|
|
}
|
2017-01-11 23:17:56 +00:00
|
|
|
|
|
|
|
@Nullable
|
2019-01-11 22:52:30 +00:00
|
|
|
public CreateCallback<T> getCreate()
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
if (createCallback.isEmpty())
|
2016-08-02 22:55:18 +00:00
|
|
|
return null;
|
2017-06-19 22:02:18 +00:00
|
|
|
if (createCallback.size() == 1)
|
|
|
|
return createCallback.get(0);
|
2016-08-02 22:55:18 +00:00
|
|
|
|
2017-06-19 22:02:18 +00:00
|
|
|
return (owner, stage) ->
|
2016-08-02 22:55:18 +00:00
|
|
|
{
|
2017-06-19 22:02:18 +00:00
|
|
|
for (CreateCallback<T> cb : this.createCallback)
|
|
|
|
cb.onCreate(owner, stage);
|
2016-08-02 22:55:18 +00:00
|
|
|
};
|
|
|
|
}
|
2018-06-27 05:00:53 +00:00
|
|
|
|
|
|
|
@Nullable
|
2019-01-11 22:52:30 +00:00
|
|
|
public ValidateCallback<T> getValidate()
|
2018-06-27 05:00:53 +00:00
|
|
|
{
|
|
|
|
if (validateCallback.isEmpty())
|
|
|
|
return null;
|
|
|
|
if (validateCallback.size() == 1)
|
|
|
|
return validateCallback.get(0);
|
|
|
|
|
|
|
|
return (owner, stage, id, key, obj) ->
|
|
|
|
{
|
|
|
|
for (ValidateCallback<T> cb : this.validateCallback)
|
|
|
|
cb.onValidate(owner, stage, id, key, obj);
|
|
|
|
};
|
|
|
|
}
|
2019-01-11 22:52:30 +00:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public BakeCallback<T> getBake()
|
|
|
|
{
|
|
|
|
if (bakeCallback.isEmpty())
|
|
|
|
return null;
|
|
|
|
if (bakeCallback.size() == 1)
|
|
|
|
return bakeCallback.get(0);
|
|
|
|
|
|
|
|
return (owner, stage) ->
|
|
|
|
{
|
|
|
|
for (BakeCallback<T> cb : this.bakeCallback)
|
|
|
|
cb.onBake(owner, stage);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public Class<T> getType()
|
|
|
|
{
|
|
|
|
return registryType;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public ResourceLocation getDefault()
|
|
|
|
{
|
|
|
|
return this.optionalDefaultKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMinId()
|
|
|
|
{
|
|
|
|
return minId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxId()
|
|
|
|
{
|
|
|
|
return maxId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getAllowOverrides()
|
|
|
|
{
|
|
|
|
return allowOverrides;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getAllowModifications()
|
|
|
|
{
|
|
|
|
return allowModifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public DummyFactory<T> getDummyFactory()
|
|
|
|
{
|
|
|
|
return dummyFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public MissingFactory<T> getMissingFactory()
|
|
|
|
{
|
|
|
|
return missingFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getSaveToDisc()
|
|
|
|
{
|
|
|
|
return saveToDisc;
|
|
|
|
}
|
2019-06-09 04:55:42 +00:00
|
|
|
|
|
|
|
public boolean getSync()
|
|
|
|
{
|
|
|
|
return sync;
|
|
|
|
}
|
2019-06-09 07:23:00 +00:00
|
|
|
|
|
|
|
public Set<ResourceLocation> getLegacyNames()
|
|
|
|
{
|
|
|
|
return legacyNames;
|
|
|
|
}
|
2016-08-02 22:55:18 +00:00
|
|
|
}
|