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.Settings; 024import com.plotsquared.core.configuration.caption.Templates; 025import com.plotsquared.core.configuration.caption.TranslatableCaption; 026import com.plotsquared.core.events.TeleportCause; 027import com.plotsquared.core.permissions.Permission; 028import com.plotsquared.core.player.PlotPlayer; 029import com.plotsquared.core.plot.Plot; 030import com.plotsquared.core.plot.PlotArea; 031import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; 032import com.plotsquared.core.plot.world.PlotAreaManager; 033import com.plotsquared.core.util.MathMan; 034import com.plotsquared.core.util.PlayerManager; 035import com.plotsquared.core.util.TabCompletions; 036import com.plotsquared.core.util.query.PlotQuery; 037import com.plotsquared.core.util.query.SortingStrategy; 038import com.plotsquared.core.util.task.RunnableVal2; 039import com.plotsquared.core.util.task.RunnableVal3; 040import net.kyori.adventure.text.minimessage.Template; 041import org.checkerframework.checker.nullness.qual.NonNull; 042 043import java.util.ArrayList; 044import java.util.Collection; 045import java.util.Collections; 046import java.util.List; 047import java.util.UUID; 048import java.util.concurrent.CompletableFuture; 049import java.util.concurrent.TimeoutException; 050 051@CommandDeclaration(command = "visit", 052 permission = "plots.visit", 053 usage = "/plot visit <player> | <alias> | <plot> [area]|[#] [#]", 054 aliases = {"v", "tp", "teleport", "goto", "warp"}, 055 requiredType = RequiredType.PLAYER, 056 category = CommandCategory.TELEPORT) 057public class Visit extends Command { 058 059 private final PlotAreaManager plotAreaManager; 060 061 @Inject 062 public Visit(final @NonNull PlotAreaManager plotAreaManager) { 063 super(MainCommand.getInstance(), true); 064 this.plotAreaManager = plotAreaManager; 065 } 066 067 private void visit( 068 final @NonNull PlotPlayer<?> player, final @NonNull PlotQuery query, final PlotArea sortByArea, 069 final RunnableVal3<Command, Runnable, Runnable> confirm, final RunnableVal2<Command, CommandResult> whenDone, int page 070 ) { 071 // We get the query once, 072 // then we get it another time further on 073 final List<Plot> unsorted = query.asList(); 074 075 if (unsorted.size() > 1) { 076 query.whereBasePlot(); 077 } 078 079 if (page == Integer.MIN_VALUE) { 080 page = 1; 081 } 082 083 PlotArea relativeArea = sortByArea; 084 if (Settings.Teleport.PER_WORLD_VISIT && sortByArea == null) { 085 relativeArea = player.getApplicablePlotArea(); 086 } 087 088 if (relativeArea != null) { 089 query.relativeToArea(relativeArea).withSortingStrategy(SortingStrategy.SORT_BY_CREATION); 090 } else { 091 query.withSortingStrategy(SortingStrategy.SORT_BY_TEMP); 092 } 093 094 final List<Plot> plots = query.asList(); 095 096 if (plots.isEmpty()) { 097 player.sendMessage(TranslatableCaption.of("invalid.found_no_plots")); 098 return; 099 } else if (plots.size() < page || page < 1) { 100 player.sendMessage( 101 TranslatableCaption.of("invalid.number_not_in_range"), 102 Template.of("min", "1"), 103 Template.of("max", String.valueOf(plots.size())) 104 ); 105 return; 106 } 107 108 final Plot plot = plots.get(page - 1); 109 if (!plot.hasOwner()) { 110 if (!player.hasPermission(Permission.PERMISSION_VISIT_UNOWNED)) { 111 player.sendMessage( 112 TranslatableCaption.of("permission.no_permission"), 113 Templates.of("node", "plots.visit.unowned") 114 ); 115 return; 116 } 117 } else if (plot.isOwner(player.getUUID())) { 118 if (!player.hasPermission(Permission.PERMISSION_VISIT_OWNED) && !player.hasPermission(Permission.PERMISSION_HOME)) { 119 player.sendMessage( 120 TranslatableCaption.of("permission.no_permission"), 121 Templates.of("node", "plots.visit.owned") 122 ); 123 return; 124 } 125 } else if (plot.isAdded(player.getUUID())) { 126 if (!player.hasPermission(Permission.PERMISSION_SHARED)) { 127 player.sendMessage( 128 TranslatableCaption.of("permission.no_permission"), 129 Templates.of("node", "plots.visit.shared") 130 ); 131 return; 132 } 133 } else { 134 // allow visit, if UntrustedVisit flag is set, or if the player has either the plot.visit.other or 135 // plot.admin.visit.untrusted permission 136 if (!plot.getFlag(UntrustedVisitFlag.class) && !player.hasPermission(Permission.PERMISSION_VISIT_OTHER) 137 && !player.hasPermission(Permission.PERMISSION_ADMIN_VISIT_UNTRUSTED)) { 138 player.sendMessage( 139 TranslatableCaption.of("permission.no_permission"), 140 Templates.of("node", "plots.visit.other") 141 ); 142 return; 143 } 144 if (plot.isDenied(player.getUUID())) { 145 if (!player.hasPermission(Permission.PERMISSION_VISIT_DENIED)) { 146 player.sendMessage( 147 TranslatableCaption.of("permission.no_permission"), 148 Template.of("node", String.valueOf(Permission.PERMISSION_VISIT_DENIED)) 149 ); 150 return; 151 } 152 } 153 } 154 155 confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND_VISIT, result -> { 156 if (result) { 157 whenDone.run(Visit.this, CommandResult.SUCCESS); 158 } else { 159 whenDone.run(Visit.this, CommandResult.FAILURE); 160 } 161 }), () -> whenDone.run(Visit.this, CommandResult.FAILURE)); 162 } 163 164 @Override 165 public CompletableFuture<Boolean> execute( 166 final PlotPlayer<?> player, 167 String[] args, 168 final RunnableVal3<Command, Runnable, Runnable> confirm, 169 final RunnableVal2<Command, CommandResult> whenDone 170 ) throws CommandException { 171 if (args.length > 3) { 172 sendUsage(player); 173 return CompletableFuture.completedFuture(false); 174 } 175 176 if (args.length == 1 && args[0].contains(":")) { 177 args = args[0].split(":"); 178 } 179 180 PlotArea sortByArea; 181 182 int page = Integer.MIN_VALUE; 183 184 switch (args.length) { 185 // /p v <user> <area> <page> 186 case 3: 187 if (!MathMan.isInteger(args[2])) { 188 player.sendMessage( 189 TranslatableCaption.of("invalid.not_valid_number"), 190 Templates.of("value", "(1, ∞)") 191 ); 192 player.sendMessage( 193 TranslatableCaption.of("commandconfig.command_syntax"), 194 Templates.of("value", getUsage()) 195 ); 196 return CompletableFuture.completedFuture(false); 197 } 198 page = Integer.parseInt(args[2]); 199 // /p v <name> <area> [page] 200 // /p v <name> [page] 201 case 2: 202 if (page != Integer.MIN_VALUE || !MathMan.isInteger(args[1])) { 203 sortByArea = this.plotAreaManager.getPlotAreaByString(args[1]); 204 if (sortByArea == null) { 205 player.sendMessage( 206 TranslatableCaption.of("invalid.not_valid_number"), 207 Templates.of("value", "(1, ∞)") 208 ); 209 player.sendMessage( 210 TranslatableCaption.of("commandconfig.command_syntax"), 211 Templates.of("value", getUsage()) 212 ); 213 return CompletableFuture.completedFuture(false); 214 } 215 216 final PlotArea finalSortByArea = sortByArea; 217 int finalPage1 = page; 218 PlayerManager.getUUIDsFromString(args[0], (uuids, throwable) -> { 219 if (throwable instanceof TimeoutException) { 220 player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout")); 221 } else if (throwable != null || uuids.size() != 1) { 222 player.sendMessage( 223 TranslatableCaption.of("commandconfig.command_syntax"), 224 Templates.of("value", getUsage()) 225 ); 226 } else { 227 final UUID uuid = uuids.toArray(new UUID[0])[0]; 228 PlotQuery query = PlotQuery.newQuery(); 229 if (Settings.Teleport.VISIT_MERGED_OWNERS) { 230 query.whereBasePlot().ownersInclude(uuid); 231 } else { 232 query.whereBasePlot().ownedBy(uuid); 233 } 234 this.visit( 235 player, 236 query, 237 finalSortByArea, 238 confirm, 239 whenDone, 240 finalPage1 241 ); 242 } 243 }); 244 break; 245 } 246 try { 247 page = Integer.parseInt(args[1]); 248 } catch (NumberFormatException ignored) { 249 player.sendMessage( 250 TranslatableCaption.of("invalid.not_a_number"), 251 Template.of("value", args[1]) 252 ); 253 return CompletableFuture.completedFuture(false); 254 } 255 // /p v <name> [page] 256 // /p v <uuid> [page] 257 // /p v <plot> [page] 258 // /p v <alias> 259 case 1: 260 final String[] finalArgs = args; 261 int finalPage = page; 262 if (args[0].length() >= 2 && !args[0].contains(";") && !args[0].contains(",")) { 263 PlotSquared.get().getImpromptuUUIDPipeline().getSingle(args[0], (uuid, throwable) -> { 264 if (throwable instanceof TimeoutException) { 265 // The request timed out 266 player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout")); 267 } else if (uuid != null && (Settings.Teleport.VISIT_MERGED_OWNERS 268 ? !PlotQuery.newQuery().ownersInclude(uuid).anyMatch() 269 : !PlotQuery.newQuery().ownedBy(uuid).anyMatch())) { 270 // It was a valid UUID but the player has no plots 271 player.sendMessage(TranslatableCaption.of("errors.player_no_plots")); 272 } else if (uuid == null) { 273 // player not found, so we assume it's an alias if no page was provided 274 if (finalPage == Integer.MIN_VALUE) { 275 this.visit( 276 player, 277 PlotQuery.newQuery().withAlias(finalArgs[0]), 278 player.getApplicablePlotArea(), 279 confirm, 280 whenDone, 281 1 282 ); 283 } else { 284 player.sendMessage( 285 TranslatableCaption.of("errors.invalid_player"), 286 Template.of("value", finalArgs[0]) 287 ); 288 } 289 } else { 290 this.visit( 291 player, 292 Settings.Teleport.VISIT_MERGED_OWNERS 293 ? PlotQuery.newQuery().ownersInclude(uuid).whereBasePlot() 294 : PlotQuery.newQuery().ownedBy(uuid).whereBasePlot(), 295 null, 296 confirm, 297 whenDone, 298 finalPage 299 ); 300 } 301 }); 302 } else { 303 // Try to parse a plot 304 final Plot plot = Plot.getPlotFromString(player, finalArgs[0], true); 305 if (plot != null) { 306 this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1); 307 } 308 } 309 break; 310 case 0: 311 // /p v is invalid 312 player.sendMessage( 313 TranslatableCaption.of("commandconfig.command_syntax"), 314 Templates.of("value", getUsage()) 315 ); 316 return CompletableFuture.completedFuture(false); 317 default: 318 } 319 320 return CompletableFuture.completedFuture(true); 321 } 322 323 @Override 324 public Collection<Command> tab(PlotPlayer<?> player, String[] args, boolean space) { 325 final List<Command> completions = new ArrayList<>(); 326 switch (args.length - 1) { 327 case 0 -> completions.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); 328 case 1 -> { 329 completions.addAll( 330 TabCompletions.completeAreas(args[1])); 331 if (args[1].isEmpty()) { 332 // if no input is given, only suggest 1 - 3 333 completions.addAll( 334 TabCompletions.asCompletions("1", "2", "3")); 335 break; 336 } 337 completions.addAll( 338 TabCompletions.completeNumbers(args[1], 10, 999)); 339 } 340 case 2 -> { 341 if (args[2].isEmpty()) { 342 // if no input is given, only suggest 1 - 3 343 completions.addAll( 344 TabCompletions.asCompletions("1", "2", "3")); 345 break; 346 } 347 completions.addAll( 348 TabCompletions.completeNumbers(args[2], 10, 999)); 349 } 350 } 351 352 return completions; 353 } 354 355}