/
OpenTradesCommand.java
146 lines (127 loc) · 6.11 KB
/
OpenTradesCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.objects.TradeTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Bukkit;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantRecipe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class OpenTradesCommand extends AbstractCommand {
// <--[command]
// @Name OpenTrades
// @Syntax opentrades [<entity>/<trade>|...] (title:<title>) (players:<player>|...)
// @Required 1
// @Short Opens the specified villager entity's trading inventory or a list of trades.
// @Group player
//
// @Description
// Forces a player to open a villager's trading inventory or a virtual trading inventory.
// If an entity is specified, only one player can be specified.
// Otherwise, if a list of trades is specified, more than one player can be specified.
// If the title is not specified, no title will be applied to the virtual trading inventory.
// If no player is specified, by default the attached player will be forced to trade.
//
// @Tags
// <PlayerTag.selected_trade_index>
// <EntityTag.is_trading>
// <EntityTag.trades>
// <EntityTag.trading_with>
//
// @Usage
// Use to open an unusable trade.
// - opentrades TradeTag
//
// @Usage
// Use to open a list of trades with an optional title.
// - opentrades TradeTag[result=i@stone;inputs=li@i@stone;max_uses=9999]|TradeTag[result=i@barrier] "title:Useless Trades"
//
// @Usage
// Use to force a player to trade with a villager.
// - opentrades <def[villager_entity]>
// -->
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("trades")
&& !scriptEntry.hasObject("entity")
&& arg.matchesArgumentList(TradeTag.class)) {
scriptEntry.addObject("trades", arg.asType(ListTag.class).filter(TradeTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("trades")
&& !scriptEntry.hasObject("entity")
&& arg.matchesArgumentType(EntityTag.class)) {
scriptEntry.addObject("entity", arg.asType(EntityTag.class));
}
else if (arg.matchesPrefix("title")) {
scriptEntry.addObject("title", arg.asElement());
}
else if (arg.matchesPrefix("players")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("players", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("trades") && !scriptEntry.hasObject("entity")) {
throw new InvalidArgumentsException("Must specify a villager entity or a list of trades for the player(s) to trade with!");
}
scriptEntry.defaultObject("title", new ElementTag(""))
.defaultObject("players", Arrays.asList(Utilities.getEntryPlayer(scriptEntry)));
}
public void execute(ScriptEntry scriptEntry) {
String title = scriptEntry.getElement("title").asString();
EntityTag entity = scriptEntry.getObjectTag("entity");
List<TradeTag> trades = (List<TradeTag>) scriptEntry.getObject("trades");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(entity != null ? ArgumentHelper.debugObj("entity", entity) : "")
+ (trades != null ? ArgumentHelper.debugList("trades", trades) : "")
+ (title.isEmpty() ? ArgumentHelper.debugObj("title", title) : "")
+ ArgumentHelper.debugList("players", players));
}
if (entity != null) {
if (players.size() > 1) {
Debug.echoError("No more than one player can access the same entity!");
return;
}
if (entity.getBukkitEntity() instanceof Merchant) {
PlayerTag player = players.get(0);
if (player.isValid() && player.isOnline()) {
player.getPlayerEntity().openMerchant((Merchant) entity.getBukkitEntity(), true);
}
else {
Debug.echoError("Tried to make a nonexistent or offline player trade with a villager entity!");
}
return;
}
Debug.echoError("The specified entity isn't a merchant!");
return;
}
List<MerchantRecipe> recipes = new ArrayList<>();
for (TradeTag trade : trades) {
recipes.add(trade.getRecipe());
}
for (PlayerTag player : players) {
if (player.isValid() && player.isOnline()) {
Merchant merchant = Bukkit.createMerchant(title);
merchant.setRecipes(recipes);
player.getPlayerEntity().openMerchant(merchant, true);
}
else {
Debug.echoError("Tried to make a nonexistent or offline player view a virtual trading inventory!");
}
}
}
}