me.deecaad.core.file.SerializerEnumException Maven / Gradle / Ivy
package me.deecaad.core.file;
import me.deecaad.core.utils.EnumUtil;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class SerializerEnumException extends SerializerException {
private final Set options;
private final String actual;
private final boolean allowWildcard;
public > SerializerEnumException(@NotNull String name, Class enumClass,
String actual, boolean allowWildcard, @NotNull String location) {
super(name, getMessages(enumClass, actual, allowWildcard), location);
this.options = EnumUtil.getOptions(enumClass);
this.actual = actual;
this.allowWildcard = allowWildcard;
}
public > SerializerEnumException(@NotNull Serializer> serializer, Class enumClass,
String actual, boolean allowWildcard, @NotNull String location) {
super(serializer, getMessages(enumClass, actual, allowWildcard), location);
this.options = EnumUtil.getOptions(enumClass);
this.actual = actual;
this.allowWildcard = allowWildcard;
// 1.13+ remove the legacy materials, so they don't clutter the console.
if (enumClass == Material.class) {
this.options.removeIf(name -> name.startsWith("LEGACY_"));
}
}
public Set getOptions() {
return new HashSet<>(options);
}
public String getActual() {
return actual;
}
public boolean isAllowWildcard() {
return allowWildcard;
}
private static > String[] getMessages(Class enumClass, String actual, boolean allowWildcard) {
// With enums, there is usually the option to use '$' as a wildcard.
// Sometimes, we are not allowed to use wildcards, so we should warn
// the user about that.
boolean usesWildcard = actual.startsWith("$");
if (usesWildcard && !allowWildcard) {
return new String[]{
"You tried to use a wildcard ('$') when wildcards aren't allowed to be used!",
forValue(actual),
didYouMean(actual, enumClass)
};
}
String link = "https://github.com/WeaponMechanics/MechanicsMain/wiki/References";
if (enumClass == Material.class)
link += "#materials";
else if (enumClass == Sound.class)
link += "#sounds";
else if (enumClass == Particle.class)
link += "#particles";
else if (enumClass == EntityType.class)
link += "#entities";
else
link = null;
List list = new LinkedList<>();
list.add("Could not match config to any " + enumClass.getSimpleName());
list.add(forValue(actual));
list.add(didYouMean(actual, enumClass));
if (link != null)
list.add(enumClass.getSimpleName() + " Reference: " + link);
return list.toArray(new String[0]);
}
}