Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Fix %pronouns_pronouns% placeholder only showing one set, fix mysql s…
Browse files Browse the repository at this point in the history
…tore invalid cache state after clearing
  • Loading branch information
lucyydotp committed Mar 11, 2023
1 parent b2f28df commit 89f86a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import net.lucypoulton.pronouns.common.placeholder.Placeholder.Result;
import net.lucypoulton.pronouns.common.util.EnumUtil;

import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;

public class Placeholders {

private static String applyModifiers(PronounSet set, String value, String[] modifiers) {
private static String applyModifiers(List<PronounSet> set, String value, String[] modifiers) {
var out = value;
for (final String mod : modifiers) {
out = switch (mod.toLowerCase(Locale.ROOT)) {
Expand All @@ -24,7 +25,7 @@ private static String applyModifiers(PronounSet set, String value, String[] modi
case 1 -> out.toUpperCase(Locale.ROOT);
default -> out.substring(0, 1).toUpperCase(Locale.ROOT) + out.substring(1).toLowerCase(Locale.ROOT);
};
case "nounset" -> set.equals(PronounSet.Builtins.UNSET) ? "" : out;
case "nounset" -> set.get(0).equals(PronounSet.Builtins.UNSET) ? "" : out;
default -> out;
};
}
Expand All @@ -38,33 +39,34 @@ private static String applyModifiers(PronounSet set, String value, String[] modi
public final Placeholder possessiveAdj = forPronoun("possessiveadj", PronounSet::possessiveAdj);
public final Placeholder possessive = forPronoun("possessive", PronounSet::possessive);
public final Placeholder reflexive = forPronoun("reflexive", PronounSet::reflexive);
public final Placeholder pronouns = forPronoun("pronouns", PronounSet::toString);

public final Placeholder pronouns = forPronoun("pronouns", (sets, args) -> Result.of(PronounSet.format(sets)));
public final Placeholder all = forPronoun("all", PronounSet::toFullString);

public final Placeholder verb = forPronoun("verb", (set, value) -> {
final var verb = EnumUtil.getByName(Conjugator.class, value[0]);
return verb.map(conjugator -> Result.of(conjugator.conjugate(set.plural())))
return verb.map(conjugator -> Result.of(conjugator.conjugate(set.get(0).plural())))
.orElseGet(() -> Result.fail("Unknown verb " + value[0]));
});

public final Placeholder conjugate = forPronoun("conj", (set, value) -> {
if (value.length == 1) return Result.fail("Missing options for conjugation");
return Result.of(value[set.plural() ? 1 : 0]);
return Result.of(value[set.get(0).plural() ? 1 : 0]);
});

private Placeholder forPronoun(String name, BiFunction<PronounSet, String[], Result> value) {
private Placeholder forPronoun(String name, BiFunction<List<PronounSet>, String[], Result> value) {
return new Placeholder(name, ((sender, s) -> {
if (sender.uuid().isEmpty()) return Result.fail("No player");
final var set = plugin.store().sets(sender.uuid().get()).get(0);
final var sets = plugin.store().sets(sender.uuid().get());
final var split = s.split("[_ ]");
final var out = value.apply(set, split);
if (out.success()) return Result.of(applyModifiers(set,out.message(), split));
final var out = value.apply(sets, split);
if (out.success()) return Result.of(applyModifiers(sets, out.message(), split));
return out;
}));
}

private Placeholder forPronoun(String name, Function<PronounSet, String> value) {
return forPronoun(name, (set, str) -> Result.of(value.apply(set)));
return forPronoun(name, (set, str) -> Result.of(value.apply(set.get(0))));
}

public Placeholders(ProNouns plugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public List<PronounSet> sets(UUID player) {

@Override
public void set(UUID player, @NotNull List<PronounSet> sets) {
cache.put(player, sets);
if (sets.size() == 0) cache.remove(player);
else cache.put(player, sets);
plugin.executorService().submit(() -> push(player, sets));
}

Expand Down

0 comments on commit 89f86a9

Please sign in to comment.