/
CmdSell.java
197 lines (176 loc) · 6.57 KB
/
CmdSell.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (C) Timothy Baxendale
* Portions derived from Shops Copyright (c) 2012 Nathan Dinsmore and Sam Lazarus.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.tbax.baxshops.internal.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.tbax.baxshops.*;
import org.tbax.baxshops.commands.BaxCommand;
import org.tbax.baxshops.commands.CmdActor;
import org.tbax.baxshops.errors.PrematureAbortException;
import org.tbax.baxshops.internal.Permissions;
import org.tbax.baxshops.internal.Resources;
import org.tbax.baxshops.internal.ShopPlugin;
import org.tbax.baxshops.internal.items.ItemUtil;
import org.tbax.baxshops.internal.notification.SaleRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public final class CmdSell extends ShopCommand
{
@Override
public @NotNull String getName()
{
return "sell";
}
@Override
public @NotNull String[] getAliases()
{
return new String[] { "s" };
}
@Override
public String getPermission()
{
return Permissions.SHOP_TRADER_SELL;
}
@Override
public @NotNull CommandHelp getHelp(@NotNull CmdActor actor)
{
CommandHelp help = new CommandHelp(this, "sell an item");
help.setLongDescription("Sell an item to a shop or send a request if sell requests for the shop are active");
help.setArgs(
new CommandHelpArgument("quantity", "the quantity to you wish to sell", false)
);
return help;
}
@Override
public boolean hasValidArgCount(@NotNull CmdActor actor)
{
return actor.getNumArgs() == 2 || actor.getNumArgs() == 1;
}
@Override
public boolean requiresSelection(@NotNull ShopCmdActor actor)
{
return true;
}
@Override
public boolean requiresOwner(@NotNull ShopCmdActor actor)
{
return false;
}
@Override
public boolean requiresPlayer(@NotNull CmdActor actor)
{
return true;
}
@Override
public boolean requiresItemInHand(@NotNull ShopCmdActor actor)
{
return actor.getNumArgs() < 2 || !BaxQuantity.isAny(actor.getArg(1));
}
@Override
public boolean useAlternative(CmdActor actor)
{
return ((ShopCmdActor)actor).isOwner();
}
@Override
public @NotNull Class<? extends BaxCommand> getAlternative()
{
return CmdRestock.class;
}
@Override
public boolean allowsExclusion(ShopCmdActor actor)
{
return actor.getNumArgs() == 2 && BaxQuantity.isAny(actor.getArg(1));
}
@Override
public void onShopCommand(@NotNull ShopCmdActor actor) throws PrematureAbortException
{
if (actor.getNumArgs() == 1) {
assert actor.getItemInHand() != null;
actor.appendArg(actor.getItemInHand().getAmount());
}
BaxShop shop = actor.getShop();
assert shop != null;
if (requiresItemInHand(actor) && !shop.contains(actor.getItemInHand()))
actor.exitError("The owner of the shop isn't buying %s", ItemUtil.getName(actor.getItemInHand()));
List<BaxEntry> items = actor.peekArgFromInventory(1).stream()
.filter(BaxEntry::canSell)
.collect(Collectors.toList());
if (items.isEmpty()) {
if (requiresItemInHand(actor)) {
actor.exitError("The owner of the shop isn't buying %s", ItemUtil.getName(actor.getItemInHand()));
}
else {
actor.exitWarning("You did not have anything to sell at this shop.");
}
}
double total = 0.0;
for (BaxEntry entry : items) {
if (entry.getAmount() > 0) {
double price = sell(actor, entry);
if (price >= 0.0) {
total = MathUtil.add(total, price);
}
}
}
if (total > 0.0) {
actor.sendMessage("You earned %s.", Format.money(total));
actor.sendMessage(Resources.CURRENT_BALANCE, Format.money2(ShopPlugin.getEconomy().getBalance(actor.getPlayer())));
}
else if (shop.hasFlagSellRequests()) {
actor.sendMessage("Your money will be deposited when the buyer accepts the sale.");
}
}
static double sell(ShopCmdActor actor, BaxEntry entry) throws PrematureAbortException
{
BaxShop shop = actor.getShop();
String name = ItemUtil.getName(entry.getItemStack());
double price = MathUtil.multiply(entry.getAmount(), entry.getRefundPrice());
if (shop.hasFlagSellRequests()) {
SaleRequest request = new SaleRequest(shop.getId(), shop.getOwner(), actor.getPlayer(), entry);
ShopPlugin.sendNotification(shop.getOwner(), request);
actor.sendMessage("Your request to sell %s for %s has been sent.",
Format.itemName(entry.getAmount(), name), Format.money(price)
);
return 0;
}
else {
PlayerUtil.sellItem(shop, shop.getOwner(), actor.getPlayer(), entry);
PlayerUtil.takeFromInventory(actor.getPlayer().getInventory(), entry.getItemStack(), entry.getAmount(), actor.getShop().hasFlagSmartStack());
actor.sendMessage(
"You have sold %s for %s to %s.",
Format.itemName(entry.getAmount(), name),
Format.money(price),
Format.username(shop.getOwner().getName())
);
return price;
}
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args)
{
CmdActor actor = (CmdActor)sender;
if (actor.getNumArgs() == 2) {
return Arrays.asList("all", "any", "most", "stack");
}
return super.onTabComplete(sender, command, alias, args);
}
}