/
TradeTag.java
158 lines (133 loc) · 4.9 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
package com.denizenscript.denizen.objects;
import com.denizenscript.denizencore.objects.*;
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.Collections;
public class TradeTag implements ObjectTag, Adjustable {
// <--[ObjectType]
// @name TradeTag
// @prefix trade
// @base ElementTag
// @implements PropertyHolderObject
// @ExampleTagBase trade[trade[result=diamond;max_use=10;inputs=stick]]
// @ExampleValues trade[result=diamond;max_use=10;inputs=stick]
// @format
// The identity format for trades is just the text 'trade'. All other data is specified through properties.
//
// @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>
// inputs=<item>(|<item>)
// uses=<number of uses>
// max_uses=<maximum number of uses>
// has_xp=true/false
//
// For example, the following command opens a virtual merchant inventory with two merchant trades.
// The first trade offers a sponge for two emeralds, 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>
// - opentrades trade[max_uses=10;inputs=emerald[quantity=2];result=sponge]|trade[result=barrier]
// </code>
//
// -->
@Fetchable("trade")
public static TradeTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
if (ObjectFetcher.isObjectWithProperties(string)) {
return ObjectFetcher.getObjectFromWithProperties(TradeTag.class, string, context);
}
string = CoreUtilities.toLowerCase(string).replace("trade@", "");
if (string.toLowerCase().matches("trade")) {
MerchantRecipe recipe = new MerchantRecipe(new ItemStack(Material.AIR), 0);
recipe.setIngredients(Collections.singletonList(new ItemStack(Material.AIR)));
return new TradeTag(recipe);
}
return null;
}
public static boolean matches(String str) {
return valueOf(str, CoreUtilities.noDebugContext) != null;
}
public TradeTag(MerchantRecipe recipe) {
this.recipe = recipe;
}
@Override
public TradeTag duplicate() {
MerchantRecipe result = new MerchantRecipe(recipe.getResult(), recipe.getUses(), recipe.getMaxUses(), recipe.hasExperienceReward(), recipe.getVillagerExperience(), recipe.getPriceMultiplier());
result.setIngredients(recipe.getIngredients());
return new TradeTag(result);
}
@Override
public String toString() {
return identify();
}
private MerchantRecipe recipe;
public MerchantRecipe getRecipe() {
return recipe;
}
public void setRecipe(MerchantRecipe recipe) {
this.recipe = recipe;
}
@Override
public String getPrefix() {
return "trade";
}
@Override
public TradeTag setPrefix(String prefix) {
return this;
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "Trade";
}
@Override
public String debuggable() {
return "<LG>trade@trade<Y>" + PropertyParser.getPropertiesDebuggable(this);
}
@Override
public String identify() {
return "trade@trade" + PropertyParser.getPropertiesString(this);
}
@Override
public String identifySimple() {
return identify();
}
@Override
public Object getJavaObject() {
return recipe;
}
public static void registerTags() {
PropertyParser.registerPropertyTagHandlers(TradeTag.class, tagProcessor);
}
public static ObjectTagProcessor<TradeTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
@Override
public void applyProperty(Mechanism mechanism) {
adjust(mechanism);
}
@Override
public void adjust(Mechanism mechanism) {
CoreUtilities.autoPropertyMechanism(this, mechanism);
}
}