001/* 002 * This file is part of Baritone. 003 * 004 * Baritone is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Lesser General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * Baritone is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public License 015 * along with Baritone. If not, see <https://www.gnu.org/licenses/>. 016 */ 017 018package baritone.api.command.datatypes; 019 020import baritone.api.IBaritone; 021import baritone.api.cache.IWaypoint; 022import baritone.api.cache.IWaypointCollection; 023import baritone.api.command.exception.CommandException; 024import baritone.api.command.helpers.TabCompleteHelper; 025 026import java.util.Comparator; 027import java.util.stream.Stream; 028 029public enum ForWaypoints implements IDatatypeFor<IWaypoint[]> { 030 INSTANCE; 031 032 @Override 033 public IWaypoint[] get(IDatatypeContext ctx) throws CommandException { 034 final String input = ctx.getConsumer().getString(); 035 final IWaypoint.Tag tag = IWaypoint.Tag.getByName(input); 036 037 // If the input doesn't resolve to a valid tag, resolve by name 038 return tag == null 039 ? getWaypointsByName(ctx.getBaritone(), input) 040 : getWaypointsByTag(ctx.getBaritone(), tag); 041 } 042 043 @Override 044 public Stream<String> tabComplete(IDatatypeContext ctx) throws CommandException { 045 return new TabCompleteHelper() 046 .append(getWaypointNames(ctx.getBaritone())) 047 .sortAlphabetically() 048 .prepend(IWaypoint.Tag.getAllNames()) 049 .filterPrefix(ctx.getConsumer().getString()) 050 .stream(); 051 } 052 053 public static IWaypointCollection waypoints(IBaritone baritone) { 054 return baritone.getWorldProvider().getCurrentWorld().getWaypoints(); 055 } 056 057 public static IWaypoint[] getWaypoints(IBaritone baritone) { 058 return waypoints(baritone).getAllWaypoints().stream() 059 .sorted(Comparator.comparingLong(IWaypoint::getCreationTimestamp).reversed()) 060 .toArray(IWaypoint[]::new); 061 } 062 063 public static String[] getWaypointNames(IBaritone baritone) { 064 return Stream.of(getWaypoints(baritone)) 065 .map(IWaypoint::getName) 066 .filter(name -> !name.isEmpty()) 067 .toArray(String[]::new); 068 } 069 070 public static IWaypoint[] getWaypointsByTag(IBaritone baritone, IWaypoint.Tag tag) { 071 return waypoints(baritone).getByTag(tag).stream() 072 .sorted(Comparator.comparingLong(IWaypoint::getCreationTimestamp).reversed()) 073 .toArray(IWaypoint[]::new); 074 } 075 076 public static IWaypoint[] getWaypointsByName(IBaritone baritone, String name) { 077 return Stream.of(getWaypoints(baritone)) 078 .filter(waypoint -> waypoint.getName().equalsIgnoreCase(name)) 079 .toArray(IWaypoint[]::new); 080 } 081}