From 53d4225e7cdcaf48482f51451442a1a59f701ec8 Mon Sep 17 00:00:00 2001 From: mooziii <63669478+mooziii@users.noreply.github.com> Date: Wed, 30 Mar 2022 14:06:31 +0200 Subject: [PATCH] Add allColors() and allDecorations() --- .../java/net/axay/kspigot/chat/KColors.java | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/axay/kspigot/chat/KColors.java b/src/main/java/net/axay/kspigot/chat/KColors.java index 2710ea03..cbcd6e92 100644 --- a/src/main/java/net/axay/kspigot/chat/KColors.java +++ b/src/main/java/net/axay/kspigot/chat/KColors.java @@ -4,10 +4,7 @@ import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @SuppressWarnings("unused") @@ -739,14 +736,31 @@ public class KColors { */ public static final TextColor YELLOWGREEN = TextColor.color(154, 205, 50); - public static List values() { - Field[] fields = KColors.class.getFields(); - List finalList = new ArrayList<>(); //yes this could be done with a simple Arrays.stream but didn't work because java is stupid (because of the exception) - for (Field field : fields) { - try { - finalList.add((TextColor) field.get(null)); - } catch (IllegalAccessException ignored) {} - } - return finalList; + public static List allColors() { + return Arrays.stream(KColors.class.getFields()) + .filter(field -> field.getType() == TextColor.class) + .map(field -> { + try { + return (TextColor) field.get(null); + } catch (IllegalArgumentException | IllegalAccessException ignored) { + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + public static List allDecorations() { + return Arrays.stream(KColors.class.getFields()) + .filter(field -> field.getType() == TextDecoration.class) + .map(field -> { + try { + return (TextDecoration) field.get(null); + } catch (IllegalArgumentException | IllegalAccessException ignored) { + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } }