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

Allow any RecipeType (in theory)

This commit is contained in:
TheBusyBiscuit 2021-02-06 14:00:56 +01:00
parent a5a16621ee
commit e6494da8c9
4 changed files with 131 additions and 94 deletions

View File

@ -29,7 +29,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
* *
* @see AbstractAutoCrafter * @see AbstractAutoCrafter
* @see VanillaRecipe * @see VanillaRecipe
* @see EnhancedRecipe * @see SlimefunItemRecipe
* *
*/ */
public abstract class AbstractRecipe { public abstract class AbstractRecipe {
@ -120,18 +120,20 @@ public abstract class AbstractRecipe {
/** /**
* This static accessor is for {@link SlimefunItem} recipes. * This static accessor is for {@link SlimefunItem} recipes.
* Note that the {@link SlimefunItem} must be crafted using an {@link EnhancedCraftingTable}, * Note that the {@link SlimefunItem} must be crafted using the specified {@link RecipeType},
* otherwise null will be returned. * otherwise null will be returned.
* *
* @param item * @param item
* The {@link SlimefunItem} the recipe belongs to * The {@link SlimefunItem} the recipe belongs to
* @param recipeType
* The {@link RecipeType}
* *
* @return The wrapped {@link AbstractRecipe} or null * @return The wrapped {@link AbstractRecipe} or null
*/ */
@Nullable @Nullable
public static AbstractRecipe of(@Nullable SlimefunItem item) { public static AbstractRecipe of(@Nullable SlimefunItem item, @Nonnull RecipeType recipeType) {
if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) { if (item != null && item.getRecipeType().equals(recipeType)) {
return new EnhancedRecipe(item); return new SlimefunItemRecipe(item);
} else { } else {
return null; return null;
} }

View File

@ -1,29 +1,12 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable; import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/** /**
@ -34,77 +17,14 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
* *
* @see AbstractAutoCrafter * @see AbstractAutoCrafter
* @see VanillaAutoCrafter * @see VanillaAutoCrafter
* @see EnhancedRecipe * @see SlimefunItemRecipe
* *
*/ */
public class EnhancedAutoCrafter extends AbstractAutoCrafter { public class EnhancedAutoCrafter extends SlimefunAutoCrafter {
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public EnhancedAutoCrafter(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { public EnhancedAutoCrafter(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe, RecipeType.ENHANCED_CRAFTING_TABLE);
}
@Override
@Nullable
public AbstractRecipe getSelectedRecipe(@Nonnull Block b) {
BlockState state = PaperLib.getBlockState(b, false).getState();
if (state instanceof Skull) {
// Read the stored value from persistent data storage
String value = PersistentDataAPI.getString((Skull) state, recipeStorageKey);
SlimefunItem item = SlimefunItem.getByID(value);
if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) {
return AbstractRecipe.of(item);
}
}
return null;
}
@Override
protected void updateRecipe(@Nonnull Block b, @Nonnull Player p) {
ItemStack itemInHand = p.getInventory().getItemInMainHand();
SlimefunItem item = SlimefunItem.getByItem(itemInHand);
if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) {
// Fixes #1161
if (item.canUse(p, true)) {
AbstractRecipe recipe = AbstractRecipe.of(item);
if (recipe != null) {
ChestMenu menu = new ChestMenu(getItemName());
menu.setPlayerInventoryClickable(false);
menu.setEmptySlotsClickable(false);
ChestMenuUtils.drawBackground(menu, background);
ChestMenuUtils.drawBackground(menu, 45, 46, 47, 48, 50, 51, 52, 53);
menu.addItem(49, new CustomItem(Material.CRAFTING_TABLE, ChatColor.GREEN + SlimefunPlugin.getLocalization().getMessage(p, "messages.auto-crafting.select")));
menu.addMenuClickHandler(49, (pl, stack, slot, action) -> {
setSelectedRecipe(b, recipe);
p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.recipe-set");
showRecipe(p, b, recipe);
return false;
});
AsyncRecipeChoiceTask task = new AsyncRecipeChoiceTask();
recipe.show(menu, task);
menu.open(p);
p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
if (!task.isEmpty()) {
task.start(menu.toInventory());
}
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes");
}
}
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes");
}
} }
} }

View File

@ -0,0 +1,116 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* This extension of the {@link AbstractAutoCrafter} allows you to implement any
* {@link RecipeType}.
* The concrete implementation for this can be seen in the {@link EnhancedAutoCrafter} but
* it theoretically works for any {@link RecipeType}.
*
* @author TheBusyBiscuit
*
* @see EnhancedAutoCrafter
*
*/
public class SlimefunAutoCrafter extends AbstractAutoCrafter {
/**
* The targeted {@link RecipeType} that is being crafted here.
*/
private final RecipeType targetRecipeType;
@ParametersAreNonnullByDefault
protected SlimefunAutoCrafter(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, RecipeType targetRecipeType) {
super(category, item, recipeType, recipe);
this.targetRecipeType = targetRecipeType;
}
@Override
@Nullable
public AbstractRecipe getSelectedRecipe(@Nonnull Block b) {
BlockState state = PaperLib.getBlockState(b, false).getState();
if (state instanceof Skull) {
// Read the stored value from persistent data storage
String value = PersistentDataAPI.getString((Skull) state, recipeStorageKey);
SlimefunItem item = SlimefunItem.getByID(value);
if (item != null) {
return AbstractRecipe.of(item, targetRecipeType);
}
}
return null;
}
@Override
protected void updateRecipe(@Nonnull Block b, @Nonnull Player p) {
ItemStack itemInHand = p.getInventory().getItemInMainHand();
SlimefunItem item = SlimefunItem.getByItem(itemInHand);
if (item != null && item.getRecipeType().equals(targetRecipeType)) {
// Fixes #1161
if (item.canUse(p, true)) {
AbstractRecipe recipe = AbstractRecipe.of(item, targetRecipeType);
if (recipe != null) {
ChestMenu menu = new ChestMenu(getItemName());
menu.setPlayerInventoryClickable(false);
menu.setEmptySlotsClickable(false);
ChestMenuUtils.drawBackground(menu, background);
ChestMenuUtils.drawBackground(menu, 45, 46, 47, 48, 50, 51, 52, 53);
menu.addItem(49, new CustomItem(Material.CRAFTING_TABLE, ChatColor.GREEN + SlimefunPlugin.getLocalization().getMessage(p, "messages.auto-crafting.select")));
menu.addMenuClickHandler(49, (pl, stack, slot, action) -> {
setSelectedRecipe(b, recipe);
p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.recipe-set");
showRecipe(p, b, recipe);
return false;
});
AsyncRecipeChoiceTask task = new AsyncRecipeChoiceTask();
recipe.show(menu, task);
menu.open(p);
p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
if (!task.isEmpty()) {
task.start(menu.toInventory());
}
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes");
}
}
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes");
}
}
}

View File

@ -9,29 +9,28 @@ import javax.annotation.Nonnull;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask; import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; 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.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
/** /**
* This {@link AbstractRecipe} implementation stands for a {@link SlimefunItem} which * This {@link AbstractRecipe} implementation stands for a {@link SlimefunItem} which
* is crafted using an {@link EnhancedCraftingTable}. * is crafted using any {@link RecipeType}.
* *
* @author TheBusyBiscuit * @author TheBusyBiscuit
* *
* @see EnhancedCraftingTable * @see SlimefunAutoCrafter
* @see EnhancedAutoCrafter
* *
*/ */
class EnhancedRecipe extends AbstractRecipe { class SlimefunItemRecipe extends AbstractRecipe {
private final int[] slots = { 11, 12, 13, 20, 21, 22, 29, 30, 31 }; private final int[] slots = { 11, 12, 13, 20, 21, 22, 29, 30, 31 };
private final SlimefunItem item; private final SlimefunItem item;
EnhancedRecipe(@Nonnull SlimefunItem item) { SlimefunItemRecipe(@Nonnull SlimefunItem item) {
super(getInputs(item), item.getRecipeOutput()); super(getInputs(item), item.getRecipeOutput());
this.item = item; this.item = item;
} }