Allow hex colour codes for lobby particles

The colour screen gets a button to type a colour as #RRGGBB or #RGB,
next to picking one of the dye colours.
This commit is contained in:
2026-10-26 09:48:46 +01:00
parent d77c014409
commit 15036a7ba0
3 changed files with 38 additions and 0 deletions
@@ -88,6 +88,10 @@ PARTICLE_NUMBER_TYPE = §eClick to type a value
PARTICLE_COLOR_EDITOR = §0Colours
PARTICLE_COLOR_REMOVE = §eClick to remove
PARTICLE_COLOR_ADD = §aAdd colour
PARTICLE_COLOR_HEX = §eEnter hex code
PARTICLE_COLOR_HEX_EXAMPLE = §7For example #FF8800
PARTICLE_COLOR_HEX_TITLE = Hex colour
PARTICLE_COLOR_HEX_INVALID = §cThat is not a hex colour like #FF8800
PARTICLE_COLOR_RANDOM = §eUse random colours
PARTICLE_SLOT_EMPTY = §7Empty slot
PARTICLE_SLOT_CREATE = §eClick to create
@@ -88,6 +88,10 @@ PARTICLE_NUMBER_TYPE = §eZum Eingeben klicken
PARTICLE_COLOR_EDITOR = §0Farben
PARTICLE_COLOR_REMOVE = §eZum Entfernen klicken
PARTICLE_COLOR_ADD = §aFarbe hinzufügen
PARTICLE_COLOR_HEX = §eHex-Code eingeben
PARTICLE_COLOR_HEX_EXAMPLE = §7Zum Beispiel #FF8800
PARTICLE_COLOR_HEX_TITLE = Hex-Farbe
PARTICLE_COLOR_HEX_INVALID = §cDas ist keine Hex-Farbe wie #FF8800
PARTICLE_COLOR_RANDOM = §eZufällige Farben verwenden
PARTICLE_SLOT_EMPTY = §7Leerer Slot
PARTICLE_SLOT_CREATE = §eZum Erstellen klicken
@@ -124,6 +124,23 @@ class ValueEditors {
apply.test(next);
reopen.run();
}, reopen)));
// Same position as typing a value on the number screen
inventory.setItem(13, new SWItem(Material.NAME_TAG, msg("PARTICLE_COLOR_HEX", player), List.of(msg("PARTICLE_COLOR_HEX_EXAMPLE", player)), false, clickType -> {
SWAnvilInv anvil = new SWAnvilInv(player, msg("PARTICLE_COLOR_HEX_TITLE", player), "#");
anvil.setItem(Material.NAME_TAG);
anvil.setCallback(input -> Bukkit.getScheduler().runTask(LobbySystem.getInstance(), () -> {
Color color = parseHex(input);
if (color == null) {
LobbySystem.getMessage().send("PARTICLE_COLOR_HEX_INVALID", player);
} else {
List<Color> next = new ArrayList<>(colors);
next.add(color);
apply.test(next);
}
reopen.run();
}));
anvil.open();
}));
}
inventory.setItem(RESET, new SWItem(Material.WHITE_DYE, msg("PARTICLE_COLOR_RANDOM", player), clickType -> {
apply.test(List.of());
@@ -134,6 +151,19 @@ class ValueEditors {
inventory.open();
}
// Accepts #RRGGBB and #RGB, with or without the #; null for anything else
Color parseHex(String input) {
String hex = input.trim();
if (hex.startsWith("#")) hex = hex.substring(1);
if (hex.length() == 3) {
hex = "" + hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2);
}
if (!hex.matches("[0-9a-fA-F]{6}")) {
return null;
}
return new Color(Integer.parseInt(hex, 16));
}
private Material dye(Color color) {
for (DyeColor dye : DyeColor.values()) {
if ((dye.getColor().asRGB() & 0xFFFFFF) == (color.getRGB() & 0xFFFFFF)) {