Don't check same plot over and over again

This commit is contained in:
SirYwell 2021-09-07 09:32:34 +02:00
parent b224a8d1b8
commit c25a0c65a2

View File

@ -139,13 +139,18 @@ public interface AutoService extends Service<AutoService.AutoQuery, List<Plot>>
@Override @Override
public List<Plot> handle(@NonNull AutoQuery autoQuery) { public List<Plot> handle(@NonNull AutoQuery autoQuery) {
Plot plot; Plot plot;
PlotId nextId = autoQuery.getStartId();
do { do {
synchronized (plotLock) { synchronized (plotLock) {
plot = autoQuery.getPlotArea().getNextFreePlot(autoQuery.getPlayer(), autoQuery.getStartId()); plot = autoQuery.getPlotArea().getNextFreePlot(autoQuery.getPlayer(), nextId);
if (plot != null && plotCandidateCache.getIfPresent(plot.getId()) == null) { if (plot != null && plotCandidateCache.getIfPresent(plot.getId()) == null) {
plotCandidateCache.put(plot.getId(), plot); plotCandidateCache.put(plot.getId(), plot);
return Collections.singletonList(plot); return Collections.singletonList(plot);
} }
// if the plot is already in the cache, we want to make sure we skip it the next time
if (plot != null) {
nextId = plot.getId();
}
} }
} while (plot != null); } while (plot != null);
return null; return null;