forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TransactionMessageSender.java
121 lines (96 loc) · 4.65 KB
/
TransactionMessageSender.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
package com.Acrobot.ChestShop.Listeners.PostTransaction;
import java.util.ArrayList;
import java.util.UUID;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.ComponentUtils;
/**
* @author Acrobot
*/
public class TransactionMessageSender implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public static void onTransaction(TransactionEvent event) {
if (event.getTransactionType() == TransactionEvent.TransactionType.BUY) {
sendBuyMessage(event);
} else {
sendSellMessage(event);
}
}
protected static void sendBuyMessage(TransactionEvent event) {
BaseComponent[] itemName = parseItemInformation(event.getStock());
String owner = NameManager.getFullNameFor(event.getOwner().getUniqueId());
Player player = event.getClient();
String price = Economy.formatBalance(event.getPrice());
if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) {
BaseComponent[] message = formatMessage(Messages.YOU_BOUGHT_FROM_SHOP.replace("%owner", owner), itemName, price);
player.spigot().sendMessage(message);
}
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwner())) {
BaseComponent[] message = formatMessage(Messages.SOMEBODY_BOUGHT_FROM_YOUR_SHOP.replace("%buyer", player.getName()), itemName, price);
sendMessageToOwner(message, event);
}
}
protected static void sendSellMessage(TransactionEvent event) {
BaseComponent[] itemName = parseItemInformation(event.getStock());
String owner = NameManager.getFullNameFor(event.getOwner().getUniqueId());
Player player = event.getClient();
String price = Economy.formatBalance(event.getPrice());
if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) {
BaseComponent[] message = formatMessage(Messages.YOU_SOLD_TO_SHOP.replace("%buyer", owner), itemName, price);
player.spigot().sendMessage(message);
}
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwner())) {
BaseComponent[] message = formatMessage(Messages.SOMEBODY_SOLD_TO_YOUR_SHOP.replace("%seller", player.getName()), itemName, price);
sendMessageToOwner(message, event);
}
}
private static BaseComponent[] parseItemInformation(ItemStack[] items) {
ItemStack[] stock = InventoryUtil.mergeSimilarStacks(items);
ArrayList<BaseComponent> message = new ArrayList<>();
// StringBuilder message = new StringBuilder(15);
// Joiner joiner = Joiner.on(' ');
for (ItemStack item : stock) {
if (!message.isEmpty()) {
message.add(new TextComponent(", "));
}
message.add(new TextComponent(item.getAmount() + " "));
message.add(ComponentUtils.getLocalizedItemName(item.getType()));
}
return message.toArray(new BaseComponent[message.size()]);
}
private static void sendMessageToOwner(BaseComponent[] message, TransactionEvent event) {
UUID owner = event.getOwner().getUniqueId();
Player player = Bukkit.getPlayer(owner);
if (player != null) {
player.spigot().sendMessage(message);
}
}
private static BaseComponent[] formatMessage(String message, BaseComponent[] itemName, String price) {
message = Messages.prefix(message).replace("%price", price);
String search = "%item";
int start = message.indexOf(search);
if (start < 0) {
return TextComponent.fromLegacyText(message);
}
BaseComponent[] messageComponents = TextComponent.fromLegacyText(message.substring(0, start));
BaseComponent last = messageComponents[messageComponents.length - 1];
if (itemName.length > 0) {
last.addExtra(new TextComponent(itemName));
}
last.addExtra(new TextComponent(TextComponent.fromLegacyText(message.substring(start + search.length()))));
return messageComponents;
}
}