Skip to content

Commit

Permalink
Optimizations + Price format option
Browse files Browse the repository at this point in the history
- Optimizations (#47)
- Added missing settings
- Added price format option to add commas

Optimizations:
- Pagination no longer uses a new class all the time
- Pagination system now uses array_slice()
- DB is now updated when a listing is created or expired
- Other small things
  • Loading branch information
Shock95 committed Aug 18, 2020
1 parent 406b16b commit ae18be6
Show file tree
Hide file tree
Showing 30 changed files with 404 additions and 464 deletions.
2 changes: 0 additions & 2 deletions .poggit.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
--- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/Shock95x/AuctionHouse
branches:
- master
projects:
AuctionHouse:
path: ""
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ blacklist:
| /ah | AuctionHouse main command, opens the shop menu if there are no specified parameters |
| /ah shop | Opens the shop menu |
| /ah sell **[price]** | Allows player to list items in their hand on the auction house. **[price]** is the amount that the player is listing the item to sell for |
| /ah listings | Shows all active listings of the player|
| /ah listings **[player]**| Shows all active listings of a specific player|
| /ah admin | Opens the AuctionHouse admin menu (OP Command)|
| /ah reload | Allows player to reload the config and save the database (OP command) |
| /ah about | Shows AuctionHouse version the server is running and author of this plugin |
| /ah listings | Shows all active listings of the player |
| /ah listings **[player]**| Shows all active listings of a specific player |
| /ah admin | Opens the AuctionHouse admin menu (OP Command) |
| /ah reload | Allows player to reload configuration files (OP command) |
| /ah about | Shows AuctionHouse version the server is running |
---
## API
### Events
Expand All @@ -76,6 +76,8 @@ You can contribute to this project by creating a new language file and opening a

## Credits / Virions Used
- [InvMenu](https://github.com/Muqsit/InvMenu) (Muqsit)
- [libasynql](https://github.com/poggit/libasynql) (SOFe)
- [await-generator](https://github.com/SOF3/await-generator) (SOFe)
- [Commando](https://github.com/CortexPE/Commando) (CortexPE)
- [ConfigUpdater](https://github.com/JackMD/ConfigUpdater) (JackMD)
- [UpdateNotifier](https://github.com/JackMD/UpdateNotifier) (JackMD)
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ permissions:
description: Opens the AuctionHouse admin menu.
auctionhouse.command.reload:
default: op
description: Allows player to reload configuration files and save to database.
description: Allows player to reload configuration files.
4 changes: 3 additions & 1 deletion resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ creative-sale: false
max-items: 45
# Shows item lore on the auction house
show-lore: true
# Formats price with commas (ex. 1,000,000)
price-formatted: true
# Items that cannot be listed on the auction. Refer to https://minecraftitemids.com/ or https://minecraft-ids.grahamedgecombe.com/ for a list of item ids.
blacklist:
- '1000' #Example items, these items dont exist in MC, but you should use ones that do if you want.
- '1000' # Example items, these items dont exist in MC, but you should use ones that do if you want.
- '1001:12'
- '12333:4'

Expand Down
4 changes: 2 additions & 2 deletions resources/language/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ lang-version: 2

# The titles of the Auction House UI
menu-name: "&l&6Auction House"
listings-menu-name: "&6Active Listings"
expired-menu-name: "&6Cancelled / Expired Listings"
listings-menu-name: "&aActive Listings"
expired-menu-name: "&cCancelled / Expired Listings"
purchase-menu-name: "Confirm Purchase"
admin-menu-name: "&l&cAdmin Menu"
manage-listing-name: "&l&eManage Listing"
Expand Down
10 changes: 9 additions & 1 deletion resources/statements/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ DELETE FROM auctions
WHERE id = :id;
-- # }

-- # { expired
-- # :id string
-- # :expired bool
UPDATE auctions
SET expired = :expired
WHERE id = :id;
-- # }

-- # { insert
-- # :uuid string
-- # :username string
Expand All @@ -33,7 +41,7 @@ WHERE id = :id;
-- # :id int
-- # :end_time int
-- # :expired bool
REPLACE INTO auctions(uuid, username, price, nbt, id, end_time, expired) VALUES (:uuid, :username, :price, :nbt, :id, :end_time, :expired)
INSERT INTO auctions(uuid, username, price, nbt, id, end_time, expired) VALUES (:uuid, :username, :price, :nbt, :id, :end_time, :expired)
-- # }

-- # }
8 changes: 8 additions & 0 deletions resources/statements/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ DELETE FROM auctions
WHERE id = :id;
-- # }

-- # { expired
-- # :id string
-- # :expired bool
UPDATE auctions
SET expired = :expired
WHERE id = :id;
-- # }

-- # { insert
-- # :uuid string
-- # :username string
Expand Down
9 changes: 4 additions & 5 deletions src/shock95x/auctionhouse/AuctionHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,22 @@ public function onEnable() : void {
$this->getServer()->getPluginManager()->disablePlugin($this);
return;
}
Settings::setMonetaryUnit($this->getEconomyProvider()->getMonetaryUnit());
if($this->getServer()->getPluginManager()->getPlugin("InvCrashFix") == null) {
$this->getLogger()->warning("InvCrashFix is required to fix client crashes on 1.16, download it here: https://poggit.pmmp.io/ci/Muqsit/InvCrashFix");
}
$this->getServer()->getCommandMap()->register($this->getDescription()->getName(), new AHCommand($this, "ah", "AuctionHouse command"));
}

public function onDisable() {
if(!isset($this->database)) return;
$this->database->save();
$this->database->close();
if(isset($this->database)) {
$this->database->close();
}
}

public function reload() {
Locale::init($this);
$this->getConfig()->reload();
Settings::init($this->getConfig());
$this->database->save();
$this->getLogger()->info("Configuration files reloaded");
}

Expand Down
67 changes: 39 additions & 28 deletions src/shock95x/auctionhouse/auction/Listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,65 @@
namespace shock95x\auctionhouse\auction;

use pocketmine\item\Item;
use pocketmine\nbt\BigEndianNBTStream;
use pocketmine\nbt\tag\CompoundTag;
use shock95x\auctionhouse\AuctionHouse;
use shock95x\auctionhouse\database\utils\BinaryStringParserInstance;
use shock95x\auctionhouse\utils\Utils;
use shock95x\auctionhouse\utils\Settings;

class Listing {

private $data;
private $item;
private $id;
private $uuid;
private $price;
private $username;
private $endTime;
private $expired;

public function __construct(array $data, BinaryStringParserInstance $parser) {
$this->data = $data;
$nbt = (new BigEndianNBTStream())->readCompressed($parser->decode($data["nbt"]));
assert($nbt instanceof CompoundTag);
$this->item = Item::nbtDeserialize($nbt);
}
private $item;

public function __construct(int $id, string $uuid, int $price, string $username, int $endTime, bool $expired, Item $item) {
$this->id = $id;
$this->uuid = $uuid;
$this->price = $price;
$this->username = $username;
$this->endTime = $endTime;
$this->expired = $expired;
$this->item = $item;
}

public function getItem() : Item {
public function getItem(): Item {
return $this->item;
}

public function getPrice(bool $monetaryUnit = false) {
return $monetaryUnit ? AuctionHouse::getInstance()->economyProvider->getMonetaryUnit() . $this->data["price"] : $this->data["price"];
public function getPrice(bool $monetaryUnit = false, bool $formatted = false) {
$price = $this->price;
if($formatted) {
$price = number_format($price);
}
if($monetaryUnit) {
$price = Settings::getMonetaryUnit() . $price;
}
return $price;
}

public function getSeller(bool $uuid = false) : string {
return $uuid ? $this->data["uuid"] : $this->data["username"];
public function getSeller(bool $uuid = false): string {
return $uuid ? $this->uuid : $this->username;
}

public function getMarketId() : int {
return $this->data["id"];
public function getMarketId(): int {
return $this->id;
}

public function setEndTime() {
$this->data["end_time"] = Utils::getEndTime();
public function setEndTime(int $time): void {
$this->endTime = $time;
}

public function getEndTime() : int {
return $this->data["end_time"];
public function getEndTime(): int {
return $this->endTime;
}

public function setExpired() {
$this->data["expired"] = true;
public function setExpired(bool $expired = true): void {
$this->expired = $expired;
}

public function isExpired() {
return $this->data["expired"];
public function isExpired(): bool{
return $this->expired;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class AboutCommand extends BaseSubCommand {
protected function prepare(): void {}

public function onRun(CommandSender $sender, string $aliasUsed, array $args): void {
$sender->sendMessage(Utils::prefixMessage(TextFormat::BLUE . "This server is running AuctionHouse v" . $this->getPlugin()->getDescription()->getVersion() . " by Shock95x"));
$sender->sendMessage(Utils::prefixMessage(TextFormat::BLUE . "This server is running AuctionHouse " . TextFormat::GOLD . "v" . $this->getPlugin()->getDescription()->getVersion() . TextFormat::BLUE . " by " . TextFormat::GREEN . "Shock95x"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use pocketmine\item\Item;
use pocketmine\nbt\BigEndianNBTStream;
use pocketmine\Player;
use shock95x\auctionhouse\AuctionHouse;
use shock95x\auctionhouse\database\DataHolder;
Expand Down Expand Up @@ -57,7 +56,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo
}
if($listingPrice != 0) $this->getEconomy()->subtractMoney($sender, $listingPrice);
$sender->getInventory()->removeItem($item);
DataHolder::addListing($sender, (int) $price, (new BigEndianNBTStream())->writeCompressed($item->nbtSerialize()));
DataHolder::addListing($sender, $item, (int) $price);
$sender->sendMessage(str_replace(["@player", "@item", "@price", "@amount"], [$sender->getName(), $item->getName(), $this->getEconomy()->getMonetaryUnit() . $price, $item->getCount()], Locale::getMessage($sender, "item-listed", true)));
}

Expand Down
39 changes: 29 additions & 10 deletions src/shock95x/auctionhouse/database/DataHolder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace shock95x\auctionhouse\database;

use pocketmine\item\Item;
use pocketmine\nbt\BigEndianNBTStream;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\Player;
use shock95x\auctionhouse\auction\Listing;
use shock95x\auctionhouse\AuctionHouse;
Expand All @@ -15,20 +18,28 @@ class DataHolder {
private static $listings;
private static $database;

/** @var BigEndianNBTStream */
public static $endianStream;

public function __construct(Database $database) {
self::$database = $database;
self::$endianStream = new BigEndianNBTStream();
}

public function loadListings() {
self::$listings = array();
Await::f2c(function () {
$rows = (array) yield self::$database->fetchAll();
foreach($rows as $listing) {
self::$listings[] = new Listing($listing, self::$database->getParser());
$nbt = $listing["nbt"];
$nbt = self::$endianStream->readCompressed(self::$database->getParser()->decode($nbt));
if($nbt instanceof CompoundTag) {
self::$listings[] = new Listing($listing["id"], $listing["uuid"], $listing["price"], $listing["username"], $listing["end_time"], $listing["expired"], Item::nbtDeserialize($nbt));
}
}
});
DataHolder::$database->getConnector()->waitAll();
AuctionHouse::getInstance()->getScheduler()->scheduleRepeatingTask(new ListingExpireTask(DataHolder::$database), 6000);
self::$database->getConnector()->waitAll();
AuctionHouse::getInstance()->getScheduler()->scheduleRepeatingTask(new ListingExpireTask(), 1200);
}

/**
Expand Down Expand Up @@ -92,11 +103,14 @@ public static function getListingById(int $id) : ?Listing {
return null;
}

public static function addListing(Player $player, int $price, string $nbt) {
$listing = new Listing(["uuid" => $player->getRawUniqueId(), "username" => $player->getName(), "price" => $price, "nbt" => self::$database->getParser()->encode($nbt), "id" => time(), "end_time" => Utils::getEndTime(), "expired" => false], self::$database->getParser());
self::$listings[] = $listing;
(new AuctionStartEvent($listing))->call();
}
public static function addListing(Player $player, Item $item, int $price) {
$listing = new Listing(time(), $player->getRawUniqueId(), $price, $player->getName(), Utils::getEndTime(), false, $item);
self::$listings[] = $listing;

$nbt = self::$endianStream->writeCompressed($item->nbtSerialize());
self::$database->insert($listing->getSeller(true), $listing->getSeller(), $listing->getPrice(), $nbt, $listing->getEndTime(), $listing->isExpired(), $listing->getMarketId());
(new AuctionStartEvent($listing))->call();
}

/**
* @param bool $expired
Expand All @@ -105,7 +119,7 @@ public static function addListing(Player $player, int $price, string $nbt) {
public static function getListings(bool $expired = false) {
if(!$expired) {
$array = [];
foreach ((array)self::$listings as $listing) {
foreach ((array) self::$listings as $listing) {
if(!$listing->isExpired()) {
$array[] = $listing;
}
Expand All @@ -115,11 +129,16 @@ public static function getListings(bool $expired = false) {
return self::$listings;
}

public static function setExpired(Listing $auction, bool $expired = true) {
$auction->setExpired($expired);
self::$database->setExpired($auction->getMarketId(), $expired);
}

public static function removeAuction(Listing $auction) {
$index = array_search($auction, (array) self::$listings);
if($index !== false){
unset(self::$listings[$index]);
}
self::$database->deleteFromId($auction->getMarketId());
self::$database->delete($auction->getMarketId());
}
}
19 changes: 5 additions & 14 deletions src/shock95x/auctionhouse/database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
namespace shock95x\auctionhouse\database;

use Generator;
use pocketmine\nbt\BigEndianNBTStream;
use pocketmine\utils\Config;
use poggit\libasynql\DataConnector;
use poggit\libasynql\libasynql;
use poggit\libasynql\SqlError;
use shock95x\auctionhouse\AuctionHouse;
use shock95x\auctionhouse\database\utils\BinaryStringParser;
use shock95x\auctionhouse\database\utils\BinaryStringParserInstance;
use shock95x\auctionhouse\event\AuctionEndEvent;
use SOFe\AwaitGenerator\Await;

class Database {
Expand Down Expand Up @@ -65,17 +63,6 @@ protected function asyncSelect(string $query, array $args = []): Generator {
return yield Await::ONCE;
}

public function save() {
$list = DataHolder::getListings();
foreach($list as $listing) {
if(time() >= $listing->getEndTime()) {
$listing->setExpired();
(new AuctionEndEvent($listing, AuctionEndEvent::EXPIRED))->call();
}
$this->insert($listing->getSeller(true), $listing->getSeller(), $listing->getPrice(), (new BigEndianNBTStream())->writeCompressed($listing->getItem()->nbtSerialize()), $listing->getEndTime(), $listing->isExpired(), $listing->getMarketId());
}
}

/**
* @param string $uuid
* @param string $username
Expand All @@ -102,7 +89,11 @@ public function fetchAll() : Generator {
return yield Await::ONCE;
}

public function deleteFromId(string $id) {
public function setExpired(string $id, bool $expired = true) {
$this->database->executeGeneric(Query::EXPIRED, ["id" => $id, "expired" => $expired]);
}

public function delete(string $id) {
$this->database->executeGeneric(Query::DELETE, ["id" => $id]);
}

Expand Down
1 change: 1 addition & 0 deletions src/shock95x/auctionhouse/database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ class Query {
const INIT = "auctionhouse.init";
const INSERT = "auctionhouse.insert";
const DELETE = "auctionhouse.delete";
const EXPIRED = "auctionhouse.expired";
const FETCH_ALL = "auctionhouse.fetch.all";
}
Loading

0 comments on commit ae18be6

Please sign in to comment.