1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Split up recipe classes

This commit is contained in:
TheBusyBiscuit 2021-02-05 19:53:48 +01:00
parent 7d50d0eb32
commit 53ac633927
4 changed files with 189 additions and 88 deletions

View File

@ -1,8 +1,6 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -12,13 +10,10 @@ import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe; import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe; import org.bukkit.inventory.ShapelessRecipe;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.RecipeChoiceTask; import io.github.thebusybiscuit.slimefun4.implementation.tasks.RecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
@ -37,88 +32,6 @@ public abstract class AbstractRecipe {
this.result = result; this.result = result;
} }
@Nullable
public static AbstractRecipe of(@Nullable Recipe recipe) {
if (recipe instanceof ShapedRecipe) {
return new AbstractRecipe((ShapedRecipe) recipe) {
@Override
public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) {
// TODO Auto-generated method stub
}
};
} else if (recipe instanceof ShapelessRecipe) {
return new AbstractRecipe((ShapelessRecipe) recipe) {
@Override
public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) {
// TODO Auto-generated method stub
}
};
} else {
return null;
}
}
@Nullable
public static AbstractRecipe of(@Nonnull SlimefunItem item) {
return new AbstractRecipe(item) {
@Override
public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) {
// TODO Auto-generated method stub
}
};
}
private AbstractRecipe(@Nonnull SlimefunItem item) {
Validate.notNull(item, "The SlimefunItem should not be null!");
Validate.isTrue(item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE), "The item must be crafted in an enhanced crafting table!");
result = item.getRecipeOutput();
inputs = new ArrayList<>();
for (int i = 0; i < 9; i++) {
ItemStack ingredient = item.getRecipe()[i];
inputs.add(stack -> SlimefunUtils.isItemSimilar(stack, ingredient, true));
}
}
private AbstractRecipe(@Nonnull ShapelessRecipe recipe) {
this(new ArrayList<>(recipe.getChoiceList()), recipe.getResult());
}
private AbstractRecipe(@Nonnull ShapedRecipe recipe) {
this(getChoices(recipe), recipe.getResult());
}
@Nonnull
private static Collection<Predicate<ItemStack>> getChoices(@Nonnull ShapedRecipe recipe) {
List<Predicate<ItemStack>> choices = new ArrayList<>();
for (String row : recipe.getShape()) {
for (char c : row.toCharArray()) {
RecipeChoice choice = recipe.getChoiceMap().get(c);
if (choice != null) {
choices.add(choice);
}
}
}
return choices;
}
@Nonnull
private static RecipeChoice[] getShape(@Nonnull Recipe recipe) {
return SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe);
}
@Nonnull @Nonnull
public Collection<Predicate<ItemStack>> getInputs() { public Collection<Predicate<ItemStack>> getInputs() {
return inputs; return inputs;
@ -131,4 +44,24 @@ public abstract class AbstractRecipe {
public abstract void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task); public abstract void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task);
@Nullable
public static AbstractRecipe of(@Nullable Recipe recipe) {
if (recipe instanceof ShapedRecipe) {
return new VanillaRecipe((ShapedRecipe) recipe);
} else if (recipe instanceof ShapelessRecipe) {
return new VanillaRecipe((ShapelessRecipe) recipe);
} else {
return null;
}
}
@Nullable
public static AbstractRecipe of(@Nullable SlimefunItem item) {
if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) {
return new EnhancedRecipe(item);
} else {
return null;
}
}
} }

View File

@ -0,0 +1,51 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters;
import java.util.ArrayList;
import javax.annotation.Nonnull;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.RecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
/**
* This {@link AbstractRecipe} implementation stands for a {@link SlimefunItem} which
* is crafted using an {@link EnhancedCraftingTable}.
*
* @author TheBusyBiscuit
*
* @see EnhancedCraftingTable
* @see EnhancedAutoCrafter
*
*/
class EnhancedRecipe extends AbstractRecipe {
private final int[] slots = { 11, 12, 13, 20, 21, 22, 29, 30, 31 };
private final SlimefunItem item;
EnhancedRecipe(@Nonnull SlimefunItem item) {
super(new ArrayList<>(), item.getRecipeOutput());
this.item = item;
for (int i = 0; i < 9; i++) {
ItemStack ingredient = item.getRecipe()[i];
getInputs().add(stack -> SlimefunUtils.isItemSimilar(stack, ingredient, true));
}
}
@Override
public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) {
menu.addItem(24, getResult().clone(), ChestMenuUtils.getEmptyClickHandler());
ItemStack[] recipe = item.getRecipe();
for (int i = 0; i < 9; i++) {
menu.addItem(slots[i], recipe[i], ChestMenuUtils.getEmptyClickHandler());
}
}
}

View File

@ -0,0 +1,99 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.RecipeChoice.MaterialChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.RecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
/**
* The {@link VanillaRecipe} implements an {@link AbstractRecipe} and represents a
* {@link ShapedRecipe} or {@link ShapelessRecipe}.
*
* @author TheBusyBiscuit
*
* @see VanillaAutoCrafter
*
*/
class VanillaRecipe extends AbstractRecipe {
private final int[] slots = { 11, 12, 13, 20, 21, 22, 29, 30, 31 };
private final Recipe recipe;
VanillaRecipe(@Nonnull ShapelessRecipe recipe) {
super(new ArrayList<>(recipe.getChoiceList()), recipe.getResult());
this.recipe = recipe;
}
VanillaRecipe(@Nonnull ShapedRecipe recipe) {
super(getChoices(recipe), recipe.getResult());
this.recipe = recipe;
}
@Nonnull
private static Collection<Predicate<ItemStack>> getChoices(@Nonnull ShapedRecipe recipe) {
List<Predicate<ItemStack>> choices = new ArrayList<>();
for (String row : recipe.getShape()) {
for (char c : row.toCharArray()) {
RecipeChoice choice = recipe.getChoiceMap().get(c);
if (choice != null) {
choices.add(choice);
}
}
}
return choices;
}
@Nonnull
private static RecipeChoice[] getShape(@Nonnull Recipe recipe) {
return SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe);
}
@Override
public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) {
menu.addItem(24, getResult().clone(), ChestMenuUtils.getEmptyClickHandler());
RecipeChoice[] choices = SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe);
ItemStack[] items = new ItemStack[9];
if (choices.length == 1 && choices[0] instanceof MaterialChoice) {
items[4] = new ItemStack(((MaterialChoice) choices[0]).getChoices().get(0));
if (((MaterialChoice) choices[0]).getChoices().size() > 1) {
task.add(slots[4], (MaterialChoice) choices[0]);
}
} else {
for (int i = 0; i < choices.length; i++) {
if (choices[i] instanceof MaterialChoice) {
items[i] = new ItemStack(((MaterialChoice) choices[i]).getChoices().get(0));
if (((MaterialChoice) choices[i]).getChoices().size() > 1) {
task.add(slots[i], (MaterialChoice) choices[i]);
}
}
}
}
for (int i = 0; i < 9; i++) {
menu.addItem(slots[i], items[i], ChestMenuUtils.getEmptyClickHandler());
}
}
}

View File

@ -9,6 +9,8 @@ import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Keyed; import org.bukkit.Keyed;
import org.bukkit.Material; import org.bukkit.Material;
@ -138,16 +140,32 @@ public class RecipeType implements Keyed {
} }
@Override @Override
public NamespacedKey getKey() { public final NamespacedKey getKey() {
return key; return key;
} }
@Override
public final boolean equals(Object obj) {
if (obj instanceof RecipeType) {
return ((RecipeType) obj).getKey().equals(this.getKey());
} else {
return false;
}
}
@Override
public final int hashCode() {
return getKey().hashCode();
}
@ParametersAreNonnullByDefault
private static void registerBarterDrop(ItemStack[] recipe, ItemStack output) { private static void registerBarterDrop(ItemStack[] recipe, ItemStack output) {
if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) {
SlimefunPlugin.getRegistry().getBarteringDrops().add(output); SlimefunPlugin.getRegistry().getBarteringDrops().add(output);
} }
} }
@ParametersAreNonnullByDefault
private static void registerMobDrop(ItemStack[] recipe, ItemStack output) { private static void registerMobDrop(ItemStack[] recipe, ItemStack output) {
String mob = ChatColor.stripColor(recipe[4].getItemMeta().getDisplayName()).toUpperCase(Locale.ROOT).replace(' ', '_'); String mob = ChatColor.stripColor(recipe[4].getItemMeta().getDisplayName()).toUpperCase(Locale.ROOT).replace(' ', '_');
EntityType entity = EntityType.valueOf(mob); EntityType entity = EntityType.valueOf(mob);