Skip to content

Commit

Permalink
[1.9.3] Made getByRegex return latest versions first
Browse files Browse the repository at this point in the history
  • Loading branch information
3arthqu4ke committed Apr 1, 2024
1 parent 9ff42ea commit bfbd21f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.earth.headlessmc.api;

import java.util.List;
import java.util.Queue;
import java.util.regex.Pattern;

/**
Expand All @@ -20,8 +18,8 @@ static <T extends HasName> T getByName(String name, Iterable<T> nameables) {

static <T extends HasName> T getByRegex(Pattern regex, Iterable<T> nameables) {
T best = null;
for (T t : nameables) {
if (regex.matcher(t.getName()).find() && (best == null || String.CASE_INSENSITIVE_ORDER.compare(t.getName(), best.getName()) < 0)) {
for (T t : nameables) { // compare so that later versions get picked, e.g. fabric-loader-0.15.9-1.20.4 over fabric-loader-0.15.7-1.20.4
if (regex.matcher(t.getName()).find() && (best == null || String.CASE_INSENSITIVE_ORDER.compare(t.getName(), best.getName()) > 0)) {
best = t;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getName() {

getByRegex = HasName.getByRegex(Pattern.compile("Test[0-9]"), names);
assertNotNull(getByRegex);
assertEquals("Test0", getByRegex.getName()); // should always be the first
assertEquals("Test" + i, getByRegex.getName()); // should always be the first
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testFindByRegex() {
command.execute("test", "Object1", "-regex");
assertEquals("Object1", command.obj.getName());
command.execute("test", "Object.*", "-regex");
assertEquals("Object1", command.obj.getName());
assertEquals("Object3", command.obj.getName());
}

private static class TestObject implements HasName, HasId {
Expand Down

0 comments on commit bfbd21f

Please sign in to comment.