sound manager, item loading and saving, auction window pagination

This commit is contained in:
Kiran Hart 2021-02-11 16:59:41 -05:00
parent 3719443666
commit 5c98e852ff
7 changed files with 193 additions and 44 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target/
.idea
/src/test/

View File

@ -96,6 +96,7 @@ public class AuctionHouse extends TweetyPlugin {
@Override
public void onPluginDisable() {
this.auctionPlayerManager.closeAuctionHouse();
this.auctionItemManager.saveItems();
instance = null;
}

View File

@ -58,19 +58,6 @@ public class AuctionItem {
this.remainingTime = remainingTime;
}
public AuctionItem(UUID owner, UUID highestBidder, ItemStack originalItem, AuctionItemCategory category, double basePrice, double bidStartPrice, double bidIncPrice, double currentPrice, int remainingTime) {
this.owner = owner;
this.highestBidder = highestBidder;
this.originalItem = originalItem;
this.category = category;
this.key = UUID.randomUUID();
this.basePrice = basePrice;
this.bidStartPrice = bidStartPrice;
this.bidIncPrice = bidIncPrice;
this.currentPrice = currentPrice;
this.remainingTime = remainingTime;
}
public void updateRemainingTime(int removeAmount) {
this.remainingTime = Math.max(this.remainingTime - removeAmount, 0);
}
@ -101,41 +88,66 @@ public class AuctionItem {
long[] times = AuctionAPI.getInstance().getRemainingTimeValues(this.remainingTime);
if (type == AuctionStackType.MAIN_AUCTION_HOUSE) {
Settings.AUCTION_ITEM_AUCTION_STACK.getStringList().forEach(line -> {
lore.add(TextUtils.formatText(line
.replace("%seller%", theSeller)
.replace("%buynowprice%", basePrice)
.replace("%currentprice%", currentPrice)
.replace("%bidincrement%", bidIncPrice)
.replace("%highestbidder%", highestBidder)
.replace("%remaining_days%", String.valueOf(times[0]))
.replace("%remaining_hours%", String.valueOf(times[1]))
.replace("%remaining_minutes%", String.valueOf(times[2]))
.replace("%remaining_seconds%", String.valueOf(times[3]))
));
});
if (this.bidStartPrice <= 0) {
Settings.AUCTION_ITEM_AUCTION_STACK.getStringList().forEach(line -> {
lore.add(TextUtils.formatText(line
.replace("%seller%", theSeller)
.replace("%buynowprice%", basePrice)
.replace("%remaining_days%", String.valueOf(times[0]))
.replace("%remaining_hours%", String.valueOf(times[1]))
.replace("%remaining_minutes%", String.valueOf(times[2]))
.replace("%remaining_seconds%", String.valueOf(times[3]))
));
});
} else {
Settings.AUCTION_ITEM_AUCTION_STACK_WITH_BID.getStringList().forEach(line -> {
lore.add(TextUtils.formatText(line
.replace("%seller%", theSeller)
.replace("%buynowprice%", basePrice)
.replace("%currentprice%", currentPrice)
.replace("%bidincrement%", bidIncPrice)
.replace("%highestbidder%", highestBidder)
.replace("%remaining_days%", String.valueOf(times[0]))
.replace("%remaining_hours%", String.valueOf(times[1]))
.replace("%remaining_minutes%", String.valueOf(times[2]))
.replace("%remaining_seconds%", String.valueOf(times[3]))
));
});
}
lore.addAll(this.bidStartPrice <= 0 ? Settings.AUCTION_PURCHASE_CONTROLS_BID_OFF.getStringList().stream().map(TextUtils::formatText).collect(Collectors.toList()) : Settings.AUCTION_PURCHASE_CONTROLS_BID_ON.getStringList().stream().map(TextUtils::formatText).collect(Collectors.toList()));
} else {
Settings.AUCTION_ITEM_LISTING_STACK.getStringList().forEach(line -> {
lore.add(TextUtils.formatText(line
.replace("%seller%", theSeller)
.replace("%buynowprice%", basePrice)
.replace("%currentprice%", currentPrice)
.replace("%bidincrement%", bidIncPrice)
.replace("%highestbidder%", highestBidder)
.replace("%remaining_days%", String.valueOf(times[0]))
.replace("%remaining_hours%", String.valueOf(times[1]))
.replace("%remaining_minutes", String.valueOf(times[2]))
.replace("%remaining_seconds", String.valueOf(times[3]))
));
});
if (this.bidStartPrice <= 0) {
Settings.AUCTION_ITEM_LISTING_STACK.getStringList().forEach(line -> {
lore.add(TextUtils.formatText(line
.replace("%seller%", theSeller)
.replace("%buynowprice%", basePrice)
.replace("%remaining_days%", String.valueOf(times[0]))
.replace("%remaining_hours%", String.valueOf(times[1]))
.replace("%remaining_minutes%", String.valueOf(times[2]))
.replace("%remaining_seconds%", String.valueOf(times[3]))
));
});
} else {
Settings.AUCTION_ITEM_LISTING_STACK_WITH_BID.getStringList().forEach(line -> {
lore.add(TextUtils.formatText(line
.replace("%seller%", theSeller)
.replace("%buynowprice%", basePrice)
.replace("%currentprice%", currentPrice)
.replace("%bidincrement%", bidIncPrice)
.replace("%highestbidder%", highestBidder)
.replace("%remaining_days%", String.valueOf(times[0]))
.replace("%remaining_hours%", String.valueOf(times[1]))
.replace("%remaining_minutes%", String.valueOf(times[2]))
.replace("%remaining_seconds%", String.valueOf(times[3]))
));
});
}
}
meta.setLore(lore);
itemStack.setItemMeta(meta);
// itemStack = NBTEditor.set(itemStack, getKey(), "AuctionItemKey");
// itemStack = NBTEditor.set(itemStack, this.bidStartPrice <= 0 ? "NOT_BID_ITEM" : "IS_BID_ITEM", "AuctionItemType");
return itemStack;
}
}

View File

@ -5,6 +5,7 @@ import ca.tweetzy.auctionhouse.auction.AuctionItem;
import ca.tweetzy.auctionhouse.auction.AuctionPlayer;
import ca.tweetzy.auctionhouse.auction.AuctionStackType;
import ca.tweetzy.auctionhouse.helpers.ConfigurationItemHelper;
import ca.tweetzy.auctionhouse.managers.SoundManager;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.core.inventory.TInventory;
import com.google.common.collect.Lists;
@ -29,9 +30,10 @@ public class AuctionHouseGUI extends TInventory {
public AuctionHouseGUI(AuctionPlayer auctionPlayer) {
this.auctionPlayer = auctionPlayer;
this.auctionPlayer.setViewingAuctionHouse(true);
this.items = Lists.partition(AuctionHouse.getInstance().getAuctionItemManager().getFilteredItems(auctionPlayer.getPreferredCategory()), 45);
setTitle(Settings.GUI_AUCTION_HOUSE_TITLE.getString());
setPage(1);
setPage(auctionPlayer.getCurrentAuctionPage());
setRows(6);
setDynamic(false);
}
@ -39,6 +41,35 @@ public class AuctionHouseGUI extends TInventory {
@Override
public void onClick(InventoryClickEvent e, int slot) {
e.setCancelled(true);
switch (slot) {
case 45:
break;
case 46:
break;
case 48:
if (getPage() > 1) {
auctionPlayer.setCurrentAuctionPage(auctionPlayer.getCurrentAuctionPage() - 1);
auctionPlayer.getPlayer().openInventory(new AuctionHouseGUI(auctionPlayer).getInventory());
SoundManager.getInstance().playSound(auctionPlayer.getPlayer(), Settings.SOUNDS_NAVIGATE_GUI_PAGES.getString(), 1f, 1f);
}
break;
case 49:
auctionPlayer.setCurrentAuctionPage(1);
auctionPlayer.getPlayer().openInventory(new AuctionHouseGUI(auctionPlayer).getInventory());
break;
case 50:
if (getPage() < this.items.size()) {
auctionPlayer.setCurrentAuctionPage(auctionPlayer.getCurrentAuctionPage() + 1);
auctionPlayer.getPlayer().openInventory(new AuctionHouseGUI(auctionPlayer).getInventory());
SoundManager.getInstance().playSound(auctionPlayer.getPlayer(), Settings.SOUNDS_NAVIGATE_GUI_PAGES.getString(), 1f, 1f);
}
break;
case 51:
break;
default:
break;
}
}
@Override

View File

@ -4,11 +4,14 @@ import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.auction.AuctionItem;
import ca.tweetzy.auctionhouse.auction.AuctionItemCategory;
import ca.tweetzy.auctionhouse.helpers.MaterialCategorizer;
import ca.tweetzy.core.utils.TextUtils;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
@ -23,11 +26,55 @@ public class AuctionItemManager {
private final LinkedList<AuctionItem> auctionItems = new LinkedList<>();
public void loadItems() {
ConfigurationSection section = AuctionHouse.getInstance().getData().getConfigurationSection("auction items");
if (section == null || section.getKeys(false).size() == 0) return;
for (String nodes : section.getKeys(false)) {
String key = AuctionHouse.getInstance().getData().getString("auction items." + nodes + ".key");
double basePrice = AuctionHouse.getInstance().getData().getDouble("auction items." + nodes + ".base price");
double bidStartPrice = AuctionHouse.getInstance().getData().getDouble("auction items." + nodes + ".bid start price");
double bidIncrementPrice = AuctionHouse.getInstance().getData().getDouble("auction items." + nodes + ".bid increment price");
double currentPrice = AuctionHouse.getInstance().getData().getDouble("auction items." + nodes + ".current price");
auctionItems.add(new AuctionItem(
UUID.fromString(AuctionHouse.getInstance().getData().getString("auction items." + nodes + ".owner")),
UUID.fromString(AuctionHouse.getInstance().getData().getString("auction items." + nodes + ".highest bidder")),
AuctionHouse.getInstance().getData().getItemStack("auction items." + nodes + ".item"),
AuctionItemCategory.valueOf(AuctionHouse.getInstance().getData().getString("auction items." + nodes + ".category")),
UUID.fromString(key),
basePrice,
bidStartPrice,
bidIncrementPrice,
currentPrice,
AuctionHouse.getInstance().getData().getInt("auction items." + nodes + ".remaining time")
));
Bukkit.getConsoleSender().sendMessage(TextUtils.formatText(String.format("&aLoaded Item&f: &e%s &f(&a$%s&f) (&a$%.2f&f) (&a$%.2f&f) (&a$%.2f&f)", key, basePrice, bidStartPrice, bidIncrementPrice, currentPrice)));
}
AuctionHouse.getInstance().getData().set("auction items", null);
AuctionHouse.getInstance().getData().save();
}
public void saveItems() {
auctionItems.forEach(auctionItem -> {
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".owner", auctionItem.getOwner().toString());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".highest bidder", auctionItem.getHighestBidder().toString());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".category", auctionItem.getCategory().name());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".key", auctionItem.getKey().toString());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".base price", auctionItem.getBasePrice());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".bid start price", auctionItem.getBidStartPrice());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".bid increment price", auctionItem.getBidIncPrice());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".current price", auctionItem.getCurrentPrice());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".remaining time", auctionItem.getRemainingTime());
AuctionHouse.getInstance().getData().set("auction items." + auctionItem.getKey().toString() + ".item", auctionItem.getOriginalItem());
});
AuctionHouse.getInstance().getData().save();
}
public List<AuctionItem> getFilteredItems(AuctionItemCategory category) {
switch(category){
switch (category) {
case BLOCKS:
return auctionItems.stream().filter(auctionItem -> MaterialCategorizer.getMaterialCategory(auctionItem.getOriginalItem()) == AuctionItemCategory.BLOCKS).collect(Collectors.toList());
case ARMOR:
@ -44,7 +91,7 @@ public class AuctionItemManager {
public void addItem(AuctionItem item) {
Bukkit.getServer().getScheduler().runTaskAsynchronously(AuctionHouse.getInstance(), () -> {
auctionItems.add(item);
auctionItems.addFirst(item);
});
}
}

View File

@ -0,0 +1,42 @@
package ca.tweetzy.auctionhouse.managers;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.core.compatibility.XSound;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.Arrays;
/**
* The current file has been created by Kiran Hart
* Date Created: February 11 2021
* Time Created: 3:30 p.m.
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
*/
public class SoundManager {
private static SoundManager instance;
public static SoundManager getInstance() {
if (instance == null) {
instance = new SoundManager();
}
return instance;
}
private SoundManager(){}
public void playSound(Player player, String sound, float volume, float pitch) {
player.playSound(player.getLocation(), XSound.matchXSound(sound).get().parseSound(), volume, pitch);
}
public void playSound(Player[] players, String sound, float volume, float pitch) {
Arrays.stream(players).forEach(p -> p.playSound(p.getLocation(), XSound.matchXSound(sound).get().parseSound(), volume, pitch));
}
public void playSound(Player player, String sound, float volume, float pitch, int delay) {
Bukkit.getServer().getScheduler().runTaskLater(AuctionHouse.getInstance(), () -> playSound(player, sound, volume, delay), delay);
}
}

View File

@ -132,6 +132,21 @@ public class Settings {
* AUCTION STACKS
* ===============================*/
public static final ConfigSetting AUCTION_ITEM_AUCTION_STACK = new ConfigSetting(config, "auction items.auction stack", Arrays.asList(
"&7-------------------------",
"&eSeller&f: &b%seller%",
"",
"&eBuy Now: &a$%buynowprice%",
"",
"&eTime Left: &b%remaining_days%&f days &b%remaining_hours%&f hours &b%remaining_minutes%&f minutes &b%remaining_seconds% seconds"
), "This the item stack lore that will be appended to", "auction items in /ah (lore will be applied first, then these)");
public static final ConfigSetting AUCTION_ITEM_LISTING_STACK = new ConfigSetting(config, "auction items.listing stack", Arrays.asList(
"&7-------------------------",
"&eBuy Now: &a$%buynowprice%",
"&eTime Left: &b%remaining_days%&f:&b%remaining_hours%&f:&b%remaining_minutes%&f:&b%remaining_seconds%"
), "This the item stack lore that will be appended to", "auction items in /ah listings (lore will be applied first, then these)");
public static final ConfigSetting AUCTION_ITEM_AUCTION_STACK_WITH_BID = new ConfigSetting(config, "auction items.auction stack with bid", Arrays.asList(
"&7-------------------------",
"&eSeller&f: &b%seller%",
"",
@ -143,7 +158,7 @@ public class Settings {
"&eTime Left: &b%remaining_days%&f days &b%remaining_hours%&f hours &b%remaining_minutes%&f minutes &b%remaining_seconds% seconds"
), "This the item stack lore that will be appended to", "auction items in /ah (lore will be applied first, then these)");
public static final ConfigSetting AUCTION_ITEM_LISTING_STACK = new ConfigSetting(config, "auction items.listing stack", Arrays.asList(
public static final ConfigSetting AUCTION_ITEM_LISTING_STACK_WITH_BID = new ConfigSetting(config, "auction items.listing stack with bid", Arrays.asList(
"&7-------------------------",
"&eBuy Now: &a$%buynowprice%",
"&eCurrent Price: &a$%currentprice%",