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