PlotSquared/Core/src/main/java/com/plotsquared/core/command/Claim.java

179 lines
8.2 KiB
Java
Raw Normal View History

/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.command;
2014-09-22 13:02:14 +02:00
2020-07-11 17:19:19 +02:00
import com.google.inject.Inject;
2020-04-16 06:14:33 +02:00
import com.plotsquared.core.configuration.CaptionUtility;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.database.DBFunc;
import com.plotsquared.core.events.PlayerClaimPlotEvent;
import com.plotsquared.core.events.PlotMergeEvent;
import com.plotsquared.core.events.Result;
import com.plotsquared.core.location.Direction;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.MetaDataAccess;
import com.plotsquared.core.player.PlayerMetaDataKeys;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.player.PlotPlayer;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.util.EconHandler;
import com.plotsquared.core.util.EventDispatcher;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.util.Expression;
import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.task.TaskManager;
2020-07-12 21:49:05 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2014-12-28 13:02:30 +01:00
2020-07-17 16:25:57 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@CommandDeclaration(command = "claim", aliases = "c", description = "Claim the current plot you're standing on", category = CommandCategory.CLAIMING, requiredType = RequiredType.PLAYER, permission = "plots.claim", usage = "/plot claim")
2015-09-13 06:04:31 +02:00
public class Claim extends SubCommand {
2016-03-23 02:41:37 +01:00
2020-07-17 16:25:57 +02:00
private static final Logger logger =
LoggerFactory.getLogger("P2/" + Claim.class.getSimpleName());
2020-07-12 21:49:05 +02:00
private final EventDispatcher eventDispatcher;
2020-07-11 17:19:19 +02:00
private final EconHandler econHandler;
Merge branch 'v6' into feature/v6/platform # Conflicts: # Bukkit/build.gradle # Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java # Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java # Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitRegionManager.java # Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java # Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SquirrelIdUUIDService.java # Core/build.gradle # Core/src/main/java/com/plotsquared/core/PlotSquared.java # Core/src/main/java/com/plotsquared/core/command/Claim.java # Core/src/main/java/com/plotsquared/core/command/Debug.java # Core/src/main/java/com/plotsquared/core/command/DebugExec.java # Core/src/main/java/com/plotsquared/core/command/Purge.java # Core/src/main/java/com/plotsquared/core/command/Trim.java # Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java # Core/src/main/java/com/plotsquared/core/database/SQLManager.java # Core/src/main/java/com/plotsquared/core/generator/HybridPlotWorld.java # Core/src/main/java/com/plotsquared/core/generator/HybridUtils.java # Core/src/main/java/com/plotsquared/core/generator/SquarePlotManager.java # Core/src/main/java/com/plotsquared/core/generator/SquarePlotWorld.java # Core/src/main/java/com/plotsquared/core/listener/ProcessedWEExtent.java # Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java # Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java # Core/src/main/java/com/plotsquared/core/plot/Plot.java # Core/src/main/java/com/plotsquared/core/plot/PlotArea.java # Core/src/main/java/com/plotsquared/core/plot/PlotInventory.java # Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java # Core/src/main/java/com/plotsquared/core/plot/expiration/PlotAnalysis.java # Core/src/main/java/com/plotsquared/core/plot/flag/FlagContainer.java # Core/src/main/java/com/plotsquared/core/plot/flag/types/BlockTypeWrapper.java # Core/src/main/java/com/plotsquared/core/plot/message/PlotMessage.java # Core/src/main/java/com/plotsquared/core/util/LegacyConverter.java # Core/src/main/java/com/plotsquared/core/util/MainUtil.java # Core/src/main/java/com/plotsquared/core/util/RegionManager.java # Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java # Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java
2020-07-14 19:05:05 +02:00
2020-07-14 18:49:40 +02:00
@Inject public Claim(@Nonnull final EventDispatcher eventDispatcher,
2020-07-17 16:25:57 +02:00
@Nullable final EconHandler econHandler) {
this.eventDispatcher = eventDispatcher;
2020-07-11 17:19:19 +02:00
this.econHandler = econHandler;
}
Merge branch 'v6' into feature/v6/platform # Conflicts: # Bukkit/build.gradle # Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java # Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java # Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitRegionManager.java # Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java # Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SquirrelIdUUIDService.java # Core/build.gradle # Core/src/main/java/com/plotsquared/core/PlotSquared.java # Core/src/main/java/com/plotsquared/core/command/Claim.java # Core/src/main/java/com/plotsquared/core/command/Debug.java # Core/src/main/java/com/plotsquared/core/command/DebugExec.java # Core/src/main/java/com/plotsquared/core/command/Purge.java # Core/src/main/java/com/plotsquared/core/command/Trim.java # Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java # Core/src/main/java/com/plotsquared/core/database/SQLManager.java # Core/src/main/java/com/plotsquared/core/generator/HybridPlotWorld.java # Core/src/main/java/com/plotsquared/core/generator/HybridUtils.java # Core/src/main/java/com/plotsquared/core/generator/SquarePlotManager.java # Core/src/main/java/com/plotsquared/core/generator/SquarePlotWorld.java # Core/src/main/java/com/plotsquared/core/listener/ProcessedWEExtent.java # Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java # Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java # Core/src/main/java/com/plotsquared/core/plot/Plot.java # Core/src/main/java/com/plotsquared/core/plot/PlotArea.java # Core/src/main/java/com/plotsquared/core/plot/PlotInventory.java # Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java # Core/src/main/java/com/plotsquared/core/plot/expiration/PlotAnalysis.java # Core/src/main/java/com/plotsquared/core/plot/flag/FlagContainer.java # Core/src/main/java/com/plotsquared/core/plot/flag/types/BlockTypeWrapper.java # Core/src/main/java/com/plotsquared/core/plot/message/PlotMessage.java # Core/src/main/java/com/plotsquared/core/util/LegacyConverter.java # Core/src/main/java/com/plotsquared/core/util/MainUtil.java # Core/src/main/java/com/plotsquared/core/util/RegionManager.java # Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java # Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java
2020-07-14 19:05:05 +02:00
@Override public boolean onCommand(final PlotPlayer<?> player, String[] args) {
String schematic = null;
2015-09-13 06:04:31 +02:00
if (args.length >= 1) {
2014-11-16 10:48:18 +01:00
schematic = args[0];
}
2019-09-08 20:02:45 +02:00
Location location = player.getLocation();
Plot plot = location.getPlotAbs();
2015-09-13 06:04:31 +02:00
if (plot == null) {
return sendMessage(player, Captions.NOT_IN_PLOT);
2015-09-13 06:04:31 +02:00
}
final PlayerClaimPlotEvent event = this.eventDispatcher.callClaim(player, plot, schematic);
schematic = event.getSchematic();
if (event.getEventResult() == Result.DENY) {
sendMessage(player, Captions.EVENT_DENIED, "Claim");
return true;
}
boolean force = event.getEventResult() == Result.FORCE;
2019-09-08 20:02:45 +02:00
int currentPlots = Settings.Limit.GLOBAL ?
player.getPlotCount() :
player.getPlotCount(location.getWorldName());
final PlotArea area = plot.getArea();
try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) {
int grants = 0;
if (currentPlots >= player.getAllowedPlots() && !force) {
if (metaDataAccess.has()) {
grants = metaDataAccess.get().orElse(0);
if (grants <= 0) {
metaDataAccess.remove();
return sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS);
}
} else {
return sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS);
}
}
if (!plot.canClaim(player)) {
return sendMessage(player, Captions.PLOT_IS_CLAIMED);
}
if (schematic != null && !schematic.isEmpty()) {
if (area.isSchematicClaimSpecify()) {
if (!area.hasSchematic(schematic)) {
return sendMessage(player, Captions.SCHEMATIC_INVALID, "non-existent: " + schematic);
}
if (!Permissions.hasPermission(player, CaptionUtility.format(player, Captions.PERMISSION_CLAIM_SCHEMATIC.getTranslated(),
schematic)) && !Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_COMMAND_SCHEMATIC)
&& !force) {
return sendMessage(player, Captions.NO_SCHEMATIC_PERMISSION, schematic);
}
}
}
if ((this.econHandler != null) && area.useEconomy() && !force) {
Expression<Double> costExr = area.getPrices().get("claim");
double cost = costExr.evaluate((double) currentPlots);
if (cost > 0d) {
if (this.econHandler.getMoney(player) < cost) {
return sendMessage(player, Captions.CANNOT_AFFORD_PLOT, "" + cost);
}
this.econHandler.withdrawMoney(player, cost);
sendMessage(player, Captions.REMOVED_BALANCE, cost + "");
2015-09-13 06:04:31 +02:00
}
2014-11-16 10:48:18 +01:00
}
if (grants > 0) {
if (grants == 1) {
metaDataAccess.remove();
} else {
metaDataAccess.set(grants - 1);
}
sendMessage(player, Captions.REMOVED_GRANTED_PLOT, "1", (grants - 1));
2016-02-27 21:04:57 +01:00
}
}
int border = area.getBorder();
if (border != Integer.MAX_VALUE && plot.getDistanceFromOrigin() > border && !force) {
return !sendMessage(player, Captions.BORDER);
}
plot.setOwnerAbs(player.getUUID());
final String finalSchematic = schematic;
2020-07-17 16:25:57 +02:00
DBFunc.createPlotSafe(plot, () -> {
try {
2020-07-17 16:38:07 +02:00
TaskManager.getPlatformImplementation().sync(() -> {
2020-07-17 16:25:57 +02:00
if (!plot.claim(player, true, finalSchematic, false)) {
logger.info(Captions.PREFIX.getTranslated() + String
.format("Failed to claim plot %s", plot.getId().toCommaSeparatedString()));
sendMessage(player, Captions.PLOT_NOT_CLAIMED);
plot.setOwnerAbs(null);
} else if (area.isAutoMerge()) {
PlotMergeEvent mergeEvent = Claim.this.eventDispatcher
.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player);
if (mergeEvent.getEventResult() == Result.DENY) {
sendMessage(player, Captions.EVENT_DENIED, "Auto merge on claim");
} else {
plot.autoMerge(mergeEvent.getDir(), mergeEvent.getMax(), player.getUUID(), true);
}
}
2020-07-17 16:25:57 +02:00
return null;
});
} catch (final Exception e) {
e.printStackTrace();
}
2020-07-17 16:25:57 +02:00
}, () -> {
2020-07-12 21:49:05 +02:00
logger.info(Captions.PREFIX.getTranslated() + String
2020-04-30 12:33:59 +02:00
.format("Failed to add plot %s to the database",
plot.getId().toCommaSeparatedString()));
2020-04-07 22:12:52 +02:00
sendMessage(player, Captions.PLOT_NOT_CLAIMED);
plot.setOwnerAbs(null);
2020-04-07 22:12:52 +02:00
});
return true;
2014-11-16 10:48:18 +01:00
}
2014-09-22 13:02:14 +02:00
}