mirror of
https://github.com/architectury/architectury-api.git
synced 2026-03-28 03:56:59 -05:00
[ci skip] Add defaultId to RegistrarBuilder (#474)
* Add defaultId to RegistrarBuilder * Make the default key an registrar option --------- Co-authored-by: shedaniel <daniel@shedaniel.me>
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of architectury.
|
||||||
|
* Copyright (C) 2020, 2021, 2022 architectury
|
||||||
|
*
|
||||||
|
* This program 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; either
|
||||||
|
* version 3 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 program; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.architectury.registry.registries.options;
|
||||||
|
|
||||||
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
|
||||||
|
public record DefaultIdRegistrarOption(ResourceLocation defaultId) implements RegistrarOption {
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ import dev.architectury.registry.registries.Registrar;
|
|||||||
import dev.architectury.registry.registries.RegistrarBuilder;
|
import dev.architectury.registry.registries.RegistrarBuilder;
|
||||||
import dev.architectury.registry.registries.Registries;
|
import dev.architectury.registry.registries.Registries;
|
||||||
import dev.architectury.registry.registries.RegistrySupplier;
|
import dev.architectury.registry.registries.RegistrySupplier;
|
||||||
|
import dev.architectury.registry.registries.options.DefaultIdRegistrarOption;
|
||||||
import dev.architectury.registry.registries.options.RegistrarOption;
|
import dev.architectury.registry.registries.options.RegistrarOption;
|
||||||
import dev.architectury.registry.registries.options.StandardRegistrarOption;
|
import dev.architectury.registry.registries.options.StandardRegistrarOption;
|
||||||
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
|
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
|
||||||
@@ -91,7 +92,7 @@ public class RegistriesImpl {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> RegistrarBuilder<T> builder(Class<T> type, ResourceLocation registryId) {
|
public <T> RegistrarBuilder<T> builder(Class<T> type, ResourceLocation registryId) {
|
||||||
return new RegistrarBuilderWrapper<>(modId, FabricRegistryBuilder.createSimple(type, registryId));
|
return new RegistrarBuilderWrapper<>(modId, type, registryId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,24 +121,35 @@ public class RegistriesImpl {
|
|||||||
|
|
||||||
public static class RegistrarBuilderWrapper<T> implements RegistrarBuilder<T> {
|
public static class RegistrarBuilderWrapper<T> implements RegistrarBuilder<T> {
|
||||||
private final String modId;
|
private final String modId;
|
||||||
private FabricRegistryBuilder<T, MappedRegistry<T>> builder;
|
private final Class<T> type;
|
||||||
|
private final ResourceLocation registryId;
|
||||||
|
private final List<Consumer<FabricRegistryBuilder<T, ? extends MappedRegistry<T>>>> apply = new ArrayList<>();
|
||||||
|
@Nullable
|
||||||
|
private ResourceLocation defaultId;
|
||||||
|
|
||||||
public RegistrarBuilderWrapper(String modId, FabricRegistryBuilder<T, MappedRegistry<T>> builder) {
|
public RegistrarBuilderWrapper(String modId, Class<T> type, ResourceLocation registryId) {
|
||||||
this.modId = modId;
|
this.modId = modId;
|
||||||
this.builder = builder;
|
this.type = type;
|
||||||
|
this.registryId = registryId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Registrar<T> build() {
|
public Registrar<T> build() {
|
||||||
|
final var builder = defaultId == null
|
||||||
|
? FabricRegistryBuilder.createSimple(type, registryId)
|
||||||
|
: FabricRegistryBuilder.createDefaulted(type, registryId, defaultId);
|
||||||
|
apply.forEach(consumer -> consumer.accept(builder));
|
||||||
return Registries.get(modId).get(builder.buildAndRegister());
|
return Registries.get(modId).get(builder.buildAndRegister());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RegistrarBuilder<T> option(RegistrarOption option) {
|
public RegistrarBuilder<T> option(RegistrarOption option) {
|
||||||
if (option == StandardRegistrarOption.SAVE_TO_DISC) {
|
if (option == StandardRegistrarOption.SAVE_TO_DISC) {
|
||||||
this.builder.attribute(RegistryAttribute.PERSISTED);
|
this.apply.add(builder -> builder.attribute(RegistryAttribute.PERSISTED));
|
||||||
} else if (option == StandardRegistrarOption.SYNC_TO_CLIENTS) {
|
} else if (option == StandardRegistrarOption.SYNC_TO_CLIENTS) {
|
||||||
this.builder.attribute(RegistryAttribute.SYNCED);
|
this.apply.add(builder -> builder.attribute(RegistryAttribute.SYNCED));
|
||||||
|
} else if (option instanceof DefaultIdRegistrarOption opt) {
|
||||||
|
this.defaultId = opt.defaultId();
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import dev.architectury.registry.registries.Registrar;
|
|||||||
import dev.architectury.registry.registries.RegistrarBuilder;
|
import dev.architectury.registry.registries.RegistrarBuilder;
|
||||||
import dev.architectury.registry.registries.Registries;
|
import dev.architectury.registry.registries.Registries;
|
||||||
import dev.architectury.registry.registries.RegistrySupplier;
|
import dev.architectury.registry.registries.RegistrySupplier;
|
||||||
|
import dev.architectury.registry.registries.options.DefaultIdRegistrarOption;
|
||||||
import dev.architectury.registry.registries.options.RegistrarOption;
|
import dev.architectury.registry.registries.options.RegistrarOption;
|
||||||
import dev.architectury.registry.registries.options.StandardRegistrarOption;
|
import dev.architectury.registry.registries.options.StandardRegistrarOption;
|
||||||
import net.minecraft.core.Registry;
|
import net.minecraft.core.Registry;
|
||||||
@@ -306,6 +307,8 @@ public class RegistriesImpl {
|
|||||||
this.saveToDisk = true;
|
this.saveToDisk = true;
|
||||||
} else if (option == StandardRegistrarOption.SYNC_TO_CLIENTS) {
|
} else if (option == StandardRegistrarOption.SYNC_TO_CLIENTS) {
|
||||||
this.syncToClients = true;
|
this.syncToClients = true;
|
||||||
|
} else if (option instanceof DefaultIdRegistrarOption opt) {
|
||||||
|
this.builder.setDefaultKey(opt.defaultId());
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user