Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UpgradeBuyEvent Cancellable #313

Merged
merged 3 commits into from
Jan 29, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
import com.andrei1058.bedwars.api.arena.team.ITeam;
import com.andrei1058.bedwars.api.upgrades.TeamUpgrade;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

public class UpgradeBuyEvent extends Event {
public class UpgradeBuyEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS = new HandlerList();

private TeamUpgrade teamUpgrade;
private Player player;
private final IArena arena;
private ITeam team;
private boolean cancelled = false;

/**
* Called when a Team Upgrade is bought
Expand Down Expand Up @@ -72,4 +74,18 @@ public HandlerList getHandlers() {
public static HandlerList getHandlerList() {
return HANDLERS;
}

@Override
public boolean isCancelled() {
return cancelled;
}

/**
* Cancel event. Purchase will be cancelled.
*/
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.bukkit.Sound;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -270,6 +271,10 @@ public void onClick(Player player, ClickType clickType, ITeam team) {
return;
}

final UpgradeBuyEvent event;
Bukkit.getPluginManager().callEvent(event = new UpgradeBuyEvent(this, player, team));
if(event.isCancelled()) return;

if (currency == Material.AIR) {
BedWars.getEconomy().buyAction(player, money);
} else {
Expand All @@ -281,7 +286,6 @@ public void onClick(Player player, ClickType clickType, ITeam team) {
p1.sendMessage(Language.getMsg(p1, Messages.UPGRADES_UPGRADE_BOUGHT_CHAT).replace("{playername}", player.getName()).replace("{player}", player.getDisplayName()).replace("{upgradeName}",
ChatColor.stripColor(Language.getMsg(p1, Messages.UPGRADES_BASE_TRAP_ITEM_NAME_PATH + getName().replace("base-trap-", "")).replace("{color}", ""))));
}
Bukkit.getPluginManager().callEvent(new UpgradeBuyEvent(this, player, team));
UpgradesManager.getMenuForArena(team.getArena()).open(player);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public void onClick(Player player, ClickType clickType, ITeam team) {
return;
}

final UpgradeBuyEvent event;
Bukkit.getPluginManager().callEvent(event = new UpgradeBuyEvent(this, player, team));
if(event.isCancelled()) return;

if (ut.getCurrency() == Material.AIR) {
BedWars.getEconomy().buyAction(player, ut.getCost());
} else {
Expand All @@ -150,7 +154,6 @@ public void onClick(Player player, ClickType clickType, ITeam team) {
ChatColor.stripColor(Language.getMsg(p1, Messages.UPGRADES_UPGRADE_TIER_ITEM_NAME.replace("{name}", getName()
.replace("upgrade-", "")).replace("{tier}", ut.getName())))).replace("{color}", ""));
}
Bukkit.getPluginManager().callEvent(new UpgradeBuyEvent(this, player, team));

ImmutableMap<Integer, MenuContent> menuContentBySlot = UpgradesManager.getMenuForArena(Arena.getArenaByPlayer(player)).getMenuContentBySlot();
Inventory inv = player.getOpenInventory().getTopInventory();
Expand Down