From 99133ee4855cf09fbbaf6b779699827ffa8ef4f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 13 May 2023 16:53:56 +0200 Subject: [PATCH] port over alias :) --- .../com/plotsquared/core/command/Add.java | 172 ------------ .../com/plotsquared/core/command/Alias.java | 210 --------------- .../com/plotsquared/core/command/Claim.java | 244 ------------------ .../plotsquared/core/command/MainCommand.java | 3 - .../core/commands/CommandAlias.java | 136 ++++++++++ .../core/commands/CommandClaim.java | 3 +- .../core/events/PlayerAutoPlotEvent.java | 1 - .../core/events/PlayerClaimPlotEvent.java | 1 - 8 files changed, 137 insertions(+), 633 deletions(-) delete mode 100644 Core/src/main/java/com/plotsquared/core/command/Add.java delete mode 100644 Core/src/main/java/com/plotsquared/core/command/Alias.java delete mode 100644 Core/src/main/java/com/plotsquared/core/command/Claim.java create mode 100644 Core/src/main/java/com/plotsquared/core/commands/CommandAlias.java diff --git a/Core/src/main/java/com/plotsquared/core/command/Add.java b/Core/src/main/java/com/plotsquared/core/command/Add.java deleted file mode 100644 index d70129c27..000000000 --- a/Core/src/main/java/com/plotsquared/core/command/Add.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * PlotSquared, a land and world management plugin for Minecraft. - * Copyright (C) IntellectualSites - * Copyright (C) IntellectualSites team and contributors - * - * 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 . - */ -package com.plotsquared.core.command; - -import com.google.inject.Inject; -import com.plotsquared.core.configuration.Settings; -import com.plotsquared.core.configuration.caption.TranslatableCaption; -import com.plotsquared.core.database.DBFunc; -import com.plotsquared.core.permissions.Permission; -import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.plot.Plot; -import com.plotsquared.core.util.EventDispatcher; -import com.plotsquared.core.util.PlayerManager; -import com.plotsquared.core.util.TabCompletions; -import com.plotsquared.core.util.task.RunnableVal2; -import com.plotsquared.core.util.task.RunnableVal3; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.Tag; -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; -import org.checkerframework.checker.nullness.qual.NonNull; - -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeoutException; - -@CommandDeclaration(command = "add", - usage = "/plot add ", - category = CommandCategory.SETTINGS, - permission = "plots.add", - requiredType = RequiredType.PLAYER) -public class Add extends Command { - - private final EventDispatcher eventDispatcher; - - @Inject - public Add(final @NonNull EventDispatcher eventDispatcher) { - super(MainCommand.getInstance(), true); - this.eventDispatcher = eventDispatcher; - } - - @Override - public CompletableFuture execute( - final PlotPlayer player, - String[] args, - RunnableVal3 confirm, - RunnableVal2 whenDone - ) throws CommandException { - final Plot plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot")); - checkTrue(plot.hasOwner(), TranslatableCaption.of("info.plot_unowned")); - checkTrue( - plot.isOwner(player.getUUID()) || player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_TRUST), - TranslatableCaption.of("permission.no_plot_perms") - ); - checkTrue(args.length == 1, - TranslatableCaption.of("commandconfig.command_syntax"), - TagResolver.resolver("value", Tag.inserting(Component.text("/plot add "))) - ); - final CompletableFuture future = new CompletableFuture<>(); - PlayerManager.getUUIDsFromString(args[0], (uuids, throwable) -> { - if (throwable != null) { - if (throwable instanceof TimeoutException) { - player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout")); - } else { - player.sendMessage( - TranslatableCaption.of("errors.invalid_player"), - TagResolver.resolver("value", Tag.inserting(Component.text(args[0]))) - ); - } - future.completeExceptionally(throwable); - return; - } else { - try { - checkTrue(!uuids.isEmpty(), TranslatableCaption.of("errors.invalid_player"), - TagResolver.resolver("value", Tag.inserting(Component.text(args[0]))) - ); - Iterator iterator = uuids.iterator(); - int size = plot.getTrusted().size() + plot.getMembers().size(); - while (iterator.hasNext()) { - UUID uuid = iterator.next(); - if (uuid == DBFunc.EVERYONE && !(player.hasPermission(Permission.PERMISSION_TRUST_EVERYONE) || player.hasPermission( - Permission.PERMISSION_ADMIN_COMMAND_TRUST))) { - player.sendMessage( - TranslatableCaption.of("errors.invalid_player"), - TagResolver.resolver("value", Tag.inserting( - PlayerManager.resolveName(uuid).toComponent(player) - )) - ); - iterator.remove(); - continue; - } - if (plot.isOwner(uuid)) { - player.sendMessage( - TranslatableCaption.of("member.already_added"), - TagResolver.resolver("player", Tag.inserting( - PlayerManager.resolveName(uuid).toComponent(player) - )) - ); - iterator.remove(); - continue; - } - if (plot.getMembers().contains(uuid)) { - player.sendMessage( - TranslatableCaption.of("member.already_added"), - TagResolver.resolver("player", Tag.inserting( - PlayerManager.resolveName(uuid).toComponent(player) - )) - ); - iterator.remove(); - continue; - } - size += plot.getTrusted().contains(uuid) ? 0 : 1; - } - checkTrue(!uuids.isEmpty(), null); - int localAddSize = plot.getMembers().size(); - int maxAddSize = player.hasPermissionRange(Permission.PERMISSION_ADD, Settings.Limit.MAX_PLOTS); - if (localAddSize >= maxAddSize) { - player.sendMessage( - TranslatableCaption.of("members.plot_max_members_added"), - TagResolver.resolver("amount", Tag.inserting(Component.text(localAddSize))) - ); - return; - } - // Success - confirm.run(this, () -> { - for (UUID uuid : uuids) { - if (uuid != DBFunc.EVERYONE) { - if (!plot.removeTrusted(uuid)) { - if (plot.getDenied().contains(uuid)) { - plot.removeDenied(uuid); - } - } - } - plot.addMember(uuid); - this.eventDispatcher.callMember(player, plot, uuid, true); - player.sendMessage(TranslatableCaption.of("member.member_added")); - } - }, null); - } catch (final Throwable exception) { - future.completeExceptionally(exception); - return; - } - } - future.complete(true); - }); - return future; - } - - @Override - public Collection tab(final PlotPlayer player, final String[] args, final boolean space) { - return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); - } - -} diff --git a/Core/src/main/java/com/plotsquared/core/command/Alias.java b/Core/src/main/java/com/plotsquared/core/command/Alias.java deleted file mode 100644 index a52e47ef2..000000000 --- a/Core/src/main/java/com/plotsquared/core/command/Alias.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * PlotSquared, a land and world management plugin for Minecraft. - * Copyright (C) IntellectualSites - * Copyright (C) IntellectualSites team and contributors - * - * 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 . - */ -package com.plotsquared.core.command; - -import com.plotsquared.core.PlotSquared; -import com.plotsquared.core.configuration.Settings; -import com.plotsquared.core.configuration.caption.TranslatableCaption; -import com.plotsquared.core.location.Location; -import com.plotsquared.core.permissions.Permission; -import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.plot.Plot; -import com.plotsquared.core.util.MathMan; -import com.plotsquared.core.util.query.PlotQuery; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.Tag; -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.TimeoutException; - -@CommandDeclaration(command = "alias", - permission = "plots.alias", - usage = "/plot alias ", - aliases = {"setalias", "sa", "name", "rename", "setname", "seta", "nameplot"}, - category = CommandCategory.SETTINGS, - requiredType = RequiredType.PLAYER) -public class Alias extends SubCommand { - - private static final Command SET_COMMAND = new Command(null, false, "set", null, RequiredType.NONE, null) { - }; - private static final Command REMOVE_COMMAND = new Command(null, false, "remove", null, RequiredType.NONE, null) { - }; - - @Override - public boolean onCommand(PlotPlayer player, String[] args) { - - if (args.length == 0) { - sendUsage(player); - return false; - } - - Location location = player.getLocation(); - Plot plot = location.getPlotAbs(); - if (plot == null) { - player.sendMessage(TranslatableCaption.of("errors.not_in_plot")); - return false; - } - - if (!plot.hasOwner()) { - player.sendMessage(TranslatableCaption.of("working.plot_not_claimed")); - return false; - } - - boolean result = false; - - boolean owner = plot.isOwner(player.getUUID()); - boolean permission; - boolean admin; - switch (args[0].toLowerCase()) { - case "set" -> { - if (args.length != 2) { - sendUsage(player); - return false; - } - permission = isPermitted(player, Permission.PERMISSION_ALIAS_SET); - admin = isPermitted(player, Permission.PERMISSION_ADMIN_ALIAS_SET); - if (!admin && !owner) { - player.sendMessage(TranslatableCaption.of("permission.no_plot_perms")); - return false; - } - if (permission) { // is either admin or owner - setAlias(player, plot, args[1]); - return true; - } else { - player.sendMessage( - TranslatableCaption.of("permission.no_permission"), - TagResolver.resolver( - "node", - Tag.inserting(Permission.PERMISSION_ALIAS_SET) - ) - ); - } - } - case "remove" -> { - permission = isPermitted(player, Permission.PERMISSION_ALIAS_REMOVE); - admin = isPermitted(player, Permission.PERMISSION_ADMIN_ALIAS_REMOVE); - if (!admin && !owner) { - player.sendMessage(TranslatableCaption.of("permission.no_plot_perms")); - return false; - } - if (permission) { - result = removeAlias(player, plot); - } else { - player.sendMessage( - TranslatableCaption.of("permission.no_permission"), - TagResolver.resolver( - "node", - Tag.inserting(Permission.PERMISSION_ALIAS_REMOVE) - ) - ); - } - } - default -> { - sendUsage(player); - result = false; - } - } - - return result; - } - - @Override - public Collection tab(PlotPlayer player, String[] args, boolean space) { - final List commands = new ArrayList<>(2); - if (args.length == 1) { - if ("set".startsWith(args[0])) { - commands.add(SET_COMMAND); - } - if ("remove".startsWith(args[0])) { - commands.add(REMOVE_COMMAND); - } - return commands; - } - return Collections.emptySet(); - } - - private void setAlias(PlotPlayer player, Plot plot, String alias) { - if (alias.isEmpty()) { - sendUsage(player); - } else if (alias.length() >= 50) { - player.sendMessage(TranslatableCaption.of("alias.alias_too_long")); - } else if (MathMan.isInteger(alias)) { - player.sendMessage(TranslatableCaption.of("flag.not_valid_value")); // TODO this is obviously wrong - } else { - if (PlotQuery.newQuery().inArea(plot.getArea()) - .withAlias(alias) - .anyMatch()) { - player.sendMessage( - TranslatableCaption.of("alias.alias_is_taken"), - TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) - ); - return; - } - if (Settings.UUID.OFFLINE) { - plot.setAlias(alias); - player.sendMessage( - TranslatableCaption.of("alias.alias_set_to"), - TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) - ); - return; - } - PlotSquared.get().getImpromptuUUIDPipeline().getSingle(alias, ((uuid, throwable) -> { - if (throwable instanceof TimeoutException) { - player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout")); - } else if (uuid != null) { - player.sendMessage( - TranslatableCaption.of("alias.alias_is_taken"), - TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) - ); - } else { - plot.setAlias(alias); - player.sendMessage( - TranslatableCaption.of("alias.alias_set_to"), - TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) - ); - } - })); - } - } - - private boolean removeAlias(PlotPlayer player, Plot plot) { - String alias = plot.getAlias(); - if (!plot.getAlias().isEmpty()) { - player.sendMessage( - TranslatableCaption.of("alias.alias_removed"), - TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) - ); - } else { - player.sendMessage( - TranslatableCaption.of("alias.no_alias_set") - ); - } - plot.setAlias(null); - return true; - } - - private boolean isPermitted(PlotPlayer player, Permission permission) { - return player.hasPermission(permission); - } - -} diff --git a/Core/src/main/java/com/plotsquared/core/command/Claim.java b/Core/src/main/java/com/plotsquared/core/command/Claim.java deleted file mode 100644 index e9848685c..000000000 --- a/Core/src/main/java/com/plotsquared/core/command/Claim.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * PlotSquared, a land and world management plugin for Minecraft. - * Copyright (C) IntellectualSites - * Copyright (C) IntellectualSites team and contributors - * - * 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 . - */ -package com.plotsquared.core.command; - -import com.google.inject.Inject; -import com.plotsquared.core.configuration.Settings; -import com.plotsquared.core.configuration.caption.TranslatableCaption; -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.permissions.Permission; -import com.plotsquared.core.player.MetaDataAccess; -import com.plotsquared.core.player.PlayerMetaDataKeys; -import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.plot.Plot; -import com.plotsquared.core.plot.PlotArea; -import com.plotsquared.core.util.EconHandler; -import com.plotsquared.core.util.EventDispatcher; -import com.plotsquared.core.util.PlotExpression; -import com.plotsquared.core.util.task.TaskManager; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.Tag; -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.checkerframework.checker.nullness.qual.NonNull; - -@CommandDeclaration( - command = "claim", - aliases = "c", - category = CommandCategory.CLAIMING, - requiredType = RequiredType.PLAYER, permission = "plots.claim", - usage = "/plot claim") -public class Claim extends SubCommand { - - private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + Claim.class.getSimpleName()); - - private final EventDispatcher eventDispatcher; - private final EconHandler econHandler; - - @Inject - public Claim( - final @NonNull EventDispatcher eventDispatcher, - final @NonNull EconHandler econHandler - ) { - this.eventDispatcher = eventDispatcher; - this.econHandler = econHandler; - } - - @Override - public boolean onCommand(final PlotPlayer player, String[] args) { - String schematic = null; - if (args.length >= 1) { - schematic = args[0]; - } - Location location = player.getLocation(); - Plot plot = location.getPlotAbs(); - if (plot == null) { - player.sendMessage(TranslatableCaption.of("errors.not_in_plot")); - return false; - } - final PlayerClaimPlotEvent event = this.eventDispatcher.callClaim(player, plot, schematic); - schematic = event.getSchematic(); - if (event.getEventResult() == Result.DENY) { - player.sendMessage( - TranslatableCaption.of("events.event_denied"), - TagResolver.resolver("value", Tag.inserting(Component.text("Claim"))) - ); - return true; - } - boolean force = event.getEventResult() == Result.FORCE; - int currentPlots = Settings.Limit.GLOBAL ? - player.getPlotCount() : - player.getPlotCount(location.getWorldName()); - - final PlotArea area = plot.getArea(); - - try (final MetaDataAccess metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) { - int grants = 0; - if (currentPlots >= player.getAllowedPlots() && !force) { - if (metaDataAccess.isPresent()) { - grants = metaDataAccess.get().orElse(0); - if (grants <= 0) { - player.sendMessage( - TranslatableCaption.of("permission.cant_claim_more_plots"), - TagResolver.resolver("amount", Tag.inserting(Component.text(grants))) - ); - metaDataAccess.remove(); - } - } else { - player.sendMessage( - TranslatableCaption.of("permission.cant_claim_more_plots"), - TagResolver.resolver("amount", Tag.inserting(Component.text(player.getAllowedPlots()))) - ); - return false; - } - } - - if (!plot.canClaim(player)) { - player.sendMessage(TranslatableCaption.of("working.plot_is_claimed")); - return false; - } - if (schematic != null && !schematic.isEmpty()) { - if (area.isSchematicClaimSpecify()) { - if (!area.hasSchematic(schematic)) { - player.sendMessage( - TranslatableCaption.of("schematics.schematic_invalid_named"), - TagResolver.builder() - .tag("schemname", Tag.inserting(Component.text(schematic))) - .tag("reason", Tag.inserting(Component.text("non-existent"))) - .build() - ); - } - if (!player.hasPermission(Permission.PERMISSION_CLAIM_SCHEMATIC - .format(schematic)) && !player.hasPermission( - "plots.admin.command.schematic" - ) && !force) { - player.sendMessage( - TranslatableCaption.of("permission.no_schematic_permission"), - TagResolver.resolver("value", Tag.inserting(Component.text(schematic))) - ); - } - } - } - if (this.econHandler.isEnabled(area) && !force) { - PlotExpression costExr = area.getPrices().get("claim"); - double cost = costExr.evaluate(currentPlots); - if (cost > 0d) { - if (!this.econHandler.isSupported()) { - player.sendMessage(TranslatableCaption.of("economy.vault_or_consumer_null")); - return false; - } - if (this.econHandler.getMoney(player) < cost) { - player.sendMessage( - TranslatableCaption.of("economy.cannot_afford_plot"), - TagResolver.builder() - .tag("money", Tag.inserting(Component.text(this.econHandler.format(cost)))) - .tag( - "balance", - Tag.inserting(Component.text(this.econHandler.format(this.econHandler.getMoney( - player)))) - ) - .build() - ); - return false; - } - this.econHandler.withdrawMoney(player, cost); - player.sendMessage( - TranslatableCaption.of("economy.removed_balance"), - TagResolver.builder() - .tag("money", Tag.inserting(Component.text(this.econHandler.format(cost)))) - .tag( - "balance", - Tag.inserting(Component.text(this.econHandler.format(this.econHandler.getMoney( - player)))) - ) - .build() - ); - } - } - if (grants > 0) { - if (grants == 1) { - metaDataAccess.remove(); - } else { - metaDataAccess.set(grants - 1); - } - player.sendMessage( - TranslatableCaption.of("economy.removed_granted_plot"), - TagResolver.builder() - .tag("usedGrants", Tag.inserting(Component.text(grants - 1))) - .tag("remainingGrants", Tag.inserting(Component.text(grants))) - .build() - ); - } - } - if (!player.hasPermission(Permission.PERMISSION_ADMIN_BYPASS_BORDER)) { - int border = area.getBorder(); - if (border != Integer.MAX_VALUE && plot.getDistanceFromOrigin() > border && !force) { - player.sendMessage(TranslatableCaption.of("border.denied")); - return false; - } - } - plot.setOwnerAbs(player.getUUID()); - final String finalSchematic = schematic; - DBFunc.createPlotSafe(plot, () -> { - try { - TaskManager.getPlatformImplementation().sync(() -> { - if (!plot.claim(player, true, finalSchematic, false, false)) { - LOGGER.info("Failed to claim plot {}", plot.getId().toCommaSeparatedString()); - player.sendMessage(TranslatableCaption.of("working.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) { - player.sendMessage( - TranslatableCaption.of("events.event_denied"), - TagResolver.resolver("value", Tag.inserting(Component.text("Auto merge on claim"))) - ); - } else { - if (plot.getPlotModificationManager().autoMerge( - mergeEvent.getDir(), - mergeEvent.getMax(), - player.getUUID(), - player, - true - )) { - eventDispatcher.callPostMerge(player, plot); - } - } - } - return null; - }); - } catch (final Exception e) { - e.printStackTrace(); - } - }, () -> { - LOGGER.info("Failed to add plot to database: {}", plot.getId().toCommaSeparatedString()); - player.sendMessage(TranslatableCaption.of("working.plot_not_claimed")); - plot.setOwnerAbs(null); - }); - return true; - } - -} diff --git a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java index 1911f24e9..4c88d5f3b 100644 --- a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java @@ -86,7 +86,6 @@ public class MainCommand extends Command { commands.add(CreateRoadSchematic.class); commands.add(DebugAllowUnsafe.class); commands.add(RegenAllRoads.class); - commands.add(Claim.class); commands.add(Auto.class); commands.add(HomeCommand.class); commands.add(Visit.class); @@ -94,7 +93,6 @@ public class MainCommand extends Command { commands.add(Clear.class); commands.add(Delete.class); commands.add(Trust.class); - commands.add(Add.class); commands.add(Leave.class); commands.add(Deny.class); commands.add(Remove.class); @@ -130,7 +128,6 @@ public class MainCommand extends Command { commands.add(Owner.class); commands.add(Desc.class); commands.add(Biome.class); - commands.add(Alias.class); commands.add(SetHome.class); commands.add(Cluster.class); commands.add(DebugImportWorlds.class); diff --git a/Core/src/main/java/com/plotsquared/core/commands/CommandAlias.java b/Core/src/main/java/com/plotsquared/core/commands/CommandAlias.java new file mode 100644 index 000000000..4e4051d10 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/commands/CommandAlias.java @@ -0,0 +1,136 @@ +package com.plotsquared.core.commands; + +import cloud.commandframework.annotations.Argument; +import cloud.commandframework.annotations.CommandMethod; +import cloud.commandframework.annotations.CommandPermission; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.commands.requirements.CommandRequirement; +import com.plotsquared.core.commands.requirements.Requirement; +import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.permissions.Permission; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.util.MathMan; +import com.plotsquared.core.util.query.PlotQuery; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.tag.Tag; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.concurrent.TimeoutException; + +public class CommandAlias implements PlotSquaredCommandContainer { + + @Requirement(CommandRequirement.PLAYER) + @Requirement(CommandRequirement.PLOT_HAS_OWNER) + @CommandPermission("plots.alias") + @CommandMethod("${command.prefix} alias set ") + public void commandAliasSet( + final @NonNull PlotPlayer sender, + final @NonNull Plot plot, + @Argument("alias") final @NonNull String alias + ) { + final boolean isOwner = plot.isOwner(sender.getUUID()); + + if (!isOwner && !sender.hasPermission(Permission.PERMISSION_ADMIN_ALIAS_SET)) { + sender.sendMessage(TranslatableCaption.of("permission.no_plot_perms")); + return; + } else if (!sender.hasPermission(Permission.PERMISSION_ALIAS_SET)) { + sender.sendMessage( + TranslatableCaption.of("permission.no_permission"), + TagResolver.resolver( + "node", + Tag.inserting(Permission.PERMISSION_ALIAS_SET) + ) + ); + return; + } + + if (alias.length() >= 50) { + sender.sendMessage(TranslatableCaption.of("alias.alias_too_long")); + return; + } + + if (MathMan.isInteger(alias)) { + sender.sendMessage(TranslatableCaption.of("flag.not_valid_value")); // TODO this is obviously wrong + return; + } + + if (PlotQuery.newQuery().inArea(plot.getArea()) + .withAlias(alias) + .anyMatch()) { + sender.sendMessage( + TranslatableCaption.of("alias.alias_is_taken"), + TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) + ); + return; + } + + if (Settings.UUID.OFFLINE) { + plot.setAlias(alias); + sender.sendMessage( + TranslatableCaption.of("alias.alias_set_to"), + TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) + ); + return; + } + + PlotSquared.get().getImpromptuUUIDPipeline().getSingle(alias, ((uuid, throwable) -> { + if (throwable instanceof TimeoutException) { + sender.sendMessage(TranslatableCaption.of("players.fetching_players_timeout")); + } else if (uuid != null) { + sender.sendMessage( + TranslatableCaption.of("alias.alias_is_taken"), + TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) + ); + } else { + plot.setAlias(alias); + sender.sendMessage( + TranslatableCaption.of("alias.alias_set_to"), + TagResolver.resolver("alias", Tag.inserting(Component.text(alias))) + ); + } + })); + } + + @Requirement(CommandRequirement.PLAYER) + @Requirement(CommandRequirement.PLOT_HAS_OWNER) + @CommandPermission("plots.alias") + @CommandMethod("${command.prefix} alias remove") + public void commandAliasRemove( + final @NonNull PlotPlayer sender, + final @NonNull Plot plot + ) { + final boolean isOwner = plot.isOwner(sender.getUUID()); + + if (!isOwner && !sender.hasPermission(Permission.PERMISSION_ADMIN_ALIAS_REMOVE)) { + sender.sendMessage(TranslatableCaption.of("permission.no_plot_perms")); + return; + } else if (!sender.hasPermission(Permission.PERMISSION_ALIAS_REMOVE)) { + sender.sendMessage( + TranslatableCaption.of("permission.no_permission"), + TagResolver.resolver( + "node", + Tag.inserting(Permission.PERMISSION_ALIAS_REMOVE) + ) + ); + return; + } + + if (plot.getAlias().isEmpty()) { + sender.sendMessage( + TranslatableCaption.of("alias.no_alias_set") + ); + return; + } + + final String currentAlias = plot.getAlias(); + plot.setAlias(null); + + sender.sendMessage( + TranslatableCaption.of("alias.alias_removed"), + TagResolver.resolver("alias", Tag.inserting(Component.text(currentAlias))) + ); + } +} diff --git a/Core/src/main/java/com/plotsquared/core/commands/CommandClaim.java b/Core/src/main/java/com/plotsquared/core/commands/CommandClaim.java index 4a113ee60..069f3cd1a 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/CommandClaim.java +++ b/Core/src/main/java/com/plotsquared/core/commands/CommandClaim.java @@ -4,7 +4,6 @@ import cloud.commandframework.annotations.Argument; import cloud.commandframework.annotations.CommandMethod; import cloud.commandframework.annotations.CommandPermission; import com.google.inject.Inject; -import com.plotsquared.core.command.Claim; import com.plotsquared.core.commands.requirements.CommandRequirement; import com.plotsquared.core.commands.requirements.Requirement; import com.plotsquared.core.configuration.Settings; @@ -34,7 +33,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class CommandClaim implements PlotSquaredCommandContainer { - private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + Claim.class.getSimpleName()); + private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + CommandClaim.class.getSimpleName()); private final EventDispatcher eventDispatcher; private final EconHandler econHandler; diff --git a/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java index 97b4cd5fb..4aa7df8d4 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java @@ -18,7 +18,6 @@ */ package com.plotsquared.core.events; -import com.plotsquared.core.command.Claim; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; diff --git a/Core/src/main/java/com/plotsquared/core/events/PlayerClaimPlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlayerClaimPlotEvent.java index d763b52d7..a7572adb4 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlayerClaimPlotEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerClaimPlotEvent.java @@ -18,7 +18,6 @@ */ package com.plotsquared.core.events; -import com.plotsquared.core.command.Claim; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import org.checkerframework.checker.nullness.qual.Nullable;