2012-08-10 21:44:32 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners.PreTransaction;
|
2012-08-10 18:56:16 +02:00
|
|
|
|
2012-09-20 22:38:11 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.MaterialUtil;
|
2019-11-19 21:50:17 +01:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2017-07-04 17:06:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Commands.Toggle;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Messages;
|
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2017-10-29 23:50:24 +01:00
|
|
|
import com.Acrobot.ChestShop.Database.Account;
|
2018-08-23 23:10:24 +02:00
|
|
|
import com.Acrobot.ChestShop.Economy.Economy;
|
2012-08-10 18:56:16 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
|
2019-05-27 18:11:09 +02:00
|
|
|
import com.google.common.collect.HashBasedTable;
|
2020-06-28 19:00:09 +02:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
2019-05-27 18:11:09 +02:00
|
|
|
import com.google.common.collect.Table;
|
2017-10-29 23:50:24 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2017-07-04 17:06:34 +02:00
|
|
|
import org.bukkit.Location;
|
2012-08-10 18:56:16 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
2019-05-27 18:11:09 +02:00
|
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
2012-09-20 22:38:11 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2020-06-28 19:00:09 +02:00
|
|
|
import java.util.Collections;
|
2019-05-27 18:11:09 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2013-01-24 22:35:28 +01:00
|
|
|
import static com.Acrobot.ChestShop.Configuration.Messages.CLIENT_DEPOSIT_FAILED;
|
|
|
|
import static com.Acrobot.ChestShop.Configuration.Messages.NOT_ENOUGH_STOCK_IN_YOUR_SHOP;
|
2017-07-04 17:06:34 +02:00
|
|
|
import static com.Acrobot.ChestShop.Configuration.Messages.NOT_ENOUGH_SPACE_IN_YOUR_SHOP;
|
2012-11-23 21:04:13 +01:00
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class ErrorMessageSender implements Listener {
|
2019-05-27 18:11:09 +02:00
|
|
|
|
|
|
|
private static Table<UUID, String, Long> notificationCooldowns = HashBasedTable.create();
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public static void onQuit(PlayerQuitEvent event) {
|
|
|
|
if (Properties.NOTIFICATION_MESSAGE_COOLDOWN > 0) {
|
|
|
|
notificationCooldowns.rowMap().remove(event.getPlayer().getUniqueId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public static void onMessage(PreTransactionEvent event) {
|
|
|
|
if (!event.isCancelled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:00:09 +02:00
|
|
|
Messages.Message message = null;
|
2012-08-10 18:56:16 +02:00
|
|
|
|
2012-09-20 22:38:11 +02:00
|
|
|
switch (event.getTransactionOutcome()) {
|
2013-01-14 22:18:32 +01:00
|
|
|
case SHOP_DOES_NOT_BUY_THIS_ITEM:
|
|
|
|
message = Messages.NO_BUYING_HERE;
|
|
|
|
break;
|
|
|
|
case SHOP_DOES_NOT_SELL_THIS_ITEM:
|
|
|
|
message = Messages.NO_SELLING_HERE;
|
|
|
|
break;
|
|
|
|
case CLIENT_DOES_NOT_HAVE_PERMISSION:
|
|
|
|
message = Messages.NO_PERMISSION;
|
|
|
|
break;
|
|
|
|
case CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY:
|
|
|
|
message = Messages.NOT_ENOUGH_MONEY;
|
|
|
|
break;
|
|
|
|
case SHOP_DOES_NOT_HAVE_ENOUGH_MONEY:
|
|
|
|
message = Messages.NOT_ENOUGH_MONEY_SHOP;
|
|
|
|
break;
|
|
|
|
case NOT_ENOUGH_SPACE_IN_CHEST:
|
2019-09-15 00:49:40 +02:00
|
|
|
if (Properties.SHOW_MESSAGE_FULL_SHOP && !Properties.CSTOGGLE_TOGGLES_FULL_SHOP || !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) {
|
2017-07-04 17:06:34 +02:00
|
|
|
Location loc = event.getSign().getLocation();
|
2020-06-28 19:00:09 +02:00
|
|
|
sendMessageToOwner(event.getOwnerAccount(), NOT_ENOUGH_SPACE_IN_YOUR_SHOP, new String[]{
|
|
|
|
"price", Economy.formatBalance(event.getExactPrice()),
|
|
|
|
"seller", event.getClient().getName(),
|
|
|
|
"world", loc.getWorld().getName(),
|
|
|
|
"x", String.valueOf(loc.getBlockX()),
|
|
|
|
"y", String.valueOf(loc.getBlockY()),
|
|
|
|
"z", String.valueOf(loc.getBlockZ())
|
|
|
|
}, event.getStock());
|
2017-07-04 17:06:34 +02:00
|
|
|
}
|
2013-01-14 22:18:32 +01:00
|
|
|
message = Messages.NOT_ENOUGH_SPACE_IN_CHEST;
|
|
|
|
break;
|
|
|
|
case NOT_ENOUGH_SPACE_IN_INVENTORY:
|
|
|
|
message = Messages.NOT_ENOUGH_SPACE_IN_INVENTORY;
|
|
|
|
break;
|
|
|
|
case NOT_ENOUGH_STOCK_IN_INVENTORY:
|
|
|
|
message = Messages.NOT_ENOUGH_ITEMS_TO_SELL;
|
|
|
|
break;
|
|
|
|
case NOT_ENOUGH_STOCK_IN_CHEST:
|
2019-09-15 00:49:40 +02:00
|
|
|
if (Properties.SHOW_MESSAGE_OUT_OF_STOCK && !Properties.CSTOGGLE_TOGGLES_OUT_OF_STOCK || !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) {
|
2017-07-04 17:06:34 +02:00
|
|
|
Location loc = event.getSign().getLocation();
|
2020-06-28 19:00:09 +02:00
|
|
|
sendMessageToOwner(event.getOwnerAccount(), NOT_ENOUGH_STOCK_IN_YOUR_SHOP, new String[]{
|
2020-06-28 23:55:21 +02:00
|
|
|
"price", Economy.formatBalance(event.getExactPrice()),
|
|
|
|
"buyer", event.getClient().getName(),
|
|
|
|
"world", loc.getWorld().getName(),
|
|
|
|
"x", String.valueOf(loc.getBlockX()),
|
|
|
|
"y", String.valueOf(loc.getBlockY()),
|
|
|
|
"z", String.valueOf(loc.getBlockZ())
|
2020-06-28 19:00:09 +02:00
|
|
|
}, event.getStock());
|
2017-07-04 17:06:34 +02:00
|
|
|
}
|
2013-01-14 22:18:32 +01:00
|
|
|
message = Messages.NOT_ENOUGH_STOCK;
|
|
|
|
break;
|
2013-01-24 22:35:28 +01:00
|
|
|
case CLIENT_DEPOSIT_FAILED:
|
|
|
|
message = Messages.CLIENT_DEPOSIT_FAILED;
|
|
|
|
break;
|
2013-01-14 22:18:32 +01:00
|
|
|
case SHOP_DEPOSIT_FAILED:
|
2020-06-28 19:00:09 +02:00
|
|
|
sendMessageToOwner(event.getOwnerAccount(), CLIENT_DEPOSIT_FAILED, new String[0]);
|
2013-01-24 22:35:28 +01:00
|
|
|
message = Messages.SHOP_DEPOSIT_FAILED;
|
2013-01-14 22:18:32 +01:00
|
|
|
break;
|
|
|
|
case SHOP_IS_RESTRICTED:
|
|
|
|
message = Messages.ACCESS_DENIED;
|
|
|
|
break;
|
|
|
|
case INVALID_SHOP:
|
|
|
|
message = Messages.INVALID_SHOP_DETECTED;
|
|
|
|
break;
|
2020-10-31 20:55:48 +01:00
|
|
|
case CREATIVE_MODE_PROTECTION:
|
|
|
|
message = Messages.TRADE_DENIED_CREATIVE_MODE;
|
|
|
|
break;
|
2015-08-06 14:33:15 +02:00
|
|
|
default:
|
2015-08-01 09:52:13 +02:00
|
|
|
break;
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message != null) {
|
2020-06-28 19:00:09 +02:00
|
|
|
message.sendWithPrefix(event.getClient());
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:00:09 +02:00
|
|
|
private static void sendMessageToOwner(Account ownerAccount, Messages.Message message, String[] replacements, ItemStack... stock) {
|
2017-10-29 23:50:24 +01:00
|
|
|
Player player = Bukkit.getPlayer(ownerAccount.getUuid());
|
2019-11-19 21:50:17 +01:00
|
|
|
if (player != null || Properties.BUNGEECORD_MESSAGES) {
|
2019-05-27 18:11:09 +02:00
|
|
|
|
|
|
|
if (Properties.NOTIFICATION_MESSAGE_COOLDOWN > 0) {
|
2020-06-28 19:00:09 +02:00
|
|
|
String cacheKey = message.getKey() + "|" + String.join(",", replacements) + "|" + MaterialUtil.getItemList(stock);
|
|
|
|
Long last = notificationCooldowns.get(ownerAccount.getUuid(), cacheKey);
|
2019-05-27 18:11:09 +02:00
|
|
|
if (last != null && last + Properties.NOTIFICATION_MESSAGE_COOLDOWN * 1000 > System.currentTimeMillis()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-28 19:00:09 +02:00
|
|
|
notificationCooldowns.put(ownerAccount.getUuid(), cacheKey, System.currentTimeMillis());
|
2019-05-27 18:11:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 21:50:17 +01:00
|
|
|
if (player != null) {
|
2020-06-28 19:00:09 +02:00
|
|
|
if (Properties.SHOWITEM_MESSAGE && MaterialUtil.Show.sendMessage(player, message, stock, Collections.emptyMap(), replacements)) {
|
2019-11-19 21:50:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-06-28 19:00:09 +02:00
|
|
|
String items = MaterialUtil.getItemList(stock);
|
|
|
|
message.sendWithPrefix(player,
|
|
|
|
ImmutableMap.of("material", items, "item", items), replacements);
|
2019-11-19 21:50:17 +01:00
|
|
|
} else {
|
2020-06-28 19:00:09 +02:00
|
|
|
String items = MaterialUtil.getItemList(stock);
|
|
|
|
ChestShop.sendBungeeMessage(ownerAccount.getName(), message,
|
|
|
|
ImmutableMap.of("material", items, "item", items), replacements);
|
2018-08-23 23:10:24 +02:00
|
|
|
}
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|