/* * Minecraft Forge * Copyright (c) 2016. * * 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.List; import com.google.common.collect.Lists; import net.minecraft.util.ResourceLocation; import net.minecraftforge.registries.IForgeRegistry.*; import javax.annotation.Nullable; public class RegistryBuilder> { private ResourceLocation registryName; private Class registryType; private ResourceLocation optionalDefaultKey; private int minId = 0; private int maxId = Integer.MAX_VALUE - 1; private List> addCallback = Lists.newArrayList(); private List> clearCallback = Lists.newArrayList(); private List> createCallback = Lists.newArrayList(); private boolean saveToDisc = true; private boolean allowOverrides = true; private boolean allowModifications = false; private DummyFactory dummyFactory; public RegistryBuilder setName(ResourceLocation name) { this.registryName = name; return this; } public RegistryBuilder setType(Class type) { this.registryType = type; return this; } public RegistryBuilder setIDRange(int min, int max) { this.minId = min; this.maxId = max; return this; } public RegistryBuilder setMaxID(int max) { return this.setIDRange(0, max); } public RegistryBuilder setDefaultKey(ResourceLocation key) { this.optionalDefaultKey = key; return this; } @SuppressWarnings("unchecked") public RegistryBuilder addCallback(Object inst) { if (inst instanceof AddCallback) this.add((AddCallback)inst); if (inst instanceof ClearCallback) this.add((ClearCallback)inst); if (inst instanceof CreateCallback) this.add((CreateCallback)inst); if (inst instanceof DummyFactory) this.set((DummyFactory)inst); return this; } public RegistryBuilder add(AddCallback add) { this.addCallback.add(add); return this; } public RegistryBuilder add(ClearCallback clear) { this.clearCallback.add(clear); return this; } public RegistryBuilder add(CreateCallback create) { this.createCallback.add(create); return this; } public RegistryBuilder set(DummyFactory factory) { this.dummyFactory = factory; return this; } public RegistryBuilder disableSaving() { this.saveToDisc = false; return this; } public RegistryBuilder disableOverrides() { this.allowOverrides = false; return this; } public RegistryBuilder allowModification() { this.allowModifications = true; return this; } public IForgeRegistry create() { return RegistryManager.ACTIVE.createRegistry(registryName, registryType, optionalDefaultKey, minId, maxId, getAdd(), getClear(), getCreate(), saveToDisc, allowOverrides, allowModifications, dummyFactory); } @Nullable private AddCallback getAdd() { if (addCallback.isEmpty()) return null; if (addCallback.size() == 1) return addCallback.get(0); return (owner, stage, id, obj, old) -> { for (AddCallback cb : this.addCallback) cb.onAdd(owner, stage, id, obj, old); }; } @Nullable private ClearCallback getClear() { if (clearCallback.isEmpty()) return null; if (clearCallback.size() == 1) return clearCallback.get(0); return (owner, stage) -> { for (ClearCallback cb : this.clearCallback) cb.onClear(owner, stage); }; } @Nullable private CreateCallback getCreate() { if (createCallback.isEmpty()) return null; if (createCallback.size() == 1) return createCallback.get(0); return (owner, stage) -> { for (CreateCallback cb : this.createCallback) cb.onCreate(owner, stage); }; } }