mirror of
https://github.com/kiranhart/Auction-House.git
synced 2025-02-16 19:21:20 +01:00
Auction end discord messages (for won by bid)
This commit is contained in:
parent
58e159f436
commit
7b290afcdb
@ -171,11 +171,11 @@ public class AuctionAPI {
|
||||
.addField(Settings.DISCORD_MSG_FIELD_SELLER_NAME.getString(), Settings.DISCORD_MSG_FIELD_SELLER_VALUE.getString().replace("%seller%", seller.getName() != null ? seller.getName() : "Player Lost o.O"), Settings.DISCORD_MSG_FIELD_SELLER_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_BUYER_NAME.getString(), isNew ? "No Buyer" : Settings.DISCORD_MSG_FIELD_BUYER_VALUE.getString().replace("%buyer%", buyer.getName() != null ? buyer.getName() : "Player Lost o.O"), Settings.DISCORD_MSG_FIELD_BUYER_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_BUY_NOW_PRICE_NAME.getString(), Settings.DISCORD_MSG_FIELD_BUY_NOW_PRICE_VALUE.getString().replace("%buy_now_price%", this.getFriendlyNumber(auctionItem.getBasePrice())), Settings.DISCORD_MSG_FIELD_BUY_NOW_PRICE_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_FINAL_PRICE_NAME.getString(), Settings.DISCORD_MSG_FIELD_FINAL_PRICE_VALUE.getString().replace("%final_price%", this.getFriendlyNumber(isBid ? auctionItem.getCurrentPrice() : auctionItem.getBasePrice())), Settings.DISCORD_MSG_FIELD_FINAL_PRICE_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_FINAL_PRICE_NAME.getString(), isNew ? "Not Sold" : Settings.DISCORD_MSG_FIELD_FINAL_PRICE_VALUE.getString().replace("%final_price%", this.getFriendlyNumber(isBid ? auctionItem.getCurrentPrice() : auctionItem.getBasePrice())), Settings.DISCORD_MSG_FIELD_FINAL_PRICE_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_IS_BID_NAME.getString(), Settings.DISCORD_MSG_FIELD_IS_BID_VALUE.getString().replace("%is_bid%", String.valueOf(isBid)), Settings.DISCORD_MSG_FIELD_IS_BID_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_PURCHASE_TYPE_NAME.getString(), isNew ? "Was not bought" : Settings.DISCORD_MSG_FIELD_PURCHASE_TYPE_VALUE.getString().replace("%purchase_type%", saleType == AuctionSaleType.USED_BIDDING_SYSTEM ? "Won Bid" : "Bought Immediately"), Settings.DISCORD_MSG_FIELD_PURCHASE_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_ITEM_NAME.getString(), Settings.DISCORD_MSG_FIELD_ITEM_VALUE.getString().replace("%item_name%", auctionItem.getItemName()), Settings.DISCORD_MSG_FIELD_ITEM_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_ITEM_AMOUNT_NAME.getString(), Settings.DISCORD_MSG_FIELD_ITEM_AMOUNT_VALUE.getString().replace("%item_value%", String.valueOf(this.deserializeItem(auctionItem.getRawItem()).getAmount())), Settings.DISCORD_MSG_FIELD_ITEM_AMOUNT_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_ITEM_NAME.getString(), auctionItem.getItemName(), Settings.DISCORD_MSG_FIELD_ITEM_INLINE.getBoolean())
|
||||
.addField(Settings.DISCORD_MSG_FIELD_ITEM_AMOUNT_NAME.getString(), Settings.DISCORD_MSG_FIELD_ITEM_AMOUNT_VALUE.getString().replace("%item_amount%", String.valueOf(this.deserializeItem(auctionItem.getRawItem()).getAmount())), Settings.DISCORD_MSG_FIELD_ITEM_AMOUNT_INLINE.getBoolean())
|
||||
);
|
||||
|
||||
try {
|
||||
|
@ -1,6 +1,11 @@
|
||||
package ca.tweetzy.auctionhouse.api.events;
|
||||
|
||||
import ca.tweetzy.auctionhouse.auction.AuctionItem;
|
||||
import ca.tweetzy.auctionhouse.auction.AuctionSaleType;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
@ -11,28 +16,25 @@ import org.bukkit.event.HandlerList;
|
||||
* Time Created: 9:01 p.m.
|
||||
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
|
||||
*/
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
public class AuctionEndEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private boolean cancelled;
|
||||
|
||||
private OfflinePlayer originalOwner;
|
||||
private OfflinePlayer buyer;
|
||||
private AuctionItem auctionItem;
|
||||
private AuctionSaleType saleType;
|
||||
|
||||
public AuctionEndEvent(AuctionItem auctionItem) {
|
||||
public AuctionEndEvent(OfflinePlayer originalOwner, OfflinePlayer buyer, AuctionItem auctionItem, AuctionSaleType saleType) {
|
||||
super(true);
|
||||
this.originalOwner = originalOwner;
|
||||
this.buyer = buyer;
|
||||
this.auctionItem = auctionItem;
|
||||
}
|
||||
|
||||
public AuctionItem getAuctionItem() {
|
||||
return auctionItem;
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
this.saleType = saleType;
|
||||
}
|
||||
|
||||
public HandlerList getHandlers() {
|
||||
|
@ -65,8 +65,8 @@ public class AuctionItem implements Serializable {
|
||||
|
||||
public String getItemName() {
|
||||
ItemStack stack = AuctionAPI.getInstance().deserializeItem(this.rawItem);
|
||||
if (stack == null) return "";
|
||||
if (!stack.hasItemMeta()) return "";
|
||||
if (stack == null) return "Invalid Item";
|
||||
if (!stack.hasItemMeta()) return WordUtils.capitalize(stack.getType().name().toLowerCase().replace("_", " "));
|
||||
return stack.getItemMeta().hasDisplayName() ? ChatColor.stripColor(stack.getItemMeta().getDisplayName()) : WordUtils.capitalize(stack.getType().name().toLowerCase().replace("_", " "));
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ca.tweetzy.auctionhouse.commands;
|
||||
|
||||
import ca.tweetzy.auctionhouse.AuctionHouse;
|
||||
import ca.tweetzy.auctionhouse.api.events.AuctionStartEvent;
|
||||
import ca.tweetzy.auctionhouse.auction.AuctionItem;
|
||||
import ca.tweetzy.auctionhouse.auction.AuctionPlayer;
|
||||
import ca.tweetzy.auctionhouse.helpers.MaterialCategorizer;
|
||||
@ -12,6 +13,7 @@ import ca.tweetzy.core.compatibility.XMaterial;
|
||||
import ca.tweetzy.core.utils.NumberUtils;
|
||||
import ca.tweetzy.core.utils.PlayerUtils;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -87,8 +89,7 @@ public class CommandSell extends AbstractCommand {
|
||||
return ReturnType.FAILURE;
|
||||
}
|
||||
|
||||
// list the item
|
||||
AuctionHouse.getInstance().getAuctionItemManager().addItem(new AuctionItem(
|
||||
AuctionItem item = new AuctionItem(
|
||||
player.getUniqueId(),
|
||||
player.getUniqueId(),
|
||||
itemToSell,
|
||||
@ -100,7 +101,14 @@ public class CommandSell extends AbstractCommand {
|
||||
basePrice,
|
||||
allowedTime,
|
||||
false
|
||||
));
|
||||
);
|
||||
|
||||
AuctionStartEvent startEvent = new AuctionStartEvent(player, item);
|
||||
Bukkit.getServer().getPluginManager().callEvent(startEvent);
|
||||
if (startEvent.isCancelled()) return ReturnType.FAILURE;
|
||||
|
||||
// list the item
|
||||
AuctionHouse.getInstance().getAuctionItemManager().addItem(item);
|
||||
|
||||
AuctionHouse.getInstance().getLocale().getMessage("auction.listed.nobid")
|
||||
.processPlaceholder("amount", itemToSell.getAmount())
|
||||
@ -162,7 +170,7 @@ public class CommandSell extends AbstractCommand {
|
||||
return ReturnType.FAILURE;
|
||||
}
|
||||
|
||||
AuctionHouse.getInstance().getAuctionItemManager().addItem(new AuctionItem(
|
||||
AuctionItem item = new AuctionItem(
|
||||
player.getUniqueId(),
|
||||
player.getUniqueId(),
|
||||
itemToSell,
|
||||
@ -174,7 +182,13 @@ public class CommandSell extends AbstractCommand {
|
||||
bidStartPrice,
|
||||
allowedTime,
|
||||
false
|
||||
));
|
||||
);
|
||||
|
||||
AuctionStartEvent startEvent = new AuctionStartEvent(player, item);
|
||||
Bukkit.getServer().getPluginManager().callEvent(startEvent);
|
||||
if (startEvent.isCancelled()) return ReturnType.FAILURE;
|
||||
|
||||
AuctionHouse.getInstance().getAuctionItemManager().addItem(item);
|
||||
|
||||
AuctionHouse.getInstance().getLocale().getMessage("auction.listed.withbid")
|
||||
.processPlaceholder("amount", itemToSell.getAmount())
|
||||
|
@ -20,16 +20,15 @@ public class AuctionListeners implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onAuctionStart(AuctionStartEvent e) {
|
||||
if (Settings.DISCORD_ALERT_ON_AUCTION_START.getBoolean()) {
|
||||
Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(AuctionHouse.getInstance(), () -> {
|
||||
Settings.DISCORD_WEBHOOKS.getStringList().forEach(hook -> AuctionAPI.getInstance().sendDiscordMessage(hook, e.getSeller(), e.getSeller(), e.getAuctionItem(), AuctionSaleType.USED_BIDDING_SYSTEM, true, e.getAuctionItem().getBidStartPrice() >= Settings.MIN_AUCTION_START_PRICE.getDouble()));
|
||||
}, 0L);
|
||||
if (Settings.DISCORD_ENABLED.getBoolean() && Settings.DISCORD_ALERT_ON_AUCTION_START.getBoolean()) {
|
||||
Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(AuctionHouse.getInstance(), () -> Settings.DISCORD_WEBHOOKS.getStringList().forEach(hook -> AuctionAPI.getInstance().sendDiscordMessage(hook, e.getSeller(), e.getSeller(), e.getAuctionItem(), AuctionSaleType.USED_BIDDING_SYSTEM, true, e.getAuctionItem().getBidStartPrice() >= Settings.MIN_AUCTION_START_PRICE.getDouble())), 1L);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onAuctionEnd(AuctionEndEvent e) {
|
||||
|
||||
if (Settings.DISCORD_ENABLED.getBoolean() && Settings.DISCORD_ALERT_ON_AUCTION_FINISH.getBoolean()) {
|
||||
Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(AuctionHouse.getInstance(), () -> Settings.DISCORD_WEBHOOKS.getStringList().forEach(hook -> AuctionAPI.getInstance().sendDiscordMessage(hook, e.getOriginalOwner(), e.getBuyer(), e.getAuctionItem(), e.getSaleType(), false, e.getSaleType() == AuctionSaleType.USED_BIDDING_SYSTEM)), 1L);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ public class Settings {
|
||||
public static final ConfigSetting DISCORD_ENABLED = new ConfigSetting(config, "discord.enabled", true, "Should the discord webhook feature be enabled?");
|
||||
public static final ConfigSetting DISCORD_ALERT_ON_AUCTION_START = new ConfigSetting(config, "discord.alert on auction start", true, "Should a message be sent to the discord server when someone lists a new auction item");
|
||||
public static final ConfigSetting DISCORD_ALERT_ON_AUCTION_FINISH = new ConfigSetting(config, "discord.alert on auction finish", true, "Should a message when an auction finishes?");
|
||||
public static final ConfigSetting DISCORD_WEBHOOKS = new ConfigSetting(config, "discord.webhooks", Collections.singletonList(""), "A list of webhook urls (channels) you want a message sent to");
|
||||
public static final ConfigSetting DISCORD_WEBHOOKS = new ConfigSetting(config, "discord.webhooks", Collections.singletonList("https://discord.com/api/webhooks/821837927444119563/Yd3cWzVB56Tk_VuN1Lv2iGgvsbZt2YV5SDyCkVo6EjRAUqJk3nA2nSG9PH_Bl6rcFNnz"), "A list of webhook urls (channels) you want a message sent to");
|
||||
public static final ConfigSetting DISCORD_MSG_USERNAME = new ConfigSetting(config, "discord.user.username", "Auction House", "The name of the user who will send the message");
|
||||
public static final ConfigSetting DISCORD_MSG_PFP = new ConfigSetting(config, "discord.user.avatar picture", "https://cdn.kiranhart.com/spigot/auctionhouse/icon.png", "The name of the user who will send the message");
|
||||
public static final ConfigSetting DISCORD_MSG_USE_RANDOM_COLOUR = new ConfigSetting(config, "discord.msg.use random colour", true, "The name of the user who will send the message");
|
||||
@ -69,7 +69,7 @@ public class Settings {
|
||||
public static final ConfigSetting DISCORD_MSG_FIELD_FINAL_PRICE_INLINE = new ConfigSetting(config, "discord.msg.final price.inline", true);
|
||||
|
||||
public static final ConfigSetting DISCORD_MSG_FIELD_IS_BID_NAME = new ConfigSetting(config, "discord.msg.is bid.name", "Was Bid");
|
||||
public static final ConfigSetting DISCORD_MSG_FIELD_IS_BID_VALUE = new ConfigSetting(config, "discord.msg.is bid.value", "%is_bid");
|
||||
public static final ConfigSetting DISCORD_MSG_FIELD_IS_BID_VALUE = new ConfigSetting(config, "discord.msg.is bid.value", "%is_bid%");
|
||||
public static final ConfigSetting DISCORD_MSG_FIELD_IS_BID_INLINE = new ConfigSetting(config, "discord.msg.is bid.inline", true);
|
||||
|
||||
public static final ConfigSetting DISCORD_MSG_FIELD_PURCHASE_TYPE_NAME = new ConfigSetting(config, "discord.msg.purchase type.name", "Purchase Type");
|
||||
|
@ -3,6 +3,7 @@ package ca.tweetzy.auctionhouse.tasks;
|
||||
import ca.tweetzy.auctionhouse.AuctionHouse;
|
||||
import ca.tweetzy.auctionhouse.api.AuctionAPI;
|
||||
import ca.tweetzy.auctionhouse.api.events.AuctionEndEvent;
|
||||
import ca.tweetzy.auctionhouse.auction.AuctionSaleType;
|
||||
import ca.tweetzy.auctionhouse.settings.Settings;
|
||||
import ca.tweetzy.core.utils.PlayerUtils;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
@ -49,21 +50,22 @@ public class TickAuctionsTask extends BukkitRunnable {
|
||||
|
||||
AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().stream().filter(item -> item.getRemainingTime() <= 0).collect(Collectors.toList()).iterator().forEachRemaining(item -> {
|
||||
// call the auction end event
|
||||
AuctionEndEvent auctionEndEvent = new AuctionEndEvent(item);
|
||||
AuctionHouse.getInstance().getServer().getPluginManager().callEvent(auctionEndEvent);
|
||||
AuctionEndEvent auctionEndEvent = null;
|
||||
|
||||
// if the event is cancelled then stop
|
||||
if (!auctionEndEvent.isCancelled()) {
|
||||
// check if the auction item owner is the same as the highest bidder
|
||||
if (item.getOwner().equals(item.getHighestBidder())) {
|
||||
// was not sold
|
||||
item.setExpired(true);
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
|
||||
} else {
|
||||
// the item was sold ?? then do the checks
|
||||
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(item.getHighestBidder());
|
||||
if (offlinePlayer.isOnline()) {
|
||||
if (AuctionHouse.getInstance().getEconomy().has(offlinePlayer, item.getCurrentPrice())) {
|
||||
// check if the auction item owner is the same as the highest bidder
|
||||
if (item.getOwner().equals(item.getHighestBidder())) {
|
||||
// was not sold
|
||||
item.setExpired(true);
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
|
||||
} else {
|
||||
// the item was sold ?? then do the checks
|
||||
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(item.getHighestBidder());
|
||||
if (offlinePlayer.isOnline()) {
|
||||
if (AuctionHouse.getInstance().getEconomy().has(offlinePlayer, item.getCurrentPrice())) {
|
||||
auctionEndEvent = new AuctionEndEvent(Bukkit.getOfflinePlayer(item.getOwner()), offlinePlayer, item, AuctionSaleType.USED_BIDDING_SYSTEM);
|
||||
AuctionHouse.getInstance().getServer().getPluginManager().callEvent(auctionEndEvent);
|
||||
|
||||
if (!auctionEndEvent.isCancelled()) {
|
||||
// since they're online, try to add the item to their inventory
|
||||
PlayerUtils.giveItem(offlinePlayer.getPlayer(), AuctionAPI.getInstance().deserializeItem(item.getRawItem()));
|
||||
// withdraw money and give to the owner
|
||||
@ -87,28 +89,35 @@ public class TickAuctionsTask extends BukkitRunnable {
|
||||
|
||||
AuctionHouse.getInstance().getAuctionItemManager().removeItem(item.getKey());
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, false);
|
||||
} else {
|
||||
// they don't have enough money to buy it, so send it back to the original owner
|
||||
item.setExpired(true);
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
|
||||
}
|
||||
|
||||
} else {
|
||||
// offline, so save their purchase in the collection inventory
|
||||
if (AuctionHouse.getInstance().getEconomy().has(offlinePlayer, item.getCurrentPrice())) {
|
||||
// they don't have enough money to buy it, so send it back to the original owner
|
||||
item.setExpired(true);
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
|
||||
}
|
||||
} else {
|
||||
// offline, so save their purchase in the collection inventory
|
||||
if (AuctionHouse.getInstance().getEconomy().has(offlinePlayer, item.getCurrentPrice())) {
|
||||
auctionEndEvent = new AuctionEndEvent(Bukkit.getOfflinePlayer(item.getOwner()), offlinePlayer, item, AuctionSaleType.USED_BIDDING_SYSTEM);
|
||||
AuctionHouse.getInstance().getServer().getPluginManager().callEvent(auctionEndEvent);
|
||||
|
||||
if (!auctionEndEvent.isCancelled()) {
|
||||
// withdraw money and give to the owner
|
||||
AuctionHouse.getInstance().getEconomy().withdrawPlayer(offlinePlayer, item.getCurrentPrice());
|
||||
AuctionHouse.getInstance().getEconomy().depositPlayer(Bukkit.getOfflinePlayer(item.getOwner()), item.getCurrentPrice());
|
||||
item.setOwner(offlinePlayer.getUniqueId());
|
||||
item.setExpired(true);
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
|
||||
} else {
|
||||
// they don't have enough money to buy it, so send it back to the original owner
|
||||
item.setExpired(true);
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
|
||||
}
|
||||
} else {
|
||||
// they don't have enough money to buy it, so send it back to the original owner
|
||||
item.setExpired(true);
|
||||
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
|
Loading…
Reference in New Issue
Block a user