Skip to content
This repository has been archived by the owner on Oct 29, 2022. It is now read-only.

Commit

Permalink
Add Plot Selling and Buying
Browse files Browse the repository at this point in the history
Add method EconomyProvider::getMoney()
  • Loading branch information
DerDevHD authored and jasonw4331 committed Oct 5, 2020
1 parent a1dec42 commit 4e4052e
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 2 deletions.
8 changes: 8 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ permissions:
default: true
myplot.command.clone:
default: true
myplot.command.sell:
default: true
myplot.command.buy:
default: true
myplot.admin:
default: op
children:
Expand Down Expand Up @@ -114,6 +118,10 @@ permissions:
default: op
myplot.admin.clone:
default: op
myplot.admin.sell:
default: op
myplot.admin.buy:
default: op
myplot.claimplots:
default: op
children:
Expand Down
22 changes: 21 additions & 1 deletion resources/eng.ini
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,24 @@ kick.attemptkick=Player {%0} attempt to kick you
kick.success1=You successfully kicked {%0} from plot {%1}
kick.success2=Player {%0} kicked you from plot {%1}
kick.form=Kick
kick.dropdown=Player Name
kick.dropdown=Player Name

; the /p sell Subcommand
sell.name=sell
sell.desc=Sells a plot
sell.usage=/p sell <price: float>
sell.unclaimed=This plot isn't claimed
sell.already=This plot is already for sale
sell.minprice=The price should be greater than 0
sell.success=You successfully released your plot for selling (${%0})
; the /p buy Subcommand
buy.name=buy
buy.desc=Buys a plot
buy.usage=/p buy
buy.noself=You can't buy your plot from yourself
buy.notinsale=This plot isn't in sale
buy.nomoney=You don't have enough money for buying this plot
buy.confirm=Are you sure to buy Plot {%0} for ${%1}? To confirm, use /p buy confirm
buy.success=You successfully bought Plot {%0} for ${%1}
buy.sold={%0} bought your Plot ({%1}) for ${%2}
22 changes: 21 additions & 1 deletion resources/langtemplate.ini
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,24 @@ kick.attemptkick=
kick.success1=
kick.success2=
kick.form=
kick.dropdown=
kick.dropdown=

; the /p sell Subcommand
sell.name=
sell.desc=
sell.usage=
sell.unclaimed=
sell.already=
sell.minprice=
sell.success=

; the /p buy Subcommand
buy.name=
buy.desc=
buy.usage=
buy.noself=
buy.notinsale=
buy.nomoney=
buy.confirm=
buy.success=
buy.sold=
4 changes: 4 additions & 0 deletions src/MyPlot/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MyPlot\subcommand\AddHelperSubCommand;
use MyPlot\subcommand\AutoSubCommand;
use MyPlot\subcommand\BiomeSubCommand;
use MyPlot\subcommand\BuySubCommand;
use MyPlot\subcommand\ClaimSubCommand;
use MyPlot\subcommand\ClearSubCommand;
use MyPlot\subcommand\CloneSubCommand;
Expand All @@ -25,6 +26,7 @@
use MyPlot\subcommand\PvpSubCommand;
use MyPlot\subcommand\RemoveHelperSubCommand;
use MyPlot\subcommand\ResetSubCommand;
use MyPlot\subcommand\SellSubCommand;
use MyPlot\subcommand\SetOwnerSubCommand;
use MyPlot\subcommand\SubCommand;
use MyPlot\subcommand\UnDenySubCommand;
Expand Down Expand Up @@ -79,6 +81,8 @@ public function __construct(MyPlot $plugin) {
$this->loadSubCommand(new ListSubCommand($plugin, "list"));
$this->loadSubCommand(new PvpSubCommand($plugin, "pvp"));
$this->loadSubCommand(new KickSubCommand($plugin, "kick"));
$this->loadSubCommand(new SellSubCommand($plugin, "sell"));
$this->loadSubCommand(new BuySubCommand($plugin, "buy"));
$styler = $this->getPlugin()->getServer()->getPluginManager()->getPlugin("WorldStyler");
if($styler !== null) {
$this->loadSubCommand(new CloneSubCommand($plugin, "clone"));
Expand Down
59 changes: 59 additions & 0 deletions src/MyPlot/MyPlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,65 @@ public function removePlotDenied(Plot $plot, string $player) : bool {
return $this->savePlot($ev->getPlot());
}

/**
* Sells a plot
*
* @api
*
* @param Plot $plot
* @param float $price
*
* @return bool
*/
public function sellPlot(Plot $plot, float $price) : bool {
if($this->getEconomyProvider() === null)
return false;
if($plot->isForSale())
return false;
if($price <= 0)
return false;
$plot->price = $price;
return $this->savePlot($plot);
}

/**
* Checks if a player can buy a plot
*
* @api
*
* @param Plot $plot
* @param Player $player
*
* @return bool
*/
public function canBuyPlot(Plot $plot, Player $player) : bool {
return $this->getEconomyProvider() !== null and $plot->isForSale() and $this->getEconomyProvider()->getMoney($player) >= $plot->price;
}

/**
* Buys a plot
*
* @api
*
* @param Plot $plot
* @param Player $player
*
* @return bool
*/
public function buyPlot(Plot $plot, Player $player) : bool {
if($this->getEconomyProvider() === null)
return false;
if(!$plot->isForSale())
return false;
if(!$this->getEconomyProvider()->reduceMoney($player, $plot->price))
return false;
$plot->owner = $player->getName();
$plot->helpers = [];
$plot->denied = [];
$plot->price = 0;
return $this->savePlot($plot);
}

/**
* Returns the PlotLevelSettings of all the loaded levels
*
Expand Down
8 changes: 8 additions & 0 deletions src/MyPlot/provider/EconomyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
use pocketmine\Player;

interface EconomyProvider {

/**
* @param Player $player
*
* @return float
*/
public function getMoney(Player $player) : float;

/**
* @param Player $player
* @param float $amount
Expand Down
9 changes: 9 additions & 0 deletions src/MyPlot/provider/EconomySProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public function __construct(EconomyAPI $plugin) {
$this->plugin = $plugin;
}

/**
* @param Player $player
*
* @return float
*/
public function getMoney(Player $player) : float {
return ($money = $this->plugin->myMoney($player)) === false ? 0 : $money;
}

/**
* @param Player $player
* @param float $amount
Expand Down
70 changes: 70 additions & 0 deletions src/MyPlot/subcommand/BuySubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace MyPlot\subcommand;

use MyPlot\forms\MyPlotForm;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\utils\TextFormat;
use function count;
use function explode;
use function is_numeric;
use function strtolower;

class BuySubCommand extends SubCommand
{
/**
* @param CommandSender $sender
*
* @return bool
*/
public function canUse(CommandSender $sender) : bool {
return ($sender instanceof Player) and $sender->hasPermission("myplot.command.buy");
}

/**
* @param Player $sender
* @param string[] $args
*
* @return bool
*/
public function execute(CommandSender $sender, array $args) : bool {
if($this->getPlugin()->getEconomyProvider() === null){
$sender->sendMessage("NO-ECONOMY-PLUGIN");
return true;
}
$plot = $this->getPlugin()->getPlotByPosition($sender->asPosition());
if($plot === null){
$sender->sendMessage(TextFormat::RED . $this->translateString("notinplot"));
return true;
}
if($plot->name === $sender->getName() and !$sender->hasPermission("myplot.admin.buy")){
$sender->sendMessage(TextFormat::RED . $this->translateString("buy.noself"));
return true;
}
if(!$plot->isForSale()){
$sender->sendMessage(TextFormat::RED . $this->translateString("buy.notinsale"));
return true;
}
if(!$this->getPlugin()->canBuyPlot($plot, $sender)){
$sender->sendMessage(TextFormat::RED . $this->translateString("buy.nomoney"));
return true;
}
if(strtolower($args[0] ?? "") != $this->translateString("confirm")){
$sender->sendMessage($this->translateString("buy.confirm", [TextFormat::GREEN . "{$plot->X};{$plot->Z}" . TextFormat::RESET, TextFormat::GREEN . $plot->price . TextFormat::RESET]));
return true;
}
$oldOwner = $this->getPlugin()->getServer()->getPlayer($plot->owner);
$clone = clone $plot;
$this->getPlugin()->buyPlot($plot, $sender);
$sender->sendMessage($this->translateString("buy.success", [TextFormat::GREEN . "{$plot->X};{$plot->Z}" . TextFormat::RESET, TextFormat::GREEN . $clone->price . TextFormat::RESET]));
if($oldOwner instanceof Player)
$oldOwner->sendMessage($this->translateString("buy.sold", [TextFormat::GREEN . $sender->getName() . TextFormat::RESET, TextFormat::GREEN . "{$plot->X};{$plot->Z}" . TextFormat::RESET, TextFormat::GREEN . $clone->price . TextFormat::RESET]));
return true;
}

public function getForm(?Player $player = null) : ?MyPlotForm {
// TODO: Implement getForm() method.
return null;
}
}
69 changes: 69 additions & 0 deletions src/MyPlot/subcommand/SellSubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace MyPlot\subcommand;

use MyPlot\forms\MyPlotForm;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\utils\TextFormat;
use function count;
use function explode;
use function is_numeric;

class SellSubCommand extends SubCommand
{
/**
* @param CommandSender $sender
*
* @return bool
*/
public function canUse(CommandSender $sender) : bool {
return ($sender instanceof Player) and $sender->hasPermission("myplot.command.sell");
}

/**
* @param Player $sender
* @param string[] $args
*
* @return bool
*/
public function execute(CommandSender $sender, array $args) : bool {
if($this->getPlugin()->getEconomyProvider() === null){
$sender->sendMessage("NO-ECONOMY-PLUGIN");
return true;
}
if(empty($args))
return false;
$plot = $this->getPlugin()->getPlotByPosition($sender->asPosition());
if($plot === null){
$sender->sendMessage(TextFormat::RED . $this->translateString("notinplot"));
return true;
}
if($plot->owner == ""){
$sender->sendMessage(TextFormat::RED . "sell.unclaimed");
return true;
}
if($plot->name !== $sender->getName() and !$sender->hasPermission("myplot.admin.sell")){
$sender->sendMessage(TextFormat::RED . $this->translateString("notowner"));
return true;
}
if($plot->isForSale()){
$sender->sendMessage(TextFormat::RED . $this->translateString("sell.already"));
return true;
}
if(!is_numeric($price = $args[0]))
return false;
if((float) $price <= 0){
$sender->sendMessage(TextFormat::RED . $this->translateString("sell.minprice"));
return true;
}
$this->getPlugin()->sellPlot($plot, (float) $price);
$sender->sendMessage($this->translateString("sell.success", [TextFormat::GREEN . $price . TextFormat::RESET]));
return true;
}

public function getForm(?Player $player = null) : ?MyPlotForm {
// TODO: Implement getForm() method.
return null;
}
}

0 comments on commit 4e4052e

Please sign in to comment.