diff --git a/bukkit/src/main/java/com/blank038/servermarket/api/event/PlayerSaleEvent.java b/bukkit/src/main/java/com/blank038/servermarket/api/event/PlayerSaleEvent.java index 622dbb3..f11e127 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/api/event/PlayerSaleEvent.java +++ b/bukkit/src/main/java/com/blank038/servermarket/api/event/PlayerSaleEvent.java @@ -4,6 +4,7 @@ import com.blank038.servermarket.internal.cache.sale.SaleCache; import lombok.Getter; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; @@ -39,10 +40,35 @@ public Buy(Player who, MarketData marketData, SaleCache cache) { } } - public static class Sell extends PlayerSaleEvent { + public abstract static class Sell extends PlayerSaleEvent { public Sell(Player who, MarketData marketData, SaleCache cache) { super(who, marketData, cache); } + + public static class Pre extends PlayerSaleEvent.Sell implements Cancellable { + private boolean cancelled; + + public Pre(Player who, MarketData marketData, SaleCache cache) { + super(who, marketData, cache); + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + } + + public static class Post extends PlayerSaleEvent.Sell { + + public Post(Player who, MarketData marketData, SaleCache cache) { + super(who, marketData, cache); + } + } } } diff --git a/bukkit/src/main/java/com/blank038/servermarket/internal/cache/sale/SaleCache.java b/bukkit/src/main/java/com/blank038/servermarket/internal/cache/sale/SaleCache.java index a7e2755..4fc188f 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/internal/cache/sale/SaleCache.java +++ b/bukkit/src/main/java/com/blank038/servermarket/internal/cache/sale/SaleCache.java @@ -3,6 +3,7 @@ import com.blank038.servermarket.internal.data.DataContainer; import com.blank038.servermarket.internal.enums.PayType; import lombok.Getter; +import lombok.Setter; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.inventory.ItemStack; @@ -17,27 +18,13 @@ */ @Getter public class SaleCache { - /** - * 存储物品的 UUID - */ private final String saleUUID, ownerUUID, ownerName, ecoType, sourceMarket; - /** - * 商品对应的物品 - */ - private final ItemStack saleItem; - /** - * 商品对应的货币 - */ private final PayType payType; - /** - * 价格 - */ private final double price; - /** - * 发布时间 - */ private final long postTime; private final List saleTypes = new ArrayList<>(); + @Setter + private ItemStack saleItem; public SaleCache(String market, ConfigurationSection section) { this.saleUUID = section.getString("sale-uuid"); diff --git a/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java b/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java index 3a01477..5e86a41 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java +++ b/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java @@ -119,12 +119,17 @@ private void performSellCommand(Player player, String[] args) { .replace("%economy%", this.marketData.getEconomyName())); return; } - // send taxes - ServerMarketApi.sendTaxes(this.marketData.getPaymentType(), this.marketData.getEconomyType(), tax); - player.getInventory().setItemInMainHand(null); // initial SaleCache SaleCache saleItem = new SaleCache(UUID.randomUUID().toString(), this.marketData.getMarketKey(), player.getUniqueId().toString(), player.getName(), cloneItem, PayType.VAULT, this.marketData.getEconomyType(), price, System.currentTimeMillis()); + PlayerSaleEvent.Sell.Pre sellPreEvent = new PlayerSaleEvent.Sell.Pre(player, this.marketData, saleItem); + Bukkit.getPluginManager().callEvent(sellPreEvent); + if (sellPreEvent.isCancelled()) { + return; + } + // send taxes + ServerMarketApi.sendTaxes(this.marketData.getPaymentType(), this.marketData.getEconomyType(), tax); + player.getInventory().setItemInMainHand(null); CompletableFuture future = CompletableFuture.supplyAsync(() -> { // add sale to storage handler return ServerMarket.getStorageHandler().addSale(this.marketData.getMarketKey(), saleItem); @@ -132,8 +137,8 @@ private void performSellCommand(Player player, String[] args) { future.thenAccept((result) -> { if (result) { // call PlayerSaleEvent.Sell - PlayerSaleEvent.Sell event = new PlayerSaleEvent.Sell(player, this.marketData, saleItem); - Bukkit.getPluginManager().callEvent(event); + PlayerSaleEvent.Sell.Post sellPostEvent = new PlayerSaleEvent.Sell.Post(player, this.marketData, saleItem); + Bukkit.getPluginManager().callEvent(sellPostEvent); player.sendMessage(I18n.getStrAndHeader("sell")); // 判断是否公告 diff --git a/bukkit/src/main/java/com/blank038/servermarket/internal/listen/impl/CoreListener.java b/bukkit/src/main/java/com/blank038/servermarket/internal/listen/impl/CoreListener.java index dabb8e9..92bceaa 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/internal/listen/impl/CoreListener.java +++ b/bukkit/src/main/java/com/blank038/servermarket/internal/listen/impl/CoreListener.java @@ -13,7 +13,7 @@ public class CoreListener extends AbstractListener { @EventHandler - public void onSaleSell(PlayerSaleEvent.Sell event) { + public void onSaleSell(PlayerSaleEvent.Sell.Post event) { SaleLog saleLog = SaleLog.builder() .logType(LogType.SELL) .sourceMarket(event.getMarketData().getSourceId())