Skip to content

Commit

Permalink
feat: Move standard keys to dedicated class
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed May 29, 2024
1 parent f709063 commit 83f3425
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,17 @@
package com.xpdustry.distributor.api.audience;

import com.xpdustry.distributor.api.component.ComponentLike;
import com.xpdustry.distributor.api.key.Key;
import com.xpdustry.distributor.api.metadata.MetadataContainer;
import com.xpdustry.distributor.api.permission.PermissionProvider;
import com.xpdustry.distributor.api.player.MUUID;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import mindustry.game.Team;

public interface Audience {

Key<String> NAME = Key.of(Key.DISTRIBUTOR_NAMESPACE, "name", String.class);

Key<String> DISPLAY_NAME = Key.of(Key.DISTRIBUTOR_NAMESPACE, "display-name", String.class);

Key<MUUID> MUUID = Key.of(Key.DISTRIBUTOR_NAMESPACE, "muuid", MUUID.class);

Key<Locale> LOCALE = Key.of(Key.DISTRIBUTOR_NAMESPACE, "locale", Locale.class);

Key<Team> TEAM = Key.of(Key.DISTRIBUTOR_NAMESPACE, "team", Team.class);

static Audience of(final Audience... audiences) {
if (audiences.length == 0) {
return Audience.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.xpdustry.distributor.api.audience;

import com.xpdustry.distributor.api.key.StandardKeys;
import com.xpdustry.distributor.api.player.MUUID;
import mindustry.game.Team;
import mindustry.gen.Player;
Expand All @@ -35,7 +36,7 @@ public interface AudienceProvider {
default Audience getTeam(final Team team) {
return getPlayers()
.asStream()
.filter(a -> a.getMetadata().getMetadata(Audience.TEAM).orElse(null) == team)
.filter(a -> a.getMetadata().getMetadata(StandardKeys.TEAM).orElse(null) == team)
.collect(Audience.collectToAudience());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.api.key;

import com.xpdustry.distributor.api.player.MUUID;
import java.util.Locale;
import mindustry.game.Team;

public final class StandardKeys {

public static final Key<String> NAME = Key.of(Key.DISTRIBUTOR_NAMESPACE, "name", String.class);
public static final Key<String> DISPLAY_NAME = Key.of(Key.DISTRIBUTOR_NAMESPACE, "display-name", String.class);
public static final Key<MUUID> MUUID = Key.of(Key.DISTRIBUTOR_NAMESPACE, "muuid", MUUID.class);
public static final Key<Locale> LOCALE = Key.of(Key.DISTRIBUTOR_NAMESPACE, "locale", Locale.class);
public static final Key<Team> TEAM = Key.of(Key.DISTRIBUTOR_NAMESPACE, "team", Team.class);

private StandardKeys() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import com.xpdustry.distributor.api.Distributor;
import com.xpdustry.distributor.api.DistributorProvider;
import com.xpdustry.distributor.api.audience.Audience;
import com.xpdustry.distributor.api.component.ListComponent;
import com.xpdustry.distributor.api.component.style.ComponentColor;
import com.xpdustry.distributor.api.key.StandardKeys;
import com.xpdustry.distributor.api.metadata.MetadataContainer;
import com.xpdustry.distributor.api.translation.BundleTranslationSource;
import com.xpdustry.distributor.api.translation.Translation;
Expand Down Expand Up @@ -147,7 +147,9 @@ void test_translatable_arguments() {

private MindustryComponentAppendable createAppendable(final Locale locale) {
return new MindustryComponentAppendable(
MetadataContainer.builder().putConstant(Audience.LOCALE, locale).build(),
MetadataContainer.builder()
.putConstant(StandardKeys.LOCALE, locale)
.build(),
new StandardComponentRendererProvider());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.xpdustry.distributor.api.audience.Audience;
import com.xpdustry.distributor.api.component.ComponentLike;
import com.xpdustry.distributor.api.component.render.ComponentAppendable;
import com.xpdustry.distributor.api.key.StandardKeys;
import com.xpdustry.distributor.api.metadata.MetadataContainer;
import com.xpdustry.distributor.api.permission.PermissionProvider;
import java.net.URI;
Expand All @@ -47,13 +48,13 @@ final class PlayerAudience implements Audience {
this.player = player;
this.permissions = PermissionProvider.from(player);
this.metadata = MetadataContainer.builder()
.putSupplier(Audience.NAME, () -> player.getInfo().plainLastName())
.putSupplier(Audience.DISPLAY_NAME, player::coloredName)
.putConstant(Audience.MUUID, com.xpdustry.distributor.api.player.MUUID.from(player))
.putSupplier(StandardKeys.NAME, () -> player.getInfo().plainLastName())
.putSupplier(StandardKeys.DISPLAY_NAME, player::coloredName)
.putConstant(StandardKeys.MUUID, com.xpdustry.distributor.api.player.MUUID.from(player))
.putSupplier(
Audience.LOCALE,
StandardKeys.LOCALE,
() -> Locale.forLanguageTag(player.locale().replace('-', '_')))
.putSupplier(Audience.TEAM, player::team)
.putSupplier(StandardKeys.TEAM, player::team)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.xpdustry.distributor.api.audience.Audience;
import com.xpdustry.distributor.api.component.ComponentLike;
import com.xpdustry.distributor.api.component.render.ComponentAppendable;
import com.xpdustry.distributor.api.key.StandardKeys;
import com.xpdustry.distributor.api.metadata.MetadataContainer;
import java.util.Locale;

Expand All @@ -30,9 +31,9 @@ final class ServerAudience implements Audience {
static final Audience INSTANCE = new ServerAudience();

private final MetadataContainer metadata = MetadataContainer.builder()
.putConstant(Audience.NAME, "server")
.putConstant(Audience.DISPLAY_NAME, "Server")
.putSupplier(Audience.LOCALE, Locale::getDefault)
.putConstant(StandardKeys.NAME, "server")
.putConstant(StandardKeys.DISPLAY_NAME, "Server")
.putSupplier(StandardKeys.LOCALE, Locale::getDefault)
.build();

private ServerAudience() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.xpdustry.distributor.common.component.render;

import com.xpdustry.distributor.api.DistributorProvider;
import com.xpdustry.distributor.api.audience.Audience;
import com.xpdustry.distributor.api.component.Component;
import com.xpdustry.distributor.api.component.ListComponent;
import com.xpdustry.distributor.api.component.TemporalComponent;
Expand All @@ -29,6 +28,7 @@
import com.xpdustry.distributor.api.component.render.ComponentAppendable;
import com.xpdustry.distributor.api.component.render.ComponentRenderer;
import com.xpdustry.distributor.api.component.render.ComponentRendererProvider;
import com.xpdustry.distributor.api.key.StandardKeys;
import com.xpdustry.distributor.api.translation.ComponentAwareTranslation;
import java.time.ZoneId;
import java.util.Locale;
Expand Down Expand Up @@ -86,8 +86,10 @@ public void render(final TemporalComponent component, final ComponentAppendable
.getFormat()
.toFormatter()
.withZone(ZoneId.of("UTC"))
.withLocale(
appendable.getContext().getMetadata(Audience.LOCALE).orElseGet(Locale::getDefault))
.withLocale(appendable
.getContext()
.getMetadata(StandardKeys.LOCALE)
.orElseGet(Locale::getDefault))
.formatTo(component.getTemporal(), appendable);
}
}
Expand All @@ -102,7 +104,10 @@ public void render(final TranslatableComponent component, final ComponentAppenda
.getGlobalTranslationSource()
.getTranslationOrMissing(
component.getKey(),
appendable.getContext().getMetadata(Audience.LOCALE).orElseGet(Locale::getDefault));
appendable
.getContext()
.getMetadata(StandardKeys.LOCALE)
.orElseGet(Locale::getDefault));
if (translation instanceof ComponentAwareTranslation aware) {
aware.formatTo(component.getParameters(), appendable);
} else {
Expand Down

0 comments on commit 83f3425

Please sign in to comment.