-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTradeCraftItemShop.java
More file actions
228 lines (182 loc) · 7.77 KB
/
TradeCraftItemShop.java
File metadata and controls
228 lines (182 loc) · 7.77 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
public abstract class TradeCraftItemShop extends TradeCraftShop {
public TradeCraftItemShop(TradeCraft plugin, Sign sign, Chest chest) {
super(plugin, sign, chest);
}
public void handleRightClick(Player player) {
if (isOwnedByPlayer(player)) {
handleOwnerClick(player);
} else {
handlePatronClick(player);
}
}
private void handleOwnerClick(Player player) {
if (!chestContentsAreOK()) {
plugin.sendMessage(player, "The chest has more than one type of item in it!");
return;
}
if (getChestItemCount() == 0) {
int goldAmount = withdrawGold();
if (goldAmount > 0) {
populateChest(Item.Type.GoldIngot.getId(), goldAmount);
plugin.sendMessage(player, "Withdrew %1$d gold.", goldAmount);
} else {
int itemAmount = withdrawItems();
if (itemAmount > 0) {
populateChest(getItemType(), itemAmount);
plugin.sendMessage(player, "Withdrew %1$d %2$s.", itemAmount, getItemName());
} else {
plugin.sendMessage(player, "There is nothing to withdraw.");
}
}
} else if (getChestItemType() == Item.Type.GoldIngot.getId()) {
depositGold(getChestItemCount());
plugin.sendMessage(player, "Deposited %1$d gold.", getChestItemCount());
populateChest(0, 0);
int itemAmount = withdrawItems();
if (itemAmount > 0) {
populateChest(getItemType(), itemAmount);
plugin.sendMessage(player, "Withdrew %1$d %2$s.", itemAmount, getItemName());
}
} else if (getChestItemType() == getItemType()) {
depositItems(getChestItemCount());
populateChest(0, 0);
plugin.sendMessage(player, "Deposited %1$d %2$s.", getChestItemCount(), getItemName());
} else {
plugin.sendMessage(player, "You can't deposit that here!");
}
}
private void handlePatronClick(Player player) {
boolean playerIsInBuyGroup = plugin.playerIsInGroup(player, plugin.properties.getGroupRequiredToBuyFromShops());
boolean playerIsInSellGroup = plugin.playerIsInGroup(player, plugin.properties.getGroupRequiredToSellToShops());
if (getChestItemCount() == 0) {
if (playerIsInBuyGroup && playerCanBuy()) {
plugin.sendMessage(player,
"You can buy %1$d %2$s for %3$d gold.",
getBuyAmount(),
getItemName(),
getBuyValue());
}
if (playerIsInSellGroup && playerCanSell()) {
plugin.sendMessage(player,
"You can sell %1$d %2$s for %3$d gold.",
getSellAmount(),
getItemName(),
getSellValue());
}
plugin.sendMessage(player, "The chest is empty.");
return;
}
if (!chestContentsAreOK()) {
plugin.sendMessage(player, "The chest has more than one type of item in it!");
return;
}
if (getChestItemType() == Item.Type.GoldIngot.getId()) {
if (!playerIsInBuyGroup) {
plugin.sendMessage(player, "You are not allowed to buy from shops!");
} else {
playerWantsToBuy(player);
}
} else if (getChestItemType() == getItemType()) {
if (!playerIsInSellGroup) {
plugin.sendMessage(player, "You are not allowed to sell to shops!");
} else {
playerWantsToSell(player);
}
} else {
plugin.sendMessage(player, "You can't sell that here!");
}
}
private void playerWantsToBuy(Player player) {
if (!playerCanBuy()) {
plugin.sendMessage(player, "You can't buy that here!");
return;
}
int goldPlayerWantsToSpend = getChestItemCount();
int amountPlayerWantsToBuy = goldPlayerWantsToSpend * getBuyAmount() / getBuyValue();
if (amountPlayerWantsToBuy == 0) {
plugin.sendMessage(player,
"You need to spend at least %1$d gold to get any %2$s.",
getBuyValue(),
getItemName());
return;
}
if (amountPlayerWantsToBuy > getItemsInShop()) {
plugin.sendMessage(player,
"Cannot buy. This shop only has %1$d %2$s.",
getItemsInShop(),
getItemName());
return;
}
int requiredGoldForThatAmount = amountPlayerWantsToBuy * getBuyValue() / getBuyAmount();
updateItemAndGoldAmounts(-amountPlayerWantsToBuy, requiredGoldForThatAmount);
chest.clear();
chest.add(Item.Type.GoldIngot.getId(), goldPlayerWantsToSpend - requiredGoldForThatAmount);
chest.add(getItemType(), amountPlayerWantsToBuy);
chest.update();
plugin.sendMessage(player,
"You bought %1$d %2$s for %3$d gold.",
amountPlayerWantsToBuy,
getItemName(),
requiredGoldForThatAmount);
}
private void playerWantsToSell(Player player) {
if (!playerCanSell()) {
plugin.sendMessage(player, "You can't sell that here!");
return;
}
int amountPlayerWantsToSell = getChestItemCount();
int goldPlayerShouldReceive = amountPlayerWantsToSell * getSellValue() / getSellAmount();
if (goldPlayerShouldReceive == 0) {
plugin.sendMessage(player,
"You need to sell at least %1$d %2$s to get any gold.",
getSellAmount(),
getItemName());
return;
}
if (goldPlayerShouldReceive > getGoldInShop()) {
plugin.sendMessage(player,
"Cannot sell. This shop only has %1$d gold.",
getGoldInShop());
return;
}
int amountThatCanBeSold = goldPlayerShouldReceive * getSellAmount() / getSellValue();
updateItemAndGoldAmounts(amountThatCanBeSold, -goldPlayerShouldReceive);
chest.clear();
chest.add(getItemType(), amountPlayerWantsToSell - amountThatCanBeSold);
chest.add(Item.Type.GoldIngot.getId(), goldPlayerShouldReceive);
chest.update();
plugin.sendMessage(player,
"You sold %1$d %2$s for %3$d gold.",
amountThatCanBeSold,
getItemName(),
goldPlayerShouldReceive);
}
public int getChestItemType() {
return chest.id;
}
public int getChestItemCount() {
return chest.total;
}
public boolean chestContentsAreOK() {
return chest.containsOnlyOneItemType();
}
public void populateChest(int id, int amount) {
chest.populateChest(id, amount);
}
public abstract boolean isOwnedByPlayer(Player player);
public abstract int getItemType();
public abstract String getItemName();
public abstract boolean playerCanBuy();
public abstract boolean playerCanSell();
public abstract int getBuyAmount();
public abstract int getBuyValue();
public abstract int getSellAmount();
public abstract int getSellValue();
public abstract int getItemsInShop();
public abstract int getGoldInShop();
public abstract void depositItems(int amount);
public abstract void depositGold(int amount);
public abstract int withdrawItems();
public abstract int withdrawGold();
public abstract void updateItemAndGoldAmounts(int itemAdjustment, int goldAdjustment);
}