Registry API to create modded registries, close #21

This commit is contained in:
shedaniel
2021-01-25 11:09:20 +08:00
parent 5d4a779d05
commit a4beace95c
12 changed files with 249 additions and 30 deletions

View File

@@ -25,5 +25,5 @@ import net.minecraft.world.item.crafting.RecipeSerializer;
/**
* The equivalent of {@link RecipeSerializer} to use in common that has forge registry entries extended.
*/
public abstract class AbstractRecipeSerializer<T extends Recipe<?>> implements RecipeSerializer<T> {
public abstract class AbstractRecipeSerializer<T extends Recipe<?>> extends RegistryEntry<T> implements RecipeSerializer<T> {
}

View File

@@ -0,0 +1,26 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021 shedaniel
*
* 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 me.shedaniel.architectury.core;
/**
* An entry in registries, will extend {@code ForgeRegistryEntry} on forge.
*/
public class RegistryEntry<T> {
}

View File

@@ -20,9 +20,12 @@
package me.shedaniel.architectury.registry;
import me.shedaniel.architectury.annotations.ExpectPlatform;
import me.shedaniel.architectury.core.RegistryEntry;
import me.shedaniel.architectury.registry.registries.RegistryBuilder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
@@ -36,6 +39,7 @@ public final class Registries {
private final RegistryProvider provider;
private final String modId;
@NotNull
public static Registries get(String modId) {
return REGISTRIES.computeIfAbsent(modId, Registries::new);
}
@@ -45,15 +49,24 @@ public final class Registries {
this.modId = modId;
}
@NotNull
public <T> Registry<T> get(ResourceKey<net.minecraft.core.Registry<T>> key) {
return this.provider.get(key);
}
@NotNull
@Deprecated
public <T> Registry<T> get(net.minecraft.core.Registry<T> registry) {
return this.provider.get(registry);
}
@NotNull
@SafeVarargs
public final <T extends RegistryEntry<T>> RegistryBuilder<T> builder(ResourceLocation registryId, T... typeGetter) {
if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!");
return this.provider.builder((Class<T>) typeGetter.getClass().getComponentType(), registryId);
}
/**
* Forge: If the object is {@code IForgeRegistryEntry}, use `getRegistryName`, else null
* Fabric: Use registry
@@ -79,8 +92,8 @@ public final class Registries {
* Forge: If the object is {@code IForgeRegistryEntry}, use `getRegistryName`, else null
* Fabric: null
*/
@Deprecated
@Nullable
@Deprecated
public static <T> ResourceLocation getRegistryName(T object) {
return getId(object, (ResourceKey<net.minecraft.core.Registry<T>>) null);
}
@@ -90,6 +103,7 @@ public final class Registries {
throw new AssertionError();
}
@NotNull
public String getModId() {
return modId;
}
@@ -100,5 +114,7 @@ public final class Registries {
@Deprecated
<T> Registry<T> get(net.minecraft.core.Registry<T> registry);
<T extends RegistryEntry<T>> RegistryBuilder<T> builder(Class<T> type, ResourceLocation registryId);
}
}

View File

@@ -0,0 +1,42 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021 shedaniel
*
* 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 me.shedaniel.architectury.registry.registries;
import me.shedaniel.architectury.core.RegistryEntry;
import me.shedaniel.architectury.registry.Registry;
import org.jetbrains.annotations.NotNull;
public interface RegistryBuilder<T extends RegistryEntry<T>> {
@NotNull
Registry<T> build();
@NotNull
RegistryBuilder<T> option(@NotNull RegistryOption option);
@NotNull
default RegistryBuilder<T> saveToDisc() {
return option(StandardRegistryOption.SAVE_TO_DISC);
}
@NotNull
default RegistryBuilder<T> syncToClients() {
return option(StandardRegistryOption.SYNC_TO_CLIENTS);
}
}

View File

@@ -0,0 +1,23 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021 shedaniel
*
* 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 me.shedaniel.architectury.registry.registries;
public interface RegistryOption {
}

View File

@@ -0,0 +1,31 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021 shedaniel
*
* 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 me.shedaniel.architectury.registry.registries;
public enum StandardRegistryOption implements RegistryOption {
/**
* Denote that the registry should save to disc and persist. Defaulted false.
*/
SAVE_TO_DISC,
/**
* Denote that the registry should sync its contents to clients. Defaulted false.
*/
SYNC_TO_CLIENTS,
}