Skip to content

Commit

Permalink
fix: Fix PlayerLookup and LinearEnumRankNode issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Jun 3, 2024
1 parent 9a245d0 commit 341c977
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
*/
package com.xpdustry.distributor.api.player;

import arc.util.Strings;
import com.xpdustry.distributor.api.collection.MindustryCollections;
import com.xpdustry.distributor.internal.annotation.DistributorDataClassWithBuilder;
import java.text.Normalizer;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import mindustry.gen.Groups;
import mindustry.gen.Player;
import org.immutables.value.Value;
Expand All @@ -44,10 +41,7 @@ public interface PlayerLookup {
static PlayerLookup create() {
// https://stackoverflow.com/a/4122200
return new PlayerLookupImpl(
() -> MindustryCollections.immutableList(Groups.player),
string -> Normalizer.normalize(Strings.stripColors(string), Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
.toLowerCase(Locale.ROOT));
() -> MindustryCollections.immutableList(Groups.player), PlayerLookupImpl.DEFAULT_NORMALIZER);
}

/**
Expand All @@ -57,7 +51,7 @@ static PlayerLookup create() {
* @param normalizer the normalizer
* @return the player lookup
*/
static PlayerLookup create(final Supplier<Collection<Player>> provider, final Function<String, String> normalizer) {
static PlayerLookup create(final Supplier<Collection<Player>> provider, final UnaryOperator<String> normalizer) {
return new PlayerLookupImpl(provider, normalizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,30 @@
package com.xpdustry.distributor.api.player;

import arc.Core;
import arc.util.Strings;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import mindustry.gen.Player;
import mindustry.net.Administration;

final class PlayerLookupImpl implements PlayerLookup {

static final UnaryOperator<String> DEFAULT_NORMALIZER =
string -> Normalizer.normalize(Strings.stripColors(string), Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
.toLowerCase(Locale.ROOT);

private final Supplier<Collection<Player>> provider;
private final Function<String, String> normalizer;

PlayerLookupImpl(final Supplier<Collection<Player>> provider, final Function<String, String> normalizer) {
PlayerLookupImpl(final Supplier<Collection<Player>> provider, final UnaryOperator<String> normalizer) {
this.provider = provider;
this.normalizer = normalizer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void find_by_uuid_with_all() {
}

private void assertQueryResult(final PlayerLookup.Query query, final Player... result) {
final var lookup = new PlayerLookupImpl(() -> PLAYERS, PlayerLookup.DEFAULT_NORMALIZER);
final var lookup = new PlayerLookupImpl(() -> PLAYERS, PlayerLookupImpl.DEFAULT_NORMALIZER);
assertThat(lookup.findOnlinePlayers(query)).containsExactlyInAnyOrder(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package com.xpdustry.distributor.api.permission.rank;

import java.util.Locale;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -37,18 +35,13 @@ public interface EnumRankNode<E extends Enum<E>> extends RankNode {
* </ul>
*
* @param value the rank value
* @param nameExtractor the extractor for the rank name
* @param namespace the rank namespace
* @param ascending whether the enum ranks are in ascending order
* @param <E> the enum type
* @return the created enum rank node
*/
static <E extends Enum<E>> RankNode linear(
final E value, final Function<E, String> nameExtractor, boolean ascending) {
return new LinearEnumRankNode<>(value, nameExtractor, ascending);
}

static <E extends Enum<E>> RankNode linear(final E value, boolean ascending) {
return linear(value, e -> e.name().toLowerCase(Locale.ROOT), ascending);
static <E extends Enum<E>> RankNode linear(final E value, final String namespace, boolean ascending) {
return new LinearEnumRankNode<>(value, namespace, ascending);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
*/
package com.xpdustry.distributor.api.permission.rank;

import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.Nullable;

record LinearEnumRankNode<E extends Enum<E>>(E value, Function<E, String> nameProvider, boolean ascending)
implements EnumRankNode<E> {
record LinearEnumRankNode<E extends Enum<E>>(E value, String namespace, boolean ascending) implements EnumRankNode<E> {

@Override
public String getName() {
return this.nameProvider.apply(this.value);
return this.namespace + "_" + this.value.name().toLowerCase();
}

@Override
Expand All @@ -40,11 +38,11 @@ public E getValue() {
final var constants = this.value.getDeclaringClass().getEnumConstants();
if (this.ascending) {
return (this.value.ordinal() > 0)
? new LinearEnumRankNode<>(constants[this.value.ordinal() - 1], this.nameProvider, true)
? new LinearEnumRankNode<>(constants[this.value.ordinal() - 1], namespace, true)
: null;
} else {
return (this.value.ordinal() + 1 < constants.length)
? new LinearEnumRankNode<>(constants[this.value.ordinal() + 1], this.nameProvider, false)
? new LinearEnumRankNode<>(constants[this.value.ordinal() + 1], namespace, false)
: null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
package com.xpdustry.distributor.api.permission.rank;

import com.xpdustry.distributor.api.key.Key;
import java.util.List;
import java.util.Locale;
import mindustry.gen.Player;

final class MindustryRankSource implements RankSource {

@Override
public List<RankNode> getRanks(final Player player) {
final var rank = player.admin() ? MindustryRank.ADMIN : MindustryRank.PLAYER;
return List.of(EnumRankNode.linear(rank, r -> "mindustry-" + r.name().toLowerCase(Locale.ROOT), true));
return List.of(EnumRankNode.linear(rank, Key.MINDUSTRY_NAMESPACE, true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ranks:
mindustry_player:
distributor.player.lookup.name: true
distributor.player.lookup.entity_id: true
distributor.player.lookup.server_id: true
distributor.player.lookup.uuid: false

mindustry_admin:
Expand Down

0 comments on commit 341c977

Please sign in to comment.