Skip to content

Commit

Permalink
Merge branch 'dev/feature' into dev/BannerPatterns
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAbsolutionism authored Dec 29, 2024
2 parents 6ae56aa + dde9aee commit 15d5b3f
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String co
.parser(registryParser);

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

}
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/sections/SecFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ When looping a simple (non-indexed) set of values, e.g. all players, the index w
"loop key {_index} and value {_value} in {list of items::*}:",
"\tbroadcast \"%{_index}% = %{_value}%\"",
"",
"for each {_index} = {_value} in {my list::*}:",
"for each {_index}, {_value} in {my list::*}:",
"\tbroadcast \"%{_index}% = %{_value}%\"",
})
@Since("INSERT VERSION")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ch.njol.skript.test.runner;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.NoDoc;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

/**
* This class is used to test whether class-info plurals are detected successfully.
* The syntax in it should never be parsed or used (even in test mode)
* and does nothing.
*/
@Name("Test Plural Class Infos")
@Description("Tests that plural class infos are identified correctly.")
@NoDoc
public class EffTestPluralClassInfos extends Effect {

static {
class Example1 {}
class Example2 {}
class Example3 {}
class Example4 {}
if (TestMode.ENABLED) {
Classes.registerClass(new ClassInfo<>(Example1.class, "testgui")
.user("example1")
.name("Test -ui"));
Classes.registerClass(new ClassInfo<>(Example2.class, "exemplus")
.user("example2")
.name("Test -i"));
Classes.registerClass(new ClassInfo<>(Example3.class, "aardwolf")
.user("example3")
.name("Test -ves"));
Classes.registerClass(new ClassInfo<>(Example4.class, "hoof")
.user("example3")
.name("Test -ves 2"));
Skript.registerEffect(EffTestPluralClassInfos.class,
"classinfo test for %testgui%",
"classinfo test for %testguis%",
"classinfo test for %exemplus%",
"classinfo test for %exempli%",
"classinfo test for %aardwolf%",
"classinfo test for %aardwolves%",
"classinfo test for %hoof%",
"classinfo test for %hooves%");
}
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
return false;
}

@Override
protected void execute(Event event) {
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "";
}

}
106 changes: 78 additions & 28 deletions src/main/java/ch/njol/skript/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract class Utils {
protected final static Deque<WordEnding> plurals = new LinkedList<>();

static {
plurals.add(new WordEnding("axe", "axes"));
plurals.add(new WordEnding("axe", "axes")); // not complete since we have battleaxe, etc.
plurals.add(new WordEnding("x", "xes"));

plurals.add(new WordEnding("ay", "ays"));
Expand All @@ -61,14 +61,20 @@ public abstract class Utils {
plurals.add(new WordEnding("oy", "oys"));
plurals.add(new WordEnding("uy", "uys"));
plurals.add(new WordEnding("kie", "kies"));
plurals.add(new WordEnding("zombie", "zombies"));
plurals.add(new WordEnding("zombie", "zombies", true));
plurals.add(new WordEnding("y", "ies"));

plurals.add(new WordEnding("wife", "wives")); // we have to do the -ife -> ives first
plurals.add(new WordEnding("wife", "wives", true)); // we have to do the -ife -> ives first
plurals.add(new WordEnding("life", "lives"));
plurals.add(new WordEnding("knife", "knives"));
plurals.add(new WordEnding("knife", "knives", true));
plurals.add(new WordEnding("ive", "ives"));
plurals.add(new WordEnding("elf", "elves")); // self shelf elf

plurals.add(new WordEnding("lf", "lves")); // self shelf elf wolf half etc.
plurals.add(new WordEnding("thief", "thieves", true));
plurals.add(new WordEnding("ief", "iefs")); // chiefs, fiefs, briefs

plurals.add(new WordEnding("hoof", "hooves"));

plurals.add(new WordEnding("fe", "ves"));// most -f words' plurals can end in -fs as well as -ves

plurals.add(new WordEnding("h", "hes"));
Expand All @@ -79,16 +85,18 @@ public abstract class Utils {
plurals.add(new WordEnding("api", "apis")); // api fix
plurals.add(new WordEnding("us", "i"));

plurals.add(new WordEnding("hoe", "hoes"));
plurals.add(new WordEnding("toe", "toes"));
plurals.add(new WordEnding("hoe", "hoes", true));
plurals.add(new WordEnding("toe", "toes", true));
plurals.add(new WordEnding("foe", "foes", true));
plurals.add(new WordEnding("woe", "woes", true));
plurals.add(new WordEnding("o", "oes"));

plurals.add(new WordEnding("alias", "aliases"));
plurals.add(new WordEnding("gas", "gases"));
plurals.add(new WordEnding("alias", "aliases", true));
plurals.add(new WordEnding("gas", "gases", true));

plurals.add(new WordEnding("child", "children"));
plurals.add(new WordEnding("child", "children")); // grandchild, etc.

plurals.add(new WordEnding("sheep", "sheep"));
plurals.add(new WordEnding("sheep", "sheep", true));

// general ending
plurals.add(new WordEnding("", "s"));
Expand All @@ -104,7 +112,7 @@ public static String join(final Object[] objects) {
b.append(", ");
b.append(Classes.toString(objects[i]));
}
return "" + b.toString();
return b.toString();
}

public static String join(final Iterable<?> objects) {
Expand All @@ -118,7 +126,7 @@ public static String join(final Iterable<?> objects) {
first = false;
b.append(Classes.toString(o));
}
return "" + b.toString();
return b.toString();
}

@SuppressWarnings("unchecked")
Expand All @@ -134,7 +142,7 @@ public static Pair<String, Integer> getAmount(String s) {
} else if (s.matches("an? .+")) {
return new Pair<>(s.split(" ", 2)[1], 1);
}
return new Pair<>(s, Integer.valueOf(-1));
return new Pair<>(s, -1);
}

// public final static class AmountResponse {
Expand Down Expand Up @@ -267,14 +275,55 @@ public static File getFile(Plugin plugin) {
public static NonNullPair<String, Boolean> getEnglishPlural(String word) {
assert word != null;
if (word.isEmpty())
return new NonNullPair<>("", Boolean.FALSE);
return new NonNullPair<>("", false);
if (!couldBeSingular(word)) {
for (final WordEnding ending : plurals) {
if (ending.isCompleteWord()) {
// Complete words shouldn't be used as partial pieces
if (word.length() != ending.plural().length())
continue;
}
if (word.endsWith(ending.plural()))
return new NonNullPair<>(
word.substring(0, word.length() - ending.plural().length()) + ending.singular(),
true
);
if (word.endsWith(ending.plural().toUpperCase(Locale.ENGLISH)))
return new NonNullPair<>(
word.substring(0, word.length() - ending.plural().length())
+ ending.singular().toUpperCase(Locale.ENGLISH),
true
);
}
}
return new NonNullPair<>(word, false);
}

private static boolean couldBeSingular(String word) {
for (final WordEnding ending : plurals) {
if (word.endsWith(ending.plural()))
return new NonNullPair<>(word.substring(0, word.length() - ending.plural().length()) + ending.singular(), Boolean.TRUE);
if (word.endsWith(ending.plural().toUpperCase(Locale.ENGLISH)))
return new NonNullPair<>(word.substring(0, word.length() - ending.plural().length()) + ending.singular().toUpperCase(Locale.ENGLISH), Boolean.TRUE);
if (ending.singular().isBlank())
continue;
if (ending.isCompleteWord() && ending.singular().length() != word.length())
continue; // Skip complete words

if (word.endsWith(ending.singular()) || word.toLowerCase().endsWith(ending.singular())) {
return true;
}
}
return new NonNullPair<>(word, Boolean.FALSE);
return false;
}

/**
* Adds a singular/plural word override for the given words.
* This is inserted first in the list of words to be checked: it will always be matched
* and will override all other plurality rules.
* This will only match the word <s>exactly</s>, and will not apply to derivations of the word.
*
* @param singular The singular form of the word
* @param plural The plural form of the word
*/
public static void addPluralOverride(String singular, String plural) {
Utils.plurals.addFirst(new WordEnding(singular, plural, true));
}

/**
Expand All @@ -286,6 +335,11 @@ public static NonNullPair<String, Boolean> getEnglishPlural(String word) {
public static String toEnglishPlural(String word) {
assert word != null && word.length() != 0;
for (WordEnding ending : plurals) {
if (ending.isCompleteWord()) {
// Complete words shouldn't be used as partial pieces
if (word.length() != ending.singular().length())
continue;
}
if (word.endsWith(ending.singular()))
return word.substring(0, word.length() - ending.singular().length()) + ending.plural();
}
Expand Down Expand Up @@ -791,13 +845,10 @@ public static boolean isInteger(Number... numbers) {
return true;
}

protected static class WordEnding { // To be a record in 2.10

private final String singular, plural;
protected record WordEnding(String singular, String plural, boolean isCompleteWord) {

private WordEnding(String singular, String plural) {
this.singular = singular;
this.plural = plural;
public WordEnding(String singular, String plural) {
this(singular, plural, false);
}

public String singular() {
Expand All @@ -811,8 +862,7 @@ public String plural() {
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (!(object instanceof WordEnding)) return false;
WordEnding ending = (WordEnding) object;
if (!(object instanceof WordEnding ending)) return false;
return Objects.equals(singular, ending.singular) && Objects.equals(plural, ending.plural);
}

Expand Down

0 comments on commit 15d5b3f

Please sign in to comment.