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
This commit is contained in:
Phoenix616 2018-08-22 00:21:34 +01:00
parent 3ae71cf13a
commit fc62ab0823
13 changed files with 97 additions and 27 deletions

View File

@ -21,7 +21,7 @@ import com.Acrobot.ChestShop.Listeners.ItemInfoListener;
import com.Acrobot.ChestShop.Listeners.Modules.DiscountModule;
import com.Acrobot.ChestShop.Listeners.Modules.PriceRestrictionModule;
import com.Acrobot.ChestShop.Listeners.Player.*;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.CreationFeeGetter;
import com.Acrobot.ChestShop.Listeners.PreShopCreation.CreationFeeGetter;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.MessageSender;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.ShopCreationLogger;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.SignSticker;

View File

@ -37,14 +37,14 @@ public class Economy {
CurrencyAddEvent event = new CurrencyAddEvent(BigDecimal.valueOf(amount), name, world);
ChestShop.callEvent(event);
return true;
return event.isAdded();
}
public static boolean subtract(UUID name, World world, double amount) {
CurrencySubtractEvent event = new CurrencySubtractEvent(BigDecimal.valueOf(amount), name, world);
ChestShop.callEvent(event);
return true;
return event.isSubtracted();
}
public static boolean hasEnough(UUID name, World world, double amount) {

View File

@ -2,6 +2,7 @@ package com.Acrobot.ChestShop.Events;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
@ -10,7 +11,7 @@ import org.bukkit.event.HandlerList;
*
* @author Acrobot
*/
public class PreShopCreationEvent extends Event {
public class PreShopCreationEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private Player creator;
@ -30,10 +31,21 @@ public class PreShopCreationEvent extends Event {
*
* @return Is event cancelled?
*/
@Override
public boolean isCancelled() {
return outcome != CreationOutcome.SHOP_CREATED_SUCCESSFULLY;
}
/**
* Set if event is cancelled. This sets a generic {@link CreationOutcome#OTHER};
*
* @param cancel Cancel the event?
*/
@Override
public void setCancelled(boolean cancel) {
outcome = CreationOutcome.OTHER;
}
/**
* Returns the outcome of the event
*

View File

@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.Inventory;
@ -15,7 +16,7 @@ import org.bukkit.inventory.ItemStack;
*
* @author Acrobot
*/
public class TransactionEvent extends Event {
public class TransactionEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final TransactionType type;
@ -30,6 +31,8 @@ public class TransactionEvent extends Event {
private final Sign sign;
private boolean cancelled = false;
public TransactionEvent(PreTransactionEvent event, Sign sign) {
this.type = event.getTransactionType();
@ -133,6 +136,16 @@ public class TransactionEvent extends Event {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
/**
* Possible transaction types
*/

View File

@ -118,7 +118,7 @@ public class ReserveListener implements Listener {
final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
if (lastSeen != null && provided()) {
economyAPI.addHoldings(event.getTarget(), event.getAmount(), event.getWorld().getName());
event.setAdded(economyAPI.addHoldings(event.getTarget(), event.getAmount(), event.getWorld().getName()));
}
}
@ -130,7 +130,7 @@ public class ReserveListener implements Listener {
final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
if (lastSeen != null && provided()) {
economyAPI.removeHoldings(event.getTarget(), event.getAmount(), event.getWorld().getName());
event.setSubtracted(economyAPI.removeHoldings(event.getTarget(), event.getAmount(), event.getWorld().getName()));
}
}
@ -149,6 +149,8 @@ public class ReserveListener implements Listener {
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(currencySubtractEvent.getAmount(), event.getReceiver(), event.getWorld());
onCurrencyAdd(currencyAddEvent);
event.setTransferred(currencyAddEvent.isAdded());
}
@EventHandler

View File

@ -146,7 +146,8 @@ public class VaultListener implements Listener {
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
if (lastSeen != null) {
provider.depositPlayer(lastSeen, world.getName(), event.getDoubleAmount());
EconomyResponse response = provider.depositPlayer(lastSeen, world.getName(), event.getDoubleAmount());
event.setAdded(response.type == EconomyResponse.ResponseType.SUCCESS);
}
}
@ -161,7 +162,8 @@ public class VaultListener implements Listener {
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
if (lastSeen != null) {
provider.withdrawPlayer(lastSeen, world.getName(), event.getDoubleAmount());
EconomyResponse response = provider.withdrawPlayer(lastSeen, world.getName(), event.getDoubleAmount());
event.setSubtracted(response.type == EconomyResponse.ResponseType.SUCCESS);
}
}
@ -180,6 +182,8 @@ public class VaultListener implements Listener {
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(currencySubtractEvent.getAmount(), event.getReceiver(), event.getWorld());
onCurrencyAdd(currencyAddEvent);
event.setTransferred(currencyAddEvent.isAdded());
}
@EventHandler

View File

@ -34,6 +34,8 @@ public class ServerAccountCorrector implements Listener {
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencyAddEvent);
event.setAdded(currencyAddEvent.isAdded());
}
@EventHandler(priority = EventPriority.LOWEST)
@ -54,6 +56,8 @@ public class ServerAccountCorrector implements Listener {
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencySubtractEvent);
event.setSubtracted(currencySubtractEvent.isSubtracted());
}
@EventHandler(priority = EventPriority.LOWEST)

View File

@ -16,33 +16,60 @@ import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL
* @author Acrobot
*/
public class EconomicModule implements Listener {
@EventHandler
@EventHandler(ignoreCancelled = true)
public static void onBuyTransaction(TransactionEvent event) {
if (event.getTransactionType() != BUY) {
return;
}
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(BigDecimal.valueOf(event.getPrice()),
event.getOwnerAccount().getUuid(),
event.getSign().getWorld());
ChestShop.callEvent(currencyAddEvent); // java.lang.StackOverflowError
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(
BigDecimal.valueOf(event.getPrice()),
event.getOwnerAccount().getUuid(),
event.getSign().getWorld());
ChestShop.callEvent(currencyAddEvent);
if (!currencyAddEvent.isAdded()) {
event.setCancelled(true);
return;
}
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
ChestShop.callEvent(currencySubtractEvent);
if (!currencySubtractEvent.isSubtracted()) {
event.setCancelled(true);
CurrencySubtractEvent currencyResetEvent = new CurrencySubtractEvent(
BigDecimal.valueOf(event.getPrice()),
event.getOwnerAccount().getUuid(),
event.getSign().getWorld());
ChestShop.callEvent(currencyResetEvent);
}
}
@EventHandler
@EventHandler(ignoreCancelled = true)
public static void onSellTransaction(TransactionEvent event) {
if (event.getTransactionType() != SELL) {
return;
}
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(BigDecimal.valueOf(event.getPrice()),
event.getOwnerAccount().getUuid(),
event.getSign().getWorld());
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(
BigDecimal.valueOf(event.getPrice()),
event.getOwnerAccount().getUuid(),
event.getSign().getWorld());
ChestShop.callEvent(currencySubtractEvent);
if (!currencySubtractEvent.isSubtracted()) {
event.setCancelled(true);
return;
}
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
ChestShop.callEvent(currencyAddEvent);
if (!currencyAddEvent.isAdded()) {
event.setCancelled(true);
CurrencyAddEvent currencyResetEvent = new CurrencyAddEvent(
BigDecimal.valueOf(event.getPrice()),
event.getOwnerAccount().getUuid(),
event.getSign().getWorld());
ChestShop.callEvent(currencyResetEvent);
}
}
}

View File

@ -21,7 +21,7 @@ import org.bukkit.inventory.ItemStack;
*/
public class EmptyShopDeleter implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public static void onTransaction(TransactionEvent event) {
if (event.getTransactionType() != TransactionEvent.TransactionType.BUY) {
return;

View File

@ -4,6 +4,7 @@ import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -15,7 +16,7 @@ import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL
* @author Acrobot
*/
public class ItemManager implements Listener {
@EventHandler
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public static void shopItemRemover(TransactionEvent event) {
if (event.getTransactionType() != BUY) {
return;
@ -27,7 +28,7 @@ public class ItemManager implements Listener {
event.getClient().updateInventory();
}
@EventHandler
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public static void inventoryItemRemover(TransactionEvent event) {
if (event.getTransactionType() != SELL) {
return;

View File

@ -19,7 +19,7 @@ public class TransactionLogger implements Listener {
private static final String BUY_MESSAGE = "%1$s bought %2$s for %3$.2f from %4$s at %5$s";
private static final String SELL_MESSAGE = "%1$s sold %2$s for %3$.2f to %4$s at %5$s";
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onTransaction(final TransactionEvent event) {
String template = (event.getTransactionType() == BUY ? BUY_MESSAGE : SELL_MESSAGE);

View File

@ -24,7 +24,7 @@ import java.util.UUID;
* @author Acrobot
*/
public class TransactionMessageSender implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onTransaction(TransactionEvent event) {
if (event.getTransactionType() == TransactionEvent.TransactionType.BUY) {
sendBuyMessage(event);

View File

@ -1,4 +1,4 @@
package com.Acrobot.ChestShop.Listeners.PostShopCreation;
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
@ -6,12 +6,13 @@ 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.ShopCreatedEvent;
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;
@ -24,8 +25,8 @@ import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
*/
public class CreationFeeGetter implements Listener {
@EventHandler
public static void onShopCreation(ShopCreatedEvent event) {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public static void onShopCreation(PreShopCreationEvent event) {
double shopCreationPrice = Properties.SHOP_CREATION_PRICE;
if (shopCreationPrice == 0) {
@ -45,6 +46,12 @@ public class CreationFeeGetter implements Listener {
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),