Implement /search result ordering by distance

This commit is contained in:
Gabriel Tofvesson 2021-06-30 00:04:05 +02:00
parent cb10fa6720
commit a3f0fcf5aa

View File

@ -15,6 +15,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import java.util.Comparator;
import java.util.logging.Logger; import java.util.logging.Logger;
import static dev.w1zzrd.invtweaks.listener.TabCompletionListener.getMaterialMatching; import static dev.w1zzrd.invtweaks.listener.TabCompletionListener.getMaterialMatching;
@ -49,11 +50,12 @@ public class SearchCommandExecutor extends ConfigurableCommandExecutor<SearchCon
) return true; ) return true;
final Player player = (Player) sender; final Player player = (Player) sender;
final Location playerLocation = player.getLocation();
final SearchConfig config = getConfig(); final SearchConfig config = getConfig();
final List<BlockState> matches = searchChunks( final List<BlockState> matches = searchChunks(
player.getLocation().getChunk(), playerLocation.getChunk(),
config.getSearchRadiusX(), config.getSearchRadiusX(),
Material.CHEST, Material.TRAPPED_CHEST, Material.SHULKER_BOX Material.CHEST, Material.TRAPPED_CHEST, Material.SHULKER_BOX
); );
@ -62,6 +64,8 @@ public class SearchCommandExecutor extends ConfigurableCommandExecutor<SearchCon
if (assertTrue(matches.size() != 0, ERR_NO_INVENTORIES, sender)) if (assertTrue(matches.size() != 0, ERR_NO_INVENTORIES, sender))
return true; return true;
matches.sort(Comparator.comparingDouble(state -> state.getLocation().distanceSquared(playerLocation)));
final InventoryHolder result; final InventoryHolder result;
FIND_RESULT: FIND_RESULT: