Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supplier to EnumClassInfo #7097

Open
wants to merge 5 commits into
base: dev/feature
Choose a base branch
from
Open
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
22 changes: 2 additions & 20 deletions src/main/java/ch/njol/skript/classes/ClassInfo.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.classes;

import ch.njol.skript.SkriptAPIException;
Expand All @@ -29,6 +11,8 @@
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.arithmetic.Arithmetics;
import org.skriptlang.skript.lang.arithmetic.Operator;

import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -37,8 +21,6 @@
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.skriptlang.skript.lang.arithmetic.Operator;
import org.skriptlang.skript.lang.arithmetic.Arithmetics;

/**
* @author Peter Güttinger
Expand Down
51 changes: 18 additions & 33 deletions src/main/java/ch/njol/skript/classes/EnumClassInfo.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.classes;

import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.DefaultExpression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.util.EnumUtils;
import ch.njol.skript.util.StringMode;
import ch.njol.util.coll.iterator.ArrayIterator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -33,41 +18,41 @@
public class EnumClassInfo<T extends Enum<T>> extends ClassInfo<T> {

/**
* @param c The class
* @param enumClass The class
* @param codeName The name used in patterns
* @param languageNode The language node of the type
*/
public EnumClassInfo(Class<T> c, String codeName, String languageNode) {
this(c, codeName, languageNode, new EventValueExpression<>(c));
public EnumClassInfo(Class<T> enumClass, String codeName, String languageNode) {
this(enumClass, codeName, languageNode, new EventValueExpression<>(enumClass));
}

/**
* @param c The class
* @param enumClass The class
* @param codeName The name used in patterns
* @param languageNode The language node of the type
* @param defaultExpression The default expression of the type
*/
public EnumClassInfo(Class<T> c, String codeName, String languageNode, DefaultExpression<T> defaultExpression) {
super(c, codeName);
EnumUtils<T> enumUtils = new EnumUtils<>(c, languageNode);
public EnumClassInfo(Class<T> enumClass, String codeName, String languageNode, DefaultExpression<T> defaultExpression) {
super(enumClass, codeName);
EnumUtils<T> enumUtils = new EnumUtils<>(enumClass, languageNode);
usage(enumUtils.getAllNames())
.serializer(new EnumSerializer<>(c))
.serializer(new EnumSerializer<>(enumClass))
.defaultExpression(defaultExpression)
.supplier(() -> new ArrayIterator<>(enumClass.getEnumConstants()))
.parser(new Parser<T>() {
@Override
@Nullable
public T parse(String s, ParseContext context) {
return enumUtils.parse(s);
public @Nullable T parse(String input, ParseContext context) {
return enumUtils.parse(input);
}

@Override
public String toString(T o, int flags) {
return enumUtils.toString(o, flags);
public @NotNull String toString(T constant, int flags) {
return enumUtils.toString(constant, StringMode.MESSAGE);
}

@Override
public String toVariableNameString(T o) {
return o.name();
public @NotNull String toVariableNameString(T constant) {
return enumUtils.toString(constant, StringMode.VARIABLE_NAME);
}
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/ch/njol/skript/util/EnumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public String toString(E enumerator, int flags) {
return s != null ? s : enumerator.name();
}

/**
* This method returns the string representation of an enumerator
* @param enumerator The enumerator to represent as a string
* @param flag not currently used
* @return A string representation of the enumerator
*/
public String toString(E enumerator, StringMode flag) {
return toString(enumerator, flag.ordinal());
}

/**
* @return A comma-separated string containing a list of all names representing the enumerators.
* Note that some entries may represent the same enumerator.
Expand Down