Skip to content

Commit

Permalink
feat: Add support for Team in TranslatableComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Jun 8, 2024
1 parent b4dd1f3 commit a385655
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.xpdustry.distributor.api.component.style.TextStyle;
import com.xpdustry.distributor.api.translation.TranslationArguments;
import mindustry.ctype.MappableContent;
import mindustry.game.Team;

/**
* A component that displays a translatable text.
Expand Down Expand Up @@ -112,7 +113,7 @@ static TranslatableComponent translatable(final String key, final TextStyle text
* @return the translatable component
*/
static TranslatableComponent translatable(final MappableContent content) {
return new TranslatableComponentImpl(TextStyle.none(), getKey(content), TranslationArguments.empty());
return translatable(content, TextStyle.none());
}

/**
Expand All @@ -123,7 +124,7 @@ static TranslatableComponent translatable(final MappableContent content) {
* @return the translatable component
*/
static TranslatableComponent translatable(final MappableContent content, final ComponentColor textColor) {
return new TranslatableComponentImpl(TextStyle.of(textColor), getKey(content), TranslationArguments.empty());
return translatable(content, TextStyle.of(textColor));
}

/**
Expand All @@ -134,11 +135,48 @@ static TranslatableComponent translatable(final MappableContent content, final C
* @return the translatable component
*/
static TranslatableComponent translatable(final MappableContent content, final TextStyle textStyle) {
return new TranslatableComponentImpl(textStyle, getKey(content), TranslationArguments.empty());
return new TranslatableComponentImpl(
textStyle,
"mindustry." + content.getContentType().name() + "." + content.name + ".name",
TranslationArguments.empty());
}

private static String getKey(final MappableContent content) {
return "mindustry." + content.getContentType().name() + "." + content.name + ".name";
/**
* Creates a new translatable component with the specified team.
*
* @param team the team
* @return the translatable component
*/
static TranslatableComponent translatable(final Team team) {
return translatable(team, TextStyle.none());
}

/**
* Creates a new translatable component with the specified team and text color.
*
* @param team the team
* @param textColor the text color
* @return the translatable component
*/
static TranslatableComponent translatable(final Team team, final ComponentColor textColor) {
return translatable(team, TextStyle.of(textColor));
}

/**
* Creates a new translatable component with the specified team and text textStyle.
*
* @param team the team
* @param textStyle the text textStyle
* @return the translatable component
*/
static TranslatableComponent translatable(final Team team, final TextStyle textStyle) {
if (team.id < Team.baseTeams.length) {
return new TranslatableComponentImpl(
textStyle, "mindustry.team." + team.name + ".name", TranslationArguments.empty());
} else {
return new TranslatableComponentImpl(
textStyle, "distributor.component.team", TranslationArguments.array(team.id));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.xpdustry.distributor.api.DistributorProvider;
import com.xpdustry.distributor.api.event.EventBus;
import com.xpdustry.distributor.api.scheduler.PluginScheduler;
import com.xpdustry.distributor.api.test.ManageSchedulerExtension;
import com.xpdustry.distributor.api.test.ManageScheduler;
import com.xpdustry.distributor.api.test.TestPlugin;
import com.xpdustry.distributor.api.test.TestScheduler;
import com.xpdustry.distributor.api.util.Priority;
Expand All @@ -44,7 +44,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@ExtendWith(ManageSchedulerExtension.class)
@ExtendWith(ManageScheduler.class)
@SuppressWarnings({"UnusedMethod", "UnusedVariable"})
public final class EventHandlerProcessorTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.xpdustry.distributor.api.scheduler.Cancellable;
import com.xpdustry.distributor.api.scheduler.MindustryTimeUnit;
import com.xpdustry.distributor.api.scheduler.PluginScheduler;
import com.xpdustry.distributor.api.test.ManageSchedulerExtension;
import com.xpdustry.distributor.api.test.ManageScheduler;
import com.xpdustry.distributor.api.test.TestScheduler;
import com.xpdustry.distributor.common.scheduler.PluginSchedulerImpl;
import java.time.Duration;
Expand All @@ -41,7 +41,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@ExtendWith(ManageSchedulerExtension.class)
@ExtendWith(ManageScheduler.class)
@SuppressWarnings({"UnusedMethod", "UnusedVariable"})
public final class TaskHandlerProcessorTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.component;

import com.xpdustry.distributor.api.key.CTypeKey;
import com.xpdustry.distributor.api.test.ManageMindustryContent;
import mindustry.Vars;
import mindustry.ctype.MappableContent;
import mindustry.game.Team;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(ManageMindustryContent.class)
public final class TranslatableComponentTest {

@Test
void test_content() {
for (final var category : CTypeKey.ALL) {
test_content(category);
}
}

<T extends MappableContent> void test_content(final CTypeKey<T> key) {
for (final var content : Vars.content.<T>getBy(key.getContentType())) {
final var component = TranslatableComponent.translatable(content);
assertThat(component.getKey())
.isEqualTo("mindustry." + key.getContentType().name() + "." + content.name + ".name");
}
}

@Test
void test_team() {
for (final var team : Team.all) {
final var component = TranslatableComponent.translatable(team);
if (team.id < Team.baseTeams.length) {
assertThat(component.getKey()).isEqualTo("mindustry.team." + team.name + ".name");
} else {
assertThat(component.getKey()).isEqualTo("distributor.component.team");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.test;

import arc.Core;
import arc.mock.MockSettings;
import mindustry.Vars;
import mindustry.core.ContentLoader;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public final class ManageMindustryContent implements BeforeAllCallback {

private static boolean INITIALIZED = false;

@Override
public void beforeAll(final ExtensionContext context) {
if (!INITIALIZED) {
INITIALIZED = true;
Vars.content = new ContentLoader();
Core.settings = new MockSettings();
Vars.content.createBaseContent();
Vars.content.init();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields;

public final class ManageSchedulerExtension
public final class ManageScheduler
implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback, AfterAllCallback {

private final Map<Field, PluginSchedulerImpl> schedulers = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
distributor.component.team=Team {0}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
distributor.component.team=Équipe {0}

0 comments on commit a385655

Please sign in to comment.