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

Almost done with the vanilla auto crafter

This commit is contained in:
TheBusyBiscuit 2021-02-02 20:27:04 +01:00
parent 2c22983f0a
commit 581199271c
2 changed files with 102 additions and 2 deletions

View File

@ -6,10 +6,12 @@ import java.util.List;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
@ -28,11 +30,22 @@ public class AbstractRecipe {
this.result = result;
}
public AbstractRecipe(@Nonnull ShapelessRecipe recipe) {
@Nullable
public static AbstractRecipe wrapRecipe(@Nullable Recipe recipe) {
if (recipe instanceof ShapedRecipe) {
return new AbstractRecipe((ShapedRecipe) recipe);
} else if (recipe instanceof ShapelessRecipe) {
return new AbstractRecipe((ShapelessRecipe) recipe);
} else {
return null;
}
}
private AbstractRecipe(@Nonnull ShapelessRecipe recipe) {
this(new ArrayList<>(recipe.getChoiceList()), recipe.getResult());
}
public AbstractRecipe(@Nonnull ShapedRecipe recipe) {
private AbstractRecipe(@Nonnull ShapedRecipe recipe) {
this(getChoices(recipe), recipe.getResult());
}

View File

@ -0,0 +1,87 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
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.Recipe;
import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.PatternUtils;
import io.papermc.lib.PaperLib;
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;
public class VanillaAutoCrafter extends AbstractAutoCrafter {
private final NamespacedKey recipeStorageKey;
@ParametersAreNonnullByDefault
public VanillaAutoCrafter(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
recipeStorageKey = new NamespacedKey(SlimefunPlugin.instance(), "recipe_key");
}
@Override
@Nullable
public AbstractRecipe getSelectedRecipe(@Nonnull Block b) {
return AbstractRecipe.wrapRecipe(getRecipe(b));
}
@Nullable
private Recipe getRecipe(@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);
if (value != null) {
String[] values = PatternUtils.COLON.split(value);
/*
* Normally this constructor should not be used.
* But it is completely fine for this purpose since we only use
* it for lookups.
*/
@SuppressWarnings("deprecation")
NamespacedKey key = new NamespacedKey(values[0], values[1]);
return Bukkit.getRecipe(key);
}
}
return null;
}
@Override
protected boolean matches(@Nonnull ItemStack item, @Nonnull Predicate<ItemStack> predicate) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
// Slimefunitems should be ignored (unless allowed)
if (sfItem == null || sfItem.isUseableInWorkbench()) {
return super.matches(item, predicate);
} else {
return false;
}
}
@Override
protected void onRightClick(@Nonnull Player p, @Nonnull Block b) {
// TODO: Implement Recipe Chooser
}
}