-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
TradeInputs.java
105 lines (86 loc) · 3.36 KB
/
TradeInputs.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.denizenscript.denizen.objects.properties.trade;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.TradeTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class TradeInputs implements Property {
public static boolean describes(ObjectTag recipe) {
return recipe instanceof TradeTag;
}
public static TradeInputs getFrom(ObjectTag recipe) {
if (!describes(recipe)) {
return null;
}
return new TradeInputs((TradeTag) recipe);
}
public static final String[] handledMechs = new String[] {
"inputs"
};
private TradeTag recipe;
public TradeInputs(TradeTag recipe) {
this.recipe = recipe;
}
public String getPropertyString() {
if (recipe.getRecipe() == null) {
return null;
}
return getIngredientsList().identify();
}
public ListTag getIngredientsList() {
ListTag result = new ListTag();
for (ItemStack item : recipe.getRecipe().getIngredients()) {
result.addObject(new ItemTag(item));
}
return result;
}
public String getPropertyId() {
return "inputs";
}
public static void registerTags() {
// <--[tag]
// @attribute <TradeTag.inputs>
// @returns ListTag(ItemTag)
// @mechanism TradeTag.inputs
// @description
// Returns the list of items required to make the trade.
// -->
PropertyParser.<TradeInputs>registerTag("inputs", (attribute, recipe) -> {
return recipe.getIngredientsList();
});
}
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object TradeTag
// @name inputs
// @input ListTag(ItemTag)
// @description
// Sets the items required to make a successful trade. Use an empty input to make the trade impossible.
// NOTE: If a more than two items are specified, then only the first two items will be used.
// @tags
// <TradeTag.input>
// -->
if (mechanism.matches("inputs")) {
List<ItemStack> ingredients = new ArrayList<>();
List<ItemTag> list = mechanism.valueAsType(ListTag.class).filter(ItemTag.class, mechanism.context);
if (!mechanism.hasValue() || list.isEmpty()) {
recipe.getRecipe().setIngredients(ingredients);
return;
}
for (ItemTag item : list) {
ingredients.add(item.getItemStack());
}
if (ingredients.size() > 2) {
Debug.echoError("Trade recipe input was given " + list.size() + " items. Only using the first two items!");
ingredients = ingredients.subList(0, 2);
}
recipe.getRecipe().setIngredients(ingredients);
}
}
}