mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-19 19:25:48 +00:00
Refactored recipe service for electric furnaces
This commit is contained in:
parent
7515aaaa62
commit
70738a45ee
@ -1,10 +1,13 @@
|
||||
package io.github.thebusybiscuit.slimefun4.core.services;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.inventory.FurnaceRecipe;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
@ -29,6 +32,8 @@ import io.github.thebusybiscuit.slimefun4.implementation.guide.ChestSlimefunGuid
|
||||
public class MinecraftRecipeService {
|
||||
|
||||
private final Plugin plugin;
|
||||
private final List<Consumer<RecipeSnapshot>> subscriptions = new LinkedList<>();
|
||||
|
||||
private RecipeSnapshot snapshot;
|
||||
|
||||
/**
|
||||
@ -49,6 +54,23 @@ public class MinecraftRecipeService {
|
||||
*/
|
||||
public void refresh() {
|
||||
snapshot = new RecipeSnapshot(plugin);
|
||||
|
||||
for (Consumer<RecipeSnapshot> subscriber : subscriptions) {
|
||||
subscriber.accept(snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method subscribes to the underlying {@link RecipeSnapshot}.
|
||||
* When the {@link Server} has finished loading and a {@link Collection} of all
|
||||
* {@link Recipe Recipes} is created, the given callback will be run.
|
||||
*
|
||||
* @param subscription
|
||||
* A callback to run when the {@link RecipeSnapshot} has been created.
|
||||
*/
|
||||
public void subscribe(Consumer<RecipeSnapshot> subscription) {
|
||||
Validate.notNull(subscription, "Callback must not be null!");
|
||||
subscriptions.add(subscription);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,7 +83,7 @@ public class MinecraftRecipeService {
|
||||
* @return An {@link Optional} describing the furnace output of the given {@link ItemStack}
|
||||
*/
|
||||
public Optional<ItemStack> getFurnaceOutput(ItemStack input) {
|
||||
if (input == null) {
|
||||
if (snapshot == null || input == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@ -114,7 +136,7 @@ public class MinecraftRecipeService {
|
||||
* @return An array of {@link Recipe Recipes} to craft the given {@link ItemStack}
|
||||
*/
|
||||
public Recipe[] getRecipesFor(ItemStack item) {
|
||||
if (item == null) {
|
||||
if (snapshot == null || item == null) {
|
||||
return new Recipe[0];
|
||||
}
|
||||
else {
|
||||
|
@ -1,15 +1,12 @@
|
||||
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.FurnaceRecipe;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
import org.bukkit.inventory.RecipeChoice;
|
||||
import org.bukkit.inventory.RecipeChoice.MaterialChoice;
|
||||
|
||||
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
|
||||
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
|
||||
import me.mrCookieSlime.Slimefun.Objects.Category;
|
||||
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
|
||||
@ -23,20 +20,17 @@ public abstract class ElectricFurnace extends AContainer {
|
||||
|
||||
@Override
|
||||
public void registerDefaultRecipes() {
|
||||
Iterator<Recipe> iterator = Bukkit.recipeIterator();
|
||||
while (iterator.hasNext()) {
|
||||
Recipe recipe = iterator.next();
|
||||
|
||||
if (recipe instanceof FurnaceRecipe) {
|
||||
RecipeChoice choice = ((FurnaceRecipe) recipe).getInputChoice();
|
||||
|
||||
SlimefunPlugin.getMinecraftRecipes().subscribe(snapshot -> {
|
||||
for (FurnaceRecipe recipe : snapshot.getRecipes(FurnaceRecipe.class)) {
|
||||
RecipeChoice choice = recipe.getInputChoice();
|
||||
|
||||
if (choice instanceof MaterialChoice) {
|
||||
for (Material input : ((MaterialChoice) choice).getChoices()) {
|
||||
registerRecipe(4, new ItemStack[] { new ItemStack(input) }, new ItemStack[] { recipe.getResult() });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user