Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.
Open
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
75 changes: 60 additions & 15 deletions src/AndreasHGK/SellAll/SellAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
use pocketmine\utils\Config;
use pocketmine\utils\TextFormat;
use pocketmine\Player;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;

class SellAll extends PluginBase{
class SellAll extends PluginBase implements Listener {

const CFGVERSION = 1.3;

public $cfg;
public $msg;
public $msgfile;
public $multiplier;

public function getSellPrice(Item $item) : ?float {
return $this->cfg[$item->getID().":".$item->getDamage()] ?? $this->cfg[$item->getID()] ?? null;
Expand All @@ -34,7 +37,8 @@ public function onEnable() : void{
@mkdir($this->getDataFolder());
$this->saveDefaultConfig();
$this->cfg = $this->getConfig()->getAll();
$this->saveResource("messages.yml");;
$this->saveResource("messages.yml");
$this->multiplier = new Config($this->getDataFolder() . "multipliers.yml", Config::YAML);
$this->msgfile = new Config($this->getDataFolder() . "messages.yml", Config::YAML, []);
$this->msg = $this->msgfile->getAll();
if(!isset($this->cfg["cfgversion"])){
Expand All @@ -56,12 +60,47 @@ public function replaceVars($str, array $vars) : string{
return $str;
}

public function onJoin(PlayerJoinEvent $event) : void {
$player = $event->getPlayer();
if (!$this->multiplier->exists($player->getName())) {
$this->multiplier->set($player->getName(), 1);
$this->multiplier->save();
}
}

public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{
if(!($sender instanceof Player) && !(isset($args[0]) && $args[0] === "reload")){
$sender->sendMessage(TextFormat::colorize("&cPlease execute this command in-game"));
return true;
}
switch($command->getName()){
case "addmultiplier":
if (!$sender->hasPermission("sellall.addmultiplier")) {
$sender->sendMessage(TextFormat::colorize($this->msg["error.permission"]));
return true;
}
if (empty($args[0])) {
$sender->sendMessage(TextFormat::RED . "Example: /addmultiplier (player) (amount)");
return true;
}
if (empty($args[1])) {
$sender->sendMessage(TextFormat::RED . "Example: /addmultiplier (player) (amount)");
return true;
}
$p = $this->getServer()->getPlayer($args[0]);
if ($p == null) {
$sender->sendMessage(TextFormat::RED . "That Player Is Not Online!");
return true;
}
if (!is_numeric($args[1])) {
$sender->sendMessage(TextFormat::RED . "The amount must be numeric!");
return true;
}
$this->multiplier->set($p->getName(), $this->multiplier->get($p->getName()) + $args[1]);
$this->multiplier->save();
$sender->sendMessage(TextFormat::GREEN . "Succesfully Set " . $p->getName() . "'s Multiplier To x" . $this->multiplier->get($p->getName()));
return true;
break;
case "sell":
if(!$sender->hasPermission("sellall.command")){
$sender->sendMessage(TextFormat::colorize($this->msg["error.permission"]));
Expand All @@ -75,25 +114,27 @@ public function onCommand(CommandSender $sender, Command $command, string $label
$price = $this->cfg[$item->getID().":".$item->getDamage()];
$count = $item->getCount();
$totalprice = $price * $count;
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$totalprice);
$actualprice = $totalprice * $this->multiplier->get($sender->getName());
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$actualprice);
$item->setCount($item->getCount() - (int)$count);
$sender->getInventory()->setItemInHand($item);
$sender->sendMessage(TextFormat::colorize($this->replaceVars($this->msg["success.sell"], array(
"AMOUNT" => (string)$count,
"ITEMNAME" => $item->getName(),
"MONEY" => (string)$totalprice))));
"MONEY" => (string)$actualprice))));
return true;
}elseif(isset($this->cfg[$item->getID()])){
$price = $this->cfg[$item->getID()];
$count = $item->getCount();
$totalprice = $price * $count;
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$totalprice);
$actualprice = $totalprice * $this->multiplier->get($sender->getName());
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$actualprice);
$item->setCount($item->getCount() - (int)$count);
$sender->getInventory()->setItemInHand($item);
$sender->sendMessage(TextFormat::colorize($this->replaceVars($this->msg["success.sell"], array(
"AMOUNT" => (string)$count,
"ITEMNAME" => $item->getName(),
"MONEY" => (string)$totalprice))));
"MONEY" => (string)$actualprice))));
return true;
}
$sender->sendMessage(TextFormat::colorize($this->msg["error.not-found"]));
Expand All @@ -115,11 +156,12 @@ public function onCommand(CommandSender $sender, Command $command, string $label
}
$inventory->sendContents($sender);
$totalprice = $count * $price;
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$totalprice);
$actualprice = $totalprice * $this->multiplier->get($sender->getName());
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$actualprice);
$sender->sendMessage(TextFormat::colorize($this->replaceVars($this->msg["success.sell"], array(
"AMOUNT" => (string)$count,
"ITEMNAME" => $item->getName(),
"MONEY" => (string)$totalprice))));
"MONEY" => (string)$actualprice))));
return true;
}elseif(isset($this->cfg[$item->getID()])){
$price = $this->cfg[$item->getID()];
Expand All @@ -132,11 +174,12 @@ public function onCommand(CommandSender $sender, Command $command, string $label
}
$inventory->sendContents($sender);
$totalprice = $count * $price;
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$totalprice);
$actualprice = $totalprice * $this->multiplier->get($sender->getName());
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$actualprice);
$sender->sendMessage(TextFormat::colorize($this->replaceVars($this->msg["success.sell"], array(
"AMOUNT" => (string)$count,
"ITEMNAME" => $item->getName(),
"MONEY" => (string)$totalprice))));
"MONEY" => (string)$actualprice))));
return true;
}
$sender->sendMessage(TextFormat::colorize($this->msg["error.not-found"]));
Expand All @@ -160,9 +203,10 @@ public function onCommand(CommandSender $sender, Command $command, string $label
$sender->sendMessage(TextFormat::colorize($this->msg["error.no.sellables"]));
return true;
}
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$revenue);
$actualprice = $revenue * $this->multiplier->get($sender->getName());
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$actualprice);
$sender->sendMessage(TextFormat::colorize($this->replaceVars($this->msg["success.sell.inventory"], array(
"MONEY" => (string)$revenue))));
"MONEY" => (string)$actualprice))));
return true;
break;

Expand Down Expand Up @@ -204,7 +248,7 @@ public function onCommand(CommandSender $sender, Command $command, string $label
$revenue = $revenue + ($item->getCount() * $this->cfg[$item->getID().":".$item->getDamage()]);
$sender->getInventory()->remove($item);
}elseif(isset($this->cfg[$item->getID()])){
$revenue = $revenue + ($item->getCount() * $this->cfg[$item->getID()]);
$revenue = $rev$actualprice = $totalprice * $this->multiplier->get($sender->getName());enue + ($item->getCount() * $this->cfg[$item->getID()]);
$sender->getInventory()->remove($item);
}
}
Expand All @@ -214,9 +258,10 @@ public function onCommand(CommandSender $sender, Command $command, string $label
$sender->sendMessage(TextFormat::colorize($group["failed"]));
return true;
}
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$revenue);
$actualprice = $revenue * $this->multiplier->get($sender->getName());
EconomyAPI::getInstance()->addMoney($sender->getName(), (int)$actualprice);
$sender->sendMessage(TextFormat::colorize($this->replaceVars($group["success"], array(
"MONEY" => (string)$revenue))));
"MONEY" => (string)$actualprice))));
return true;
}
$sender->sendMessage(TextFormat::colorize($this->replaceVars($this->msg["error.argument"], array(
Expand Down