Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ private boolean dispatchCommand(CommandSender sender, Command cmd, String label,
p.sendMessage("搂cRGB values must be numbers (e.g. 128,128,128)");
} else if(result == 3) {
p.sendMessage("搂cEach RGB value must be between 0 and 255");
} else if(!rgb.equalsIgnoreCase(g.getRGB()) && !RandomRGB.isFree(rgb)) {
} else if(result == 4) {
p.sendMessage("搂c0,0,0 is reserved for unclaimed land; pick another colour");
} else if(!RandomRGB.sameRGB(rgb, g.getRGB()) && !RandomRGB.isFree(rgb)) {
p.sendMessage("搂cThat colour is already used");
} else {
g.setRGB(rgb);
Expand Down Expand Up @@ -837,9 +839,12 @@ private boolean dispatchCommand(CommandSender sender, Command cmd, String label,
String rgb = args[1];
int result = FactionManager.validateRGB(rgb);

if(result == 0) {
Faction f = FactionManager.getByLeader(Members.key(p));
FactionManager.getMap().enqueue("nation", f.getRGB());
Faction f = FactionManager.getByLeader(Members.key(p));
String old = f.getRGB();
if(result == 0 && !RandomRGB.sameRGB(rgb, old) && !RandomRGB.isFree(rgb)) {
p.sendMessage("搂cThat colour is already used");
} else if(result == 0) {
FactionManager.getMap().enqueue("nation", old);
f.setRGB(rgb);
FactionManager.getMap().enqueue("nation", f.getRGB());
p.sendMessage("搂aFaction colour updated to 搂f" + rgb);
Expand All @@ -849,6 +854,8 @@ private boolean dispatchCommand(CommandSender sender, Command cmd, String label,
p.sendMessage("搂cRGB values must be numbers (e.g. 128,128,128)");
} else if(result == 3) {
p.sendMessage("搂cEach RGB value must be between 0 and 255");
} else if(result == 4) {
p.sendMessage("搂c0,0,0 is reserved for unclaimed land; pick another colour");
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,14 @@ public static int validateRGB(String rgb) {
if (parts.length != 3) return 1;

try {
int total = 0;
for (String part : parts) {
int value = Integer.parseInt(part.trim());
if (value < 0 || value > 255) return 3;
total += value;
}
// The web map reads land without an owner as 0,0,0.
if (total == 0) return 4;
return 0; // All good
} catch (NumberFormatException e) {
return 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static Result setColour(String id, String rawRgb) {
if (title == null) return Result.error("No title with the id " + id);
String rgb = parseRgb(rawRgb);
if (rgb == null) return Result.error("Colour must be R,G,B with each value 0-255, e.g. 200,168,80");
// The web map reads land without an owner as 0,0,0.
if (rgb.equals("0,0,0")) return Result.error("0,0,0 is reserved for unclaimed land; pick another colour");
String old = title.getRgb();
if (rgb.equals(old)) return Result.error(title.getId() + " already uses " + rgb);
for (Title other : TitleLoader.getByTier(title.getTier())) {
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/net/tfminecraft/simplefactions/utils/RandomRGB.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,29 @@ private static float hueToRGB(float p, float q, float t) {

public static boolean isFree(String rgb) {
for(Faction f : FactionManager.factions) {
if(f.getRGB().equalsIgnoreCase(rgb)) return false;
if(sameRGB(f.getRGB(), rgb)) return false;
for(Guild g : f.getGuildHandler().getGuilds()) {
if(g.getRGB().equalsIgnoreCase(rgb)) return false;
if(sameRGB(g.getRGB(), rgb)) return false;
}
}
return true;
}

/** Same colour by value, so 255,0,00 matches 255,0,0. */
public static boolean sameRGB(String left, String right) {
if (left == null || right == null) return false;
String[] a = left.trim().split(",", -1);
String[] b = right.trim().split(",", -1);
if (a.length != 3 || b.length != 3) return left.trim().equalsIgnoreCase(right.trim());
try {
for (int i = 0; i < 3; i++) {
if (Integer.parseInt(a[i].trim()) != Integer.parseInt(b[i].trim())) return false;
}
return true;
} catch (NumberFormatException e) {
return left.trim().equalsIgnoreCase(right.trim());
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,9 @@ void baseGuildAppearanceIsChangedThroughTheFactionCommands(String action, String
"faction,2,must be numbers",
"guild,2,must be numbers",
"faction,3,between 0 and 255",
"guild,3,between 0 and 255"
"guild,3,between 0 and 255",
"faction,4,reserved for unclaimed land",
"guild,4,reserved for unclaimed land"
})
void invalidColoursHaveSpecificValidationMessages(String name, int code, String message) {
guildLeader();
Expand All @@ -855,6 +857,10 @@ void validColoursQueueBothFactionMapStatesAndRespectGuildColourUniqueness() {
verify(map).enqueue("nation", "1,1,1");
verify(map).enqueue("nation", "2,2,2");
try (MockedStatic<RandomRGB> colours = mockStatic(RandomRGB.class)) {
assertTrue(run("faction", "setcolour", "3,3,3"));
verify(player).sendMessage(contains("already used"));
verify(faction, never()).setRGB("3,3,3");
clearInvocations(player);
assertTrue(run("guild", "setcolour", "2,2,2"));
verify(player).sendMessage(contains("already used"));
colours.when(() -> RandomRGB.isFree("2,2,2")).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ void joiningGuildIsAllowedForUnaffiliatedAndRealmMembersButNotGuildLeaders() {
}

@ParameterizedTest
@CsvSource(value = {"0,0,0|0", "255, 12, 3|0", "1,2|1", "1,2,3,4|1", "x,2,3|2", "-1,2,3|3", "256,2,3|3"}, delimiter = '|')
@CsvSource(value = {"0,0,0|4", "0, 0, 00|4", "0,0,1|0", "255, 12, 3|0", "1,2|1", "1,2,3,4|1", "x,2,3|2", "-1,2,3|3", "256,2,3|3"}, delimiter = '|')
void rgbValidationProvidesSpecificErrors(String input, int expected) {
assertEquals(expected, FactionManager.validateRGB(input));
assertEquals(1, FactionManager.validateRGB(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ void setColourMustBeUniqueWithinATierOnly() {
assertFalse(TitleAdminService.setColour("COUNTY_1", "1,2").ok());
}

@Test
void setColourRefusesTheUnclaimedLandColour() {
assertFalse(TitleAdminService.setColour("COUNTY_1", "0, 0, 00").ok());
assertEquals("1,1,1", county1.getRgb());
}

@Test
void addProvinceMovesItFromItsOldTitle() {
Result result = TitleAdminService.addProvinces("COUNTY_3", List.of("1"), ANY_PROVINCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ private void fund(String key, long pouch, long bank) {
assertFalse(RandomRGB.isFree("21,42,63"));
assertFalse(RandomRGB.isFree("84,105,126"));
assertTrue(RandomRGB.isFree("0,0,0"));
assertFalse(RandomRGB.isFree("21, 042,63"), "same colour spelt differently");
}

@Test void coloursMatchByValue() {
assertTrue(RandomRGB.sameRGB("255,0,00", "255,0,0"));
assertFalse(RandomRGB.sameRGB("255,0,1", "255,0,0"));
assertFalse(RandomRGB.sameRGB(null, "255,0,0"));
assertTrue(RandomRGB.sameRGB("odd", "ODD"), "unparsable colours compare as text");
assertFalse(RandomRGB.sameRGB("1,2", "1,2,3"));
assertTrue(RandomRGB.sameRGB("a,b,c", "A,B,C"));
assertFalse(RandomRGB.sameRGB("a,b,c", "1,2,3"));
}

@Test void randomFactionColorsRetryCollisionsAndRemainValidRgbValues() {
Expand Down
Loading