forked from Slimefun/Slimefun4
-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathCraftingOperation.java
More file actions
79 lines (66 loc) · 2.58 KB
/
Copy pathCraftingOperation.java
File metadata and controls
79 lines (66 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package io.github.thebusybiscuit.slimefun4.implementation.operations;
import io.github.thebusybiscuit.slimefun4.core.machines.MachineOperation;
import io.github.thebusybiscuit.slimefun4.utils.SerializingUtils;
import javax.annotation.Nonnull;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
/**
* This {@link MachineOperation} represents a crafting process.
*
* @author TheBusyBiscuit
*
*/
public class CraftingOperation implements MachineOperation {
public static String INPUT = "input";
public static String OUTPUT = "output";
private final ItemStack[] ingredients;
private final ItemStack[] results;
private final int totalTicks;
private int currentTicks = 0;
public CraftingOperation(@Nonnull MachineRecipe recipe) {
this(recipe.getInput(), recipe.getOutput(), recipe.getTicks());
}
public CraftingOperation(@Nonnull ItemStack[] ingredients, @Nonnull ItemStack[] results, int totalTicks) {
Validate.notEmpty(ingredients, "The Ingredients array cannot be empty or null");
Validate.notEmpty(results, "The results array cannot be empty or null");
Validate.isTrue(
totalTicks >= 0,
"The amount of total ticks must be a positive integer or zero, received: " + totalTicks);
this.ingredients = ingredients;
this.results = results;
this.totalTicks = totalTicks;
}
public CraftingOperation(ConfigurationSection yaml) {
this.totalTicks = yaml.getInt(TOTAL_TICKS);
this.results = SerializingUtils.loadItemStackArray(yaml, OUTPUT);
this.ingredients = SerializingUtils.loadItemStackArray(yaml, INPUT);
}
@Override
public void addProgress(int num) {
Validate.isTrue(num > 0, "Progress must be positive.");
currentTicks += num;
}
@Nonnull
public ItemStack[] getIngredients() {
return ingredients;
}
@Nonnull
public ItemStack[] getResults() {
return results;
}
@Override
public int getProgress() {
return currentTicks;
}
@Override
public int getTotalTicks() {
return totalTicks;
}
public void serializeOperation(ConfigurationSection yaml, CraftingOperation operation) {
MachineOperation.super.serializeOperation(yaml, operation);
SerializingUtils.saveItemStackArray(yaml, INPUT, getIngredients());
SerializingUtils.saveItemStackArray(yaml, OUTPUT, getResults());
}
}