/
dTrade.java
174 lines (143 loc) · 5.2 KB
/
dTrade.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
public class dTrade implements dObject, Adjustable {
// <--[language]
// @name Merchant Trades
// @group Object System
// @description
// Merchant trades are the parts of a special merchant inventory that is typically viewed by right clicking
// a villager entity. Any number of trades can fit in a single merchant inventory.
//
// Trades are represented by 'trade@trade' dObjects.
//
// The properties that can be used to customize a merchant trade are:
//
// result=<item>
// input=<item>(|<item>)
// uses=<number of uses>
// max_uses=<maximum number of uses>
// has_xp=<true/false>
//
// For example, the following task script opens a virtual merchant inventory with two merchant trades. The
// first trade offers a sponge for two emeralds for a sponge, can be used up to 10 times, and offers XP upon a
// successful transaction. The second trade has zero maximum uses and displays a barrier.
//
// <code>
// open two trades:
// type: task
// script:
// - opentrades li@trade@trade[max_uses=10;inputs=i@emerald[quantity=2];result=i@sponge|trade@trade[result=i@barrier]
// </code>
//
// For format info, see <@link language trade@>
// -->
// <--[language]
// @name trade@
// @group Object Fetcher System
// @description
// trade@ refers to the 'object identifier' of a dTrade. The 'trade@' is notation for Denizen's Object
// Fetcher. The constructor for a dNPC is just the text 'trade'. All other data is specified through properties.
//
// For general info, see <@link language Merchant Trades>
//
// -->
//////////////////
// OBJECT FETCHER
////////////////
public static dTrade valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("trade")
public static dTrade valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
///////
// Handle objects with properties through the object fetcher
Matcher m = ObjectFetcher.DESCRIBED_PATTERN.matcher(string);
if (m.matches()) {
return ObjectFetcher.getObjectFrom(dTrade.class, string, new BukkitTagContext(((BukkitTagContext) context).player,
((BukkitTagContext) context).npc, false, null, !context.debug, null));
}
string = CoreUtilities.toLowerCase(string).replace("trade@", "");
if (string.toLowerCase().matches("trade")) {
MerchantRecipe recipe = new MerchantRecipe(new ItemStack(Material.AIR), 0);
recipe.setIngredients(new ArrayList<>(Arrays.asList(new ItemStack(Material.AIR))));
return new dTrade(recipe);
}
return null;
}
public static boolean matches(String arg) {
return arg.matches("trade@trade");
}
///////////////
// Constructors
/////////////
public dTrade(MerchantRecipe recipe) {
this.recipe = recipe;
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
public String toString() {
return identify();
}
private MerchantRecipe recipe;
public MerchantRecipe getRecipe() {
return recipe;
}
public void setRecipe(MerchantRecipe recipe) {
this.recipe = recipe;
}
//////////////////////////////
// DSCRIPT ARGUMENT METHODS
/////////////////////////
public String getPrefix() {
return "trade";
}
public dTrade setPrefix(String prefix) {
return this;
}
public String debug() {
return getPrefix() + "='<A>" + identify() + "<G>' ";
}
public boolean isUnique() {
return false;
}
public String getObjectType() {
return "Trade";
}
public String identify() {
return getPrefix() + "@trade" + PropertyParser.getPropertiesString(this);
}
public String identifySimple() {
return identify();
}
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
String returned = CoreUtilities.autoPropertyTag(this, attribute);
if (returned != null) {
return returned;
}
return new Element(identify()).getAttribute(attribute);
}
public void applyProperty(Mechanism mechanism) {
adjust(mechanism);
}
public void adjust(Mechanism mechanism) {
CoreUtilities.autoPropertyMechanism(this, mechanism);
}
}