diff --git a/plugin.yml b/plugin.yml index 2b8cd54a..20cce065 100644 --- a/plugin.yml +++ b/plugin.yml @@ -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: @@ -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: diff --git a/resources/deu.ini b/resources/deu.ini index 9e551e64..cb54d9ef 100644 --- a/resources/deu.ini +++ b/resources/deu.ini @@ -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! @@ -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 \ No newline at end of file +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 +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 \ No newline at end of file diff --git a/resources/eng.ini b/resources/eng.ini index 58430b3f..5e86ff22 100644 --- a/resources/eng.ini +++ b/resources/eng.ini @@ -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 @@ -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 \ No newline at end of file +kick.dropdown=Player Name + +; the /p sell Subcommand +sell.name=sell +sell.desc=Sells a plot +sell.usage=/p sell +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} \ No newline at end of file diff --git a/resources/langtemplate.ini b/resources/langtemplate.ini index 98c60ce4..f6bf75e7 100644 --- a/resources/langtemplate.ini +++ b/resources/langtemplate.ini @@ -5,6 +5,7 @@ ; Popup when entering a plot popup= popup.owner= +popup.forsale= popup.available= popup.denied= @@ -285,4 +286,20 @@ kick.attemptkick= kick.success1= kick.success2= kick.form= -kick.dropdown= \ No newline at end of file +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= \ No newline at end of file diff --git a/src/MyPlot/Commands.php b/src/MyPlot/Commands.php index e7835537..c9cbc398 100644 --- a/src/MyPlot/Commands.php +++ b/src/MyPlot/Commands.php @@ -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; @@ -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; @@ -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")); diff --git a/src/MyPlot/EventListener.php b/src/MyPlot/EventListener.php index 4f47cd9b..fa163bbd 100644 --- a/src/MyPlot/EventListener.php +++ b/src/MyPlot/EventListener.php @@ -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]); } diff --git a/src/MyPlot/MyPlot.php b/src/MyPlot/MyPlot.php index 707e2495..d7d53add 100644 --- a/src/MyPlot/MyPlot.php +++ b/src/MyPlot/MyPlot.php @@ -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 * diff --git a/src/MyPlot/subcommand/BuySubCommand.php b/src/MyPlot/subcommand/BuySubCommand.php new file mode 100644 index 00000000..81e57b35 --- /dev/null +++ b/src/MyPlot/subcommand/BuySubCommand.php @@ -0,0 +1,62 @@ +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; + } +} \ No newline at end of file diff --git a/src/MyPlot/subcommand/SellSubCommand.php b/src/MyPlot/subcommand/SellSubCommand.php new file mode 100644 index 00000000..ca110124 --- /dev/null +++ b/src/MyPlot/subcommand/SellSubCommand.php @@ -0,0 +1,58 @@ +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; + } +} \ No newline at end of file