Skip to content

Instantly share code, notes, and snippets.

@Thunder33345
Created March 24, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thunder33345/96eb95f75f3c087e3cd342e7d2583209 to your computer and use it in GitHub Desktop.
Save Thunder33345/96eb95f75f3c087e3cd342e7d2583209 to your computer and use it in GitHub Desktop.
Commodity Market Lite
<?php
/** Created By Thunder33345 **/
namespace Thunder33345\CommodityMarketLite;
use pocketmine\event\Listener;
use pocketmine\plugin\PluginBase;
class Main extends PluginBase implements Listener
{
private $market;
public function onLoad()
{
}
public function onEnable()
{
//$this->getServer()->getPluginManager()->registerEvents($this, $this);
//$this->getServer()->getPluginManager()->registerEvents(new Market($this), $this);
$this->market = new Market($this);
}
public function onDisable()
{
}
}
<?php
/** Created By Thunder33345 **/
namespace Thunder33345\CommodityMarketLite;
use onebone\economyapi\EconomyAPI;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\tile\Sign;
use pocketmine\utils\TextFormat;
class Market implements Listener
{
private $main, $priceHandler, $api, $tax = 100;
public function __construct(Main $main)
{
$main->getServer()->getPluginManager()->registerEvents($this, $main);
$this->main = $main;
$this->priceHandler = new PriceHandler($main, $this);
$this->api = EconomyAPI::getInstance();
}
public function updateSign()
{
$sp = $this->priceHandler->getPrice() - $this->tax;
$bp = $this->priceHandler->getPrice() + $this->tax;
$main = $this->main;
$lv = $main->getServer()->getDefaultLevel();
foreach ($lv->getTiles() as $tile) {
$tile = $lv->getBlock(new Vector3($tile->getBlock()->getFloorX(), $tile->getBlock()->getFloorY(), $tile->getFloorZ()));
if ($tile instanceof Sign) {
$line1 = TextFormat::clean($tile->getText()[0], true);
if ($line1 == '[Stock] Info') {
$tile->setText('[Stock] Info','Stocks Remaining: '.'"'.$this->priceHandler->getAmount().'"','Sell Price: '.'"'.$sp.'"','Buy Price: '.'"'.$bp.'"');
} elseif ($line1 == '[Stock] Buy') {
$tile->setText('[Stock] Buy',"Buy Price: ".'"'.$bp.'"');
} elseif ($line1 == '[Stock Sell]') {
$tile->setText('[Stock] Sell',"Sell Price: ".'"'.$sp.'"');
}
}
}
}
public function onSignClick(PlayerInteractEvent $event)
{
$tile = $event->getBlock()->getLevel()->getTile(new Vector3($event->getBlock()->getFloorX(), $event->getBlock()->getFloorY(), $event->getBlock()->getFloorZ()));
if ($tile instanceof Sign) {
$line1 = TextFormat::clean($tile->getText()[0], true);
$p = $event->getPlayer();
$bp = $this->priceHandler->getPrice() + $this->tax;
$sp = $this->priceHandler->getPrice() - $this->tax;
if ($line1 == "[Stock] Info") {
$event->setCancelled(true);
$p->sendMessage('Buy Price is ' . $bp . ' Sell Price is: ' . $sp . ' Stock remaining: ' . $this->priceHandler->getAmount());
} elseif ($line1 == "[Stock] Buy") {
$event->setCancelled(true);
$this->priceHandler->reduceAmount();
$this->api->reduceMoney($p->getName(), $bp);
$p->getInventory()->addItem(Item::get(49, 0, 1));
$p->sendMessage('You bought obsidian form the market for ' . $bp);
} elseif ($line1 == "[Stock] Sell") {
$event->setCancelled(true);
if ($p->getInventory()->contains(Item::get(49, 0, 1))) {
$p->getInventory()->remove(Item::get(49, 0, 1));
$this->priceHandler->increaseAmount();
$this->api->addMoney($p->getName(), $sp);
$p->sendMessage('You sold obsidian to the market for ' . $sp);
}
}
}
}
}
<?php
/** Created By Thunder33345 **/
namespace Thunder33345\CommodityMarketLite;
class PriceHandler
{
private $main, $market;
private static $equilibriumPrice = 100, $equilibriumAmount = 50;
private static $differencesAmount = 5, $differencesPrice = 5.5;
//i didnt learned about liquiditity and so on
private $amount = 1, $price = 1;
public function __construct(Main $main, Market $market, $amount = null)
{
$this->main = $main;
$this->market = $market;
if ($amount === null) {
$this->amount = 50;
}
$this->recalculate();
}
private function recalculate()
{
$equilibriumPrice = $this::$equilibriumPrice;
$equilibriumAmount = $this::$equilibriumAmount;
$differencesAmount = $this::$differencesAmount;
$differencesPrice = $this::$differencesPrice;
$currentAmount = $this->amount;
$differences = $equilibriumAmount - $currentAmount;
$differences = $differences / $differencesAmount;
$differences = $differences * $differencesPrice;
$finalePrice = $equilibriumPrice + $differences;
$this->price = $finalePrice;
$this->market->updateSign();
}
public function reduceAmount($amount = 1)
{
$this->amount = $this->amount - $amount;
$this->recalculate();
}
public function increaseAmount($amount = 1)
{
$this->amount = $this->amount + $amount;
$this->recalculate();
}
public function setAmount($amount)
{
$this->amount = $amount;
$this->recalculate();
}
public function getInfo()
{
$return = [
"price" => $this->getPrice(),
"amount" => $this->getAmount()
];
return $return;
}
public function getPrice()
{
return $this->price;
}
public function getAmount()
{
return $this->amount;
}
}
@Thunder33345
Copy link
Author

Some old code i wrote for a dynamic market that changes price based on demand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment