diff --git a/src/main/java/ch/njol/skript/classes/EnumClassInfo.java b/src/main/java/ch/njol/skript/classes/EnumClassInfo.java index 6bd18633cf5..1339922721f 100644 --- a/src/main/java/ch/njol/skript/classes/EnumClassInfo.java +++ b/src/main/java/ch/njol/skript/classes/EnumClassInfo.java @@ -8,6 +8,8 @@ import ch.njol.util.coll.iterator.ArrayIterator; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.skriptlang.skript.lang.comparator.Comparators; +import org.skriptlang.skript.lang.comparator.Relation; /** * This class can be used for an easier writing of ClassInfos that are enums, @@ -55,6 +57,8 @@ public EnumClassInfo(Class enumClass, String codeName, String languageNode, D return enumUtils.toString(constant, StringMode.VARIABLE_NAME); } }); + + Comparators.registerComparator(enumClass, enumClass, (o1, o2) -> Relation.get(o1.ordinal() - o2.ordinal())); } } diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java index ff82f966af8..5f3d5fe2532 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java @@ -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 . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.classes.registry; import ch.njol.skript.classes.ClassInfo; @@ -23,6 +5,8 @@ import ch.njol.skript.lang.DefaultExpression; import org.bukkit.Keyed; import org.bukkit.Registry; +import org.skriptlang.skript.lang.comparator.Comparators; +import org.skriptlang.skript.lang.comparator.Relation; /** * This class can be used for easily creating ClassInfos for {@link Registry}s. @@ -48,6 +32,8 @@ public RegistryClassInfo(Class registryClass, Registry registry, String co .serializer(new RegistrySerializer(registry)) .defaultExpression(defaultExpression) .parser(registryParser); + + Comparators.registerComparator(registryClass, registryClass, (o1, o2) -> Relation.get(o1.getKey() == o2.getKey())); } } diff --git a/src/test/skript/tests/misc/registry-enum-comparators.sk b/src/test/skript/tests/misc/registry-enum-comparators.sk new file mode 100644 index 00000000000..511ce65bb70 --- /dev/null +++ b/src/test/skript/tests/misc/registry-enum-comparators.sk @@ -0,0 +1,24 @@ +test "registry and enum comparators": + # We're using loops because that is when Skript appears to hate comparing things + # Example errors when Skript doesn't have explicit comparators: + # 'Can't compare a biome with a biome' + # 'Can't compare a attribute type with a attribute type' + + + # Compare registry entries + loop all biomes: + if loop-value = plains: + assert loop-value = plains with "Should be able to compare biomes" + + loop all attribute types: + if loop-value = generic movement speed: + assert loop-value = generic movement speed with "Should be able to compare attribute types" + + # Compare enum entries + loop all teleport causes: + if loop-value = ender pearl: + assert loop-value = ender pearl with "Should be able to compare teleport causes" + + loop all spawn reasons: + if loop-value = jockey: + assert loop-value = jockey with "Should be able to compare spawn reasons"