-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
TradeTag.java
161 lines (132 loc) · 4.96 KB
/
TradeTag.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
package com.denizenscript.denizen.objects;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import java.util.Arrays;
public class TradeTag implements ObjectTag, 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 TradeTags.
//
// 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 trade@trade[max_uses=10;inputs=emerald[quantity=2];result=sponge|trade@trade[result=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 TradeTag. The 'trade@' is notation for Denizen's Object
// Fetcher. The constructor for a TradeTag is just the text 'trade'. All other data is specified through properties.
//
// For general info, see <@link language Merchant Trades>
//
// -->
//////////////////
// OBJECT FETCHER
////////////////
public static TradeTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("trade")
public static TradeTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
///////
// Handle objects with properties through the object fetcher
if (ObjectFetcher.isObjectWithProperties(string)) {
return ObjectFetcher.getObjectFrom(TradeTag.class, string, new BukkitTagContext(((BukkitTagContext) context).player,
((BukkitTagContext) context).npc, 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(Arrays.asList(new ItemStack(Material.AIR)));
return new TradeTag(recipe);
}
return null;
}
public static boolean matches(String str) {
return valueOf(str, CoreUtilities.noDebugContext) != null;
}
///////////////
// Constructors
/////////////
public TradeTag(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 TradeTag setPrefix(String prefix) {
return this;
}
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 static ObjectTagProcessor<TradeTag> tagProcessor = new ObjectTagProcessor<>();
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
public void applyProperty(Mechanism mechanism) {
adjust(mechanism);
}
public void adjust(Mechanism mechanism) {
CoreUtilities.autoPropertyMechanism(this, mechanism);
}
}