pre-parse the color instead of parsing on every get

This commit is contained in:
Pyrbu 2023-06-26 12:33:55 +02:00
parent eb1fd0cadd
commit f76d9a1830

@ -1,27 +1,29 @@
package lol.pyr.znpcsplus.util; package lol.pyr.znpcsplus.util;
public class PotionColor { public class PotionColor {
private final int color;
public static PotionColor DEFAULT = new PotionColor(0);
private final String color; public PotionColor(int color) {
public static PotionColor DEFAULT = new PotionColor("0");
public PotionColor(String color) {
this.color = color; this.color = color;
} }
public int getColor() { public PotionColor(String color) {
boolean hex = false;
if (color.startsWith("#")) { if (color.startsWith("#")) {
if (color.length() != 7) color = color.substring(1);
throw new IllegalArgumentException("Hex color must be 6 characters long"); hex = true;
return Integer.parseInt(color.substring(1), 16);
} else if (color.startsWith("0x")) {
if (color.length() != 8)
throw new IllegalArgumentException("Hex color must be 6 characters long");
return Integer.parseInt(color.substring(2), 16);
} else {
return Integer.parseInt(color);
} }
else if (color.startsWith("0x")) {
color = color.substring(2);
hex = true;
}
if (hex && color.length() != 6) throw new IllegalArgumentException("Hex color must be 6 characters long");
this.color = Integer.parseInt(color);
}
public int getColor() {
return color;
} }
public String toString() { public String toString() {