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

Comparators - add default comparators #7251

Merged
Merged
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
4 changes: 4 additions & 0 deletions src/main/java/ch/njol/skript/classes/EnumClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -55,6 +57,8 @@ public EnumClassInfo(Class<T> 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()));
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +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.registry;

import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.expressions.base.EventValueExpression;
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.
Expand All @@ -48,6 +32,8 @@ public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String co
.serializer(new RegistrySerializer<R>(registry))
.defaultExpression(defaultExpression)
.parser(registryParser);

Comparators.registerComparator(registryClass, registryClass, (o1, o2) -> Relation.get(o1.getKey() == o2.getKey()));
}

}
24 changes: 24 additions & 0 deletions src/test/skript/tests/misc/registry-enum-comparators.sk
Original file line number Diff line number Diff line change
@@ -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"
Loading