1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Added Enhanced Auto Crafter

This commit is contained in:
TheBusyBiscuit 2021-02-05 15:14:09 +01:00
parent a7e890829d
commit 6b4fe8a7a9
6 changed files with 144 additions and 12 deletions

View File

@ -41,6 +41,7 @@ import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlock;
import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine;
import io.github.thebusybiscuit.slimefun4.core.researching.Research;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.RecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.SlimefunGuideItem;

View File

@ -33,9 +33,25 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* This is the abstract super class for our auto crafters.
*
* @author TheBusyBiscuit
*
* @see VanillaAutoCrafter
* @see EnhancedAutoCrafter
*
*/
public abstract class AbstractAutoCrafter extends SlimefunItem implements EnergyNetComponent {
private int energyConsumedPerTick = -1;
/**
* The amount of energy consumed per crafting operation.
*/
private int energyConsumed = -1;
/**
* The amount of energy this machine can store.
*/
private int energyCapacity = -1;
@ParametersAreNonnullByDefault
@ -79,6 +95,7 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
Inventory inv = ((InventoryHolder) state).getInventory();
if (craft(inv, recipe)) {
// We are done crafting!
removeCharge(b.getLocation(), getEnergyConsumption());
}
}
@ -189,7 +206,7 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
* @return The rate of energy consumption
*/
public int getEnergyConsumption() {
return energyConsumedPerTick;
return energyConsumed;
}
/**
@ -202,6 +219,7 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
*
* @return This method will return the current instance of {@link AContainer}, so that can be chained.
*/
@Nonnull
public final AbstractAutoCrafter setCapacity(int capacity) {
Validate.isTrue(capacity > 0, "The capacity must be greater than zero!");
@ -221,12 +239,13 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
*
* @return This method will return the current instance of {@link AContainer}, so that can be chained.
*/
@Nonnull
public final AbstractAutoCrafter setEnergyConsumption(int energyConsumption) {
Validate.isTrue(energyConsumption > 0, "The energy consumption must be greater than zero!");
Validate.isTrue(energyCapacity > 0, "You must specify the capacity before you can set the consumption amount.");
Validate.isTrue(energyConsumption <= energyCapacity, "The energy consumption cannot be higher than the capacity (" + energyCapacity + ')');
this.energyConsumedPerTick = energyConsumption;
this.energyConsumed = energyConsumption;
return this;
}

View File

@ -10,19 +10,25 @@ import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate;
import org.bukkit.inventory.Inventory;
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;
public class AbstractRecipe {
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public abstract class AbstractRecipe {
private final Collection<Predicate<ItemStack>> inputs;
private final ItemStack result;
@ParametersAreNonnullByDefault
public AbstractRecipe(Collection<Predicate<ItemStack>> inputs, ItemStack result) {
protected AbstractRecipe(Collection<Predicate<ItemStack>> inputs, ItemStack result) {
Validate.notEmpty(inputs, "The input predicates cannot be null or an empty array");
Validate.notNull(result, "The recipe result must not be null!");
@ -31,16 +37,55 @@ public class AbstractRecipe {
}
@Nullable
public static AbstractRecipe wrapRecipe(@Nullable Recipe recipe) {
public static AbstractRecipe of(@Nullable Recipe recipe) {
if (recipe instanceof ShapedRecipe) {
return new AbstractRecipe((ShapedRecipe) recipe);
return new AbstractRecipe((ShapedRecipe) recipe) {
@Override
public void show(@Nonnull Inventory inv) {
// TODO Implement Recipe Preview
}
};
} else if (recipe instanceof ShapelessRecipe) {
return new AbstractRecipe((ShapelessRecipe) recipe);
return new AbstractRecipe((ShapelessRecipe) recipe) {
@Override
public void show(@Nonnull Inventory inv) {
// TODO Implement Recipe Preview
}
};
} else {
return null;
}
}
@Nullable
public static AbstractRecipe of(@Nonnull SlimefunItem item) {
return new AbstractRecipe(item) {
@Override
public void show(@Nonnull Inventory inv) {
// TODO Implement Recipe Preview
}
};
}
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());
}
@ -66,6 +111,11 @@ public class AbstractRecipe {
return choices;
}
@Nonnull
private static RecipeChoice[] getShape(@Nonnull Recipe recipe) {
return SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe);
}
@Nonnull
public Collection<Predicate<ItemStack>> getInputs() {
return inputs;
@ -76,4 +126,6 @@ public class AbstractRecipe {
return result;
}
public abstract void show(@Nonnull Inventory inv);
}

View File

@ -0,0 +1,56 @@
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.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 io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
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 EnhancedAutoCrafter extends AbstractAutoCrafter {
private final NamespacedKey recipeStorageKey;
@ParametersAreNonnullByDefault
public EnhancedAutoCrafter(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) {
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 onRightClick(@Nonnull Player p, @Nonnull Block b) {
// TODO: Implement Recipe Chooser
}
}

View File

@ -38,7 +38,7 @@ public class VanillaAutoCrafter extends AbstractAutoCrafter {
@Override
@Nullable
public AbstractRecipe getSelectedRecipe(@Nonnull Block b) {
return AbstractRecipe.wrapRecipe(getRecipe(b));
return AbstractRecipe.of(getRecipe(b));
}
@Nullable

View File

@ -1,4 +1,4 @@
package io.github.thebusybiscuit.slimefun4.implementation.guide;
package io.github.thebusybiscuit.slimefun4.implementation.tasks;
import java.util.HashMap;
import java.util.Map;
@ -16,6 +16,7 @@ import org.bukkit.inventory.RecipeChoice.MaterialChoice;
import io.github.thebusybiscuit.cscorelib2.collections.LoopIterator;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide;
/**
* A {@link RecipeChoiceTask} is an asynchronously repeating task that cycles
@ -27,13 +28,13 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
* @author TheBusyBiscuit
*
*/
class RecipeChoiceTask implements Runnable {
public class RecipeChoiceTask implements Runnable {
private static final int UPDATE_INTERVAL = 14;
private final Map<Integer, LoopIterator<Material>> iterators = new HashMap<>();
private Inventory inventory;
private int id;
private final Map<Integer, LoopIterator<Material>> iterators = new HashMap<>();
/**
* This will start this task for the given {@link Inventory}.
@ -43,17 +44,20 @@ class RecipeChoiceTask implements Runnable {
*/
public void start(@Nonnull Inventory inv) {
Validate.notNull(inv, "Inventory must not be null");
inventory = inv;
id = Bukkit.getScheduler().runTaskTimerAsynchronously(SlimefunPlugin.instance(), this, 0, UPDATE_INTERVAL).getTaskId();
}
public void add(int slot, @Nonnull MaterialChoice choice) {
Validate.notNull(choice, "Cannot add a null RecipeChoice");
iterators.put(slot, new LoopIterator<>(choice.getChoices()));
}
public void add(int slot, @Nonnull Tag<Material> tag) {
Validate.notNull(tag, "Cannot add a null Tag");
iterators.put(slot, new LoopIterator<>(tag.getValues()));
}