001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.command; 020 021import com.google.inject.Inject; 022import com.plotsquared.core.PlotSquared; 023import com.plotsquared.core.configuration.caption.TranslatableCaption; 024import com.plotsquared.core.events.PlayerBuyPlotEvent; 025import com.plotsquared.core.events.Result; 026import com.plotsquared.core.player.OfflinePlotPlayer; 027import com.plotsquared.core.player.PlotPlayer; 028import com.plotsquared.core.plot.Plot; 029import com.plotsquared.core.plot.PlotArea; 030import com.plotsquared.core.plot.flag.PlotFlag; 031import com.plotsquared.core.plot.flag.implementations.PriceFlag; 032import com.plotsquared.core.util.EconHandler; 033import com.plotsquared.core.util.EventDispatcher; 034import com.plotsquared.core.util.task.RunnableVal2; 035import com.plotsquared.core.util.task.RunnableVal3; 036import net.kyori.adventure.text.Component; 037import net.kyori.adventure.text.minimessage.tag.Tag; 038import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; 039import org.checkerframework.checker.nullness.qual.NonNull; 040 041import java.util.Set; 042import java.util.concurrent.CompletableFuture; 043 044@CommandDeclaration(command = "buy", 045 usage = "/plot buy", 046 permission = "plots.buy", 047 category = CommandCategory.CLAIMING, 048 requiredType = RequiredType.NONE) 049public class Buy extends Command { 050 051 private final EventDispatcher eventDispatcher; 052 private final EconHandler econHandler; 053 054 @Inject 055 public Buy( 056 final @NonNull EventDispatcher eventDispatcher, 057 final @NonNull EconHandler econHandler 058 ) { 059 super(MainCommand.getInstance(), true); 060 this.eventDispatcher = eventDispatcher; 061 this.econHandler = econHandler; 062 } 063 064 @Override 065 public CompletableFuture<Boolean> execute( 066 final PlotPlayer<?> player, String[] args, 067 RunnableVal3<Command, Runnable, Runnable> confirm, 068 final RunnableVal2<Command, CommandResult> whenDone 069 ) { 070 071 PlotArea area = player.getPlotAreaAbs(); 072 check(area, TranslatableCaption.of("errors.not_in_plot_world")); 073 check(this.econHandler.isEnabled(area), TranslatableCaption.of("economy.econ_disabled")); 074 final Plot plot; 075 if (args.length != 0) { 076 if (args.length != 1) { 077 sendUsage(player); 078 return CompletableFuture.completedFuture(false); 079 } 080 plot = check(Plot.getPlotFromString(player, args[0], true), null); 081 } else { 082 plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot")); 083 } 084 checkTrue(plot.hasOwner(), TranslatableCaption.of("info.plot_unowned")); 085 checkTrue(!plot.isOwner(player.getUUID()), TranslatableCaption.of("economy.cannot_buy_own")); 086 Set<Plot> plots = plot.getConnectedPlots(); 087 checkTrue( 088 player.getPlotCount() + plots.size() <= player.getAllowedPlots(), 089 TranslatableCaption.of("permission.cant_claim_more_plots"), 090 TagResolver.resolver("amount", Tag.inserting(Component.text(player.getAllowedPlots()))) 091 ); 092 double priceFlag = plot.getFlag(PriceFlag.class); 093 if (priceFlag <= 0) { 094 throw new CommandException(TranslatableCaption.of("economy.not_for_sale")); 095 } 096 checkTrue( 097 this.econHandler.isSupported(), 098 TranslatableCaption.of("economy.vault_or_consumer_null") 099 ); 100 101 PlayerBuyPlotEvent event = this.eventDispatcher.callPlayerBuyPlot(player, plot, priceFlag); 102 if (event.getEventResult() == Result.DENY) { 103 throw new CommandException(TranslatableCaption.of("economy.cannot_buy_blocked")); 104 } 105 106 double price = event.getEventResult() == Result.FORCE ? 0 : event.price(); 107 if (this.econHandler.getMoney(player) < price) { 108 throw new CommandException( 109 TranslatableCaption.of("economy.cannot_afford_plot"), 110 TagResolver.builder() 111 .tag("money", Tag.inserting(Component.text(this.econHandler.format(price)))) 112 .tag("balance", Tag.inserting(Component.text(this.econHandler.format(this.econHandler.getMoney(player))))) 113 .build() 114 ); 115 } 116 this.econHandler.withdrawMoney(player, price); 117 // Failure 118 // Success 119 confirm.run(this, () -> { 120 player.sendMessage( 121 TranslatableCaption.of("economy.removed_balance"), 122 TagResolver.resolver("money", Tag.inserting(Component.text(this.econHandler.format(price)))) 123 ); 124 125 OfflinePlotPlayer previousOwner = PlotSquared.platform().playerManager().getOfflinePlayer(plot.getOwnerAbs()); 126 this.econHandler.depositMoney(previousOwner, price); 127 128 PlotPlayer<?> owner = PlotSquared.platform().playerManager().getPlayerIfExists(plot.getOwnerAbs()); 129 if (owner != null) { 130 owner.sendMessage( 131 TranslatableCaption.of("economy.plot_sold"), 132 TagResolver.builder() 133 .tag("plot", Tag.inserting(Component.text(plot.getId().toString()))) 134 .tag("player", Tag.inserting(Component.text(player.getName()))) 135 .tag("price", Tag.inserting(Component.text(this.econHandler.format(price)))) 136 .build() 137 ); 138 } 139 PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(PriceFlag.class); 140 if (this.eventDispatcher.callFlagRemove(plotFlag, plot).getEventResult() != Result.DENY) { 141 plot.removeFlag(plotFlag); 142 } 143 plot.setOwner(player.getUUID()); 144 plot.getPlotModificationManager().setSign(player.getName()); 145 player.sendMessage( 146 TranslatableCaption.of("working.claimed"), 147 TagResolver.resolver("world", Tag.inserting(Component.text(plot.getArea().getWorldName()))), 148 TagResolver.resolver("plot", Tag.inserting(Component.text(plot.getId().toString()))) 149 ); 150 this.eventDispatcher.callPostPlayerBuyPlot(player, previousOwner, plot, price); 151 whenDone.run(Buy.this, CommandResult.SUCCESS); 152 }, () -> { 153 this.econHandler.depositMoney(player, price); 154 whenDone.run(Buy.this, CommandResult.FAILURE); 155 }); 156 return CompletableFuture.completedFuture(true); 157 } 158 159}