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.plotsquared.core.configuration.caption.TranslatableCaption; 022import com.plotsquared.core.location.Location; 023import com.plotsquared.core.player.PlotPlayer; 024import com.plotsquared.core.plot.Plot; 025import com.plotsquared.core.util.StringMan; 026import com.plotsquared.core.util.query.PlotQuery; 027import net.kyori.adventure.text.minimessage.Template; 028 029import java.util.Collection; 030import java.util.Locale; 031import java.util.stream.Collectors; 032import java.util.stream.Stream; 033 034@CommandDeclaration(command = "target", 035 usage = "/plot target <<X;Z> | nearest>", 036 permission = "plots.target", 037 requiredType = RequiredType.PLAYER, 038 category = CommandCategory.INFO) 039public class Target extends SubCommand { 040 041 public Target() { 042 super(); 043 } 044 045 @Override 046 public boolean onCommand(PlotPlayer<?> player, String[] args) { 047 Location location = player.getLocation(); 048 if (!location.isPlotArea()) { 049 player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world")); 050 return false; 051 } 052 if (args.length == 0) { 053 player.sendMessage( 054 TranslatableCaption.of("commandconfig.command_syntax"), 055 Template.of("value", "/plot target <<X;Z> | nearest>") 056 ); 057 return false; 058 } 059 Plot target = null; 060 if (StringMan.isEqualIgnoreCaseToAny(args[0], "near", "nearest")) { 061 int distance = Integer.MAX_VALUE; 062 for (Plot plot : PlotQuery.newQuery().inWorld(location.getWorldName())) { 063 double current = plot.getCenterSynchronous().getEuclideanDistanceSquared(location); 064 if (current < distance) { 065 distance = (int) current; 066 target = plot; 067 } 068 } 069 if (target == null) { 070 player.sendMessage(TranslatableCaption.of("invalid.found_no_plots")); 071 return false; 072 } 073 } else if ((target = Plot.getPlotFromString(player, args[0], true)) == null) { 074 return false; 075 } 076 target.getCenter(player::setCompassTarget); 077 player.sendMessage( 078 TranslatableCaption.of("compass.compass_target"), 079 Template.of("target", target.toString()) 080 ); 081 return true; 082 } 083 084 @Override 085 public Collection<Command> tab(final PlotPlayer<?> player, String[] args, boolean space) { 086 return Stream.of("<X;Z>", "nearest") 087 .filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH))) 088 .map(value -> new Command(null, false, value, "plots.target", RequiredType.PLAYER, null) { 089 }).collect(Collectors.toList()); 090 } 091 092}