/
TraderCommand.java
119 lines (96 loc) · 4.27 KB
/
TraderCommand.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
package com.morphanone.depenizenbukkit.commands.dtltraders;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.dandielo.api.traders.TraderAPI;
import net.dandielo.citizens.traders_v3.TEntityStatus;
import net.dandielo.citizens.traders_v3.traits.TraderTrait;
// Based on the class in dtlTraders itself
public class TraderCommand extends AbstractCommand {
// <--[command]
// @Name Trader
// @Syntax trader [open/close] ({buy}/sell) ({stock}/relation)
// @Group Depenizen
// @Plugin Depenizen, dtlTraders
// @Required 1
// @Stable stable
// @Short Opens or closes an NPC's trading menu.
// @Author Dandielo
// @Description
// This command allows you to open or close the trading menu of an NPC
// that has the Trader trait from dtlTraders.
// You can specify whether to open the buy or sell menu, with default as buying.
// Optionally, you can specify whether to open the stock menu or relations menu.
// This command requires an attached player and NPC.
// @Tags
// None
// @Usage
// Use to open the buy menu of an attached NPC.
// - trader open buy
// @Usage
// Use to close an open trading menu.
// - trader close
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Interpret arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", Action.valueOf(arg.getValue()));
}
else if (arg.matches("buy", "sell")) {
scriptEntry.addObject("status", TEntityStatus.valueOf(arg.getValue().toUpperCase()));
}
if (arg.matchesEnum(Context.values())) {
scriptEntry.addObject("context", Context.valueOf(arg.getValue().toUpperCase()));
}
}
BukkitScriptEntryData data = (BukkitScriptEntryData) scriptEntry.entryData;
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must have action specified!");
}
else if (!data.hasPlayer()) {
throw new InvalidArgumentsException("Requires a player for executing!");
}
else if (!data.hasNPC()) {
throw new InvalidArgumentsException("Requires a Trader NPC for executing!");
}
if (!data.getNPC().getCitizen().hasTrait(TraderTrait.class)) {
throw new InvalidArgumentsException("Requires a Trader NPC for executing!");
}
scriptEntry.defaultObject("status", TEntityStatus.BUY).defaultObject("context", Context.STOCK);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch required objects
Action action = (Action) scriptEntry.getObject("action");
TEntityStatus status = (TEntityStatus) scriptEntry.getObject("status");
Context context = (Context) scriptEntry.getObject("context");
TraderTrait trait = ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getCitizen().getTrait(TraderTrait.class);
// Debug the execution
dB.report(scriptEntry, getName(), aH.debugObj("Action", action.toString())
+ aH.debugObj("Status", status)
+ aH.debugObj("Context", context));
// Do the execution
switch(action) {
case OPEN:
TraderAPI.openTrader(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity(), trait, status, context == Context.STOCK);
break;
case CLOSE:
TraderAPI.closeTrader(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity());
break;
default:
break;
}
}
private enum Context {
STOCK, RELATION
}
private enum Action {
OPEN, CLOSE
}
}