Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use color names for team names by default #305

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import xyz.nucleoid.plasmid.api.util.ItemStackBuilder;
import xyz.nucleoid.plasmid.api.util.PlasmidCodecs;

import java.util.Optional;
import java.util.function.Function;

/**
Expand All @@ -38,14 +39,14 @@ public final record GameTeamConfig(

public static final MapCodec<GameTeamConfig> MAP_CODEC = RecordCodecBuilder.mapCodec(instance -> {
return instance.group(
PlasmidCodecs.TEXT.fieldOf("name").forGetter(GameTeamConfig::name),
PlasmidCodecs.TEXT.optionalFieldOf("name").forGetter(config -> Optional.of(config.name)),
Colors.CODEC.optionalFieldOf("color", Colors.NONE).forGetter(GameTeamConfig::colors),
Codec.BOOL.optionalFieldOf("friendly_fire", true).forGetter(GameTeamConfig::friendlyFire),
COLLISION_CODEC.optionalFieldOf("collision", AbstractTeam.CollisionRule.ALWAYS).forGetter(GameTeamConfig::collision),
VISIBILITY_CODEC.optionalFieldOf("name_tag_visibility", AbstractTeam.VisibilityRule.ALWAYS).forGetter(GameTeamConfig::nameTagVisibility),
PlasmidCodecs.TEXT.optionalFieldOf("prefix", ScreenTexts.EMPTY).forGetter(GameTeamConfig::prefix),
PlasmidCodecs.TEXT.optionalFieldOf("suffix", ScreenTexts.EMPTY).forGetter(GameTeamConfig::suffix)
).apply(instance, GameTeamConfig::new);
).apply(instance, GameTeamConfig::of);
});

public static final Codec<GameTeamConfig> CODEC = MAP_CODEC.codec();
Expand Down Expand Up @@ -107,8 +108,22 @@ public void applyToScoreboard(Team scoreboardTeam) {
scoreboardTeam.setSuffix(this.suffix());
}

public static GameTeamConfig of(Optional<Text> name, Colors colors, boolean friendlyFire, AbstractTeam.CollisionRule collision, AbstractTeam.VisibilityRule nameTagVisibility, Text prefix, Text suffix) {
return new GameTeamConfig(getNameWithColorFallback(name, colors), colors, friendlyFire, collision, nameTagVisibility, prefix, suffix);
}

private static Text getNameWithColorFallback(Optional<Text> name, Colors colors) {
return name.orElseGet(() -> {
if (colors == Colors.NONE) {
return Text.literal("Team");
} else {
return Text.translatable("color.minecraft." + colors.blockDyeColor().getName());
}
});
}

public static final class Builder {
private Text name = Text.literal("Team");
private Optional<Text> name = Optional.empty();
private Colors colors = Colors.NONE;
private boolean friendlyFire = true;
private AbstractTeam.CollisionRule collision = AbstractTeam.CollisionRule.ALWAYS;
Expand All @@ -120,7 +135,7 @@ public static final class Builder {
}

Builder(GameTeamConfig config) {
this.name = config.name;
this.name = Optional.of(config.name);
this.colors = config.colors;
this.friendlyFire = config.friendlyFire;
this.collision = config.collision;
Expand All @@ -130,7 +145,7 @@ public static final class Builder {
}

public Builder setName(Text name) {
this.name = name;
this.name = Optional.of(name);
return this;
}

Expand Down Expand Up @@ -165,7 +180,7 @@ public Builder setSuffix(Text suffix) {
}

public GameTeamConfig build() {
return new GameTeamConfig(
return GameTeamConfig.of(
this.name, this.colors,
this.friendlyFire, this.collision, this.nameTagVisibility,
this.prefix, this.suffix
Expand Down
116 changes: 116 additions & 0 deletions src/test/java/xyz/nucleoid/plasmid/test/GameTeamTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package xyz.nucleoid.plasmid.test;

import com.google.gson.JsonParser;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import net.minecraft.Bootstrap;
import net.minecraft.SharedConstants;
import net.minecraft.scoreboard.AbstractTeam;
import net.minecraft.text.Text;
import net.minecraft.util.DyeColor;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamConfig;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamConfig.Colors;

import static org.junit.jupiter.api.Assertions.*;

public class GameTeamTests {
@BeforeAll
public static void beforeAll() {
SharedConstants.createGameVersion();
Bootstrap.initialize();
}

@Test
public void testConfigParsing() {
var json = """
{
"name": {
"text": "Team Name"
},
"color": "blue",
"friendly_fire": true,
"collision": "never",
"name_tag_visibility": "never"
}
""";

var expected = new GameTeamConfig(
Text.literal("Team Name"),
Colors.from(DyeColor.BLUE),
true,
AbstractTeam.CollisionRule.NEVER,
AbstractTeam.VisibilityRule.NEVER,
Text.empty(),
Text.empty()
);

assertParsedEquals(json, expected, GameTeamConfig.CODEC);
}

@Test
public void testConfigParsingWithoutName() {
var json = """
{
"color": "red",
"friendly_fire": false,
"collision": "pushOtherTeams",
"name_tag_visibility": "hideForOtherTeams",
"prefix": {
"text": "Prefix"
},
"suffix": {
"text": "Suffix"
}
}
""";

var expected = new GameTeamConfig(
Text.translatable("color.minecraft.red"),
Colors.from(DyeColor.RED),
false,
AbstractTeam.CollisionRule.PUSH_OTHER_TEAMS,
AbstractTeam.VisibilityRule.HIDE_FOR_OTHER_TEAMS,
Text.literal("Prefix"),
Text.literal("Suffix")
);

assertParsedEquals(json, expected, GameTeamConfig.CODEC);
}

@Test
public void testConfigParsingWithoutColor() {
var json = """
{
"friendly_fire": false,
"collision": "always",
"name_tag_visibility": "hideForOwnTeam"
}
""";

var expected = new GameTeamConfig(
Text.literal("Team"),
Colors.NONE,
false,
AbstractTeam.CollisionRule.ALWAYS,
AbstractTeam.VisibilityRule.HIDE_FOR_OWN_TEAM,
Text.empty(),
Text.empty()
);

assertParsedEquals(json, expected, GameTeamConfig.CODEC);
}

private static <T> void assertParsedEquals(String json, T expected, Codec<T> codec) {
var actual = parse(json, codec);
assertEquals(expected, actual);
}

private static <T> T parse(String json, Codec<T> codec) {
var element = JsonParser.parseString(json);
var result = codec.decode(JsonOps.INSTANCE, element);

return result.getOrThrow().getFirst();
}
}
Loading