Hide the RS API behind a singleton instance

This commit is contained in:
Raoul Van den Berge
2016-09-10 14:29:21 +02:00
parent dfb94f0580
commit 408c818d04
8 changed files with 85 additions and 46 deletions

View File

@@ -0,0 +1,10 @@
package refinedstorage.api;
import refinedstorage.api.autocrafting.registry.ICraftingTaskRegistry;
import refinedstorage.api.solderer.ISoldererRegistry;
public interface IAPI {
ISoldererRegistry getSoldererRegistry();
ICraftingTaskRegistry getCraftingTaskRegistry();
}

View File

@@ -1,16 +1,25 @@
package refinedstorage.api;
import refinedstorage.api.autocrafting.registry.ICraftingTaskRegistry;
import refinedstorage.api.solderer.ISoldererRegistry;
import java.lang.reflect.Field;
public final class RefinedStorageAPI {
/**
* The solderer registry, set in pre-initialization
*/
public static ISoldererRegistry SOLDERER_REGISTRY;
private static final String API_IMPL_CLASS = "refinedstorage.apiimpl.API";
private static final String API_IMPL_FIELD = "INSTANCE";
/**
* The crafting task registry, set in pre-initialization
*/
public static ICraftingTaskRegistry CRAFTING_TASK_REGISTRY;
private static final IAPI API;
static {
try {
Class<?> apiClass = Class.forName(API_IMPL_CLASS);
Field apiField = apiClass.getField(API_IMPL_FIELD);
API = (IAPI) apiField.get(apiClass);
} catch (Exception e) {
throw new Error("The Refined Storage API implementation is unavailable, make sure Refined Storage is installed");
}
}
public static IAPI instance() {
return API;
}
}

View File

@@ -28,7 +28,7 @@ public final class NetworkUtils {
}
public static ICraftingTask createCraftingTask(INetworkMaster network, ICraftingPattern pattern) {
return RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(pattern.getId()).create(network.getNetworkWorld(), null, pattern);
return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), null, pattern);
}
public static boolean hasPattern(INetworkMaster network, ItemStack stack) {

View File

@@ -0,0 +1,24 @@
package refinedstorage.apiimpl;
import refinedstorage.api.IAPI;
import refinedstorage.api.autocrafting.registry.ICraftingTaskRegistry;
import refinedstorage.api.solderer.ISoldererRegistry;
import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskRegistry;
import refinedstorage.apiimpl.solderer.SoldererRegistry;
public class API implements IAPI {
public static final IAPI INSTANCE = new API();
private ISoldererRegistry soldererRegistry = new SoldererRegistry();
private ICraftingTaskRegistry craftingTaskRegistry = new CraftingTaskRegistry();
@Override
public ISoldererRegistry getSoldererRegistry() {
return soldererRegistry;
}
@Override
public ICraftingTaskRegistry getCraftingTaskRegistry() {
return craftingTaskRegistry;
}
}

View File

@@ -11,7 +11,7 @@ public final class RecipeMakerSolderer {
public static List<RecipeWrapperSolderer> getRecipes() {
List<RecipeWrapperSolderer> recipes = new ArrayList<>();
for (ISoldererRecipe recipe : RefinedStorageAPI.SOLDERER_REGISTRY.getRecipes()) {
for (ISoldererRecipe recipe : RefinedStorageAPI.instance().getSoldererRegistry().getRecipes()) {
List<ItemStack> inputs = new ArrayList<>();
inputs.add(recipe.getRow(0));

View File

@@ -20,7 +20,6 @@ import refinedstorage.RefinedStorageItems;
import refinedstorage.api.RefinedStorageAPI;
import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskFactoryNormal;
import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskFactoryProcessing;
import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskRegistry;
import refinedstorage.apiimpl.solderer.*;
import refinedstorage.apiimpl.storage.fluid.FluidStorageNBT;
import refinedstorage.apiimpl.storage.item.ItemStorageNBT;
@@ -46,11 +45,8 @@ public class CommonProxy {
IntegrationCraftingTweaks.register();
}
RefinedStorageAPI.SOLDERER_REGISTRY = new SoldererRegistry();
RefinedStorageAPI.CRAFTING_TASK_REGISTRY = new CraftingTaskRegistry();
RefinedStorageAPI.CRAFTING_TASK_REGISTRY.addFactory(CraftingTaskFactoryNormal.ID, new CraftingTaskFactoryNormal());
RefinedStorageAPI.CRAFTING_TASK_REGISTRY.addFactory(CraftingTaskFactoryProcessing.ID, new CraftingTaskFactoryProcessing());
RefinedStorageAPI.instance().getCraftingTaskRegistry().addFactory(CraftingTaskFactoryNormal.ID, new CraftingTaskFactoryNormal());
RefinedStorageAPI.instance().getCraftingTaskRegistry().addFactory(CraftingTaskFactoryProcessing.ID, new CraftingTaskFactoryProcessing());
int id = 0;
@@ -142,14 +138,14 @@ public class CommonProxy {
OreDictionary.registerOre("itemSilicon", RefinedStorageItems.SILICON);
// Processors
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_BASIC));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_IMPROVED));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_ADVANCED));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_SILICON));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_BASIC));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_IMPROVED));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_ADVANCED));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipePrintedProcessor(ItemProcessor.TYPE_PRINTED_SILICON));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeProcessor(ItemProcessor.TYPE_BASIC));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeProcessor(ItemProcessor.TYPE_IMPROVED));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeProcessor(ItemProcessor.TYPE_ADVANCED));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeProcessor(ItemProcessor.TYPE_BASIC));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeProcessor(ItemProcessor.TYPE_IMPROVED));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeProcessor(ItemProcessor.TYPE_ADVANCED));
// Silicon
GameRegistry.addSmelting(Items.QUARTZ, new ItemStack(RefinedStorageItems.SILICON), 0.5f);
@@ -210,7 +206,7 @@ public class CommonProxy {
);
// Disk Drive
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeBasic(
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeBasic(
new ItemStack(RefinedStorageBlocks.DISK_DRIVE),
500,
new ItemStack(RefinedStorageItems.PROCESSOR, 1, ItemProcessor.TYPE_ADVANCED),
@@ -252,7 +248,7 @@ public class CommonProxy {
);
// Crafting Grid
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeBasic(
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeBasic(
new ItemStack(RefinedStorageBlocks.GRID, 1, EnumGridType.CRAFTING.getId()),
500,
new ItemStack(RefinedStorageItems.PROCESSOR, 1, ItemProcessor.TYPE_ADVANCED),
@@ -261,7 +257,7 @@ public class CommonProxy {
));
// Pattern Grid
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeBasic(
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeBasic(
new ItemStack(RefinedStorageBlocks.GRID, 1, EnumGridType.PATTERN.getId()),
500,
new ItemStack(RefinedStorageItems.PROCESSOR, 1, ItemProcessor.TYPE_ADVANCED),
@@ -270,7 +266,7 @@ public class CommonProxy {
));
// Fluid Grid
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeBasic(
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeBasic(
new ItemStack(RefinedStorageBlocks.GRID, 1, EnumGridType.FLUID.getId()),
500,
new ItemStack(RefinedStorageItems.PROCESSOR, 1, ItemProcessor.TYPE_ADVANCED),
@@ -527,10 +523,10 @@ public class CommonProxy {
'E', new ItemStack(RefinedStorageItems.QUARTZ_ENRICHED_IRON)
));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_RANGE));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_SPEED));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_CRAFTING));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_INTERDIMENSIONAL));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_RANGE));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_SPEED));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_INTERDIMENSIONAL));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeUpgrade(ItemUpgrade.TYPE_CRAFTING));
GameRegistry.addShapedRecipe(new ItemStack(RefinedStorageItems.UPGRADE, 1, ItemUpgrade.TYPE_STACK),
"USU",
@@ -541,16 +537,16 @@ public class CommonProxy {
);
// Storage Blocks
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_1K, ItemStoragePart.TYPE_1K));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_4K, ItemStoragePart.TYPE_4K));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_16K, ItemStoragePart.TYPE_16K));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_64K, ItemStoragePart.TYPE_64K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_1K, ItemStoragePart.TYPE_1K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_4K, ItemStoragePart.TYPE_4K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_16K, ItemStoragePart.TYPE_16K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeStorage(EnumItemStorageType.TYPE_64K, ItemStoragePart.TYPE_64K));
// Fluid Storage Blocks
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_64K, ItemFluidStoragePart.TYPE_64K));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_128K, ItemFluidStoragePart.TYPE_128K));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_256K, ItemFluidStoragePart.TYPE_256K));
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_512K, ItemFluidStoragePart.TYPE_512K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_64K, ItemFluidStoragePart.TYPE_64K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_128K, ItemFluidStoragePart.TYPE_128K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_256K, ItemFluidStoragePart.TYPE_256K));
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeFluidStorage(EnumFluidStorageType.TYPE_512K, ItemFluidStoragePart.TYPE_512K));
// Crafting Monitor
GameRegistry.addRecipe(new ShapedOreRecipe(new ItemStack(RefinedStorageBlocks.CRAFTING_MONITOR),
@@ -564,7 +560,7 @@ public class CommonProxy {
));
// Interface
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeBasic(
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeBasic(
new ItemStack(RefinedStorageBlocks.INTERFACE),
200,
new ItemStack(RefinedStorageBlocks.IMPORTER),
@@ -573,7 +569,7 @@ public class CommonProxy {
));
// Fluid Interface
RefinedStorageAPI.SOLDERER_REGISTRY.addRecipe(new SoldererRecipeBasic(
RefinedStorageAPI.instance().getSoldererRegistry().addRecipe(new SoldererRecipeBasic(
new ItemStack(RefinedStorageBlocks.FLUID_INTERFACE),
200,
new ItemStack(Items.BUCKET),

View File

@@ -729,7 +729,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
if (container instanceof ICraftingPatternContainer) {
ICraftingPattern pattern = ((ICraftingPatternProvider) stack.getItem()).create(world, stack, (ICraftingPatternContainer) container);
ICraftingTaskFactory factory = RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(tag.getString(NBT_CRAFTING_TASK_TYPE));
ICraftingTaskFactory factory = RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(tag.getString(NBT_CRAFTING_TASK_TYPE));
if (factory != null) {
return factory.create(world, tag, pattern);

View File

@@ -63,7 +63,7 @@ public class TileSolderer extends TileNode {
if (items.getStackInSlot(1) == null && items.getStackInSlot(2) == null && items.getStackInSlot(3) == null) {
stop();
} else {
ISoldererRecipe newRecipe = RefinedStorageAPI.SOLDERER_REGISTRY.getRecipe(items);
ISoldererRecipe newRecipe = RefinedStorageAPI.instance().getSoldererRegistry().getRecipe(items);
if (newRecipe == null) {
stop();
@@ -131,7 +131,7 @@ public class TileSolderer extends TileNode {
readItems(items, 0, tag);
readItems(upgrades, 1, tag);
recipe = RefinedStorageAPI.SOLDERER_REGISTRY.getRecipe(items);
recipe = RefinedStorageAPI.instance().getSoldererRegistry().getRecipe(items);
if (tag.hasKey(NBT_WORKING)) {
working = tag.getBoolean(NBT_WORKING);