-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
OpenTradesCommand.java
113 lines (95 loc) · 4.72 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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.objects.dTrade;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.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 {
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("trades")
&& !scriptEntry.hasObject("entity")
&& arg.matchesArgumentList(dTrade.class)) {
scriptEntry.addObject("trades", arg.asType(dList.class).filter(dTrade.class, scriptEntry));
}
else if (!scriptEntry.hasObject("trades")
&& !scriptEntry.hasObject("entity")
&& arg.matchesArgumentType(dEntity.class)) {
scriptEntry.addObject("entity", arg.asType(dEntity.class));
}
else if (arg.matchesPrefix("title")) {
scriptEntry.addObject("title", arg.asElement());
}
else if (arg.matchesPrefix("players")
&& arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("players", arg.asType(dList.class).filter(dPlayer.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 Element(""))
.defaultObject("players", Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()));
}
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
String title = scriptEntry.getElement("title").asString();
dEntity entity = scriptEntry.getdObject("entity");
List<dTrade> trades = (List<dTrade>) scriptEntry.getObject("trades");
List<dPlayer> players = (List<dPlayer>) scriptEntry.getObject("players");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
(entity != null ? aH.debugObj("entity", entity) : "")
+ (trades != null ? aH.debugList("trades", trades) : "")
+ (title.isEmpty() ? aH.debugObj("title", title) : "")
+ aH.debugList("players", players));
}
if (entity != null) {
if (players.size() > 1) {
dB.echoError("No more than one player can access the same entity!");
return;
}
if (entity.getBukkitEntity() instanceof Merchant) {
dPlayer player = players.get(0);
if (player.isValid() && player.isOnline()) {
player.getPlayerEntity().openMerchant((Merchant) entity.getBukkitEntity(), true);
}
else {
dB.echoError("Tried to make a nonexistent or offline player trade with a villager entity!");
}
return;
}
dB.echoError("The specified entity isn't a merchant!");
return;
}
List<MerchantRecipe> recipes = new ArrayList<>();
for (dTrade trade : trades) {
recipes.add(trade.getRecipe());
}
for (dPlayer player : players) {
if (player.isValid() && player.isOnline()) {
Merchant merchant = Bukkit.createMerchant(title);
merchant.setRecipes(recipes);
player.getPlayerEntity().openMerchant(merchant, true);
}
else {
dB.echoError("Tried to make a nonexistent or offline player view a virtual trading inventory!");
}
}
}
}