Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugin/src/main/java/com/Acrobot/ChestShop/ChestShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ private void registerPreShopCreationEvents() {
registerEvent(new com.Acrobot.ChestShop.Listeners.PreShopCreation.PermissionChecker());
registerEvent(new com.Acrobot.ChestShop.Listeners.PreShopCreation.ErrorMessageSender());
registerEvent(new PriceChecker());
registerEvent(new FreePriceChecker()); // DC: block free (b:0 / s:0) shops
registerEvent(new QuantityChecker());
registerEvent(new TerrainChecker());
}
Expand All @@ -412,6 +413,7 @@ private void registerPreTransactionEvents() {

registerEvent(new InvalidNameIgnorer());
registerEvent(new CreativeModeIgnorer());
registerEvent(new FreeShopBreaker()); // DC: break pre-existing free (b:0 / s:0) shops on interact
registerEvent(new ErrorMessageSender());
registerEvent(new PermissionChecker());
registerEvent(new PriceValidator());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;

import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import java.math.BigDecimal;

import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_PRICE;

/**
* DemocracyCraft: free shops are not allowed — a buy or sell price of exactly 0
* (the {@code b:0} / {@code s:0} case). Runs after {@link PriceChecker} (LOWEST)
* has normalised the price line, reads the resulting buy/sell prices, and
* rejects creation if either offered side is priced at 0. An <em>unoffered</em>
* side ({@link PriceUtil#NO_PRICE} / -1) is fine — a buy-only or sell-only shop
* with a non-zero price is unaffected.
*/
public class FreePriceChecker implements Listener {

@EventHandler(priority = EventPriority.NORMAL)
public static void onPreShopCreation(PreShopCreationEvent event) {
String price = ChestShopSign.getPrice(event.getSignLines());
if (isFree(PriceUtil.getExactBuyPrice(price)) || isFree(PriceUtil.getExactSellPrice(price))) {
event.setOutcome(INVALID_PRICE);
}
}

private static boolean isFree(BigDecimal price) {
return price.compareTo(PriceUtil.FREE) == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.Acrobot.ChestShop.Listeners.PreTransaction;

import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import java.math.BigDecimal;

import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.INVALID_SHOP;

/**
* DemocracyCraft: free shops (a buy or sell price of exactly 0 — the {@code b:0}
* / {@code s:0} case) are blocked at creation by {@link com.Acrobot.ChestShop.Listeners.PreShopCreation.FreePriceChecker}.
* Any that pre-date that rule are cleaned up the first time someone interacts
* with them: the transaction is cancelled with {@code INVALID_SHOP} — which
* sends the "invalid shop" message via the PreTransaction ErrorMessageSender —
* and the sign is broken (firing a ShopDestroyedEvent so the shop is
* de-registered, then removing the block).
*/
public class FreeShopBreaker implements Listener {

@EventHandler(priority = EventPriority.LOWEST)
public static void onPreTransaction(PreTransactionEvent event) {
if (event.isCancelled()) {
return;
}

Sign sign = event.getSign();
if (sign == null) {
return;
}

String price = ChestShopSign.getPrice(sign);
if (!isFree(PriceUtil.getExactBuyPrice(price)) && !isFree(PriceUtil.getExactSellPrice(price))) {
return;
}

// Cancel the trade (INVALID_SHOP → "invalid shop" message to the client)
// and destroy the offending shop.
event.setCancelled(INVALID_SHOP);
SignBreak.sendShopDestroyedEvent(sign, event.getClient());
sign.getBlock().breakNaturally();
}

private static boolean isFree(BigDecimal price) {
return price.compareTo(PriceUtil.FREE) == 0;
}
}
Loading