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

Add Buying and selling plots #345

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4e4052e
Add Plot Selling and Buying
DerDevHD Oct 1, 2020
2d0cb0a
Update JSON-Provider and done some fixes
DerDevHD Oct 2, 2020
5dd030c
Sorry Jason
DerDevHD Oct 2, 2020
6cad1d8
Revert "Sorry Jason"
DerDevHD Oct 2, 2020
0748172
Add Plot-Popup
DerDevHD Oct 3, 2020
0afe72d
Add german translations
DerDevHD Oct 3, 2020
e6ef831
Fix the quotes
DerDevHD Oct 4, 2020
d2d6832
fix quotes
DerDevHD Oct 4, 2020
9c8db2d
Submit requested changes
DerDevHD Oct 4, 2020
d55ca82
fix missing '='
DerDevHD Oct 4, 2020
a855666
Remove stuff which was outside of current PR
DerDevHD Oct 4, 2020
2dbf752
Only load economy related commands if economy is loaded
jasonw4331 Oct 4, 2020
5f81f32
when buying a plot without economy, refer to claim command
jasonw4331 Oct 4, 2020
923de21
Buy command changes
jasonw4331 Oct 4, 2020
7a7d67b
Sell command changes
jasonw4331 Oct 4, 2020
7dfa09c
sold plot popup changes
jasonw4331 Oct 4, 2020
14fcd7a
We don't do hypotheticals here
jasonw4331 Oct 4, 2020
8e3bf1e
Command is not registered without economy
jasonw4331 Oct 4, 2020
1fc8c30
use events in new API
jasonw4331 Oct 4, 2020
5a80add
Better doc descriptions
jasonw4331 Oct 4, 2020
5c1cdee
forgot this earlier
jasonw4331 Oct 4, 2020
30ece71
cannot sell if price is less than 0
jasonw4331 Oct 5, 2020
8da354e
We don't import functions like PocketMine
jasonw4331 Oct 5, 2020
72f8aa1
Match to master branch
jasonw4331 Oct 5, 2020
876afed
Shrinking the diff
jasonw4331 Oct 5, 2020
6dd0610
translation changes
jasonw4331 Oct 5, 2020
4271ff5
Shrink the diff
jasonw4331 Oct 5, 2020
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
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
20 changes: 19 additions & 1 deletion resources/deu.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
; Popup when entering a plot
popup=Du hast das Grundstück {%0} betreten
popup.owner=Dieses Grundstück gehört {%0}
popup.forsale=Du kannst dieses Grundtück für ${%0} kaufen
popup.available=Dieses Grundstück ist verfügbar! Benutze /p claim, um es zu beanspruchen.
popup.denied=Du darfst dieses Grundstück nicht betreten!

Expand Down Expand Up @@ -238,4 +239,21 @@ kick.notInPlot=Der angegebene Spieler befindet sich nicht in deinem Plot
kick.cannotkick=Du kannst diesen Spieler nicht kicken
kick.attemptkick=Der Spieler {%0} hat versucht, dich von seinem plot zu kicken
kick.success1=Du hast erfolgreich {%0} von dem Plot {%1} gekickt
kick.success2=Der Spieler {%0} hat dich von dem Plot {%1} gekickt
kick.success2=Der Spieler {%0} hat dich von dem Plot {%1} gekickt

; the /p sell Subcommand
sell.name=sell
sell.desc=Verkaufe dein Grundstück
sell.usage=/p sell <preis: float>
sell.unclaimed=Dieses Grundstück hat keinen Besitzer
sell.success=Du hast dein Grunstück zum Kauf freigegeben (${%0})

; the /p buy Subcommand
buy.name=buy
buy.desc=Kaufe ein Grunstück
buy.usage=/p buy
buy.noself=Du kannst nicht dein eigenes Grundstück kaufen
buy.notforsale=Dieses Grundstück wird nicht verkauft
buy.confirm=Möchtest du wirklich das Grundstück {%0} für ${%1} kaufen? Um es zu bestätigen, benutze /p buy confirm
buy.success=Du hast das Grundstück {%0} erfolgreich für ${%1} erworben
buy.sold=Der Spieler {%0} hat dein Gruundstück ({%1}) für ${%2} gekauft
19 changes: 18 additions & 1 deletion resources/eng.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
; Popup when entering a plot
popup=You entered plot {%0}
popup.owner=Owned by {%0}
popup.forsale=This plot is for sale by {%0}. Use /p buy to claim the plot for ${%0}.
popup.available=This plot is open. Use /p claim to claim the plot for ${%0}.
popup.denied=You are denied access to this plot

Expand Down Expand Up @@ -291,4 +292,20 @@ 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.success=Your plot {%0} is now on sale for ${%1}

; the /p buy Subcommand
buy.name=buy
buy.desc=Buys a plot
buy.usage=/p buy
buy.noself="You can't buy a plot from yourself"
buy.notforsale=This plot is not for sale
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}
19 changes: 18 additions & 1 deletion resources/langtemplate.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
; Popup when entering a plot
popup=
popup.owner=
popup.forsale=
popup.available=
popup.denied=

Expand Down Expand Up @@ -285,4 +286,20 @@ kick.attemptkick=
kick.success1=
kick.success2=
kick.form=
kick.dropdown=
kick.dropdown=

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

; the /p buy Subcommand
buy.name=
buy.desc=
buy.usage=
buy.noself=
buy.notforsale=
buy.confirm=
buy.success=
buy.sold=
6 changes: 6 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,10 @@ public function __construct(MyPlot $plugin) {
$this->loadSubCommand(new ListSubCommand($plugin, "list"));
$this->loadSubCommand(new PvpSubCommand($plugin, "pvp"));
$this->loadSubCommand(new KickSubCommand($plugin, "kick"));
if($plugin->getEconomyProvider() !== null) {
$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
6 changes: 5 additions & 1 deletion src/MyPlot/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ private function onEventOnMove(Player $player, $event) : void {
$popup = $this->plugin->getLanguage()->translateString("popup", [TextFormat::GREEN . $plot]);
if(!empty($plot->owner)) {
$owner = TextFormat::GREEN . $plot->owner;
$ownerPopup = $this->plugin->getLanguage()->translateString("popup.owner", [$owner]);
if($plot->price > 0) {
$ownerPopup = $this->plugin->getLanguage()->translateString("popup.forsale", [$owner.TextFormat::RESET, $plot->price]);
}else{
$ownerPopup = $this->plugin->getLanguage()->translateString("popup.owner", [$owner]);
}
}else{
$ownerPopup = $this->plugin->getLanguage()->translateString("popup.available", [$plot->price]);
}
Expand Down
53 changes: 53 additions & 0 deletions src/MyPlot/MyPlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,59 @@ public function removePlotDenied(Plot $plot, string $player) : bool {
return $this->savePlot($ev->getPlot());
}

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

$newPlot = clone $plot;
$newPlot->price = $price;
$ev = new MyPlotSettingEvent($plot, $newPlot);
$ev->call();
if($ev->isCancelled()) {
return false;
}
$plot = $ev->getPlot();
return $this->savePlot($plot);
}

/**
* Resets the price and claims a plot in a players name
*
* @api
*
* @param Plot $plot
* @param Player $player
*
* @return bool
*/
public function buyPlot(Plot $plot, Player $player) : bool {
if($this->getEconomyProvider() === null or !$this->getEconomyProvider()->reduceMoney($player, $plot->price))
return false;

$newPlot = clone $plot;
$newPlot->owner = $player->getName();
$newPlot->helpers = [];
$newPlot->denied = [];
$newPlot->price = 0.0;
$ev = new MyPlotSettingEvent($plot, $newPlot);
$ev->call();
if($ev->isCancelled()) {
return false;
}
$plot = $ev->getPlot();
return $this->savePlot($plot);
}

/**
* Returns the PlotLevelSettings of all the loaded levels
*
Expand Down
62 changes: 62 additions & 0 deletions src/MyPlot/subcommand/BuySubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace MyPlot\subcommand;

use MyPlot\forms\MyPlotForm;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\utils\TextFormat;

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){
$command = new ClaimSubCommand($this->getPlugin(), "claim");
return $command->execute($sender, []);
}
$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->price <= 0){
$sender->sendMessage(TextFormat::RED . $this->translateString("buy.notforsale"));
return true;
}
if(strtolower($args[0] ?? "") !== $this->translateString("confirm")){
$sender->sendMessage($this->translateString("buy.confirm", ["{$plot->X};{$plot->Z}", $plot->price]));
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;
}
}
58 changes: 58 additions & 0 deletions src/MyPlot/subcommand/SellSubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace MyPlot\subcommand;

use MyPlot\forms\MyPlotForm;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\utils\TextFormat;

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(empty($args)) {
return false;
}
$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.sell")){
$sender->sendMessage(TextFormat::RED . $this->translateString("notowner"));
return true;
}
if(!is_numeric($args[0]))
return false;
$price = (float)$args[0];
if($price <= 0){
$sender->sendMessage(TextFormat::RED . $this->translateString("sell.unlist"));
}
if($this->getPlugin()->sellPlot($plot, $price)) {
$sender->sendMessage($this->translateString("sell.success", ["{$plot->X};{$plot->Z}", $price]));
}else{
$sender->sendMessage(TextFormat::RED . $this->translateString("error"));
}
return true;
}

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