ChestShop-3/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/CreationFeeGetter.java
Phoenix616 fc62ab0823 Use different economy responses and make some events cancellable
Also moved the creation fee subtraction to before the shop was created to be able to cancel the creation
2018-08-22 00:21:34 +01:00

66 lines
2.3 KiB
Java

package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
*/
public class CreationFeeGetter implements Listener {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public static void onShopCreation(PreShopCreationEvent event) {
double shopCreationPrice = Properties.SHOP_CREATION_PRICE;
if (shopCreationPrice == 0) {
return;
}
if (ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) {
return;
}
Player player = event.getPlayer();
if (Permission.has(player, NOFEE)) {
return;
}
CurrencySubtractEvent subtractionEvent = new CurrencySubtractEvent(BigDecimal.valueOf(shopCreationPrice), player);
ChestShop.callEvent(subtractionEvent);
if (!subtractionEvent.isSubtracted()) {
event.setOutcome(PreShopCreationEvent.CreationOutcome.NOT_ENOUGH_MONEY);
event.setSignLines(new String[4]);
return;
}
if (NameManager.getServerEconomyAccount() != null) {
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(
BigDecimal.valueOf(shopCreationPrice),
NameManager.getServerEconomyAccount().getUuid(),
player.getWorld());
ChestShop.callEvent(currencyAddEvent);
}
player.sendMessage(Messages.prefix(Messages.SHOP_FEE_PAID.replace("%amount", Economy.formatBalance(shopCreationPrice))));
}
}