|
| 1 | +package com.Acrobot.ChestShop.Listeners.PreTransaction; |
| 2 | + |
| 3 | +import com.Acrobot.Breeze.Utils.PriceUtil; |
| 4 | +import com.Acrobot.ChestShop.Events.PreTransactionEvent; |
| 5 | +import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak; |
| 6 | +import com.Acrobot.ChestShop.Signs.ChestShopSign; |
| 7 | +import org.bukkit.block.Sign; |
| 8 | +import org.bukkit.event.EventHandler; |
| 9 | +import org.bukkit.event.EventPriority; |
| 10 | +import org.bukkit.event.Listener; |
| 11 | + |
| 12 | +import java.math.BigDecimal; |
| 13 | + |
| 14 | +import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.INVALID_SHOP; |
| 15 | + |
| 16 | +/** |
| 17 | + * DemocracyCraft: free shops (a buy or sell price of exactly 0 — the {@code b:0} |
| 18 | + * / {@code s:0} case) are blocked at creation by {@link com.Acrobot.ChestShop.Listeners.PreShopCreation.FreePriceChecker}. |
| 19 | + * Any that pre-date that rule are cleaned up the first time someone interacts |
| 20 | + * with them: the transaction is cancelled with {@code INVALID_SHOP} — which |
| 21 | + * sends the "invalid shop" message via the PreTransaction ErrorMessageSender — |
| 22 | + * and the sign is broken (firing a ShopDestroyedEvent so the shop is |
| 23 | + * de-registered, then removing the block). |
| 24 | + */ |
| 25 | +public class FreeShopBreaker implements Listener { |
| 26 | + |
| 27 | + @EventHandler(priority = EventPriority.LOWEST) |
| 28 | + public static void onPreTransaction(PreTransactionEvent event) { |
| 29 | + if (event.isCancelled()) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + Sign sign = event.getSign(); |
| 34 | + if (sign == null) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + String price = ChestShopSign.getPrice(sign); |
| 39 | + if (!isFree(PriceUtil.getExactBuyPrice(price)) && !isFree(PriceUtil.getExactSellPrice(price))) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Cancel the trade (INVALID_SHOP → "invalid shop" message to the client) |
| 44 | + // and destroy the offending shop. |
| 45 | + event.setCancelled(INVALID_SHOP); |
| 46 | + SignBreak.sendShopDestroyedEvent(sign, event.getClient()); |
| 47 | + sign.getBlock().breakNaturally(); |
| 48 | + } |
| 49 | + |
| 50 | + private static boolean isFree(BigDecimal price) { |
| 51 | + return price.compareTo(PriceUtil.FREE) == 0; |
| 52 | + } |
| 53 | +} |
0 commit comments